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)))
def __init__(self, sockname=("localhost", 0), strategy=None, handlers={}): """Initialize the controller on the given (host,port) address Args: sockname: Socket on which to serve workers strategy: Strategy object to connect to controllers handlers: Dictionary of specialized message handlers """ super(ThreadedTCPServer, self).__init__(sockname, SocketWorkerHandler) self.message_handlers = handlers self.controller = ThreadController() self.controller.strategy = strategy self.controller.add_term_callback(self.shutdown)
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("Search strategy: CandidateDYCORS") print("Experimental design: 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(surftype=CubicRBFSurface, maxp=maxeval), exp_design=LatinHypercube(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 example_subprocess_files(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/example_subprocess_files.log"): os.remove("./logfiles/example_subprocess_files.log") logging.basicConfig(filename="./logfiles/example_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(path), "You need to build sphere_ext_files" num_threads = 4 max_evals = 200 sphere = Sphere(dim=10) rbf = RBFInterpolant(dim=sphere.dim, kernel=TPSKernel(), tail=LinearTail(sphere.dim)) slhd = SymmetricLatinHypercube( dim=sphere.dim, num_pts=2*(sphere.dim+1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = SRBFStrategy( max_evals=max_evals, opt_prob=sphere, exp_design=slhd, surrogate=rbf, asynchronous=False, 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 i in range(num_threads): 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_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("Search strategy: CandidateDYCORS") print("Experimental design: Latin Hypercube") print("Ensemble surrogates: 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(surftype=CubicRBFSurface, 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 example_matlab_engine(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/example_matlab_engine.log"): os.remove("./logfiles/example_matlab_engine.log") logging.basicConfig(filename="./logfiles/example_matlab_engine.log", level=logging.INFO) num_threads = 4 max_evals = 500 ackley = Ackley(dim=10) rbf = RBFInterpolant( dim=ackley.dim, kernel=CubicKernel(), tail=LinearTail(ackley.dim)) slhd = SymmetricLatinHypercube( dim=ackley.dim, num_pts=2*(ackley.dim+1)) # Use the serial controller (uses only one thread) 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 for _ in range(num_threads): try: worker = MatlabWorker(controller) worker.matlab = matlab.engine.start_matlab() controller.launch_worker(worker) except Exception as e: print("\nERROR: Failed to initialize a MATLAB session.\n") print(str(e)) return # Run the optimization strategy result = controller.run() # Print the final result 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 = SurrogateUnitBox(RBFInterpolant(dim=ackley.dim, kernel=CubicKernel(), tail=LinearTail(ackley.dim)), lb=ackley.lb, ub=ackley.ub) 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 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 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 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_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) 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") if os.path.exists("./logfiles/test_subprocess.log"): os.remove("./logfiles/test_subprocess.log") logging.basicConfig(filename="./logfiles/test_subprocess.log", level=logging.INFO) print("\nNumber of threads: 4") print("Maximum number of evaluations: 200") print("Search strategy: Candidate DyCORS") print("Experimental design: Latin Hypercube") print("Surrogate: Cubic RBF") assert os.path.isfile("./sphere_ext"), "You need to build sphere_ext" nthreads = 4 maxeval = 200 nsamples = nthreads data = SphereExt(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)), sampling_method=CandidateDYCORS(data=data, numcand=100*data.dim), response_surface=RBFInterpolant(surftype=CubicRBFSurface, maxp=maxeval)) # Launch the threads and give them access to the objective function for _ in range(nthreads): controller.launch_worker(DummySim(controller)) # 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_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_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 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") if os.path.exists("./logfiles/test_matlab_engine.log"): os.remove("./logfiles/test_matlab_engine.log") logging.basicConfig(filename="./logfiles/test_matlab_engine.log", level=logging.INFO) print("\nNumber of threads: 4") print("Maximum number of evaluations: 500") print("Sampling method: CandidateDYCORS") print("Experimental design: Latin Hypercube") print("Surrogate: Cubic RBF") nthreads = 4 maxeval = 500 data = Ackley(dim=10) print(data.info) # Use the serial controller (uses only one thread) controller = ThreadController() controller.strategy = \ SyncStrategyNoConstraints( worker_id=0, data=data, maxeval=maxeval, nsamples=nthreads, 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)) print("\nNOTE: You may need to specify the matlab_root keyword in " "order \n to start a MATLAB session using the matlab_wrapper " "module\n") # We need to tell MATLAB where the script is mfile_location = os.getcwd() # Launch the threads for _ in range(nthreads): worker = MatlabWorker(controller) try: worker.matlab = matlab_wrapper.MatlabSession(options='-nojvm') except Exception as err: print("\nERROR: Failed to initialize a MATLAB session.\n") exit() worker.matlab.workspace.addpath(mfile_location) 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}\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 example_matlab_engine(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/example_matlab_engine.log"): os.remove("./logfiles/example_matlab_engine.log") logging.basicConfig(filename="./logfiles/example_matlab_engine.log", level=logging.INFO) num_threads = 4 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)) # Use the serial controller (uses only one thread) 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 for _ in range(num_threads): try: worker = MatlabWorker(controller) worker.matlab = matlab.engine.start_matlab() controller.launch_worker(worker) except Exception as e: print("\nERROR: Failed to initialize a MATLAB session.\n") print(str(e)) return # Run the optimization strategy result = controller.run() # Print the final result 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_subprocess_files(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/example_subprocess_files.log"): os.remove("./logfiles/example_subprocess_files.log") logging.basicConfig(filename="./logfiles/example_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(path), "You need to build sphere_ext_files" num_threads = 4 max_evals = 200 sphere = Sphere(dim=10) rbf = RBFInterpolant(dim=sphere.dim, kernel=TPSKernel(), tail=LinearTail(sphere.dim)) slhd = SymmetricLatinHypercube(dim=sphere.dim, num_pts=2 * (sphere.dim + 1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = SRBFStrategy(max_evals=max_evals, opt_prob=sphere, exp_design=slhd, surrogate=rbf, asynchronous=False, 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 i in range(num_threads): 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(): 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 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_subprocess_partial_info(): if not os.path.exists("./logfiles"): os.makedirs("logfiles") if os.path.exists("./logfiles/example_subprocess_partial_info.log"): os.remove("./logfiles/example_subprocess_partial_info.log") logging.basicConfig( filename="./logfiles/example_subprocess_partial_info.log", level=logging.INFO) assert os.path.isfile(path), "You need to build sumfun_ext" num_threads = 4 max_evals = 200 sumfun = SumfunExt(dim=10) rbf = RBFInterpolant(dim=sumfun.dim, lb=sumfun.lb, ub=sumfun.ub, kernel=CubicKernel(), tail=LinearTail(sumfun.dim)) slhd = SymmetricLatinHypercube(dim=sumfun.dim, num_pts=2 * (sumfun.dim + 1)) # Create a strategy and a controller controller = ThreadController() controller.strategy = SRBFStrategy(max_evals=max_evals, opt_prob=sumfun, 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): controller.launch_worker(CppSim(controller)) # 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_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_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 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 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 test_random_sampling(): max_evals = 500 controller = ThreadController() controller.strategy = RandomSampling( 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 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 testSimpleThreadWorkerEvaluation(threadWorker): """Testing routine.""" print('<<<<<<<<<<<<<<<<<<<< Testing Evaluation on {0} >>>>>>>>>>>>>>>>>>>'.format(threadWorker.__name__)) samples = [0.0] controller = ThreadController() strategy = FixedSampleStrategy(samples) controller.strategy = strategy for threadID in range(1): controller.launch_worker(SimpleEventThreadWorker(controller, objective)) result = controller.run() if result.value == 1: print("Test Passed") else: print("Test Failed:") print("Final: {0:.3e} @ {1}".format(result.value, result.params))
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_dycors_sync(): max_evals = 200 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 = DYCORSStrategy( max_evals=max_evals, opt_prob=ackley, exp_design=slhd, surrogate=rbf, asynchronous=False, batch_size=num_threads) 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
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer, object): """SocketServer interface for workers to connect to controller. The socket server interface lets workers connect to a given TCP/IP port and exchange updates with the controller. The server sends messages of the form ('eval', record_id, args, extra_args) ('eval', record_id, args) ('kill', record_id) ('terminate') The default messages received are ('running', record_id) ('kill', record_id) ('cancel', record_id) ('complete', record_id, value) The set of handlers can also be extended with a dictionary of named callbacks to be invoked whenever a record update comes in. For example, to set a lower bound field, we might use the handler def set_lb(rec, value): rec.lb = value handlers = {'lb' : set_lb } This is useful for adding new types of updates without mucking around in the EvalRecord implementation. Attributes: controller: ThreadController that manages the optimization handlers: dictionary of specialized message handlers strategy: redirects to the controller strategy """ def __init__(self, sockname=("localhost", 0), strategy=None, handlers={}): """Initialize the controller on the given (host,port) address Args: sockname: Socket on which to serve workers strategy: Strategy object to connect to controllers handlers: Dictionary of specialized message handlers """ super(ThreadedTCPServer, self).__init__(sockname, SocketWorkerHandler) self.message_handlers = handlers self.controller = ThreadController() self.controller.strategy = strategy self.controller.add_term_callback(self.shutdown) def marshall(self, *args): "Convert an argument list to wire format." return pickle.dumps(args) def unmarshall(self, data): "Convert wire format back to Python arg list." return pickle.loads(data) @property def strategy(self): return self.controller.strategy @strategy.setter def strategy(self, strategy): self.controller.strategy = strategy @property def sockname(self): return self.socket.getsockname() def run(self, merit=lambda r: r.value, filter=None): thread = threading.Thread(target=self.controller.run) thread.start() self.serve_forever() thread.join() return self.controller.best_point(merit=merit, filter=filter)