def run_pygmo(): algos = [('PyGMO_DE',algorithm.de(gen=1)), ('PyGMO_DE_v1',algorithm.de(gen=1,f=.2,cr=1.0,variant=4)), #('PyGMO_DE_v2',algorithm.de(gen=1,f=.1,cr=1.0,variant=4)), #('PyGMO_DE_v3',algorithm.de(gen=1,f=.3,cr=1.0,variant=4)), #('PyGMO_DE_v4',algorithm.de(gen=1,f=.2,cr=0.9,variant=4)), ('PyGMO_JDE',algorithm.jde(gen=1)), ('PyGMO_MDE_PBX',algorithm.mde_pbx(gen=1)), ('PyGMO_PSO',algorithm.pso(gen=1,variant=5)), ('PyGMO_PSO_GEN',algorithm.pso_gen(gen=1)), ('PyGMO_BEE_COLONY',algorithm.bee_colony(gen=1)), ('PyGMO_IHS',algorithm.ihs(iter=pop_size))] run_eval("PyGMO",algos)
def example_2( algo=algorithm.de(1), prob=problem.rosenbrock(10), topo=topology.barabasi_albert( 3, 3), n_evolve=100, n_isl=1024, pop_size=20, color_code='rank'): from PyGMO import problem, algorithm, island, archipelago from matplotlib.pyplot import savefig, close archi = archipelago(algo, prob, n_isl, pop_size, topology=topo) print("Drawing Initial Condition .. ") pos = archi.draw( scale_by_degree=True, n_size=3, e_alpha=0.03, n_color=color_code) savefig('archi000', dpi=72) close() for i in range(1, n_evolve): archi.evolve(1) archi.join() print("Drawing" + str(i) + "-th evolution .. ") pos = archi.draw( layout=pos, scale_by_degree=True, n_size=3, e_alpha=0.03, n_color=color_code) savefig('archi%03d' % i, dpi=72) close()
def test_py_island(self): isl_type = py_island algo_list = [algorithm.py_example(1), algorithm.de(5)] prob_list = [problem.py_example(), problem.dejong(1)] for algo in algo_list: for prob in prob_list: self.__test_impl(isl_type, algo, prob)
def test_local_island(self): from PyGMO import local_island, algorithm, problem isl_type = local_island algo_list = [algorithm.py_example(1), algorithm.de(5)] prob_list = [problem.py_example(), problem.dejong(1)] for algo in algo_list: for prob in prob_list: self.__test_impl(isl_type,algo,prob)
def test_py_island(self): from PyGMO import py_island, algorithm, problem isl_type = py_island algo_list = [algorithm.py_example(1), algorithm.de(5)] prob_list = [problem.py_example(), problem.dejong(1)] for algo in algo_list: for prob in prob_list: self.__test_impl(isl_type,algo,prob)
def __init__(self, prob_factory: CoreProblemFactory, problem: Tuple[CoreProblem, SortedDict], evolutions: int, iter_func: Callable[..., bool]): super(DEOptimization, self).__init__(prob_factory, problem, iter_func) # default settings for algorithm self._algorithm = algorithm.de() self._logger = logging.getLogger(__name__) self._evol = evolutions
def build(self, handler: SolutionsHandler, core: CoreSystem, **kwargs) -> 'DEOptimization': de_opt = DEOptimization(self._probl_factory, self._start_problem, self._evol, self._iterate) valid_args = self.verify_arguments(**kwargs) if len(valid_args) > 0: de_opt._algorithm = algorithm.de(**valid_args) de_opt._logger.info("Arguments for {} are:\n{}".format( self.type_name, valid_args)) de_opt._handler = handler de_opt._core = core return de_opt
def test_py_island(self): # This test creates an infinite import loop on Windows when creating a new process # with Python multiprocessing module. # Normal usage of py_island on Windows is possible by using the `if __name__ == "__main__":` guard # Reference: http://stackoverflow.com/questions/20222534/python-multiprocessing-on-windows-if-name-main from PyGMO import py_island, algorithm, problem isl_type = py_island algo_list = [algorithm.py_example(1), algorithm.de(5)] prob_list = [problem.py_example(), problem.dejong(1)] for algo in algo_list: for prob in prob_list: self.__test_impl(isl_type, algo, prob)
def run_test(n_trials=200, pop_size = 20, n_gen = 500): number_of_trials = n_trials number_of_individuals = pop_size number_of_generations = n_gen prob_list = [problem.schwefel(dim = 10), problem.michalewicz(dim = 10), problem.rastrigin(dim = 10), problem.rosenbrock(dim = 10), problem.ackley(dim = 10), problem.griewank(dim = 10)] if __extensions__['gtop']: prob_list.append(problem.cassini_1()) prob_list.append(problem.cassini_2()) prob_list.append(problem.gtoc_1()) prob_list.append(problem.rosetta()) prob_list.append(problem.messenger_full()) prob_list.append(problem.tandem(prob_id = 6, max_tof = 10)) algo_list = [algorithm.pso(gen = number_of_generations), algorithm.de(gen = number_of_generations,xtol=1e-30, ftol=1e-30), algorithm.jde(gen = number_of_generations, variant_adptv=2,xtol=1e-30, ftol=1e-30), algorithm.de_1220(gen = number_of_generations, variant_adptv=2,xtol=1e-30, ftol=1e-30), algorithm.sa_corana(iter = number_of_generations*number_of_individuals,Ts = 1,Tf = 0.01), algorithm.ihs(iter = number_of_generations*number_of_individuals), algorithm.sga(gen = number_of_generations), algorithm.cmaes(gen = number_of_generations,xtol=1e-30, ftol=1e-30), algorithm.bee_colony(gen = number_of_generations/2)] print('\nTrials: ' + str(n_trials) + ' - Population size: ' + str(pop_size) + ' - Generations: ' + str(n_gen)) for prob in prob_list: print('\nTesting problem: ' + prob.get_name() + ', Dimension: ' + str(prob.dimension) ) print('With Population Size: ' + str(pop_size) ) for algo in algo_list: print(' ' + str(algo)) best = [] best_x = [] for i in range(0,number_of_trials): isl = island(algo,prob,number_of_individuals) isl.evolve(1) isl.join() best.append(isl.population.champion.f) best_x.append(isl.population.champion.x) print(' Best:\t' + str(min(best)[0])) print(' Mean:\t' + str(mean(best))) print(' Std:\t' + str(std(best)))
def test_ipy_island(self): from PyGMO import ipy_island, algorithm, problem try: from IPython.kernel import client mec = client.MultiEngineClient() if len(mec) == 0: raise RuntimeError() except ImportError as ie: return except BaseException as e: print('\nThere is a problem with parallel IPython setup. The error message is:') print(e) print('Tests for ipy_island will not be run.') return isl_type = ipy_island algo_list = [algorithm.py_example(1), algorithm.de(5)] prob_list = [problem.py_example(), problem.dejong(1)] for algo in algo_list: for prob in prob_list: self.__test_impl(isl_type,algo,prob)
def run(n_tri,n_gen,pop_size): import numpy #inital number of generations number_of_generations = 1 number_of_trials = n_tri number_of_individuals = pop_size algo_list = [algorithm.pso(gen = number_of_generations), algorithm.de(gen = number_of_generations,xtol=1e-30, ftol=1e-30), algorithm.jde(gen = number_of_generations, variant_adptv=2,xtol=1e-30, ftol=1e-30), algorithm.de_1220(gen = number_of_generations, variant_adptv=2,xtol=1e-30, ftol=1e-30), #algorithm.sa_corana(iter = number_of_generations*number_of_individuals,Ts = 1,Tf = 0.01), algorithm.ihs(iter = number_of_generations*number_of_individuals), #algorithm.sga(gen = number_of_generations), algorithm.cmaes(gen = number_of_generations,xtol=1e-30, ftol=1e-30), algorithm.bee_colony(gen = number_of_generations) ] algo_means_list = [] algo_name_list = [] print('\nTrials: ' + str(n_tri) + ' - Population size: ' + str(pop_size) + ' - Generations: ' + str(n_gen)) for algo in algo_list: print(' ' + str(algo)) algo_name_list.append(algo.get_name()) trials = [] trials_max_len = 0 for i in range(number_of_trials): print ".", trials.append(single_test(algo,total_evals,pop_size)) t_minlen = min([len(t) for t in trials]) t_new = numpy.array([t[:t_minlen-1] for t in trials ]) t_new = numpy.mean(t_new,axis=0) algo_means_list.append(t_new[:,1]) plot_multi(algo_means_list,names=algo_name_list,trials=number_of_trials)
def problem_solver(n_trials=25): from PyGMO import problem, algorithm, island, archipelago from PyGMO.topology import fully_connected from numpy import mean, median import csv results = list() prob = problem.cassini_2() de_variants = [11, 13, 15, 17] f_variants = [0, 0.2, 0.5, 0.8, 1] cr_variants = [0.3, 0.9] np_variants = [10, 25, 50] for f in f_variants: for cr in cr_variants: for np in np_variants: algos = [algorithm.de(gen=np, f=f, cr=cr) for v in de_variants] for trial in range(n_trials): archi = archipelago(topology=fully_connected()) for algo in algos: archi.push_back(island(algo, prob, 25)) archi.evolve(30) results.append( min([isl.population.champion.f[0] for isl in archi])) with open('results.csv', 'a') as out: out.write('%f;' % f) out.write('%f;' % cr) out.write('%f;' % np) out.write('\n') out.write('%f;' % mean(results)) out.write('%f;' % median(results)) out.write('%f;' % min(results)) out.write('%f;' % max(results)) out.write('\n') out.close()
def problem_solver(n_trials=25): from PyGMO import problem, algorithm, island, archipelago from PyGMO.topology import fully_connected from numpy import mean, median import csv results = list() prob = problem.cassini_2() de_variants = [11,13,15,17] f_variants = [0, 0.2, 0.5, 0.8, 1] cr_variants = [0.3, 0.9] np_variants = [10, 25, 50] for f in f_variants: for cr in cr_variants: for np in np_variants: algos = [algorithm.de(gen=np, f=f, cr=cr) for v in de_variants] for trial in range(n_trials): archi = archipelago(topology=fully_connected()) for algo in algos: archi.push_back(island(algo,prob,25)) archi.evolve(30) results.append(min([isl.population.champion.f[0] for isl in archi])) with open('results.csv', 'a') as out: out.write('%f;' % f) out.write('%f;' % cr) out.write('%f;' % np) out.write('\n') out.write('%f;' % mean(results)) out.write('%f;' % median(results)) out.write('%f;' % min(results)) out.write('%f;' % max(results)) out.write('\n') out.close()
def run_test(n_trials=200, pop_size=20, n_gen=500): """ This function runs some tests on the algorthm. Use it to verify the correct installation of PyGMO. USAGE: PyGMO.run_test(n_trials=200, pop_size = 20, n_gen = 500) * n_trials: each algorithm will be called n_trials times on the same problem to then evaluate best, mean and std * pop_size: this determines the population size * n_gen: this regulates the maximim number of function evaluation """ from PyGMO import problem, algorithm, island from numpy import mean, std number_of_trials = n_trials number_of_individuals = pop_size number_of_generations = n_gen prob_list = [ problem.schwefel( dim=10), problem.rastrigin( dim=10), problem.rosenbrock( dim=10), problem.ackley( dim=10), problem.griewank( dim=10), problem.levy5(10)] if __extensions__['gtop']: prob_list.append(problem.cassini_1()) prob_list.append(problem.gtoc_1()) prob_list.append(problem.cassini_2()) prob_list.append(problem.messenger_full()) algo_list = [ algorithm.pso( gen=number_of_generations), algorithm.mde_pbx( gen=number_of_generations, xtol=1e-30, ftol=1e-30), algorithm.de( gen=number_of_generations, xtol=1e-30, ftol=1e-30), algorithm.jde( gen=number_of_generations, memory=False, xtol=1e-30, ftol=1e-30), algorithm.de_1220( gen=number_of_generations, memory=False, xtol=1e-30, ftol=1e-30), algorithm.sa_corana( iter=number_of_generations * number_of_individuals, Ts=1, Tf=0.01), algorithm.ihs( iter=number_of_generations * number_of_individuals), algorithm.sga( gen=number_of_generations), algorithm.cmaes( gen=number_of_generations, xtol=1e-30, ftol=1e-30, memory=False), algorithm.bee_colony( gen=number_of_generations / 2)] print('\nTrials: ' + str(n_trials) + ' - Population size: ' + str(pop_size) + ' - Generations: ' + str(n_gen)) for prob in prob_list: print('\nTesting problem: ' + prob.get_name() + ', Dimension: ' + str(prob.dimension)) print('With Population Size: ' + str(pop_size)) for algo in algo_list: print(' ' + str(algo)) best = [] best_x = [] for i in range(0, number_of_trials): isl = island(algo, prob, number_of_individuals) isl.evolve(1) isl.join() best.append(isl.population.champion.f) best_x.append(isl.population.champion.x) print(' Best:\t' + str(min(best)[0])) print(' Mean:\t' + str(mean(best))) print(' Std:\t' + str(std(best)))
f = 0 for i in range(self.__dim): f += x[i]**2 return (f,) def human_readable_extra(self): text = """\tProblem dimension: %s Implemented function: f = x^2""" % str(self.__dim) return text # <codecell> if __name__ == '__main__': print("############### Differential Evolution ##########################") algo = algorithm.de(gen = 500) results_de_schaffer = [] results_de_sphere = [] for i in range(30): prob = schaffer_function_n2() #print("\nSchaffer Function") #print(prob) isl = island(algo,prob,50) #print("Before optimisation: " + str(isl.population.champion.f[0]) ) isl.evolve(10) results_de_schaffer.append( isl.population.champion.f[0] ) #print("After optimisation: " + str( isl.population.champion.f[0]) + '\n') #print("="*70 + '\n') #print("Sphere Function") prob2 = sphere_function(dim=100)