def test_if_has_all_terminals(self): filename = path.join('datasets', 'ORLibrary', 'steinb15.txt') stpg = ReaderORLibrary().parser(filename) terminals = stpg.terminals crossover = CrossoverPrimRST(stpg) parent_a = gen_random_walk(stpg) parent_b = gen_random_walk(stpg) offspring = crossover(parent_a, parent_b) vertices = set([v for v in offspring.vertices]) self.assertTrue(terminals.issubset(vertices))
def test_terminals_are_in(self): stpg = self.stpg terminals = set(stpg.terminals) individual = gen_random_walk(stpg) vertices = set([v for v in individual.vertices]) self.assertTrue(terminals.issubset(vertices))
def simulation_random_walk(params): STPG = read_problem("datasets", "ORLibrary", params["dataset"]) crossover = CrossoverRandomWalkRST(STPG) evaluation = EvaluateEdgeSet(STPG) mutate = MutationReplaceByRandomEdge(STPG) output_data_dir = os.path.join("data", "test", "edgeset", STPG.name) tracker = DataTracker(params['runtrial'], target=output_data_dir) population = (GPopulation( chromosomes=[ gen_random_walk(STPG) for _ in range(params["population_size"]) ], eval_function=evaluation, maximize=True).evaluate().normalize( norm_function=normalize).callback(update_best)) evol = (Evolution().evaluate().normalize( norm_function=normalize).callback(update_best).callback( tracker.log_evaluation).select(selection_func=roullete).crossover( combiner=crossover).mutate( mutate_function=mutate, probability=params['tx_mutation']).callback( update_generation).callback(display, every=100)) with Stagnation(interval=params["stagnation_interval"]), \ BestKnownReached(global_optimum=params['global_optimum']): result = population.evolve(evol, n=params["n_iterations"]) tracker.log_simulation(params, STPG, result) tracker.report() print(result.stoppedby)
def test_if_return_an_edgeset(self): filename = path.join('datasets', 'ORLibrary', 'steinb15.txt') stpg = ReaderORLibrary().parser(filename) crossover = CrossoverPrimRST(stpg) parent_a = gen_random_walk(stpg) parent_b = gen_random_walk(stpg) self.assertIsInstance(parent_a, EdgeSet) self.assertIsInstance(parent_b, EdgeSet) offspring = crossover(parent_a, parent_b) self.assertIsInstance(offspring, EdgeSet) self.assertFalse(offspring is parent_a) self.assertFalse(offspring is parent_b) self.assertNotEqual(offspring, parent_a) self.assertNotEqual(offspring, parent_b)
def test_if_is_it_a_tree(self): filename = path.join('datasets', 'ORLibrary', 'steinb15.txt') stpg = ReaderORLibrary().parser(filename) crossover = CrossoverKruskalRST(stpg) parent_a = gen_random_walk(stpg) parent_b = gen_random_walk(stpg) offspring = crossover(parent_a, parent_b) tree = UGraph() for edge in offspring: u, v = edge[0], edge[1] tree.add_edge(u, v) _, response = is_steiner_tree(tree, stpg) self.assertTrue(response['all_terminals_in']) self.assertFalse(response['has_cycle']) self.assertTrue(response['all_edges_are_reliable']) self.assertTrue(response['graph_is_connected'])
def test_is_steiner_tree(self): stpg = self.stpg chromosome = gen_random_walk(stpg) tree = UGraph() for edge in chromosome: v, u = edge tree.add_edge(v, u) _, response = is_steiner_tree(tree, stpg) self.assertTrue(response['all_terminals_in']) self.assertFalse(response['has_cycle']) self.assertTrue(response['all_edges_are_reliable']) self.assertTrue(response['graph_is_connected'])
def test_returning_edge_set(self): stpg = self.stpg chromosome = gen_random_walk(stpg) self.assertIsInstance(chromosome, EdgeSet)