def test_SequentialDriver_with_rosenbrock(self): """Test :class:`gc3libs.optimizer.drivers.SequentialDriver """ # Test parameters magic_seed = 100 dim = 2 pop_size = 100 lower_bounds = -2 * np.ones(dim) upper_bounds = +2 * np.ones(dim) prob_cross = 0.8 configure_logger(level=logging.CRITICAL) log = logging.getLogger("gc3.gc3libs") initial_pop = draw_population( lower_bds=lower_bounds, upper_bds=upper_bounds, dim=dim, size=pop_size, in_domain=self.rosenbrock_sample_filter, seed=magic_seed) algo = DifferentialEvolutionAlgorithm( initial_pop=initial_pop, de_step_size=0.85, # DE-stepsize ex [0, 2] prob_crossover=prob_cross, # crossover probabililty constant ex [0, 1] itermax=1000, # maximum number of iterations (generations) dx_conv_crit=None, # stop when variation among x's is < this y_conv_crit=1e-5, # stop when ofunc < y_conv_crit de_strategy='DE_local_to_best', logger=log, in_domain=self.rosenbrock_sample_filter, seed=magic_seed, after_update_opt_state=[print_stats, log_stats] #, plot_population(temp_stage_dir)] ) assert algo.de_step_size == 0.85 assert algo.prob_crossover == prob_cross assert algo.itermax == 1000 assert algo.dx_conv_crit is None assert algo.y_conv_crit == 1e-5 assert algo.de_strategy == 'DE_local_to_best' assert algo.logger == log opt = SequentialDriver( algo, target_fn=self.rosenbrock_fn, fmt="%12.8f") assert opt.target_fn == self.rosenbrock_fn # run the Diff.Evo. algorithm opt.de_opt() assert algo.has_converged() assert (algo.best_y - 0.) < algo.y_conv_crit assert (algo.best_x[0] - 1.) < 1e-3 assert (algo.best_x[1] - 1.) < 1e-3
def test_SequentialDriver_with_rosenbrock(self): """Test :class:`gc3libs.optimizer.drivers.SequentialDriver """ # Test parameters magic_seed = 100 dim = 2 pop_size = 100 lower_bounds = -2 * np.ones(dim) upper_bounds = +2 * np.ones(dim) prob_cross = 0.8 configure_logger(level=logging.CRITICAL) log = logging.getLogger("gc3.gc3libs") initial_pop = draw_population(lower_bds=lower_bounds, upper_bds=upper_bounds, dim=dim, size=pop_size, in_domain=self.rosenbrock_sample_filter, seed=magic_seed) algo = DifferentialEvolutionAlgorithm( initial_pop=initial_pop, de_step_size=0.85, # DE-stepsize ex [0, 2] prob_crossover=prob_cross, # crossover probabililty constant ex [0, 1] itermax=1000, # maximum number of iterations (generations) dx_conv_crit=None, # stop when variation among x's is < this y_conv_crit=1e-5, # stop when ofunc < y_conv_crit de_strategy='DE_local_to_best', logger=log, in_domain=self.rosenbrock_sample_filter, seed=magic_seed, after_update_opt_state=[print_stats, log_stats] #, plot_population(temp_stage_dir)] ) assert algo.de_step_size == 0.85 assert algo.prob_crossover == prob_cross assert algo.itermax == 1000 assert algo.dx_conv_crit is None assert algo.y_conv_crit == 1e-5 assert algo.de_strategy == 'DE_local_to_best' assert algo.logger == log opt = SequentialDriver(algo, target_fn=self.rosenbrock_fn, fmt="%12.8f") assert opt.target_fn == self.rosenbrock_fn # run the Diff.Evo. algorithm opt.de_opt() assert algo.has_converged() assert (algo.best_y - 0.) < algo.y_conv_crit assert (algo.best_x[0] - 1.) < 1e-3 assert (algo.best_x[1] - 1.) < 1e-3
def new_tasks(self, extra): # General settings dim = 2 lower_bounds = -2 * np.ones(dim) upper_bounds = +2 * np.ones(dim) self.optimization_dir = os.path.join(self.params.path_to_stage_dir, 'optimize_rosenbrock') # create optimization_dir if os.path.isdir(self.optimization_dir): shutil.rmtree(self.optimization_dir) os.mkdir(self.optimization_dir) log = logging.getLogger('gc3.gc3libs.EvolutionaryAlgorithm') log.setLevel(logging.DEBUG) log.propagate = 0 stream_handler = logging.StreamHandler() stream_handler.setLevel(logging.DEBUG) log_file_name = os.path.join(os.getcwd(), 'EvolutionaryAlgorithm.log') file_handler = logging.FileHandler(log_file_name, mode='w') file_handler.setLevel(logging.DEBUG) log.addHandler(stream_handler) log.addHandler(file_handler) initial_pop = draw_population(lower_bds=lower_bounds, upper_bds=upper_bounds, size=self.params.pop_size, dim=dim) de_solver = DifferentialEvolutionAlgorithm( initial_pop=initial_pop, de_step_size=0.85, # DE-stepsize ex [0, 2] prob_crossover=1, # crossover probabililty constant ex [0, 1] itermax=200, # maximum number of iterations (generations) dx_conv_crit=None, # stop when variation among x's is < this y_conv_crit=self.params. y_conv_crit, # stop when ofunc < y_conv_crit de_strategy='DE_local_to_best', logger=log, ) # create an instance globalObt jobname = 'rosenbrock' kwargs = extra.copy() kwargs['path_to_stage_dir'] = self.optimization_dir kwargs['opt_algorithm'] = de_solver kwargs['task_constructor'] = task_constructor_rosenbrock( path_to_exectuable=self.params.path_to_executable, path_to_base_dir=self.params.path_to_base_dir) kwargs['extract_value_fn'] = compute_target_rosenbrock kwargs['cur_pop_file'] = 'cur_pop' return [ParallelDriver(jobname=jobname, **kwargs)]
def drive_optimization( population_size, dim, lower_bounds, upper_bounds, # DE-specific parameters de_strategy="DE_local_to_best", de_step_size=0.85, prob_crossover=1.0, exp_cross=False, # converge-related parameters itermax=100, dx_conv_crit=1e-6, y_conv_crit=None, # misc in_domain=None, seed=None, logger=None, after_update_opt_state=[], ): """ Driver script It uses DifferentialEvolutionAlgorithm as an implementation of Ken Price's differential evolution algorithm: [[http://www1.icsi.berkeley.edu/~storn/code.html]]. """ if logger is None: configure_logger(level=logging.CRITICAL) logger = logging.getLogger("gc3.gc3libs") if in_domain is None: def _default_in_domain(pop): return (pop < upper_bounds).all(axis=1) & (pop > lower_bounds).all(axis=1) in_domain = _default_in_domain opt = DifferentialEvolutionAlgorithm( initial_pop=np.zeros((population_size, dim)), de_step_size=de_step_size, # DE-stepsize ex [0, 2] prob_crossover=prob_crossover, # crossover probabililty constant ex [0, 1] itermax=itermax, # maximum number of iterations (generations) dx_conv_crit=dx_conv_crit, # stop when variation among x's is < this y_conv_crit=y_conv_crit, # stop when ofunc < y_conv_crit de_strategy=de_strategy, logger=logger, in_domain=in_domain, ) opt.vals = np.ones(population_size) * PENALTY_VALUE # init """ Jobs: create and manage population """ try: pop = getJobs(throw=True) except Exception as ex: # server error print ex return if not pop: # empty # Initialize population using the arguments passed to the # DifferentialEvolutionParallel initialization opt.new_pop = draw_population(lower_bds=lower_bounds, upper_bds=upper_bounds, size=population_size, dim=dim) putJobs(pop2Jobs(opt)) else: # finished? finished, count = True, 0 for job in pop: finished &= job.finished count += job.finished cur_iter = job.iteration - 1 # opt iter index start with 0 print "Iter(%d): %d finished jobs" % (cur_iter + 1, count) if opt.cur_iter != cur_iter: restoreCurrentPop(opt, cur_iter) # restore current population and iteration counter if finished: # Update population and evaluate convergence newVals = np.zeros(population_size) opt.new_pop = np.zeros((population_size, dim)) k = 0 for job in pop: opt.new_pop[k, :] = job.params newVals[k] = job.result if job.result != None else PENALTY_VALUE k += 1 opt.update_opt_state(opt.new_pop, newVals) putPop(opt) print [opt.best_y, opt.best_x] if opt.cur_iter > opt.itermax: print "Maximum number of iterations exceeded after [%d] steps. " % (opt.cur_iter) # sys.exit() if not opt.has_converged(): # Generate new population and enforce constrains opt.new_pop = opt.evolve() # Push all and run again! putJobs(pop2Jobs(opt)) return True else: # Once iteration has terminated, extract `bestval` which should represent # the element in *all* populations that lead to the closest match to the # empirical value print "Calibration converged after [%d] steps. " % (opt.cur_iter) sys.exit() # # VM's: create and manage dispatchers # vms = getVMs() # # if not vms: # empty # print "[+] No running EC2 instances found, creating %d" % N_NODES # nodes = fp_ec2_create_vms(N_NODES, pubkey_file='/home/tklauser/.ssh/id_rsa.pub') # vms = [] # for node in nodes: # vm = { 'ip' : node.public_ips[0], 'vmtype' : 'Amazon', 'dateUpdate' : str(datetime.datetime.now()) } # vms.append(vm) # putVMs(vms) # else: # pass #TODO manage VMs # Then, we could also run the forwardPremium binary here; Single script solution return False