def test_simple_probs(self): """ Testing whether migration history matches the expected output for each migration_direction and distribution_type """ for migr_dir in [ migration_direction.source, migration_direction.destination ]: for dist_type in [ distribution_type.point_to_point, distribution_type.broadcast ]: prob = problem.rosenbrock(10) alg = algorithm.jde(20) archi = archipelago(alg, prob, 3, 20, migration_direction=migr_dir, distribution_type=dist_type) top = topology.ring(3) top.set_weight(0, 1, 0.0) top.set_weight(0, 2, 0.0) top.set_weight(1, 2, 0.0) top.set_weight(1, 0, 1.0) top.set_weight(2, 0, 1.0) top.set_weight(2, 1, 1.0) archi.topology = top archi.evolve(200) migr_hist = archi.dump_migr_history() # Below: After 200 evaluations, there should be some migrants from 1->0, 2->0 and 2->1 # There should be no migrants from 1->0, 2->0 and 2->1 self.assertTrue("(1,0,1)" not in migr_hist) self.assertTrue("(1,0,2)" not in migr_hist) self.assertTrue("(1,1,2)" not in migr_hist)
def __test_impl(self,isl_type,algo,prob): from PyGMO import archipelago, topology a = archipelago(topology = topology.ring()) for i in range(0,100): a.push_back(isl_type(algo,prob,6)) a.evolve(10) a.join()
def run_example5(): from PyGMO import archipelago, problem from PyGMO.algorithm import jde from PyGMO.topology import ring from PyKEP import epoch from PyKEP.planet import jpl_lp from PyKEP.trajopt import mga_1dsm # We define an Earth-Venus-Earth problem (single-objective) seq = [jpl_lp('earth'), jpl_lp('venus'), jpl_lp('earth')] prob = mga_1dsm(seq=seq) prob.set_tof(0.7, 3) prob.set_vinf(2.5) prob.set_launch_window(epoch(5844), epoch(6209)) prob.set_tof(0.7, 3) # We solve it!! algo = jde(100) topo = ring() archi = archipelago(algo, prob, 8, 20, topology=topo) print( "Running a Self-Adaptive Differential Evolution Algorithm .... on 8 parallel islands" ) archi.evolve(10) archi.join() isl = min(archi, key=lambda x: x.population.champion.f[0]) print("Done!! Best solution found is: " + str(isl.population.champion.f[0] / 1000) + " km / sec") prob.pretty(isl.population.champion.x) prob.plot(isl.population.champion.x)
def run_example5(): from PyGMO import archipelago, problem from PyGMO.algorithm import jde from PyGMO.topology import ring from PyKEP import planet_ss,epoch from PyKEP.interplanetary import mga_1dsm #We define an Earth-Venus-Earth problem (single-objective) seq = [planet_ss('earth'),planet_ss('venus'),planet_ss('earth')] prob = mga_1dsm(seq=seq) prob.set_tof(0.7,3) prob.set_vinf(2.5) prob.set_launch_window(epoch(5844),epoch(6209)) prob.set_tof(0.7,3) print prob #We solve it!! algo = jde(100) topo = ring() archi = archipelago(algo,prob,8,20, topology=topo) print "Running a Self-Adaptive Differential Evolution Algorithm .... on 8 parallel islands" archi.evolve(10); archi.join() isl = min(archi, key=lambda x:x.population.champion.f[0]) print "Done!! Best solution found is: " + str(isl.population.champion.f[0]/1000) + " km / sec" prob.pretty(isl.population.champion.x) prob.plot(isl.population.champion.x)
def test_average_path_length(self): """ Testing the average path length algorithm """ top = topology.ring(2) self.assertEqual(top.get_average_shortest_path_length(), 1.0) top = topology.ring(3) self.assertEqual(top.get_average_shortest_path_length(), 1.0) top = topology.ring(5) self.assertEqual(top.get_average_shortest_path_length(), 1.5) top = topology.ring(9) self.assertEqual(top.get_average_shortest_path_length(), 2.5) top = topology.custom() top.push_back() top.push_back() top.push_back() top.push_back() top.add_edge(0, 1) top.add_edge(1, 2) top.add_edge(2, 3) top.add_edge(3, 0) top.add_edge(0, 2) self.assertEqual(top.get_average_shortest_path_length(), 1.75)
def test_topology_serialize(self): """ Testing whether the weights are retained after serialization of an archipelago """ prob = problem.rosenbrock(10) alg = algorithm.jde(20) archi = archipelago(alg, prob, 4, 20) top = topology.ring(4) top.set_weight(0, 1, 0.01) top.set_weight(0, 3, 0.03) top.set_weight(1, 2, 0.12) top.set_weight(2, 1, 0.21) archi.topology = top archi.evolve(5) import pickle pickle.loads(pickle.dumps(archi)) self.assertEqual(archi.topology.get_weight(0, 1), 0.01) self.assertEqual(archi.topology.get_weight(0, 3), 0.03) self.assertEqual(archi.topology.get_weight(1, 2), 0.12) self.assertEqual(archi.topology.get_weight(2, 1), 0.21)
def test_ring_topo(self): """ Tests the getter/setter for the ring topology """ t = topology.ring() t.push_back() t.push_back() # By default the weights are 1.0 self.assertEqual(t.get_weight(0, 1), 1.0) self.assertEqual(t.get_weight(1, 0), 1.0) # Once you alter one weight, the remaining are still 1.0 t.set_weight(0, 1, 0.1) self.assertEqual(t.get_weight(0, 1), 0.1) self.assertEqual(t.get_weight(1, 0), 1.0) # Pushing new node should not change anything to the previous ones t.push_back() self.assertEqual(t.get_weight(0, 1), 0.1) self.assertEqual(t.get_weight(1, 0), 1.0) self.assertEqual(t.get_weight(0, 2), 1.0) self.assertEqual(t.get_weight(2, 0), 1.0) self.assertEqual(t.get_weight(1, 2), 1.0) self.assertEqual(t.get_weight(2, 1), 1.0) # Test the all-custom weights for ring topology t.set_weight(0, 1, 0.1) t.set_weight(1, 0, 0.2) t.set_weight(0, 2, 0.3) t.set_weight(2, 0, 0.4) t.set_weight(1, 2, 0.5) t.set_weight(2, 1, 0.6) self.assertEqual(t.get_weight(0, 1), 0.1) self.assertEqual(t.get_weight(1, 0), 0.2) self.assertEqual(t.get_weight(0, 2), 0.3) self.assertEqual(t.get_weight(2, 0), 0.4) self.assertEqual(t.get_weight(1, 2), 0.5) self.assertEqual(t.get_weight(2, 1), 0.6)
def test_simple_probs(self): """ Testing whether migration history matches the expected output for each migration_direction and distribution_type """ for migr_dir in [migration_direction.source, migration_direction.destination]: for dist_type in [distribution_type.point_to_point, distribution_type.broadcast]: prob = problem.rosenbrock(10) alg = algorithm.jde(20) archi = archipelago(alg, prob, 3, 20, migration_direction=migr_dir, distribution_type=dist_type) top = topology.ring(3) top.set_weight(0, 1, 0.0) top.set_weight(0, 2, 0.0) top.set_weight(1, 2, 0.0) top.set_weight(1, 0, 1.0) top.set_weight(2, 0, 1.0) top.set_weight(2, 1, 1.0) archi.topology = top archi.evolve(200) migr_hist = archi.dump_migr_history() # Below: After 200 evaluations, there should be some migrants from 1->0, 2->0 and 2->1 # There should be no migrants from 1->0, 2->0 and 2->1 self.assertTrue("(1,0,1)" not in migr_hist) self.assertTrue("(1,0,2)" not in migr_hist) self.assertTrue("(1,1,2)" not in migr_hist)
def test_chain_break(self): """ Testing 'chain breaking' after push_back in one-way-ring """ t = topology.ring(2) t.set_weight(0, 1, 0.1) t.set_weight(1, 0, 0.2) t.push_back() # In two-way ring, the weights between the first and second island should be the same after push_back self.assertEqual(t.get_weight(0, 1), 0.1) self.assertEqual(t.get_weight(1, 0), 0.2) self.assertEqual(t.get_weight(1, 2), 1.0) self.assertEqual(t.get_weight(2, 1), 1.0) self.assertEqual(t.get_weight(2, 0), 1.0) self.assertEqual(t.get_weight(0, 2), 1.0) # In one-way-ring, the edge 1->0 will be broken # and replaced by 1->2 and 2->0 with weights 1.0 each t = topology.one_way_ring(2) t.set_weight(0, 1, 0.1) t.set_weight(1, 0, 0.2) t.push_back() self.assertEqual(t.get_weight(0, 1), 0.1) self.assertEqual(t.get_weight(1, 2), 1.0) self.assertEqual(t.get_weight(2, 0), 1.0)
def optimization_run(optimization: BaseOptimization, num_islands: int, handler: SimpleCSVSolutionsHandler): solutions = optimization.solve(n_threads=num_islands, n_individuals=10, topol=topology.ring()) handler.handle_solutions(solutions, "best_solutions.csv")
def test_incorrect_weights(self): """ Testing ValueError for getter/setter """ top = topology.ring(4) self.assertRaises(ValueError, top.get_weight, 0, 2) self.assertRaises(ValueError, top.set_weight, 0, 2, 0.5)