def solve_opf_dycors_parallel(problem: AcOPFBlackBox, maxeval=1000, nthreads=4, verbose=False): """ :param problem: :return: """ print(problem.info) # (2) Experimental design # Use a symmetric Latin hypercube with 2d + 1 samples exp_des = SymmetricLatinHypercube(dim=problem.dim, npts=2 * problem.dim + 1) # (3) Surrogate model # Use a cubic RBF interpolant with a linear tail surrogate = RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=maxeval) # (4) Adaptive sampling # Use DYCORS with 100d candidate points adapt_samp = CandidateDYCORS(data=problem, numcand=100 * problem.dim) # Use the threaded controller controller = ThreadController() # (5) Use the sychronous strategy without non-bound constraints # Use 4 threads and allow for 4 simultaneous evaluations strategy = SyncStrategyNoConstraints(worker_id=0, data=problem, maxeval=maxeval, nsamples=nthreads, exp_design=exp_des, response_surface=surrogate, sampling_method=adapt_samp) controller.strategy = strategy # Launch the threads and give them access to the objective function for _ in range(nthreads): worker = BasicWorkerThread(controller, problem.objfunction) controller.launch_worker(worker) # Run the optimization strategy result = controller.run() # Print the final result if verbose: print('Best value found: {0}'.format(result.value)) print('Best solution found: {0}'.format( np.array_str(result.params[0], max_line_width=np.inf, precision=5, suppress_small=True))) return result.value
def optimization_trial(pname, data, epsilon, nthreads, maxeval, num): nsamples = nthreads print("Trial Number:" + str(num)) # Create a strategy and a controller controller = ThreadController() controller.strategy = \ MoSyncStrategyNoConstraints( worker_id=0, data=data, maxeval=maxeval, nsamples=nsamples, exp_design=SymmetricLatinHypercube(dim=data.dim, npts=2*(data.dim+1)), response_surface=RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=maxeval), sampling_method=EvolutionaryAlgorithm(data,epsilons=epsilon, cand_flag=1), archiving_method=EpsilonArchive(size_max=200,epsilon=epsilon)) # Launch the threads and give them access to the objective function for _ in range(nthreads): worker = BasicWorkerThread(controller, data.objfunction) controller.launch_worker(worker) # Run the optimization strategy def merit(r): return r.value[0] result = controller.run(merit=merit) # Save results to File X = np.loadtxt('final.txt') controller.strategy.save_plot(num) fname = pname + '_' + str(data.dim) + '_EGOMORS_' + str(maxeval) + '_' + str(num) + '_' + str(nthreads) + '.txt' np.savetxt(fname, X)
def solve_opf_dycors_serial(problem: AcOPFBlackBox, maxeval=1000, verbose=False, stop_at=False, stop_value=0): """ :param problem: :param maxeval: :param verbose: :param stop_at: :param stop_value: :return: """ print(problem.info) # (2) Experimental design # Use a symmetric Latin hypercube with 2d + 1 samples exp_des = SymmetricLatinHypercube(dim=problem.dim, npts=2 * problem.dim + 1) # (3) Surrogate model # Use a cubic RBF interpolant with a linear tail surrogate = RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=maxeval) # (4) Adaptive sampling # Use DYCORS with 100d candidate points adapt_samp = CandidateDYCORS(data=problem, numcand=100 * problem.dim) # Use the serial controller (uses only one thread) controller = SerialController(problem.objfunction) # (5) Use the sychronous strategy without non-bound constraints strategy = SyncStrategyNoConstraints(worker_id=0, data=problem, maxeval=maxeval, nsamples=1, exp_design=exp_des, response_surface=surrogate, sampling_method=adapt_samp) controller.strategy = strategy # Run the optimization strategy result = controller.run(stop_at=stop_at, stop_value=stop_value) # Print the final result if verbose: print('Best value found: {0}'.format(result.value)) print('Best solution found: {0}'.format( np.array_str(result.params[0], max_line_width=np.inf, precision=5, suppress_small=True))) # the result is x return result.value, result.params[0]
def main(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/test_penalty.log"): os.remove("./logfiles/test_penalty.log") logging.basicConfig(filename="./logfiles/test_penalty.log", level=logging.INFO) print("\nNumber of threads: 4") print("Maximum number of evaluations: 500") print("Sampling method: CandidateDYCORS") print("Experimental design: Symmetric Latin Hypercube") print("Surrogate: Cubic RBF") nthreads = 4 maxeval = 500 penalty = 1e6 nsamples = nthreads data = Keane(dim=10) print(data.info) # Create a strategy and a controller controller = ThreadController() controller.strategy = \ SyncStrategyPenalty( worker_id=0, data=data, maxeval=maxeval, nsamples=nsamples, response_surface=RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=maxeval), exp_design=SymmetricLatinHypercube(dim=data.dim, npts=2*(data.dim+1)), sampling_method=CandidateDYCORS(data=data, numcand=100*data.dim), penalty=penalty) # Launch the threads for _ in range(nthreads): worker = BasicWorkerThread(controller, data.objfunction) controller.launch_worker(worker) # Use penalty based merit def feasible_merit(record): xx = np.zeros((1, record.params[0].shape[0])) xx[0, :] = record.params[0] return record.value + controller.strategy.penalty_fun(xx)[0, 0] result = controller.run(merit=feasible_merit) best, xbest = result.value, result.params[0] print('Best value: {0}'.format(best)) print('Best solution: {0}'.format( np.array_str(xbest, max_line_width=np.inf, precision=5, suppress_small=True))) print('Feasible: {0}\n'.format( np.max(data.eval_ineq_constraints(xbest)) <= 0.0))
def main(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/test_subprocess_files.log"): os.remove("./logfiles/test_subprocess_files.log") logging.basicConfig(filename="./logfiles/test_subprocess_files.log", level=logging.INFO) print("\nNumber of threads: 4") print("Maximum number of evaluations: 200") print("Sampling method: Candidate DYCORS") print("Experimental design: Symmetric Latin Hypercube") print("Surrogate: Cubic RBF") assert os.path.isfile("./sphere_ext_files"), "You need to build sphere_ext" nthreads = 4 maxeval = 200 nsamples = nthreads data = Sphere(dim=10) print(data.info) # Create a strategy and a controller controller = ThreadController() controller.strategy = \ SyncStrategyNoConstraints( worker_id=0, data=data, maxeval=maxeval, nsamples=nsamples, exp_design=SymmetricLatinHypercube(dim=data.dim, npts=2*(data.dim+1)), sampling_method=CandidateDYCORS(data=data, numcand=100*data.dim), response_surface=RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=maxeval)) # Launch the threads and give them access to the objective function for i in range(nthreads): worker = CppSim(controller) worker.my_filename = str(i) + ".txt" controller.launch_worker(worker) # Run the optimization strategy result = controller.run() print('Best value found: {0}'.format(result.value)) print('Best solution found: {0}\n'.format( np.array_str(result.params[0], max_line_width=np.inf, precision=5, suppress_small=True)))
def main(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/test_simple_time.log"): os.remove("./logfiles/test_simple_time.log") logging.basicConfig(filename="./logfiles/test_simple_time.log", level=logging.INFO) print("\nNumber of threads: 4") print("Time budget: 30 seconds") print("Sampling method: CandidateDYCORS") print("Experimental design: Symmetric Latin Hypercube") print("Surrogate: Cubic RBF") nthreads = 4 maxeval = -30 nsamples = nthreads data = Ackley(dim=10) print(data.info) # Create a strategy and a controller controller = ThreadController() controller.strategy = \ SyncStrategyNoConstraints( worker_id=0, data=data, maxeval=maxeval, nsamples=nsamples, exp_design=SymmetricLatinHypercube(dim=data.dim, npts=2*(data.dim+1)), response_surface=RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=1000), sampling_method=CandidateDYCORS(data=data, numcand=100*data.dim)) # Launch the threads and give them access to the objective function for _ in range(nthreads): worker = BasicWorkerThread(controller, data.objfunction) controller.launch_worker(worker) # Run the optimization strategy start_time = time.time() result = controller.run() end_time = time.time() print('Run time: {0} seconds'.format(end_time - start_time)) print('Best value found: {0}'.format(result.value)) print('Best solution found: {0}\n'.format( np.array_str(result.params[0], max_line_width=np.inf, precision=5, suppress_small=True)))
def main(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/test_multisampling.log"): os.remove("./logfiles/test_multisampling.log") logging.basicConfig(filename="./logfiles/test_multisampling.log", level=logging.INFO) print("\nNumber of threads: 1") print("Maximum number of evaluations: 500") print( "Sampling method: CandidateDYCORS, Genetic Algorithm, Multi-Start Gradient" ) print("Experimental design: Latin Hypercube") print("Surrogate: Cubic RBF") nthreads = 1 maxeval = 500 nsamples = nthreads data = Ackley(dim=10) print(data.info) # Create a strategy and a controller sampling_method = [ CandidateDYCORS(data=data, numcand=100 * data.dim), GeneticAlgorithm(data=data), MultiStartGradient(data=data) ] controller = SerialController(data.objfunction) controller.strategy = \ SyncStrategyNoConstraints( worker_id=0, data=data, maxeval=maxeval, nsamples=nsamples, response_surface=RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=maxeval), exp_design=SymmetricLatinHypercube(dim=data.dim, npts=2*(data.dim + 1)), sampling_method=MultiSampling(sampling_method, [0, 1, 0, 2])) result = controller.run() best, xbest = result.value, result.params[0] print('Best value: {0}'.format(best)) print('Best solution: {0}\n'.format( np.array_str(xbest, max_line_width=np.inf, precision=5, suppress_small=True)))
def main(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/test_simple.log"): os.remove("./logfiles/test_simple.log") logging.basicConfig(filename="./logfiles/test_simple.log", level=logging.INFO) nthreads = 4 maxeval = 100 nsamples = nthreads print("\nNumber of threads: " + str(nthreads)) print("Maximum number of evaluations: " + str(maxeval)) print("Sampling method: Mixed") print("Experimental design: Symmetric Latin Hypercube") print("Surrogate: Cubic RBF") #data = LZF3() data = DTLZ4(nobj=2) num = 1 epsilons = [0.05, 0.05] # Create a strategy and a controller controller = ThreadController() #controller = SerialController(data.objfunction) controller.strategy = \ MoSyncStrategyNoConstraints( worker_id=0, data=data, maxeval=maxeval, nsamples=nsamples, exp_design=SymmetricLatinHypercube(dim=data.dim, npts=2*(data.dim+1)), response_surface=RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=maxeval), sampling_method=EvolutionaryAlgorithm(data,epsilons=epsilons, cand_flag=1), archiving_method=EpsilonArchive(size_max=200,epsilon=epsilons)) # Launch the threads and give them access to the objective function for _ in range(nthreads): worker = BasicWorkerThread(controller, data.objfunction) controller.launch_worker(worker) # Run the optimization strategy def merit(r): return r.value[0] result = controller.run(merit=merit) controller.strategy.save_plot(num)
def main(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/test_gp.log"): os.remove("./logfiles/test_gp.log") logging.basicConfig(filename="./logfiles/test_gp.log", level=logging.INFO) print("\nNumber of threads: 4") print("Maximum number of evaluations: 500") print("Sampling method: CandidateDYCORS") print("Experimental design: Symmetric Latin Hypercube") print("Surrogate: Gaussian process regression") nthreads = 4 maxeval = 500 nsamples = nthreads data = Ackley(dim=10) print(data.info) # Create a strategy and a controller controller = ThreadController() controller.strategy = \ SyncStrategyNoConstraints( worker_id=0, data=data, maxeval=maxeval, nsamples=nsamples, exp_design=SymmetricLatinHypercube(dim=data.dim, npts=2*(data.dim+1)), response_surface=GPRegression(maxp=maxeval), sampling_method=CandidateDYCORS(data=data, numcand=100*data.dim)) # Launch the threads and give them access to the objective function for _ in range(nthreads): worker = BasicWorkerThread(controller, data.objfunction) controller.launch_worker(worker) # Run the optimization strategy result = controller.run() print('Best value found: {0}'.format(result.value)) print('Best solution found: {0}\n'.format( np.array_str(result.params[0], max_line_width=np.inf, precision=5, suppress_small=True)))
def main_master(nworkers): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/test_subprocess_mpi.log"): os.remove("./logfiles/test_subprocess_mpi.log") logging.basicConfig(filename="./logfiles/test_subprocess_mpi.log", level=logging.INFO) print( "\nTesting the POAP MPI controller with {0} workers".format(nworkers)) print("Maximum number of evaluations: 200") print("Search strategy: Candidate DYCORS") print("Experimental design: Symmetric Latin Hypercube") print("Surrogate: Cubic RBF") assert os.path.isfile("./sphere_ext"), "You need to build sphere_ext" maxeval = 200 data = Sphere(dim=10) print(data.info) # Create a strategy and a controller strategy = \ SyncStrategyNoConstraints( worker_id=0, data=data, maxeval=maxeval, nsamples=nworkers, exp_design=SymmetricLatinHypercube(dim=data.dim, npts=2*(data.dim+1)), sampling_method=CandidateDYCORS(data=data, numcand=100*data.dim), response_surface=RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=maxeval)) controller = MPIController(strategy) # Run the optimization strategy result = controller.run() print('Best value found: {0}'.format(result.value)) print('Best solution found: {0}\n'.format( np.array_str(result.params[0], max_line_width=np.inf, precision=5, suppress_small=True)))
def main(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/test_ensemble.log"): os.remove("./logfiles/test_ensemble.log") logging.basicConfig(filename="./logfiles/test_ensemble.log", level=logging.INFO) print("\nNumber of threads: 5") print("Maximum number of evaluations: 250") print("Sampling method: CandidateSRBF") print("Experimental design: Symmetric Latin Hypercube + point [1,1,...,1]") print("Ensemble Surrogate: Cubic RBF, PolyReg") nthreads = 5 maxeval = 250 nsamples = nthreads data = Ackley(dim=5) print(data.info) # Use RBF + PolyReg bounds = np.vstack((data.xlow, data.xup)).T basisp = basis_TD(data.dim, 2) # use order 2 models = [ RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=maxeval), PolyRegression(bounds, basisp) ] response_surface = EnsembleSurrogate(model_list=models, maxp=maxeval) # Add an additional point to the experimental design. If a good # solution is already known you can add this point to the # experimental design extra = np.ones((1, data.dim)) # Create a strategy and a controller controller = ThreadController() controller.strategy = \ SyncStrategyNoConstraints( worker_id=0, data=data, response_surface=response_surface, maxeval=maxeval, nsamples=nsamples, exp_design=SymmetricLatinHypercube(dim=data.dim, npts=2*(data.dim+1)), sampling_method=CandidateSRBF(data=data, numcand=100*data.dim), extra=extra) # Launch the threads and give them access to the objective function for _ in range(nthreads): worker = BasicWorkerThread(controller, data.objfunction) controller.launch_worker(worker) # Run the optimization strategy result = controller.run() response_surface.compute_weights() print('Final weights: {0}'.format( np.array_str(response_surface.weights, max_line_width=np.inf, precision=5, suppress_small=True))) print('Best value found: {0}'.format(result.value)) print('Best solution found: {0}\n'.format( np.array_str(result.params[0], max_line_width=np.inf, precision=5, suppress_small=True)))
def main(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/test_extra_vals.log"): os.remove("./logfiles/test_extra_vals.log") logging.basicConfig(filename="./logfiles/test_extra_vals.log", level=logging.INFO) print("\nNumber of threads: 4") print("Maximum number of evaluations: 500") print("Sampling method: CandidateDYCORS") print("Experimental design: Symmetric Latin Hypercube") print("Surrogate: Cubic RBF") nthreads = 4 maxeval = 500 nsamples = nthreads data = Ackley(dim=10) print(data.info) nextra = 10 extra = np.random.uniform(data.xlow, data.xup, (nextra, data.dim)) extra_vals = np.nan * np.ones((nextra, 1)) for i in range(nextra): # Evaluate every second point if i % 2 == 0: extra_vals[i] = data.objfunction(extra[i, :]) # Create a strategy and a controller controller = ThreadController() controller.strategy = \ SyncStrategyNoConstraints( worker_id=0, data=data, maxeval=maxeval, nsamples=nsamples, exp_design=SymmetricLatinHypercube(dim=data.dim, npts=2*(data.dim+1)), response_surface=RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=maxeval), sampling_method=CandidateDYCORS(data=data, numcand=100*data.dim), extra=extra, extra_vals=extra_vals) # Append the known function values to the POAP database since POAP won't evaluate these points for i in range(len(extra_vals)): if not np.isnan(extra_vals[i]): record = EvalRecord(params=(np.ravel(extra[i, :]), ), status='completed') record.value = extra_vals[i] record.feasible = True controller.fevals.append(record) # Launch the threads and give them access to the objective function for _ in range(nthreads): worker = BasicWorkerThread(controller, data.objfunction) controller.launch_worker(worker) # Run the optimization strategy result = controller.run() print('Best value found: {0}'.format(result.value)) print('Best solution found: {0}\n'.format( np.array_str(result.params[0], max_line_width=np.inf, precision=5, suppress_small=True)))