def input_gen(k, numOfVar, N): """ generate dataset for kth direction k = kth axis along which to vary numofvar = total number of variables N = total number of points to be generated""" X = np.zeros([N, numOfVar]) z = np.zeros(N) rn = 100 * np.random.rand(1)[0] print(rn) A = sobol_seq.i4_sobol(numOfVar, rn)[0] rn = 100 * np.random.rand(1)[0] print(rn) B = sobol_seq.i4_sobol(N, rn)[0] for i in range(numOfVar): print(np.shape(X.T[i])) if (i == k): X.T[i] = B else: X.T[i] = A[i] # for i in range(N): # z[i] = outputGen(X[i]) return X
def get_sample(self): (x, y), self.seed = sobol_seq.i4_sobol(2, self.seed) #(x, y) = np.random.rand(2) x *= self.resx y *= self.resy u = self.get_cam(x, y) return u[0], u[1]
def input_create(k, file, numOfVar, x, UB, LB): """ description - returns an input file with numofVar variables in it and only kth row varying. (other fixed) A = matrix for keping rows other than kth rows fixed b = a number to keep the previously calculated optima (i.e. k-1th point) k = direction along which to vary (will be mulitple in this case) """ infile = open(file, 'w') #print("data", A, b, k, numOfVar) for i in range(numOfVar): if (i in k): # generate a random number for kth row rn = 100 * np.random.rand(1)[0] #print(rn) a = sobol_seq.i4_sobol(1, rn)[0][0] * (UB[i] - LB[i]) + LB[i] infile.write(str(a) + " ") else: # enter constant for rest of the rows infile.write(str(x[i]) + " ") #print("why") infile.close()
def quasi_init(seed,f=False): v =4 if f: v=5 x, seed = sobol_seq.i4_sobol(v, seed) for i in range(v): x[i] = x[i]* ranges[i] - ranges[i]/2 # produces rando float in the ranges band described above return x,seed
def my_i4_sobol_generate(dim, n, seed): import sobol_seq r = np.full((n, dim), np.nan) currentseed = seed for j in range(n): r[j, 0:dim], newseed = sobol_seq.i4_sobol(dim, currentseed) currentseed = newseed return r
def sample(self, size): res = [] for _ in range(size): vec, self.seed = sobol_seq.i4_sobol(self.dim, self.seed) res.append(vec) res = np.asarray(res) if self.scrambled: res = (res + self.bias) % 1.0 return res
def draw(self, low, high, size): num, dim = size[0], size[1] samples = [] for _ in range(num): vector, seed = sobol_seq.i4_sobol(dim, self.seed) sample = (high - low) * vector + low self.seed = seed samples.append(sample) return np.array(samples)
def create_recommendation(user): # Extract parameters. experiment_id = request.json["experiment_id"] date = dt.datetime.today() # Get the experiment corresponding to this observation. e = Experiment.query.filter_by(id=experiment_id).first() dims = e.dimensions.all() n_dims = len(dims) space = create_space(dims) # Probability of selecting a random configuration. if request.json.get("rand_prob", None): rand_prob = request.json["rand_prob"] else: rand_prob = 0. # Number of model estimation iterations to perform. Note that the meaning of # this parameter changes when switching from a Gaussian process to a # Bayesian neural network. if request.json.get("n_model_iters", None): n_model_iters = request.json["n_model_iters"] else: n_model_iters = 5 * n_dims # Number of randomly positioned observations to create. n_random = 1 * n_dims # Either use Bayesian optimization or generate a random point depending on # the number of observations collected so far. Note that an input parameter # can be provided to determine the chance of selecting a configuration at # random. Setting this parameter to one is equivalent to assuming a policy # of pure exploration. n_observed = e.observations.filter_by(pending=False).count() n_obs = e.observations.count() if n_observed < n_random or np.random.uniform() < rand_prob: # rec = encode_recommendation(space.sample().ravel(), dims) sobol_rec = sobol_seq.i4_sobol(n_dims, n_obs + 1)[0] rec = encode_recommendation(space.invert(sobol_rec).ravel(), dims) else: # Get pending and non-pending observations. observed = e.observations.filter(Observation.pending == False).all() pending = e.observations.filter(Observation.pending == True).all() X, y = decode_recommendation(observed, dims) X_pending = (decode_recommendation(pending, dims)[0] if len(pending) > 0 else None) # Create a recommendation with Bayesian optimization. bo = BayesianOptimization(e, space) c = bo.recommend(X, y, X_pending, GaussianProcess, n_model_iters) rec = encode_recommendation(c, dims) # Submit recommendation to user and store in the Thor database. It is # created initially without a response and is marked as pending. obs = Observation(str(rec), date) e.observations.append(obs) # Commit changes. db.session.commit() sleep(1) return jsonify(obs.to_dict())
def assemble_dataset(tau_max, nu_max, n_collocation, n_boundary, seed): # Sample interior points using sobol sequences and boundary points # Interior Points skip = seed interior_data = np.full((n_collocation, 3), np.nan) for j in range(n_collocation): seed = j + skip interior_data[j, :], next_seed = sobol_seq.i4_sobol(3, seed) #First column of data is /nu, Second Column is /tau and Third Column is /mu interior_data[:, 0] = interior_data[:, 0] * 2 * nu_max - nu_max interior_data[:, 1] = interior_data[:, 1] * tau_max #make sure no zeros here interior_data[:, 2] = interior_data[:, 2] * 2 - 1 #Sample Boundary Points ( tau = 0 and tau_max) n_b_1 = int(n_boundary / 2) n_b_2 = n_boundary - n_b_1 boundary_data_1 = np.full((n_b_1, 3), np.nan) for j in range(n_b_1): seed = j + skip boundary_data_1[j, :], next_seed = sobol_seq.i4_sobol(3, seed) #First column of data is /nu, Second Column is /tau and Third Column is /mu boundary_data_1[:, 0] = boundary_data_1[:, 0] * 2 * nu_max - nu_max boundary_data_1[:, 1] = tau_max #make sure no zeros here boundary_data_1[:, 2] = boundary_data_1[:, 2] * 2 - 1 boundary_data_2 = np.full((n_b_2, 3), np.nan) for j in range(n_b_2): seed = j + skip boundary_data_2[j, :], next_seed = sobol_seq.i4_sobol(3, seed) #First column of data is /nu, Second Column is /tau and Third Column is /mu boundary_data_2[:, 0] = boundary_data_2[:, 0] * 2 * nu_max - nu_max boundary_data_2[:, 1] = 0 #make sure no zeros here boundary_data_2[:, 2] = boundary_data_2[:, 2] * 2 - 1 return interior_data, boundary_data_1, boundary_data_2
def __generate_sobol_vector(self): """ Generates a next sobol vector in the current search space. :return: sobol vector as numpy array. """ # https://github.com/naught101/sobol_seq#usage vector, _ = sobol_seq.i4_sobol(self.dimensionality, self.numOfGeneratedPoints + 1) self.numOfGeneratedPoints += 1 return vector
def next(self): """ Draw the next sample from the Sampler :return: A new vector sampled from a Sobol sequence with mean 0 and standard deviation 1 """ vec, seed = i4_sobol(self.n, self.seed) self.seed = seed if seed > 1 else 2 vec = array(norm_dist.ppf(vec)) vec = vec.reshape(self.shape) return vec
def get_sobol_sequence(ranges, N=2**12, skip=20000): # for reference & citation see https://github.com/earlbellinger/asteroseismology shift = ranges[:,0] scale = np.array([(b-a) for a,b in ranges]) init_conds = [] for i in range(skip, N+skip): vals = shift+np.array(i4_sobol(len(ranges), i)[0])*scale init_conds += [[tmp for tmp in vals]] for j, val in enumerate(vals): if np.isnan(vals[j]): vals[j] = 0 return init_conds
def str_to_color(s): global str_to_color_seed if s in str_to_color_config: return str_to_color_config[s] if s in str_to_color_cache: return str_to_color_cache[s] rgb, str_to_color_seed = sobol_seq.i4_sobol(3, str_to_color_seed) rgb = (rgb * 192).astype(int) + 64 rgb = rgb.tolist() str_to_color_cache[s] = rgb return rgb
def create_test_set(self, N_points, dim, bounds=[0., 1.], seed=1): Q = 0.25 * np.array([[1, 1, 1, 1], [1, 1, -1, -1], [1, -1, -1, 1], [1, -1, 1, -1]]) # Create test set x_test = np.zeros((N_points, dim)) eta = np.zeros((N_points, dim)) i = 0 while (i < N_points): x_test[i], seed = i4_sobol(dim, seed) x_test[i] = (bounds[1] - bounds[0]) * x_test[i] + bounds[ 0] # shift/scale according to bounds eta[i] = np.dot(x_test[i], Q.T).astype(np.float32) if eta[i, 0] <= 0.25: i += 1 return x_test, eta, seed
def quasi_random_search(n_combinations, n_hyperparameters, bounds, x_train, y_train, x_test, y_test, n_fold): best_accuracy = 0 best_params = None counter = 1 for i in range(n_combinations): # Hyperparameters combinations generated by quasi random numbers with a Sobol sequence combination = np.array(sobol_seq.i4_sobol(n_hyperparameters, i + 1)[0]) for j in range(n_hyperparameters): # Put the position values into the bounds range combination[j] = bounds[j][0] + combination[j] * (bounds[j][1] - bounds[j][0]) n_layers = int(np.around(combination[0])) n_neurons = int(np.around(combination[1])) print("\n\nCombination " + str(counter) + " out of " + str(n_combinations)) logging.debug("\n\nCombination " + str(counter) + " out of " + str(n_combinations)) logging.debug("N. layers : " + str(n_layers)) logging.debug("N. neurons : " + str(n_neurons)) ann = AnnKFold(n_fold) ann.x_train_set = x_train ann.y_train_set = y_train ann.x_test_set = x_test ann.y_test_set = y_test ann.create_model(n_layers, n_neurons, len(x_test[0]), len(y_test[0])) accuracy = ann.train_model() if accuracy > best_accuracy: best_params = [n_layers, n_neurons] best_accuracy = accuracy logging.debug("Accuracy on validation set : " + str(accuracy)) counter += 1 return best_params, best_accuracy
def sobol(dim,seed): return np.array(sb.i4_sobol(dim,seed)[0])
""" Name : c12_32_scatter_sobol.py Book : Python for Finance (2nd ed.) Publisher: Packt Publishing Ltd. Author : Yuxing Yan Date : 6/6/2017 email : [email protected] [email protected] """ import sobol_seq import scipy as sp import matplotlib.pyplot as plt a=[] n=100 for i in sp.arange(2*n): t=sobol_seq.i4_sobol(1,i) a.append(t) print(a[0:10]) # x=sp.random.permutation(a[:n]) y=sp.random.permutation(a[n:]) plt.scatter(x,y,edgecolors='r') plt.show()
def sobol(dim, seed): # p = dimension , seed = rank return list(sob.i4_sobol(dim, seed)[0])
def get_minimum(particle_factory, n, bounds, pso_hyperparameters=None): global n_function_evaluations n_function_evaluations = 0 # Number of bounds must be equal to the problem dimension (number of problem hyperparameters) assert len(bounds) == n if pso_hyperparameters is None: pso_hyperparameters = PSOHyperparameters(n) w_start = pso_hyperparameters.w_start w_end = pso_hyperparameters.w_end c1 = pso_hyperparameters.c1 c2 = pso_hyperparameters.c2 swarm_size = pso_hyperparameters.swarm_size num_generations = pso_hyperparameters.num_generations max_velocity = pso_hyperparameters.max_velocity initialization_type = pso_hyperparameters.initialization_type use_local_search = pso_hyperparameters.use_local_search particles = [] global_best_position = None global_best_value = math.inf # Random initialize the particles and evaluate them print("\n\n***** Particles initialization *****") logging.debug("\n\n***** Particles initialization *****") particles_to_initialize = swarm_size if initialization_type == InitializationType.QUASI_RANDOM_USING_BORDER: # 2^n punti vengono inizializzati agli estremi della regione ammissibile (ipercubo) # JR Il codice va generalizzato ma ho bisogno di fare delle prove immediate assert n == 2 particles_to_initialize = swarm_size - 4 # Vertice 0 del quadrato (ipercubo con n = 2) particle = particle_factory.get_particle() particle.position = np.array([bounds[0][0], bounds[1][0]]) particle.velocity = np.zeros(n) particle.position_integer = np.around(particle.position) particle.w = w_start particle_value = particle.get_value() particle.best_position = np.array(particle.position) particle.best_value = particle_value if particle_value < global_best_value: global_best_value = particle_value global_best_position = np.array(particle.position_integer) particles.append(particle) print("Border Particle 0 - Position " + str(particle.position)) logging.debug("\nBorder Particle 0") logging.debug("Position : " + str(particle.position)) logging.debug("Velocity : " + str(particle.velocity)) logging.debug("Objective function value : " + str(particle_value)) # Vertice 1 particle = particle_factory.get_particle() particle.position = np.array([bounds[0][0], bounds[1][1]]) particle.velocity = np.zeros(n) particle.position_integer = np.around(particle.position) particle.w = w_start particle_value = particle.get_value() particle.best_position = np.array(particle.position) particle.best_value = particle_value if particle_value < global_best_value: global_best_value = particle_value global_best_position = np.array(particle.position_integer) particles.append(particle) print("Border Particle 1 - Position " + str(particle.position)) logging.debug("\nBorder Particle 1") logging.debug("Position : " + str(particle.position)) logging.debug("Velocity : " + str(particle.velocity)) logging.debug("Objective function value : " + str(particle_value)) # Vertice 2 particle = particle_factory.get_particle() particle.position = np.array([bounds[0][1], bounds[1][0]]) particle.velocity = np.zeros(n) particle.position_integer = np.around(particle.position) particle.w = w_start particle_value = particle.get_value() particle.best_position = np.array(particle.position) particle.best_value = particle_value if particle_value < global_best_value: global_best_value = particle_value global_best_position = np.array(particle.position_integer) particles.append(particle) print("Border Particle 2 - Position " + str(particle.position)) logging.debug("\nBorder Particle 2") logging.debug("Position : " + str(particle.position)) logging.debug("Velocity : " + str(particle.velocity)) logging.debug("Objective function value : " + str(particle_value)) # Vertice 3 particle = particle_factory.get_particle() particle.position = np.array([bounds[0][1], bounds[1][1]]) particle.velocity = np.zeros(n) particle.position_integer = np.around(particle.position) particle.w = w_start particle_value = particle.get_value() particle.best_position = np.array(particle.position) particle.best_value = particle_value if particle_value < global_best_value: global_best_value = particle_value global_best_position = np.array(particle.position_integer) particles.append(particle) print("Border Particle 3 - Position " + str(particle.position)) logging.debug("\nBorder Particle 3") logging.debug("Position : " + str(particle.position)) logging.debug("Velocity : " + str(particle.velocity)) logging.debug("Objective function value : " + str(particle_value)) for i in range(particles_to_initialize): # Initial velocity is 0 according to several papers velocity = np.zeros(n) # Initial position is formed by quasi random numbers given by a Sobol sequence position = np.array(sobol_seq.i4_sobol(n, i + 1)[0]) for j in range(n): # Put the position values into the bounds range position[j] = bounds[j][0] + position[j] * (bounds[j][1] - bounds[j][0]) particle = particle_factory.get_particle() particle.position = position particle.position_integer = np.around(position) particle.velocity = velocity particle.w = w_start particle_value = particle.get_value() particle.best_position = np.array(position) particle.best_value = particle_value if particle_value < global_best_value: global_best_value = particle_value global_best_position = np.array(particle.position_integer) print("Particle " + str(i) + " - Position " + str(particle.position)) logging.debug("\nParticle " + str(i)) logging.debug("Position : " + str(particle.position)) logging.debug("Velocity : " + str(particle.velocity)) particles.append(particle) # PSO Optimization for i in range(num_generations): print("\n\n**** Particle generation " + str(i)) logging.debug("\n\n***** Particle generation " + str(i) + " ******") # Auxiliary variables to temporary store best population indexes (the update of the best indexes is done after the particles update) global_best_position_i = global_best_position global_best_value_i = global_best_value for j in range(swarm_size): logging.debug("\nParticle " + str(j)) # Move particle particle = particles[j] # Update the random hyperparameters r1 = np.random.rand() r2 = np.random.rand() # Particle swarm optimization update formulae velocity_updated = particle.w * particle.velocity + c1 * r1 * \ (particle.best_position - particle.position) + c2 * r2 * ( global_best_position - particle.position) # Check if the updated velocity respect the velocity bounds for k in range(n): velocity_sign = np.sign(velocity_updated[k]) velocity_updated[k] = velocity_sign * min( abs(velocity_updated[k]), max_velocity[k]) position_updated = particle.position + velocity_updated # Linear decreasing inertia weight particle.w = (w_start - w_end) * (num_generations - (i + 1)) / num_generations + w_end particle.position = position_updated particle.velocity = velocity_updated # Handle integer variables particle.position_integer = np.around(particle.position) # Handle constraints using Death Penalty approach (non-feasible particles are not evaluated) if is_solution_feasible(particle.position_integer, bounds): if use_local_search: # Memetic version of the algorithm particle_value_updated = perform_memetic_variant( particle, n, bounds) # Update global best according to the new value of the particle if particle.best_value < global_best_value_i: global_best_value_i = particle.best_value global_best_position_i = particle.position_integer else: particle_value_updated = particle.get_value() n_function_evaluations += 1 # Updating best indexes if particle_value_updated < particle.best_value: particle.best_value = particle_value_updated particle.best_position = np.array( particle.position_integer) if particle_value_updated < global_best_value_i: global_best_value_i = particle_value_updated global_best_position_i = particle.position_integer logging.debug("New position : " + str(particle.position)) logging.debug("New position (int) : " + str(particle.position_integer)) logging.debug("New velocity : " + str(particle.velocity)) logging.debug("Objective function value : " + str(particle_value_updated)) else: logging.debug("New position : " + str(particle.position)) logging.debug("New position (int) : " + str(particle.position_integer)) logging.debug("New velocity : " + str(particle.velocity)) logging.debug("Particle killed at this iteration") # Update best population indexes global_best_value = global_best_value_i global_best_position = np.array(global_best_position_i) logging.debug("\n\nEnd of generation") logging.debug("Best position : " + str(global_best_position)) logging.debug("Best objective function value : " + str(global_best_value)) logging.debug("\n\nTotal function evaluations: " + str(n_function_evaluations)) return global_best_position, global_best_value
def main(N, UB, LB, file, z_star): z = [] x = [] numOfVar = get_number(compileFile) init_r = UB - LB UB = np.ones(numOfVar) * UB LB = np.ones(numOfVar) * LB # add something for block coordinate # print("numOfVar:", numOfVar) # block size = 0 # print(UB) # print(LB) #print(type(numofVar)) os.system('gcc ' + compileFile + '.c -o ' + compileFile) feval = 0 # initial radnom value of optima Z = 500 tol = 0.05 #print(abs(Z - z_star)) # to generate guess for previous number at first iteration rn = 40 * np.random.rand(1)[0] #print(rn) #print(sobol_seq.i4_sobol(2, rn)[0]) if (numOfVar > 40): div = int(numOfVar / 40) num = list(sobol_seq.i4_sobol(40, rn)[0]) for i in range(div): a = list(np.array(num) * (i + 2)) # print("hello", a) for j in range(len(a)): num.append(a[j]) # print("xn:", num, np.shape(num)) xm = num[:numOfVar] * (UB - LB) + LB else: xm = sobol_seq.i4_sobol(numOfVar, rn)[0] * (UB - LB) + LB #x0 = sobol_seq.i4_sobol(1, rn)[0][0] #print(xm) track = [] f = open(file, "w") f.write("x[0]") f.write(",") f.write("x[1]") f.write(",") f.write("Z_blackbox") f.write(",") f.write("Z_Optimum") f.write(",") f.write("UB") f.write(",") f.write("UB") f.write(",") f.write("LB") f.write(",") f.write("LB") f.write("\n") # to count how many consecutive times minima is not changing (count_bad) countb = 0 # to count how many consecutive times minima is improving (count_improvement) counti = 0 #while(feval < 50): # continuousi = 0 # continuousb = 0 while (abs(Z - z_star) > 0.0001 and feval < 200): # stop if value of Z is not changing for 3 continuous iterations # or if number of function evaluation is less than 50 # after we have looped through all the variables, start from the beginning ex = 0 if (feval > numOfVar and ex < 1): #after it has optimized wrt to all variables, we would like to shrink search region dx = (UB - LB)[0] # to halve the range UB = xm + dx / 4 LB = xm - dx / 4 # just to make sure to run this just once ex = ex + 1 if (counti > numOfVar): dx = (UB - LB) # to halve the range if (dx.all() >= 0.1): UB = xm + dx / 4 LB = xm - dx / 4 counti = 0 if (countb > numOfVar): dx = (UB - LB) # try to find better condition if (dx[0] <= init_r / 8): # make a jump # xm = xm + init_r/2 UB = xm + init_r / 4 LB = xm - init_r / 4 else: # to double the range UB = xm + dx LB = xm - dx countb = 0 countr = 0 feval = feval + 1 print("feval:", feval) # print("UB:", UB) # print("LB:", LB) while (countr < numOfVar and countb <= numOfVar and counti <= numOfVar): # plt.scatter(xm[0], xm[1]) # plt.pause(0.05) # stop if value of Z is within a tolerance of Z_star # or if we have looped through all the variables K = np.random.randint(0, high=numOfVar, size=(block_size)) # print(K) # K = [1, 2] # to remove badly fitted models r = -0.3 while (r < 0.3): #print(r) r, coeff, bias = fitting(K, numOfVar, xm, UB, LB) # print("K:", K) # print("coefficient of determination" , r) # print("equation coefficient", coeff) # print("bias", bias) # point of optima for a quadratic equation # to make sure this is also within bounds # print(xm[K]) xm[K] = minima(xm[K], coeff, bias, UB[K], LB[K]) #xm[K] = -coeff[0]/(2*coeff[1]) #print("x0", K, xm) # value at x0 from model function Z_cap = func(xm[K], coeff, bias) # actual value at x0 from blackbox function Z_cap2 = output_generator(xm) # print("Z_cap:", Z_cap) # print("Z_blacbox:", Z_cap2) countr = countr + 1 # if improvement in minima value, update and also update improvement count if (Z_cap2[0] < Z): # print("hey") Z = Z_cap2[0] counti = counti + 1 countb = 0 # if no improvement, update countb (so that we can change bounds) else: countb = countb + 1 counti = 0 t = np.zeros(10) t[0] = xm[0] t[1] = xm[1] t[2] = Z_cap2[0] t[3] = Z t[4] = UB[0] t[5] = UB[1] t[6] = LB[0] t[7] = LB[1] t[8] = counti t[9] = countb #t[8] = xm[2] track.append(t) for i in range(10): f.write(str(t[i])) f.write(",") f.write("\n") #plt.show() return Z, feval
def sobol_test05(): """ sobol_test05 tests i4_sobol. """ print( "" "SOBOL_TEST05" " I4_SOBOL computes the next element of a Sobol sequence." "" " In this test, we demonstrate how the SEED can be" " manipulated to skip ahead in the sequence, or" " to come back to any part of the sequence." "" ) target = np.array( [ [0, 1, 0.000000, 0.000000, 0.000000], [1, 2, 0.500000, 0.500000, 0.500000], [2, 3, 0.750000, 0.250000, 0.750000], [3, 4, 0.250000, 0.750000, 0.250000], [4, 5, 0.375000, 0.375000, 0.625000], [100, 101, 0.4140625, 0.2578125, 0.3046875], [101, 102, 0.9140625, 0.7578125, 0.8046875], [102, 103, 0.6640625, 0.0078125, 0.5546875], [103, 104, 0.1640625, 0.5078125, 0.0546875], [104, 105, 0.2265625, 0.4453125, 0.7421875], [3, 4, 0.250000, 0.750000, 0.250000], [4, 5, 0.375000, 0.375000, 0.625000], [5, 6, 0.875000, 0.875000, 0.125000], [6, 7, 0.625000, 0.125000, 0.375000], [7, 8, 0.125000, 0.625000, 0.875000], [98, 99, 0.7890625, 0.3828125, 0.1796875], [99, 100, 0.2890625, 0.8828125, 0.6796875], [100, 101, 0.4140625, 0.2578125, 0.3046875], [101, 102, 0.9140625, 0.7578125, 0.8046875], [102, 103, 0.6640625, 0.0078125, 0.5546875], ] ) results = np.full_like(target, np.nan) dim_num = 3 print("" " Using dimension DIM_NUM = %d\n" % dim_num) seed = 0 print("" " Seed Seed I4_SOBOL" " In Out" "") for i in range(5): [r, seed_out] = i4_sobol(dim_num, seed) out = "%6d %6d " % (seed, seed_out) for j in range(1, dim_num + 1): out += "%10f " % r[j - 1] print(out) results[i, :] = [seed, seed_out] + list(r) seed = seed_out print("" " Jump ahead by increasing SEED:" "") seed = 100 print("" " Seed Seed I4_SOBOL" " In Out" "") for i in range(5): [r, seed_out] = i4_sobol(dim_num, seed) out = "%6d %6d " % (seed, seed_out) for j in range(1, dim_num + 1): out += "%10f " % r[j - 1] print(out) results[5 + i, :] = [seed, seed_out] + list(r) seed = seed_out print("" " Jump back by decreasing SEED:" "") seed = 3 print("" " Seed Seed I4_SOBOL" " In Out" "") for i in range(5): [r, seed_out] = i4_sobol(dim_num, seed) out = "%6d %6d " % (seed, seed_out) for j in range(1, dim_num + 1): out += "%10f " % r[j - 1] print(out) results[10 + i, :] = [seed, seed_out] + list(r) seed = seed_out print("" " Jump back by decreasing SEED:" "") seed = 98 print("" " Seed Seed I4_SOBOL" " In Out" "") for i in range(5): [r, seed_out] = i4_sobol(dim_num, seed) out = "%6d %6d " % (seed, seed_out) for j in range(1, dim_num + 1): out += "%10f " % r[j - 1] print(out) results[15 + i, :] = [seed, seed_out] + list(r) seed = seed_out assert np.all(target == results) return
def sobol_test04(): """ sobol_test04 tests i4_sobol. """ print( "\nSOBOL_TEST04" " I4_SOBOL returns the next element" " of a Sobol sequence." "\n In this test, we call I4_SOBOL repeatedly." ) dim_max = 4 target = { 2: np.array( [ [0, 1, 0.000000, 0.000000], [1, 2, 0.500000, 0.500000], [2, 3, 0.750000, 0.250000], [3, 4, 0.250000, 0.750000], [4, 5, 0.375000, 0.375000], # ...................... [106, 107, 0.9765625, 0.1953125], [107, 108, 0.4765625, 0.6953125], [108, 109, 0.3515625, 0.0703125], [109, 110, 0.8515625, 0.5703125], [110, 111, 0.6015625, 0.3203125], ] ), 3: np.array( [ [0, 1, 0.000000, 0.000000, 0.000000], [1, 2, 0.500000, 0.500000, 0.500000], [2, 3, 0.750000, 0.250000, 0.750000], [3, 4, 0.250000, 0.750000, 0.250000], [4, 5, 0.375000, 0.375000, 0.625000], # ...................... [106, 107, 0.9765625, 0.1953125, 0.4921875], [107, 108, 0.4765625, 0.6953125, 0.9921875], [108, 109, 0.3515625, 0.0703125, 0.1171875], [109, 110, 0.8515625, 0.5703125, 0.6171875], [110, 111, 0.6015625, 0.3203125, 0.8671875], ] ), 4: np.array( [ [0, 1, 0.000000, 0.000000, 0.000000, 0.000000], [1, 2, 0.500000, 0.500000, 0.500000, 0.500000], [2, 3, 0.750000, 0.250000, 0.750000, 0.250000], [3, 4, 0.250000, 0.750000, 0.250000, 0.750000], [4, 5, 0.375000, 0.375000, 0.625000, 0.125000], # ...................... [106, 107, 0.9765625, 0.1953125, 0.4921875, 0.6640625], [107, 108, 0.4765625, 0.6953125, 0.9921875, 0.1640625], [108, 109, 0.3515625, 0.0703125, 0.1171875, 0.7890625], [109, 110, 0.8515625, 0.5703125, 0.6171875, 0.2890625], [110, 111, 0.6015625, 0.3203125, 0.8671875, 0.5390625], ] ), } for dim_num in range(2, dim_max + 1): seed = 0 qs = prime_ge(dim_num) print("\n Using dimension DIM_NUM = %d" % dim_num) print("\n Seed Seed I4_SOBOL" " In Out\n") results = np.full((111, 2 + dim_num), np.nan) for i in range(111): [r, seed_out] = i4_sobol(dim_num, seed) if i < 5 or 105 < i: out = "%6d %6d " % (seed, seed_out) for j in range(dim_num): out += "%10f " % r[j] print(out) elif i == 6: print(" ......................") results[i, :] = [seed, seed_out] + list(r) seed = seed_out assert np.all(target[dim_num][0:5, :] == results[0:5, :]), "Start of array doesn't match" assert np.all(target[dim_num][5:10, :] == results[106:111, :]), "End of array doesn't match" return
def Generate(self): quasi, s = i4_sobol(len(VolFns), self.seed) self.seed = s return quasi
def sobol_test05(): """ sobol_test05 tests i4_sobol. """ print('' 'SOBOL_TEST05' ' I4_SOBOL computes the next element of a Sobol sequence.' '' ' In this test, we demonstrate how the SEED can be' ' manipulated to skip ahead in the sequence, or' ' to come back to any part of the sequence.' '') target = np.array([[0, 1, 0.000000, 0.000000, 0.000000], [1, 2, 0.500000, 0.500000, 0.500000], [2, 3, 0.750000, 0.250000, 0.750000], [3, 4, 0.250000, 0.750000, 0.250000], [4, 5, 0.375000, 0.375000, 0.625000], [100, 101, 0.4140625, 0.2578125, 0.3046875], [101, 102, 0.9140625, 0.7578125, 0.8046875], [102, 103, 0.6640625, 0.0078125, 0.5546875], [103, 104, 0.1640625, 0.5078125, 0.0546875], [104, 105, 0.2265625, 0.4453125, 0.7421875], [3, 4, 0.250000, 0.750000, 0.250000], [4, 5, 0.375000, 0.375000, 0.625000], [5, 6, 0.875000, 0.875000, 0.125000], [6, 7, 0.625000, 0.125000, 0.375000], [7, 8, 0.125000, 0.625000, 0.875000], [98, 99, 0.7890625, 0.3828125, 0.1796875], [99, 100, 0.2890625, 0.8828125, 0.6796875], [100, 101, 0.4140625, 0.2578125, 0.3046875], [101, 102, 0.9140625, 0.7578125, 0.8046875], [102, 103, 0.6640625, 0.0078125, 0.5546875]]) results = np.full_like(target, np.nan) dim_num = 3 print('' ' Using dimension DIM_NUM = %d\n' % dim_num) seed = 0 print('' ' Seed Seed I4_SOBOL' ' In Out' '') for i in range(5): [r, seed_out] = i4_sobol(dim_num, seed) out = '%6d %6d ' % (seed, seed_out) for j in range(1, dim_num + 1): out += '%10f ' % r[j - 1] print(out) results[i, :] = [seed, seed_out] + list(r) seed = seed_out print('' ' Jump ahead by increasing SEED:' '') seed = 100 print('' ' Seed Seed I4_SOBOL' ' In Out' '') for i in range(5): [r, seed_out] = i4_sobol(dim_num, seed) out = '%6d %6d ' % (seed, seed_out) for j in range(1, dim_num + 1): out += '%10f ' % r[j - 1] print(out) results[5 + i, :] = [seed, seed_out] + list(r) seed = seed_out print('' ' Jump back by decreasing SEED:' '') seed = 3 print('' ' Seed Seed I4_SOBOL' ' In Out' '') for i in range(5): [r, seed_out] = i4_sobol(dim_num, seed) out = '%6d %6d ' % (seed, seed_out) for j in range(1, dim_num + 1): out += '%10f ' % r[j - 1] print(out) results[10 + i, :] = [seed, seed_out] + list(r) seed = seed_out print('' ' Jump back by decreasing SEED:' '') seed = 98 print('' ' Seed Seed I4_SOBOL' ' In Out' '') for i in range(5): [r, seed_out] = i4_sobol(dim_num, seed) out = '%6d %6d ' % (seed, seed_out) for j in range(1, dim_num + 1): out += '%10f ' % r[j - 1] print(out) results[15 + i, :] = [seed, seed_out] + list(r) seed = seed_out assert np.all(target == results) return
def sobol(dim,seed): # Generates the seed-th term of the p-dimensional Sobol Sequence (WARNING P MUST BE <=40) return list(sob.i4_sobol(dim,seed)[0])
def create_recommendation(user): # Extract parameters. experiment_id = request.json["experiment_id"] # Get the experiment corresponding to this observation. exp = Experiment.query.filter_by(id=experiment_id).first() dims = exp.dimensions.all() n_dims = len(dims) space = create_space(dims) # Which acquisition function would you like to use? acq_func = request.json.get("acq_func", "expected_improvement") # Description of this observation or experiment. description = request.json.get("description", "") # Probability of selecting a random configuration. rand_prob = request.json.get("rand_prob", 0.) # Number of model samples to draw from the Bayesian posterior. n_models = request.json.get("n_models", 5) # Number of randomly positioned observations to create. n_random = 1 * n_dims # We'll include an indicator for whether or not to return a recommendation # for each computed model or to return a single recommendation for an # integrated acquisition function. integrate_acq = request.json.get("integrate_acq", True) # Either use Bayesian optimization or generate a random point depending on # the number of observations collected so far. Note that an input parameter # can be provided to determine the chance of selecting a configuration at # random. Setting this parameter to one is equivalent to assuming a policy # of pure exploration. n_observed = exp.observations.filter_by(pending=False).count() n_obs = exp.observations.count() if integrate_acq: rec = space.invert(sobol_seq.i4_sobol(n_dims, n_obs + 1)[0].ravel()) else: rec = space.invert([ sobol_seq.i4_sobol(n_dims, n_obs + 1 + i)[0] for i in range(n_models) ]) # If the number of observations exceeds the number of initialization # observations and we're not random sampling. if n_observed >= n_random and np.random.uniform() > rand_prob: # Create a flag that is true when the Bayesian optimization algorithm # fails due to numerical instability. optimization_failed = False # Get pending and non-pending observations. observed = exp.observations.filter(Observation.pending == False).all() pending = exp.observations.filter(Observation.pending == True).all() X, y = decode_recommendation(observed, dims) X_pending = (decode_recommendation(pending, dims)[0] if len(pending) > 0 else None) # Create a recommendation with Bayesian optimization. try: bo_rec = BayesianOptimization(exp, space).recommend( X, y, X_pending, GaussianProcess, n_models, acq_func, integrate_acq) bo_rec_inv = space.invert(bo_rec) except Exception as err: optimization_failed = True logging.error(traceback.format_exc()) # There are several failure modes that we are considering here. First, # there is a case where the chosen point is too close to any other point # we've previously evaluated (up to machine precision). Second, there is # an unusual case where the recommendation from the Bayesian # optimization procedure contains NaNs. Additionally, if the Bayesian # optimization algorithm failed due to numerical instability, this is # the third failure mode. if optimization_failed: print( "Optimization failed. Using Sobol recommendation: {}.".format( rec)) description += " Sobol" elif ((cdist(np.atleast_2d(bo_rec_inv), X) < 1e-10).any() or np.isnan(bo_rec_inv).any()): print( "Invalid recommendation: {}. Using Sobol recommendation: {}.". format(bo_rec_inv, rec)) description += " Sobol" else: rec = bo_rec_inv else: description += " Sobol" # Submit recommendation to user and store in the Thor database. It is # created initially without a response and is marked as pending. O = [] for r in np.atleast_2d(rec): obs = Observation(str(encode_recommendation(r, dims)), dt.datetime.today(), description) exp.observations.append(obs) O.append(obs) # Commit changes. db.session.commit() return jsonify([o.to_dict() for o in O])
def sobol_test04(): """ sobol_test04 tests i4_sobol. """ print('\nSOBOL_TEST04' ' I4_SOBOL returns the next element' ' of a Sobol sequence.' '\n In this test, we call I4_SOBOL repeatedly.') dim_max = 4 target = { 2: np.array([ [0, 1, 0.000000, 0.000000], [1, 2, 0.500000, 0.500000], [2, 3, 0.750000, 0.250000], [3, 4, 0.250000, 0.750000], [4, 5, 0.375000, 0.375000], # ...................... [106, 107, 0.9765625, 0.1953125], [107, 108, 0.4765625, 0.6953125], [108, 109, 0.3515625, 0.0703125], [109, 110, 0.8515625, 0.5703125], [110, 111, 0.6015625, 0.3203125] ]), 3: np.array([ [0, 1, 0.000000, 0.000000, 0.000000], [1, 2, 0.500000, 0.500000, 0.500000], [2, 3, 0.750000, 0.250000, 0.750000], [3, 4, 0.250000, 0.750000, 0.250000], [4, 5, 0.375000, 0.375000, 0.625000], # ...................... [106, 107, 0.9765625, 0.1953125, 0.4921875], [107, 108, 0.4765625, 0.6953125, 0.9921875], [108, 109, 0.3515625, 0.0703125, 0.1171875], [109, 110, 0.8515625, 0.5703125, 0.6171875], [110, 111, 0.6015625, 0.3203125, 0.8671875] ]), 4: np.array([ [0, 1, 0.000000, 0.000000, 0.000000, 0.000000], [1, 2, 0.500000, 0.500000, 0.500000, 0.500000], [2, 3, 0.750000, 0.250000, 0.750000, 0.250000], [3, 4, 0.250000, 0.750000, 0.250000, 0.750000], [4, 5, 0.375000, 0.375000, 0.625000, 0.125000], # ...................... [106, 107, 0.9765625, 0.1953125, 0.4921875, 0.6640625], [107, 108, 0.4765625, 0.6953125, 0.9921875, 0.1640625], [108, 109, 0.3515625, 0.0703125, 0.1171875, 0.7890625], [109, 110, 0.8515625, 0.5703125, 0.6171875, 0.2890625], [110, 111, 0.6015625, 0.3203125, 0.8671875, 0.5390625] ]) } for dim_num in range(2, dim_max + 1): seed = 0 qs = prime_ge(dim_num) print('\n Using dimension DIM_NUM = %d' % dim_num) print('\n Seed Seed I4_SOBOL' ' In Out\n') results = np.full((111, 2 + dim_num), np.nan) for i in range(111): [r, seed_out] = i4_sobol(dim_num, seed) if (i < 5 or 105 < i): out = '%6d %6d ' % (seed, seed_out) for j in range(dim_num): out += '%10f ' % r[j] print(out) elif (i == 6): print(' ......................') results[i, :] = [seed, seed_out] + list(r) seed = seed_out assert np.all(target[dim_num][0:5, :] == results[ 0:5, :]), "Start of array doesn't match" assert np.all(target[dim_num][5:10, :] == results[ 106:111, :]), "End of array doesn't match" return
def _ask(self): vector, self.seed = i4_sobol(self.dim, self.seed) for index, param in enumerate(self.param_space): vector[index] = (param.high - param.low) * vector[index] + param.low return ParameterVector().from_array(vector, self.param_space)
def __init__(self, dim): quasi, s = i4_sobol(len(VolFns), self.seed) self.seed = s
def gen(self, n=1, outfile=None, nstop=1000000, reset=False): # maximum number of steps to try nmax = n * 1000 + nstop import sobol_seq as so #output file specified if outfile is not None: fout = open(outfile, 'w') writer = csv.writer(fout, lineterminator='\n', delimiter=' ') # generate the same points again if reset: self.npt = 1 ngen = 0 #cutoff to prevent infinite loop nstep = 0 result = [] #adaptive strategy flag adapt = False while ngen < n and nstep < nmax and not adapt: pt_test, self.npt = so.i4_sobol(self.dim, self.npt) # scale to boundary pt_test = self.dim_low + np.asarray( pt_test, dtype=np.double) * (self.dim_high - self.dim_low) #test if working if self.valid(pt_test): result.append(pt_test) # write to output if outfile is not None: writer.writerow(pt_test) ngen += 1 nstep += 1 # check and see if generation is going too slowly... if nstep >= 1000 and ngen < 100: print( "basic algorithm is too slow, switching to an adaptive algorithm." ) adapt = True if not adapt: print( "basic algorithm generated {0:d} proper points.".format(ngen)) else: print("Beginning adaptive strategy...") # define an integrand that will aid sampling # utilize the vegas package for adaptive sampling def integrand(x): pt = np.asarray(x) fx = 1.0 for expr in self.constraints: val = expr.evalf( subs={s: v for s, v in zip(self.spvar_list, pt)}) if val < 0: return 0 # cap function off at 1 fx *= (val if val < 1.0 else 1.0) return fx # declare vegas integration, will be used for random number generation integrator = vegas.Integrator( [[l, h] for l, h in zip(self.dim_low, self.dim_high)]) # try performing the integral and give points generated # max number of iterations max_it = 3 nval = 5 * n it = 0 while it < max_it: # the integrator will generate points for us int_reult = integrator(integrand, nitn=5, neval=nval) nval *= 5 integrator_list = [ np.asarray(x) for x, wt in integrator.random() if integrand(x) > 0 ] print( "adaptive algorithm generated {0:d} proper points.".format( len(integrator_list))) # if not enough good sampled points, try higher neval if len(integrator_list) < n and it < max_it - 1: it += 1 continue else: if len(integrator_list) < n: ngen = len(integrator_list) else: ngen = n result = integrator_list[:ngen] for i in result: if not self.valid(i): print("NOT valid!") if outfile is not None: for pt_test in result: writer.writerow(pt_test) break if outfile is not None: fout.close() if ngen < n: print('only {0:d} points are successfully generated.'.format(ngen)) return np.asarray(result, dtype=np.double)