def test_fixed(self): G1 = nx.MultiDiGraph() G1.add_edge(0, 0, action_number=0) G1.add_edge(0, 1, action_number=1) G1.add_edge(1, 2, action_number=0) G1.add_edge(2, 1, action_number=0) G1.add_edge(2, 0, action_number=1) G1.add_edge(2, 5, action_number=2) for node in G1: G1.node[node]['type_id'] = node G1.node[node]['parameters'] = pyevolve.G1DList.G1DList(0) G2 = nx.MultiDiGraph() G2.add_edge(0, 1, action_number=0) G2.add_edge(0, 3, action_number=1) G2.add_edge(3, 4, action_number=0) G2.add_edge(1, 4, action_number=0) for node in G2: G2.node[node]['type_id'] = node G2.node[node]['parameters'] = pyevolve.G1DList.G1DList(0) nodes_degrees = [2, 1, 3, 1, 0, 0] nodes_params = [0] * len(nodes_degrees) genome1 = graph_genome.GraphGenome(len(G1), nodes_degrees, nodes_params ) genome1.graph = G1 genome2 = graph_genome.GraphGenome(len(G2), nodes_degrees, nodes_params ) genome2.graph = G2 genome1.prune_non_connected() self.assertEqual(len(genome1), 4) self.assertEqual(len(genome2), 4) sanity_checks(self, genome1) sanity_checks(self, genome2) bogus = str(genome1) bogus = str(genome2) genome1, genome2 = graph_genome.graph_crossover(None, mom=genome1, dad=genome2) sanity_checks(self, genome1) sanity_checks(self, genome2)
def test_pickling(self): num_nodes = 100 genome = graph_genome.GraphGenome(num_nodes, self.nodes_degrees, self.nodes_params) genome.initialize() state = cPickle.dumps(genome) newgenome = cPickle.loads(state) self.assertEqual(len(genome), len(newgenome))
def test_iterator(self): num_nodes = 100 genome = graph_genome.GraphGenome(num_nodes, self.nodes_degrees, self.nodes_params) genome.initialize() all_nodes = [node for node in genome.nodes] self.assertEqual(len(all_nodes), len(genome)) sanity_checks(self, genome)
def test_mutation_crossover(self): num_nodes_dad = random.randint(5,100) num_nodes_mom = random.randint(5,100) num_nodes_types = random.randint(1, 100) nodes_degrees = [random.randint(0,50) for _ in xrange(num_nodes_types)] nodes_params = [random.randint(0,50) for _ in xrange(num_nodes_types)] dad = graph_genome.GraphGenome(num_nodes_dad, nodes_degrees, nodes_params ) mom = graph_genome.GraphGenome(num_nodes_mom, nodes_degrees, nodes_params ) mom.setParams(p_del=0.5, p_add=0.5) dad.setParams(p_del=0.5, p_add=0.5) mom.initialize() dad.initialize() sanity_checks(self, mom) sanity_checks(self, dad) for step in xrange(300): sister, brother = graph_genome.graph_crossover(None, mom=mom, dad=dad) sanity_checks(self, sister) sanity_checks(self, brother) sanity_check_all_connected(self, sister) sanity_check_all_connected(self, brother) sister.mutate(pmut=0.5, ga_engine = None) brother.mutate(pmut=0.5, ga_engine = None) sanity_checks(self, sister) sanity_checks(self, brother) sanity_check_all_connected(self, sister) sanity_check_all_connected(self, brother) dad = brother mom = sister
def test_puning(self): for _ in xrange(100): num_nodes = 100 genome = graph_genome.GraphGenome(num_nodes, self.nodes_degrees, self.nodes_params) genome.initialize() genome.prune_non_connected() sanity_checks(self, genome) sanity_check_all_connected(self, genome)
def test_crossover(self): num_nodes = 15 dad = graph_genome.GraphGenome(num_nodes, self.nodes_degrees, self.nodes_params ) mom = graph_genome.GraphGenome(num_nodes, self.nodes_degrees, self.nodes_params ) mom.setParams(p_del=0.0, p_add=0.0) dad.setParams(p_del=0.0, p_add=0.0) mom.initialize() dad.initialize() sanity_checks(self, mom) sanity_checks(self, dad) for step in xrange(100): mom, dad = graph_genome.graph_crossover(None, mom=mom, dad=dad) sanity_checks(self, mom) sanity_checks(self, dad)
def mutation_test(self): num_nodes = 20 genome = graph_genome.GraphGenome(num_nodes, self.nodes_degrees, self.nodes_params) genome.initialize() sanity_checks(self, genome) genome.setParams(p_del=0.5, p_add=0.5) for _ in xrange(100): genome.mutate(pmut=0.2) sanity_checks(self, genome) sanity_check_all_connected(self, genome)
def test_single_add_node(self): num_nodes = 10 genome = graph_genome.GraphGenome(num_nodes, self.nodes_degrees, self.nodes_params) genome.initialize() length = len(genome) sanity_checks(self, genome) genome.add_random_node() self.assertEqual(len(genome), length + 1) sanity_checks(self, genome)
def test_single_remove_node(self): num_nodes = 20 genome = graph_genome.GraphGenome(num_nodes, self.nodes_degrees, self.nodes_params) genome.initialize() sanity_checks(self, genome) genome.setParams(p_del=0.5, p_add=0.5) length = len(genome) genome.remove_random_node() self.assertEqual(len(genome), length - 1) sanity_checks(self, genome)
def test_adding_removing(self): num_nodes = 20 genome = graph_genome.GraphGenome(num_nodes, self.nodes_degrees, self.nodes_params) genome.initialize() sanity_checks(self, genome) genome.setParams(p_del=0.5, p_add=0.5) for _ in xrange(100): length = len(genome) genome.remove_random_node() self.assertEqual(len(genome), length - 1) sanity_checks(self, genome) length = len(genome) genome.add_random_node() self.assertEqual(len(genome), length + 1) sanity_checks(self, genome)
return c_sum * p_sum def eval_func(chromosome, **args): G = chromosome.graph return nx.average_clustering(nx.Graph(G)) def step_checks(ga_engine): for genome in ga_engine.getPopulation(): sanity_checks(genome) out_degrees = [1, 2, 3] node_params = [0, 0, 0] genome = graph_genome.GraphGenome(25, out_degrees, node_params) genome.evaluator.set(eval_func2) genome.setParams(p_del=0.5, p_add=0.5) ga = GSimpleGA.GSimpleGA(genome) ga.setElitism(True) ga.selector.set(Selectors.GRouletteWheel) ga.getPopulation().setParams(tournamentPool=500) ga.setGenerations(10000) ga.setPopulationSize(500) ga.setCrossoverRate(0.78) ga.setMutationRate(0.08) #ga.setMinimax(Consts.minimaxType["maximize"]) ga.setMinimax(Consts.minimaxType["minimize"])