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 main(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") logging.basicConfig(filename="./logfiles/test_mixed_integer_constraints.log", level=logging.INFO) print("\nNumber of threads: 4") print("Maximum number of evaluations: 200") print("Search strategy: CandidateDYCORS, CandidateDYCORS_INT" ", CandidateDYCORS_CONT, CandidateUniform") print("Experimental design: Symmetric Latin Hypercube") print("Surrogate: Cubic RBF") nthreads = 4 maxeval = 200 nsamples = nthreads data = LinearMI() print(data.info) def feasible_merit(record): "Merit function for ordering final answers -- kill infeasible x" x = record.params[0].reshape((1, record.params[0].shape[0])) if np.max(data.eval_ineq_constraints(x)) > 0: return np.inf return record.value exp_design = SymmetricLatinHypercube(dim=data.dim, npts=2*(data.dim+1)) response_surface = RBFInterpolant(surftype=CubicRBFSurface, maxp=maxeval) # Use a multi-search strategy for candidate points search_proc = MultiSearchStrategy( [CandidateDYCORS(data=data, numcand=100*data.dim), CandidateUniform(data=data, numcand=100*data.dim), CandidateDYCORS_INT(data=data, numcand=100*data.dim), CandidateDYCORS_CONT(data=data, numcand=100*data.dim)], [0, 1, 2, 3]) # Create a strategy and a controller controller = ThreadController() controller.strategy = \ SyncStrategyPenalty( worker_id=0, data=data, response_surface=response_surface, maxeval=maxeval, nsamples=nsamples, exp_design=exp_design, search_procedure=search_proc) # Launch the threads for _ in range(nthreads): worker = BasicWorkerThread(controller, data.objfunction) controller.launch_worker(worker) result = controller.run(merit=feasible_merit) 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 test_srbf_async(): max_evals = 200 rbf = RBFInterpolant(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub, kernel=CubicKernel(), tail=LinearTail(ackley.dim)) slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = SRBFStrategy(max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=True, batch_size=None) for _ in range(num_threads): worker = BasicWorkerThread(controller, ackley.eval) controller.launch_worker(worker) controller.run() check_strategy(controller)
def init(): print("\nInitializing run...") rbf = RBFInterpolant( dim=ackley.dim, kernel=CubicKernel(), tail=LinearTail(ackley.dim)) slhd = SymmetricLatinHypercube( dim=ackley.dim, num_pts=2*(ackley.dim+1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = SRBFStrategy( max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=True, batch_size=num_threads) print("Number of threads: {}".format(num_threads)) print("Maximum number of evaluations: {}".format(max_evals)) print("Strategy: {}".format(controller.strategy.__class__.__name__)) print("Experimental design: {}".format(slhd.__class__.__name__)) print("Surrogate: {}".format(rbf.__class__.__name__)) # Launch the threads and give them access to the objective function for _ in range(num_threads): worker = BasicWorkerThread(controller, ackley.eval) controller.launch_worker(worker) # Wrap controller in checkpoint object controller = CheckpointController(controller, fname=fname) 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 example_sop(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/example_simple.log"): os.remove("./logfiles/example_simple.log") logging.basicConfig(filename="./logfiles/example_simple.log", level=logging.INFO) print("\nNumber of threads: 8") print("Maximum number of evaluations: 500") print("Sampling method: CandidateDYCORS") print("Experimental design: Symmetric Latin Hypercube") print("Surrogate: Cubic RBF") num_threads = 8 max_evals = 500 ackley = Ackley(dim=10) rbf = RBFInterpolant(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub, kernel=CubicKernel(), tail=LinearTail(ackley.dim)) slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = SOPStrategy( max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=False, ncenters=num_threads, batch_size=num_threads, ) print("Number of threads: {}".format(num_threads)) print("Maximum number of evaluations: {}".format(max_evals)) print("Strategy: {}".format(controller.strategy.__class__.__name__)) print("Experimental design: {}".format(slhd.__class__.__name__)) print("Surrogate: {}".format(rbf.__class__.__name__)) # Launch the threads and give them access to the objective function for _ in range(num_threads): worker = BasicWorkerThread(controller, ackley.eval) 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 example_extra_vals(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/example_extra_vals.log"): os.remove("./logfiles/example_extra_vals.log") logging.basicConfig(filename="./logfiles/example_extra_vals.log", level=logging.INFO) num_threads = 4 max_evals = 500 ackley = Ackley(dim=10) num_extra = 10 extra = np.random.uniform(ackley.lb, ackley.ub, (num_extra, ackley.dim)) extra_vals = np.nan * np.ones((num_extra, 1)) for i in range(num_extra): # Evaluate every second point if i % 2 == 0: extra_vals[i] = ackley.eval(extra[i, :]) rbf = RBFInterpolant(dim=ackley.dim, kernel=CubicKernel(), tail=LinearTail(ackley.dim)) slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2*(ackley.dim+1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = SRBFStrategy( max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=True, batch_size=num_threads, extra_points=extra, extra_vals=extra_vals) print("Number of threads: {}".format(num_threads)) print("Maximum number of evaluations: {}".format(max_evals)) print("Strategy: {}".format(controller.strategy.__class__.__name__)) print("Experimental design: {}".format(slhd.__class__.__name__)) print("Surrogate: {}".format(rbf.__class__.__name__)) # 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(num_threads): worker = BasicWorkerThread(controller, ackley.eval) 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(): log_file = os.path.splitext(__file__)[0] + ".log" millis = int(round(time.time() * 1000)) print('Started: ' + str(datetime.now()) + ' (' + str(millis) + ')') if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/" + log_file): os.remove("./logfiles/" + log_file) logging.basicConfig(filename="./logfiles/" + log_file, level=logging.INFO) nthreads = int(sys.argv[1]) maxeval = int(sys.argv[2]) seed = sys.argv[3] server = sys.argv[4] np.random.seed(int(seed)) print("\nNumber of threads: " + str(nthreads)) print("Maximum number of evaluations: " + str(maxeval)) print("Search strategy: Candidate DyCORS") print("Experimental design: Symmetric Latin Hypercube") print("Surrogate: Cubic RBF") print( 'best\tf_eval_time\tresult\ttestset_result\tf_eval_count\twallclock_time\thyper-parameters' ) nsamples = nthreads data = TorchOptim(seed=seed, server=server) # 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(surftype=CubicRBFSurface, maxp=maxeval), sampling_method=CandidateDYCORS(data=data, numcand=500*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))) millis = int(round(time.time() * 1000)) print('Ended: ' + str(datetime.now()) + ' (' + str(millis) + ')')
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_projection.log"): os.remove("./logfiles/test_projection.log") logging.basicConfig(filename="./logfiles/test_projection.log", level=logging.INFO) print("\nNumber of threads: 4") print("Maximum number of evaluations: 1000") print("Sampling method: CandidateDYCORS") print("Experimental design: Latin Hypercube") print("Surrogate: Cubic RBF") nthreads = 4 maxeval = 1000 nsamples = nthreads data = AckleyUnit(dim=10) print(data.info) def projection(x): return x / np.linalg.norm(x) # Create a strategy and a controller controller = ThreadController() controller.strategy = \ SyncStrategyProjection( worker_id=0, data=data, maxeval=maxeval, nsamples=nsamples, exp_design=LatinHypercube(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), proj_fun=projection ) # 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}'.format( np.array_str(result.params[0], max_line_width=np.inf, precision=5, suppress_small=True))) print('||x||_2 = {0}\n'.format(np.linalg.norm(result.params[0])))
def test_random_sampling(): max_evals = 500 controller = ThreadController() controller.strategy = RandomStrategy(opt_prob=ackley, max_evals=max_evals) for _ in range(num_threads): worker = BasicWorkerThread(controller, ackley.eval) controller.launch_worker(worker) controller.run() assert len(controller.fevals) == max_evals for rec in controller.fevals: assert np.all(rec.params[0] <= ackley.ub) assert np.all(rec.params[0] >= ackley.lb)
def example_mars(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/example_mars.log"): os.remove("./logfiles/example_mars.log") logging.basicConfig(filename="./logfiles/example_mars.log", level=logging.INFO) num_threads = 4 max_evals = 200 ackley = Ackley(dim=5) try: mars = MARSInterpolant(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub) except Exception as e: print(str(e)) return slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = SRBFStrategy(max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=mars, asynchronous=True, batch_size=num_threads) print("Number of threads: {}".format(num_threads)) print("Maximum number of evaluations: {}".format(max_evals)) print("Strategy: {}".format(controller.strategy.__class__.__name__)) print("Experimental design: {}".format(slhd.__class__.__name__)) print("Surrogate: {}".format(mars.__class__.__name__)) # Launch the threads and give them access to the objective function for _ in range(num_threads): worker = BasicWorkerThread(controller, ackley.eval) 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") logging.basicConfig(filename="./logfiles/test_constraints.log", level=logging.INFO) print("\nNumber of threads: 4") print("Maximum number of evaluations: 500") print("Search strategy: CandidateDYCORS") print("Experimental design: Latin Hypercube") print("Surrogate: Cubic RBF with median capping") nthreads = 4 maxeval = 500 nsamples = nthreads data = Keane(dim=10) print(data.info) def feasible_merit(record): """Merit function for ordering final answers -- kill infeasible x""" x = record.params[0].reshape((1, record.params[0].shape[0])) if np.max(data.eval_ineq_constraints(x)) > 0: return np.inf return record.value # Create a strategy and a controller controller = ThreadController() controller.strategy = \ SyncStrategyPenalty( worker_id=0, data=data, maxeval=maxeval, nsamples=nsamples, response_surface=RSCapped(RBFInterpolant(surftype=CubicRBFSurface, maxp=maxeval)), exp_design=LatinHypercube(dim=data.dim, npts=2*(data.dim+1)), search_procedure=CandidateDYCORS(data=data, numcand=100*data.dim)) # Launch the threads for _ in range(nthreads): worker = BasicWorkerThread(controller, data.objfunction) controller.launch_worker(worker) result = controller.run(merit=feasible_merit) 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 resume(): print("Resuming run...\n") controller = ThreadController() # Launch the threads and give them access to the objective function for _ in range(num_threads): worker = BasicWorkerThread(controller, ackley.eval) controller.launch_worker(worker) # Wrap controller in checkpoint object controller = CheckpointController(controller, fname=fname) result = controller.resume() 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_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(): "Testing routine." logging.basicConfig(format="%(name)-18s: %(levelname)-8s %(message)s", level=logging.INFO) samples = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5] controller = ThreadController() strategy = FixedSampleStrategy(samples) strategy = CheckWorkerStrategy(controller, strategy) controller.strategy = strategy add_monitor(controller, 1) for _ in range(5): controller.launch_worker(BasicWorkerThread(controller, objective)) result = controller.run() print(("Final: {0:.3e} @ {1}".format(result.value, result.params)))
def main(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/test_kriging.log"): os.remove("./logfiles/test_kriging.log") logging.basicConfig(filename="./logfiles/test_kriging.log", level=logging.INFO) print("\nNumber of threads: 4") print("Maximum number of evaluations: 25") print("Sampling method: CandidateDYCORS, with weight 0.5") print("Experimental design: Symmetric Latin Hypercube") print("Surrogate: Cubic RBF, domain scaled to unit box") nthreads = 4 maxeval = 25 nsamples = nthreads data = Ackley(dim=2) 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=KrigingInterpolant(maxp=maxeval), sampling_method=CandidateDYCORS(data=data, numcand=100*data.dim, weights=[0.5])) # 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 test_lcb_async(): max_evals = 50 gp = GPRegressor(dim=ackley.dim) slhd = SymmetricLatinHypercube( dim=ackley.dim, num_pts=2*(ackley.dim+1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = LCBStrategy( max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=gp, asynchronous=True, batch_size=None) for _ in range(num_threads): worker = BasicWorkerThread(controller, ackley.eval) controller.launch_worker(worker) controller.run() check_strategy(controller)
def example_lower_confidence_bounds(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/example_lower_confidence_bounds.log"): os.remove("./logfiles/example_lower_confidence_bounds.log") logging.basicConfig( filename="./logfiles/example_lower_confidence_bounds.log", level=logging.INFO) num_threads = 4 max_evals = 100 hart6 = Hartman6() gp = GPRegressor(dim=hart6.dim) slhd = SymmetricLatinHypercube(dim=hart6.dim, num_pts=2 * (hart6.dim + 1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = LCBStrategy(max_evals=max_evals, opt_prob=hart6, exp_design=slhd, surrogate=gp, asynchronous=True) print("Number of threads: {}".format(num_threads)) print("Maximum number of evaluations: {}".format(max_evals)) print("Strategy: {}".format(controller.strategy.__class__.__name__)) print("Experimental design: {}".format(slhd.__class__.__name__)) print("Surrogate: {}".format(gp.__class__.__name__)) # Launch the threads and give them access to the objective function for _ in range(num_threads): worker = BasicWorkerThread(controller, hart6.eval) 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") logging.basicConfig(filename="./logfiles/test_simple.log", level=logging.INFO) print("\nNumber of threads: 4") print("Maximum number of evaluations: 1000") print("Search strategy: CandidateDYCORS") print("Experimental design: Latin Hypercube") print("Ensemble surrogates: Cubic RBF") nthreads = 4 maxeval = 1000 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=LatinHypercube(dim=data.dim, npts=2*(data.dim+1)), response_surface=RBFInterpolant(surftype=CubicRBFSurface, maxp=maxeval), search_procedure=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 test_example_simple(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/example_simple.log"): os.remove("./logfiles/example_simple.log") logging.basicConfig(filename="./logfiles/example_simple.log", level=logging.INFO) num_threads = 2 max_evals = 50 ackley = Ackley(dim=10) rbf = RBFInterpolant(dim=ackley.dim, lb=ackley.lb, ub=ackley.ub, kernel=CubicKernel(), tail=LinearTail(ackley.dim)) slhd = SymmetricLatinHypercube(dim=ackley.dim, num_pts=2 * (ackley.dim + 1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = SRBFStrategy(max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=True) # Launch the threads and give them access to the objective function for _ in range(num_threads): worker = BasicWorkerThread(controller, ackley.eval) 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 run_gdx_calibrator(dim, nworkers, script, folder, inclCentre): n_start_pts = make_multiple_of_n(2 * dim + 1, nworkers) maxeval = n_start_pts + make_multiple_of_n(min(max(300, 100 * dim), 400), 2 * nworkers) print("nparams ", dim) print("nworkers ", nworkers) print("script ", script) print("folder ", folder) print("inclCentr ", inclCentre) print(n_start_pts, "start point evaluations") print(maxeval, "total evaluations") worker = [None] * nworkers for i in range(0, nworkers): worker[i] = gdxworker(dim=dim, worker_id=(i + 1), script=script, folder=folder) controller = ThreadController() if inclCentre == 1: exp_design = GdxSymmetricLatinHypercube(dim=dim, npts=n_start_pts) else: exp_design = SymmetricLatinHypercube(dim=dim, npts=n_start_pts) sampling_methods = [ CandidateUniform(data=worker[0], numcand=min(100 * dim, 2000)), CandidateDYCORS(data=worker[0], numcand=min(100 * dim, 2000)) ] strategy = SyncStrategyNoConstraints( worker_id=0, data=worker[0], maxeval=maxeval, nsamples=nworkers, exp_design=exp_design, response_surface=RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=min(maxeval, 2000)), sampling_method=MultiSampling(sampling_methods, cycle=[0, 1])) controller.strategy = strategy for i in range(0, nworkers): worker_thread = BasicWorkerThread(controller, worker[i].objfunction) controller.launch_worker(worker_thread) return controller.run()
def pysot_cube(objective, scale, n_trials, n_dim, with_count=False): if False: if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/example_simple.log"): os.remove("./logfiles/example_simple.log") logging.basicConfig(filename="./logfiles/example_simple.log", level=logging.INFO) num_threads = 2 max_evals = n_trials gp = GenericProblem(dim=n_dim, objective=objective, scale=scale) rbf = RBFInterpolant(dim=n_dim, lb=np.array([-scale] * n_dim), ub=np.array([scale] * n_dim), kernel=CubicKernel(), tail=LinearTail(n_dim)) slhd = SymmetricLatinHypercube(dim=n_dim, num_pts=2 * (n_dim + 1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = SRBFStrategy(max_evals=max_evals, opt_prob=gp, exp_design=slhd, surrogate=rbf, asynchronous=True) # Launch the threads and give them access to the objective function for _ in range(num_threads): worker = BasicWorkerThread(controller, gp.eval) controller.launch_worker(worker) # Run the optimization strategy result = controller.run() return (result.value, gp.feval_count) if with_count else result.value
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 and no cross-terms 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 run(self): """ Run the optimization @return: Nothing """ self.problem = VoltageOptimizationProblem( self.circuit, self.options, self.max_iter, callback=self.progress_signal.emit) # # (1) Optimization problem # # print(data.info) # # # (2) Experimental design # # Use a symmetric Latin hypercube with 2d + 1 samples # exp_des = SymmetricLatinHypercube(dim=self.problem.dim, npts=2 * self.problem.dim + 1) # # # (3) Surrogate model # # Use a cubic RBF interpolant with a linear tail # surrogate = RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=self.max_eval) # # # (4) Adaptive sampling # # Use DYCORS with 100d candidate points # adapt_samp = CandidateDYCORS(data=self, numcand=100 * self.dim) # # # Use the serial controller (uses only one thread) # controller = SerialController(self.objfunction) # # # (5) Use the sychronous strategy without non-bound constraints # strategy = SyncStrategyNoConstraints(worker_id=0, # data=self, # maxeval=self.max_eval, # nsamples=1, # exp_design=exp_des, # response_surface=surrogate, # sampling_method=adapt_samp) # # controller.strategy = strategy # # # Run the optimization strategy # result = controller.run() # # # Print the final result # 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))) num_threads = 4 surrogate_model = GPRegressor(dim=self.problem.dim) sampler = SymmetricLatinHypercube(dim=self.problem.dim, num_pts=2 * (self.problem.dim + 1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = SRBFStrategy(max_evals=self.max_iter, opt_prob=self.problem, exp_design=sampler, surrogate=surrogate_model, asynchronous=True, batch_size=num_threads) print("Number of threads: {}".format(num_threads)) print("Maximum number of evaluations: {}".format(self.max_iter)) print("Strategy: {}".format(controller.strategy.__class__.__name__)) print("Experimental design: {}".format(sampler.__class__.__name__)) print("Surrogate: {}".format(surrogate_model.__class__.__name__)) # Launch the threads and give them access to the objective function for _ in range(num_threads): worker = BasicWorkerThread(controller, self.problem.eval) 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=4, suppress_small=True))) self.solution = result.params[0] # Extract function values from the controller self.optimization_values = np.array( [o.value for o in controller.fevals]) # send the finnish signal self.progress_signal.emit(0.0) self.progress_text.emit('Done!') self.done_signal.emit()
def pysot_cube(objective, n_trials, n_dim, with_count=False, method=None, design=None): """ Minimize :param objective: :param n_trials: :param n_dim: :param with_count: :return: """ logging.getLogger('pySOT').setLevel(logging.ERROR) num_threads = 1 asynchronous = True max_evals = n_trials gp = GenericProblem(dim=n_dim, objective=objective) if design == 'latin': exp_design = LatinHypercube(dim=n_dim, num_pts=2 * (n_dim + 1)) elif design == 'symmetric': exp_design = SymmetricLatinHypercube(dim=n_dim, num_pts=2 * (n_dim + 1)) elif design == 'factorial': exp_design = TwoFactorial(dim=n_dim) else: raise ValueError('design should be latin, symmetric or factorial') # Create a strategy and a controller # SRBFStrategy, EIStrategy, DYCORSStrategy,RandomStrategy, LCBStrategy controller = ThreadController() if method.lower() == 'srbf': surrogate = RBFInterpolant(dim=n_dim, lb=np.array([0.0] * n_dim), ub=np.array([1.0] * n_dim), kernel=CubicKernel(), tail=LinearTail(n_dim)) controller.strategy = SRBFStrategy(max_evals=max_evals, opt_prob=gp, exp_design=exp_design, surrogate=surrogate, asynchronous=asynchronous) elif method.lower() == 'ei': surrogate = GPRegressor(dim=n_dim, lb=np.array([0.0] * n_dim), ub=np.array([1.0] * n_dim)) controller.strategy = EIStrategy(max_evals=max_evals, opt_prob=gp, exp_design=exp_design, surrogate=surrogate, asynchronous=asynchronous) elif method.lower() == 'dycors': surrogate = RBFInterpolant(dim=n_dim, lb=np.array([0.0] * n_dim), ub=np.array([1.0] * n_dim), kernel=CubicKernel(), tail=LinearTail(n_dim)) controller.strategy = DYCORSStrategy(max_evals=max_evals, opt_prob=gp, exp_design=exp_design, surrogate=surrogate, asynchronous=asynchronous) elif method.lower() == 'lcb': surrogate = GPRegressor(dim=n_dim, lb=np.array([0.0] * n_dim), ub=np.array([1.0] * n_dim)) controller.strategy = LCBStrategy(max_evals=max_evals, opt_prob=gp, exp_design=exp_design, surrogate=surrogate, asynchronous=asynchronous) elif method.lower() == 'random': controller.strategy = RandomStrategy(max_evals=max_evals, opt_prob=gp) else: raise ValueError("Didn't recognize method passed to pysot") # Launch the threads and give them access to the objective function for _ in range(num_threads): worker = BasicWorkerThread(controller, gp.eval) controller.launch_worker(worker) # Run the optimization strategy result = controller.run() best_x = result.params[0].tolist() return (result.value, best_x, gp.feval_count) if with_count else (result.value, best_x)
def run(self): """ Function that optimizes a MicroGrid Object Args: Returns: """ self.iteration = 0 self.raw_results_df = list() self.X = list() self.Y = list() self.progress_signal.emit(0) self.progress_text_signal.emit('Optimizing facility sizes by surrogate optimization: Objective: ' + str(self.optimization_type)) # (1) Optimization problem # print(data.info) # (2) Experimental design # Use a symmetric Latin hypercube with 2d + 1 samples exp_des = SymmetricLatinHypercube(dim=self.dim, npts=2 * self.dim + 1) # (3) Surrogate model # Use a cubic RBF interpolant with a linear tail surrogate = RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=self.max_eval) # (4) Adaptive sampling # Use DYCORS with 100d candidate points adapt_samp = CandidateDYCORS(data=self, numcand=100 * self.dim) # Use the serial controller (uses only one thread) # controller = SerialController(self.objfunction) controller = ThreadController() # (5) Use the synchronous strategy without non-bound constraints strategy = SyncStrategyNoConstraints( worker_id=0, data=self, maxeval=self.max_eval, nsamples=1, 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(multiprocessing.cpu_count()): # worker = BasicWorkerThread(controller, self.objfunction) # controller.launch_worker(worker) worker = BasicWorkerThread(controller, self.objfunction) controller.launch_worker(worker) # Run the optimization strategy result = controller.run() # Print the final result 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))) self.solution = result.params[0] # Extract function values from the controller self.optimization_values = np.array([o.value for o in controller.fevals]) # format the trials DataFrame data = np.array(self.raw_results_df) if self.battery is not None: cols = ['solar (kW)', 'wind (kW)', 'storage (kWh)'] else: cols = ['solar (kW)', 'wind (kW)'] self.raw_results_df = pd.DataFrame(data=data[:, 1:], columns=cols, index=data[:, 0]) self.raw_results_df.sort_index(inplace=True) self.progress_text_signal.emit('Done!') self.done_signal.emit()
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)))
def objfunction(self, x): return benchmarks.f(x) dim = benchmarks.common_dim n_start_pts = 2 * dim + 1 maxeval = 2000 * dim worker = Worker(dim=dim) check_opt_prob(worker) nthreads = 1 # set to higher number for parallel evaluation nsamples = 40 strategy = SyncStrategyNoConstraints( worker_id=0, data=worker, maxeval=maxeval, nsamples=nsamples, exp_design=SymmetricLatinHypercube(dim=dim, npts=n_start_pts), response_surface=RBFInterpolant(kernel=CubicKernel, tail=LinearTail, maxp=min(maxeval, 50000)), sampling_method=CandidateDYCORS(data=worker, numcand=min(100 * dim, 5000))) controller = ThreadController() controller.strategy = strategy for i in range(nthreads): worker_thread = BasicWorkerThread(controller, worker.objfunction) controller.launch_worker(worker_thread) result = controller.run() print("dim", benchmarks.common_dim, ", after", benchmarks.count, "evaluations f(x) =", f"{benchmarks.best_y:.3f}")
def main(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") logging.basicConfig(filename="./logfiles/test_ensemble.log", level=logging.INFO) print("\nNumber of threads: 4") print("Maximum number of evaluations: 50") print("Search strategy: CandidateSRBF") print("Experimental design: Latin Hypercube + point [0.1, 0.5, 0.8]") print("Surrogate: Cubic RBF, Linear RBF, Thin-plate RBF, MARS") nthreads = 4 maxeval = 50 nsamples = nthreads data = Hartman3() print(data.info) # Use 3 differents RBF's and MARS as an ensemble surrogate models = [ RBFInterpolant(surftype=CubicRBFSurface, maxp=maxeval), RBFInterpolant(surftype=LinearRBFSurface, maxp=maxeval), RBFInterpolant(surftype=TPSSurface, maxp=maxeval) ] response_surface = EnsembleSurrogate(models, 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.atleast_2d([0.1, 0.5, 0.8]) # 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=LatinHypercube(dim=data.dim, npts=2*(data.dim+1)), search_procedure=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)))