def parallel_execute(run, path_to_file, prob, obj, dims): actual_objectives_nds = None print(run) path_to_file = path_to_file + '/Run_' + str(run) infile = open(path_to_file, 'rb') results_data = pickle.load(infile) infile.close() individual_nds = results_data["individuals_solutions"] surrogate_objectives_nds = results_data["obj_solutions"] for i in range(np.shape(individual_nds)[0]): if i == 0: actual_objectives_nds = np.asarray( f(prob, obj, dims, individual_nds[i, :])) else: actual_objectives_nds = np.vstack( (actual_objectives_nds, f(prob, obj, dims, individual_nds[i, :]))) eval_objs = actual_objectives_nds print(np.shape(actual_objectives_nds)) if np.shape(individual_nds)[0] > 1: non_dom_front = ndx(actual_objectives_nds) actual_objectives_nds = actual_objectives_nds[non_dom_front[0][0]] actual_individual_nds = individual_nds[non_dom_front[0][0]] else: actual_objectives_nds = actual_objectives_nds.reshape(1, obj) actual_individual_nds = individual_nds.reshape(1, dims) print(np.amax(surrogate_objectives_nds, axis=0)) print(np.amax(actual_objectives_nds, axis=0)) results_dict = { 'individual_nds': individual_nds, 'surrogate_objectives_nds': surrogate_objectives_nds, 'actual_individual_nds': actual_individual_nds, 'actual_objectives_nds': actual_objectives_nds } # Write the non-dom sorted solutions plt.rcParams["text.usetex"] = True fig = plt.figure(1, figsize=(6, 6)) ax = fig.add_subplot(111) fig = plt.figure(1, figsize=(6, 6)) fig.set_size_inches(5, 5) ax = fig.add_subplot(111) ax.set_xlabel('$f_1$') ax.set_ylabel('$f_2$') plt.xlim(-1, 10) plt.ylim(-1, 10) plt.scatter(eval_objs[:, 0], eval_objs[:, 1]) plt.scatter(surrogate_objectives_nds[:, 0], surrogate_objectives_nds[:, 1]) #plt.show() fig.savefig('dtlz6_xx_' + str(run) + '.pdf') plt.clf() plt.cla() path_to_file = path_to_file + '_NDS' outfile = open(path_to_file, 'wb') pickle.dump(results_dict, outfile) outfile.close() print("File written...") print(np.shape(actual_individual_nds))
def parallel_execute(run, path_to_file, prob, obj): actual_objectives_nds = None print(run) path_to_file = path_to_file + '/Run_' + str(run) infile = open(path_to_file, 'rb') results_data = pickle.load(infile) infile.close() print("Non-dominated sorting ...") print(np.shape(results_data["objectives_archive"])) non_dom_front = ndx(results_data["objectives_archive"]) individual_nds = results_data["individual_archive"][non_dom_front[0][0]] surrogate_objectives_nds = results_data["objectives_archive"][ non_dom_front[0][0]] for i in range(np.shape(individual_nds)[0]): if i == 0: actual_objectives_nds = np.asarray( f(prob, obj, dims, individual_nds[i, :])) else: actual_objectives_nds = np.vstack( (actual_objectives_nds, f(prob, obj, dims, individual_nds[i, :]))) print(np.shape(actual_objectives_nds)) if np.shape(individual_nds)[0] > 1: non_dom_front = ndx(actual_objectives_nds) actual_objectives_nds = actual_objectives_nds[non_dom_front[0][0]] actual_individual_nds = individual_nds[non_dom_front[0][0]] else: actual_objectives_nds = actual_objectives_nds.reshape(1, obj) actual_individual_nds = individual_nds.reshape(1, dims) print(np.amax(surrogate_objectives_nds, axis=0)) print(np.amax(actual_objectives_nds, axis=0)) results_dict = { 'individual_nds': individual_nds, 'surrogate_objectives_nds': surrogate_objectives_nds, 'actual_individual_nds': actual_individual_nds, 'actual_objectives_nds': actual_objectives_nds } # Write the non-dom sorted solutions path_to_file = path_to_file + '_NDS' outfile = open(path_to_file, 'wb') pickle.dump(results_dict, outfile) outfile.close() print("File written...") print(np.shape(actual_individual_nds))
def parallel_execute(run, path_to_file, prob, obj, dims, mat, name, sampling): dataset = ((mat['Initial_Population_' + problem_testbench])[0][run])[0] if problem_testbench == 'DDMOPP': mat = scipy.io.loadmat('./' + folder_data + '/Obj_vals_DDMOPP_' + sampling + '_AM_' + name + '_' + str(obj) + '_' + str(n_vars) + '_109.mat') y = ((mat['Obj_vals_DDMOPP'])[0][run])[0] else: y = None for ind in dataset: if y is None: y = np.asarray( f(name=prob, num_of_objectives_real=obj, num_of_variables=n_vars, x=ind)) else: y = np.vstack((y, f(name=prob, num_of_objectives_real=obj, num_of_variables=n_vars, x=ind))) actual_objectives_nds = y print(run) individual_nds = dataset surrogate_objectives_nds = y if np.shape(individual_nds)[0] > 1: non_dom_front = ndx(actual_objectives_nds) actual_objectives_nds = actual_objectives_nds[non_dom_front[0][0]] actual_individual_nds = individual_nds[non_dom_front[0][0]] else: actual_objectives_nds = actual_objectives_nds.reshape(1, obj) actual_individual_nds = individual_nds.reshape(1, dims) print(np.amax(surrogate_objectives_nds, axis=0)) print(np.amax(actual_objectives_nds, axis=0)) results_dict = { 'individual_nds': individual_nds, 'surrogate_objectives_nds': surrogate_objectives_nds, 'actual_individual_nds': actual_individual_nds, 'actual_objectives_nds': actual_objectives_nds } # Write the non-dom sorted solutions if problem_testbench == 'DDMOPP': path_to_file = path_to_file + '/Run_' + str(run) + '_SOLN' else: path_to_file = path_to_file + '/Run_' + str(run) + '_NDS' outfile = open(path_to_file, 'wb') pickle.dump(results_dict, outfile) outfile.close() print("File written...") print(np.shape(actual_individual_nds))
def parallel_execute(run, path_to_file): if metric is 'IGD': path_to_file = path_to_file + '/Run_' + str(run) + '_NDS' infile = open(path_to_file, 'rb') results_data = pickle.load(infile) infile.close() non_dom_front = results_data["actual_objectives_nds"] non_dom_surr = results_data["surrogate_objectives_nds"] # print(np.shape(non_dom_surr)) # print((np.max(non_dom_front,axis=0))) solution_ratio = np.shape(non_dom_front)[0] / np.shape(non_dom_surr)[0] return [igd(pareto_front, non_dom_front), solution_ratio] else: if problem_testbench is 'DDMOPP': soln_all = [] if mode != 0: path_to_file1 = path_to_file + '/Run_' + str(run + 1) + '_surr_all' with open(path_to_file1, 'r') as f: reader = csv.reader(f) for line in reader: soln_all.append(line) else: path_to_file1 = path_to_file + '/Run_' + str(run) + '_SOLN' infile = open(path_to_file1, 'rb') results_data = pickle.load(infile) infile.close() soln_all = results_data["actual_objectives_nds"] print(np.shape(soln_all)) soln_all = np.tile(soln_all, (400, 1)) soln_all = np.array(soln_all, dtype=np.float32) r0=np.ones(obj) r1=np.ones(obj)*-1 dx=distance.euclidean(r0,r1) ref=np.ones(obj)*dx #ref = [2] * obj print(ref) else: """ path_to_file1 = path_to_file + '/Run_' + str(run + 1) + '_soln' soln_all = [] with open(path_to_file1, 'r') as f: reader = csv.reader(f) for line in reader: soln_all.append(line) soln_all = np.array(soln_all, dtype=np.float32) """ soln_all = [] if mode != 0: path_to_file1 = path_to_file + '/Run_' + str(run + 1) + '_surr_all' with open(path_to_file1, 'r') as f: reader = csv.reader(f) for line in reader: soln_all.append(line) else: path_to_file1 = path_to_file + '/Run_' + str(run) + '_NDS' infile = open(path_to_file1, 'rb') results_data = pickle.load(infile) infile.close() soln_all = results_data["actual_objectives_nds"] print(np.shape(soln_all)) soln_all = np.tile(soln_all, (400, 1)) soln_all = np.array(soln_all, dtype=np.float32) ref = hv_ref[prob][str(obj)] # print(actual_objectives_nds) hv_iter = [] print(np.shape(soln_all)) max_iter = np.min((np.shape(soln_all)[0], int(f_eval_limit))) f_evals_per = int(np.shape(soln_all)[0] / f_iters) # for f_iter in range(int(max_iter / f_evals_per)): for f_iter in range(f_iters): # soln_all_temp = soln_all[f_iter * f_evals_per:(f_iter + 1) * f_evals_per, :] if f_iter == 0: soln_all_temp = soln_all[0:100, :] else: soln_all_temp = soln_all[f_iter * f_evals_per:(f_iter + 1) * f_evals_per, :] if np.shape(soln_all_temp)[0] > 1: non_dom_front = ndx(soln_all_temp) soln_iter = soln_all_temp[non_dom_front[0][0]] else: soln_iter = soln_all_temp.reshape(1, obj) hyp = hv(soln_iter) hyp_temp = hyp.compute(ref) """ if f_iter == 0 and mode == 0: hyp_start[run] = hyp_temp print(hyp_start[run]) if f_iter == 0 and mode == 9: hyp_temp = hyp_start[run] print(hyp_temp) print(hyp_start) """ # hv_iter = hyp_temp # else: # hv_iter = np.vstack((hv_iter, hyp_temp)) hv_iter = np.append(hv_iter, hyp_temp) # print(hv_iter) return hv_iter
def parallel_execute(run, path_to_file): if metric == 'IGD': path_to_file = path_to_file + '/Run_' + str( run) + '_NDS' infile = open(path_to_file, 'rb') results_data = pickle.load(infile) infile.close() non_dom_front = results_data[ "actual_objectives_nds"] non_dom_surr = results_data[ "surrogate_objectives_nds"] #print(np.shape(non_dom_surr)) #print((np.max(non_dom_front,axis=0))) solution_ratio = np.shape(non_dom_front)[ 0] / np.shape(non_dom_surr)[0] return [ igd(pareto_front, non_dom_front), solution_ratio ] else: if problem_testbench == 'DDMOPP': if mode == 9: path_to_file = path_to_file + '/Run_' + str( run + 1) + '_soln' actual_objectives_nds = [] with open(path_to_file, 'r') as f: reader = csv.reader(f) for line in reader: actual_objectives_nds.append( line) else: path_to_file = path_to_file + '/Run_' + str( run) + '_SOLN' infile = open(path_to_file, 'rb') results_data = pickle.load(infile) infile.close() if mode == 0: actual_objectives_nds = results_data[ "actual_objectives_nds"] else: actual_objectives_nds = results_data[ "obj_solns"] actual_objectives_nds = np.array( actual_objectives_nds, dtype=np.float32) r0 = np.ones(obj) r1 = np.ones(obj) * -1 dx = distance.euclidean(r0, r1) ref = np.ones(obj) * dx #ref = [2]*obj else: if mode == 9: path_to_file = path_to_file + '/Run_' + str( run + 1) + '_soln' actual_objectives_nds = [] with open(path_to_file, 'r') as f: reader = csv.reader(f) for line in reader: actual_objectives_nds.append( line) actual_objectives_nds = np.array( actual_objectives_nds, dtype=np.float32) else: path_to_file = path_to_file + '/Run_' + str( run) + '_NDS' infile = open(path_to_file, 'rb') results_data = pickle.load(infile) infile.close() actual_objectives_nds = results_data[ "actual_objectives_nds"] #non_dom_surr = results_data["surrogate_objectives_nds"] ref = hv_ref[prob][str(obj)] #print(actual_objectives_nds) if np.shape(actual_objectives_nds)[0] > 1: non_dom_front = ndx(actual_objectives_nds) actual_objectives_nds = actual_objectives_nds[ non_dom_front[0][0]] else: actual_objectives_nds = actual_objectives_nds.reshape( 1, obj) print(np.shape(actual_objectives_nds)) solution_ratio = 0 hyp = hv(actual_objectives_nds) hv_x = hyp.compute(ref) #print(np.amax(actual_objectives_nds,axis=0)) #ax.scatter(actual_objectives_nds[:, 0], actual_objectives_nds[:, 1],actual_objectives_nds[:, 2]) #ax.scatter(actual_objectives_nds[:, 0], actual_objectives_nds[:, 1]) return [hv_x, solution_ratio]
def parallel_execute(run, path_to_file, prob, obj): actual_objectives_nds = None print(run) path_to_file = path_to_file + '/Run_' + str(run) infile = open(path_to_file, 'rb') results_data = pickle.load(infile) infile.close() individual_nds = results_data["individuals_solutions"] surrogate_objectives_nds = results_data["obj_solutions"] individual_archive = results_data["individual_archive"] objective_archive = results_data["objectives_archive"] #print(np.shape(individual_nds)) if type(individual_archive) is dict: individual_archive = np.vstack(individual_archive.values()) objective_archive = np.vstack(objective_archive.values()) #print(np.shape(individual_archive)) #print(np.shape(objective_archive)) #print("......") path_to_file2 = path_to_file + '_pop' with open(path_to_file2, 'w') as f: writer = csv.writer(f) for line in individual_nds: writer.writerow(line) path_to_file3 = path_to_file + '_obj' with open(path_to_file3, 'w') as f: writer = csv.writer(f) for line in surrogate_objectives_nds: writer.writerow(line) print("testbench", problem_testbench == 'DTLZ') if problem_testbench == 'DTLZ': for i in range(np.shape(individual_archive)[0]): if i == 0: actual_objectives = np.asarray( fx(prob, obj, dims[0], individual_archive[i, :])) else: actual_objectives = np.vstack((actual_objectives, fx(prob, obj, dims[0], individual_archive[i, :]))) path_to_file4 = path_to_file + '_soln_all' with open(path_to_file4, 'w') as f: writer = csv.writer(f) for line in actual_objectives: writer.writerow(line) for i in range(np.shape(individual_nds)[0]): if i == 0: actual_objectives_nds = np.asarray( fx(prob, obj, dims[0], individual_nds[i, :])) else: actual_objectives_nds = np.vstack((actual_objectives_nds, fx(prob, obj, dims[0], individual_nds[i, :]))) eval_objs = actual_objectives_nds print(np.shape(actual_objectives_nds)) if np.shape(individual_nds)[0] > 1: non_dom_front = ndx(actual_objectives_nds) actual_objectives_nds = actual_objectives_nds[non_dom_front[0][0]] actual_individual_nds = individual_nds[non_dom_front[0][0]] else: actual_objectives_nds = actual_objectives_nds.reshape(1, obj) actual_individual_nds = individual_nds.reshape(1, dims) print(np.amax(surrogate_objectives_nds, axis=0)) print(np.amax(actual_objectives_nds, axis=0)) results_dict = { 'individual_nds': individual_nds, 'surrogate_objectives_nds': surrogate_objectives_nds, 'actual_individual_nds': actual_individual_nds, 'actual_objectives_nds': actual_objectives_nds } path_to_file4x = path_to_file + '_NDS' outfile = open(path_to_file4x, 'wb') pickle.dump(results_dict, outfile) outfile.close() path_to_file5 = path_to_file + '_pop_all' with open(path_to_file5, 'w') as f: writer = csv.writer(f) for line in individual_archive: writer.writerow(line) path_to_file6 = path_to_file + '_surr_all' with open(path_to_file6, 'w') as f: writer = csv.writer(f) for line in objective_archive: writer.writerow(line) print("File written...")
def run_optimizer_strat3(problem_testbench, problem_name, nobjs, nvars, sampling, nsamples, is_data, surrogate_type, run): start = time.time() time_taken_all = 0 inducing_inputs_dict = {} inducing_inputs_dict_2 = {} prediction_all = None prediction = [] Z = [] frac = 0.8 print("Read dataset...") if is_data is True: x, y = read_dataset(problem_testbench, problem_name, nobjs, nvars, sampling, nsamples, run) print("Non dominated sorting...") # find non-domiated samples if np.shape(y)[0] > 1: non_dom_front_samples = ndx(y) y_nd = y[non_dom_front_samples[0][0]] x_nd = x[non_dom_front_samples[0][0]] else: y_nd = y.reshape(1, nobjs) x_nd = x.reshape(1, nobjs) num_non_dom_samples = np.shape(x_nd)[0] print("Non dominated samples") print(y_nd) z_samples = max_samples - num_non_dom_samples if z_samples <= max_samples * frac: z_samples = int(max_samples * frac) print("Non-dom samples = ") print(num_non_dom_samples) print("1st model samples = ") print(z_samples) print("Building first model...") # build sparseGP model with (max_samples - non_dom_samples) surrogate_problem, time_taken = build_surrogates( problem_testbench, problem_name, nobjs, nvars, nsamples, is_data, x, y, "generic_sparsegp", z_samples={'z_samples': z_samples}) print("Models built!") # get the samples used in the sparse model and their indices for i in range(nobjs): inducing_inputs_dict[str(i)] = np.asarray( surrogate_problem.objectives[i]._model.m.inducing_inputs) print("The inducing inputs:") print(inducing_inputs_dict) # predict all the samples objective values for i in range(nobjs): for j in range(nsamples): pred = np.asarray(surrogate_problem.objectives[i]._model.predict( x[j].reshape(1, nvars))).reshape(1, 2)[0, 0] prediction.append(pred) prediction = np.asarray(prediction).reshape((2000, 1)) if prediction_all is None: prediction_all = prediction else: prediction_all = np.hstack((prediction_all, prediction)) prediction = [] y_pred = prediction_all print("Prediciton All:") print(y_pred) # perform non dom sort on the predicted samples if np.shape(y)[0] > 1: non_dom_front = ndx(y_pred) y_pred_nd = y_pred[non_dom_front[0][0]] x_pred_nd = x[non_dom_front[0][0]] else: y_pred_nd = y_pred.reshape(1, nobjs) x_pred_nd = x.reshape(1, nobjs) num_non_dom = np.shape(x_pred_nd)[0] print("Non dominated predictions") print(y_pred_nd) # check whether the actual non-dom samples are still non dominated. non_dom_add_index = np.setxor1d(non_dom_front[0][0], non_dom_front_samples[0][0], assume_unique=True) # if not, include them along with the sparse samples. if np.shape(non_dom_add_index)[0] > (max_samples - z_samples): non_dom_max = max_samples - z_samples else: non_dom_max = np.shape(non_dom_add_index)[0] x_add = x[non_dom_add_index[0:non_dom_max]] y_add = y[non_dom_add_index[0:non_dom_max]] for i in range(nobjs): inducing_inputs_dict_2['Z'] = np.vstack((np.asarray( surrogate_problem.objectives[i]._model.m.inducing_inputs), x_add)) Z.append(inducing_inputs_dict_2) print("Samples to be used for 2nd model:") print(np.shape(inducing_inputs_dict_2['Z'])) inducing_inputs_dict_2 = {} # rebuild the models print("Building 2nd surogates...") surrogate_problem_final, time_taken = build_surrogates( problem_testbench, problem_name, nobjs, nvars, nsamples, is_data, x, y, "generic_sparsegp_strat1", Z=Z) print("Finished building 2nd surrogates!") # perform optimization end = time.time() time_taken = end - start time_taken_all = time_taken_all + time_taken print("Total time taken:") print(time_taken_all) population = optimize_surrogates(surrogate_problem_final) print("Optimization completed!") results_dict = { 'individual_archive': population.individuals_archive, 'objectives_archive': population.objectives_archive, 'uncertainty_archive': population.uncertainty_archive, 'individuals_solutions': population.individuals, 'obj_solutions': population.objectives, 'uncertainty_solutions': population.uncertainity, 'time_taken': time_taken_all } return results_dict
def run_optimizer_strat1(problem_testbench, problem_name, nobjs, nvars, sampling, nsamples, is_data, surrogate_type, run): time_taken_all = 0 inducing_inputs_dict = {} Z = [] if is_data is True: x, y = read_dataset(problem_testbench, problem_name, nobjs, nvars, sampling, nsamples, run) # find non-domiated samples if np.shape(y)[0] > 1: non_dom_front = ndx(y) y_nd = y[non_dom_front[0][0]] x_nd = x[non_dom_front[0][0]] else: y_nd = y.reshape(1, nobjs) x_nd = x.reshape(1, nobjs) num_non_dom = np.shape(x_nd)[0] z_samples = max_samples - num_non_dom print("Non-dom samples = ") print(num_non_dom) print("1st model sample = ") print(z_samples) # build sparseGP model with 70% cost surrogate_problem, time_taken = build_surrogates( problem_testbench, problem_name, nobjs, nvars, nsamples, is_data, x, y, "generic_sparsegp", z_samples={'z_samples': z_samples}) # Add non-dominated samples strategy for i in range(nobjs): inducing_inputs_dict['Z'] = np.vstack((np.asarray( surrogate_problem.objectives[i]._model.m.inducing_inputs), x_nd)) Z.append(inducing_inputs_dict) inducing_inputs_dict = {} #population = optimize_surrogates(surrogate_problem) time_taken_all = time_taken_all + time_taken print(time_taken) # find K nearest neighbour samples with remaining 30% cost # ---- to be done later # choose few points by clustering ---- later # Build sparseGP with the combination of previous optimized points and KNN points surrogate_problem_2, time_taken = build_surrogates( problem_testbench, problem_name, nobjs, nvars, nsamples, is_data, x, y, "generic_sparsegp_strat1", Z=Z) time_taken_all = time_taken_all + time_taken print(time_taken) print("Total time taken:") print(time_taken_all) population = optimize_surrogates(surrogate_problem_2) results_dict = { 'individual_archive': population.individuals_archive, 'objectives_archive': population.objectives_archive, 'uncertainty_archive': population.uncertainty_archive, 'individuals_solutions': population.individuals, 'obj_solutions': population.objectives, 'uncertainty_solutions': population.uncertainity, 'time_taken': time_taken_all } return results_dict