<s> from __future__ import division <EOL> import numpy as np <EOL> import random as rd <EOL> from . import common_args <EOL> from . . util import scale_samples , read_param_file , compute_groups_matrix <EOL> from . optimal_trajectories import return_max_combo <EOL> from . morris_util import * <EOL> from operator import or_ <EOL> try : <EOL> from gurobipy import * <EOL> except ImportError : <EOL> _has_gurobi = False <EOL> else : <EOL> _has_gurobi = True <EOL> def sample ( problem , N , num_levels , grid_jump , optimal_trajectories = None , local_optimization = False ) : <EOL> """<STR_LIT>""" <EOL> if grid_jump >= num_levels : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if problem . get ( '<STR_LIT>' ) : <EOL> sample = sample_groups ( problem , N , num_levels , grid_jump ) <EOL> else : <EOL> sample = sample_oat ( problem , N , num_levels , grid_jump ) <EOL> if optimal_trajectories : <EOL> assert type ( optimal_trajectories ) == int , "<STR_LIT>" <EOL> if optimal_trajectories < <NUM_LIT:2> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if optimal_trajectories >= N : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if _has_gurobi == False and local_optimization == False and optimal_trajectories > <NUM_LIT:10> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> sample = compute_optimised_trajectories ( problem , <EOL> sample , <EOL> N , <EOL> optimal_trajectories , <EOL> local_optimization ) <EOL> scale_samples ( sample , problem [ '<STR_LIT>' ] ) <EOL> return sample <EOL> def sample_oat ( problem , N , num_levels , grid_jump ) : <EOL> D = problem [ '<STR_LIT>' ] <EOL> B = np . tril ( np . ones ( [ D + <NUM_LIT:1> , D ] , dtype = int ) , - <NUM_LIT:1> ) + np . triu ( - <NUM_LIT:1> * np . ones ( [ D + <NUM_LIT:1> , D ] , dtype = int ) ) <EOL> delta = grid_jump / ( num_levels - <NUM_LIT:1> ) <EOL> X = np . empty ( [ N * ( D + <NUM_LIT:1> ) , D ] ) <EOL> for j in range ( N ) : <EOL> DM = np . diag ( [ rd . choice ( [ - <NUM_LIT:1> , <NUM_LIT:1> ] ) for _ in range ( D ) ] ) <EOL> perm = np . random . permutation ( D ) <EOL> P = np . zeros ( [ D , D ] ) <EOL> for i in range ( D ) : <EOL> P [ i , perm [ i ] ] = <NUM_LIT:1> <EOL> x_base = np . empty ( [ D + <NUM_LIT:1> , D ] ) <EOL> for i in range ( D ) : <EOL> x_base [ : , i ] = ( <EOL> rd . choice ( np . arange ( num_levels - grid_jump ) ) ) / ( num_levels - <NUM_LIT:1> ) <EOL> index_list = np . arange ( D + <NUM_LIT:1> ) + j * ( D + <NUM_LIT:1> ) <EOL> delta_diag = np . diag ( [ delta for _ in range ( D ) ] ) <EOL> X [ index_list , : ] = <NUM_LIT:0.5> * ( np . mat ( B ) * np . mat ( P ) * np . mat ( DM ) + <NUM_LIT:1> ) * np . mat ( delta_diag ) + np . mat ( x_base ) <EOL> return X <EOL> def sample_groups ( problem , N , num_levels , grid_jump ) : <EOL> '''<STR_LIT>''' <EOL> G , group_names = compute_groups_matrix ( problem [ '<STR_LIT>' ] , problem [ '<STR_LIT>' ] ) <EOL> if G is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if type ( G ) is not np . matrixlib . defmatrix . matrix : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> k = G . shape [ <NUM_LIT:0> ] <EOL> g = G . shape [ <NUM_LIT:1> ] <EOL> sample = np . empty ( ( N * ( g + <NUM_LIT:1> ) , k ) ) <EOL> sample = np . array ( [ generate_trajectory ( G , num_levels , grid_jump ) for n in range ( N ) ] ) <EOL> return sample . reshape ( ( N * ( g + <NUM_LIT:1> ) , k ) ) <EOL> def compute_optimised_trajectories ( problem , input_sample , N , k_choices , local_optimization = False ) : <EOL> '''<STR_LIT>''' <EOL> num_params = problem [ '<STR_LIT>' ] <EOL> groups = compute_groups_matrix ( problem [ '<STR_LIT>' ] , num_params ) <EOL> if np . any ( ( input_sample < <NUM_LIT:0> ) | ( input_sample > <NUM_LIT:1> ) ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if _has_gurobi == True and local_optimization == False : <EOL> maximum_combo = return_max_combo ( input_sample , <EOL> N , <EOL> num_params , <EOL> k_choices , <EOL> groups ) <EOL> else : <EOL> maximum_combo = find_optimum_combination ( input_sample , <EOL> N , <EOL> num_params , <EOL> k_choices , <EOL> groups , <EOL> local_optimization ) <EOL> num_groups = None <EOL> if groups is not None : <EOL> num_groups = groups [ <NUM_LIT:0> ] . shape [ <NUM_LIT:1> ] <EOL> output = compile_output ( input_sample , <EOL> N , <EOL> num_params , <EOL> maximum_combo , <EOL> num_groups ) <EOL> return output <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> parser = common_args . create ( ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , type = int , required = True , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = int , required = False , <EOL> default = <NUM_LIT:4> , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , type = int , required = False , <EOL> default = <NUM_LIT:2> , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = int , required = False , <EOL> default = None , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = bool , required = True , <EOL> default = False , help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> np . random . seed ( args . seed ) <EOL> rd . seed ( args . seed ) <EOL> problem = read_param_file ( args . paramfile ) <EOL> param_values = sample ( problem , args . samples , args . levels , args . grid_jump , args . k_optimal , args . local ) <EOL> np . savetxt ( args . output , param_values , delimiter = args . delimiter , <EOL> fmt = '<STR_LIT>' + str ( args . precision ) + '<STR_LIT:e>' ) </s>
