Пример #1
0
    def test_nsga2_simple(self):
        size = 14
        master_graph = nx.grid_graph([size, size])
        master_graph.graph['districts'] = 2
        nx.set_node_attributes(master_graph, {i: 1
                                              for i in master_graph},
                               name='pop')
        nx.set_node_attributes(master_graph, {(i, j): box(i, j, i + 1, j + 1)
                                              for (i, j) in master_graph},
                               name='shape')

        frontier, data = genetics.run_nsga2(master_graph,
                                            [PopulationEquality(master_graph)],
                                            multiprocess=True,
                                            max_generations=100,
                                            pop_size=500,
                                            optimize=True)

        for gen_id in data:
            if gen_id % 20 != 0:
                continue
            gen_data = data[gen_id]
            random_pareto: Candidate = random.choice(
                gen_data.get('pareto_frontier'))
            random_pareto.plot(save=True)
            random_pareto.export()

        print("\n".join("{}".format(i.chromosome.get_component_scores())
                        for i in frontier))
Пример #2
0
 def setUpClass(cls):
     cls.master_graph = nx.cycle_graph(10)
     cls.master_graph.graph['districts'] = 2
     nx.set_node_attributes(cls.master_graph,
                            {i: 1
                             for i in cls.master_graph},
                            name='pop')
     Chromosome.objectives = [PopulationEquality(cls.master_graph)]
Пример #3
0
 def setUp(self):
     self.simple_graph = nx.path_graph(4)
     nx.set_node_attributes(self.simple_graph, {i: 1
                                                for i in range(4)},
                            name='pop')
     Chromosome.objectives = [
         PopulationEquality(self.simple_graph, districts=2)
     ]
Пример #4
0
    def test_find_best_neighbor_simple(self):
        master_graph = nx.path_graph(6)
        nx.set_node_attributes(master_graph, {i: 1
                                              for i in master_graph},
                               name='pop')
        master_graph = nx.freeze(master_graph)
        Chromosome.objectives = [PopulationEquality(master_graph)]

        s1 = Chromosome(master_graph, [1, 1, 2, 2, 2, 2])
        s2 = find_best_neighbor(s1)

        self.assertEqual(s2.get_scores(), [0.0])
        self.assertEqual(s2.get_assignment(), [1, 1, 1, 2, 2, 2])

        s3 = find_best_neighbor(s2)
        self.assertIsNone(s3)
Пример #5
0
    def test_optimize_simple(self):
        master_graph = nx.path_graph(6)
        nx.set_node_attributes(master_graph, {i: 1
                                              for i in master_graph},
                               name='pop')
        Chromosome.objectives = [PopulationEquality(master_graph)]

        chromosome = Chromosome(master_graph, [1, 2, 2, 2, 2, 2])
        best_state = optimize(chromosome)

        self.assertEqual(best_state.get_scores(), [0.0])
        self.assertEqual(best_state.get_assignment(), [1, 1, 1, 2, 2, 2])

        better_state = optimize(best_state)
        best_state.normalize()
        better_state.normalize()
        self.assertEqual(best_state, better_state)
Пример #6
0
    def setUp(self):
        self.master_graph = nx.Graph()
        nx.add_path(self.master_graph, [0, 1, 2])
        nx.add_path(self.master_graph, [5, 6, 7])
        nx.add_path(self.master_graph, [2, 3, 4, 5])
        self.master_graph.graph['order'] = {i: i for i in range(8)}
        self.master_graph.graph['districts'] = 2
        nx.set_node_attributes(self.master_graph,
                               {i: 1
                                for i in self.master_graph},
                               name='pop')
        self.master_graph = nx.freeze(self.master_graph)

        Chromosome.objectives = [PopulationEquality(self.master_graph)]

        self.m_tqdm_patch = patch('elbridge.evolution.search.tqdm')
        self.m_tqdm = self.m_tqdm_patch.start()
        self.m_tqdm.side_effect = lambda x, *y, **z: x
Пример #7
0
    def test_connect_vertices_complex_graph(self):
        graph = nx.grid_graph([3, 3])
        nx.set_node_attributes(graph, {i: 1 for i in graph}, name='pop')
        Chromosome.objectives = [PopulationEquality(graph, districts=2)]

        chromosome = Chromosome(graph, [1, 1, 1, 1, 1, 1, 2, 2, 2])

        chromosome = chromosome.connect_vertices(((1, 1), (2, 1)))
        self.assertEqual(chromosome.get_assignment(),
                         [1, 1, 1, 1, 1, 1, 2, 1, 2])

        chromosome = chromosome.connect_vertices(((2, 1), (2, 0)))
        self.assertEqual(chromosome.get_assignment(),
                         [1, 1, 1, 1, 1, 1, 1, 1, 2])

        chromosome = chromosome.connect_vertices(((2, 1), (2, 2)))
        self.assertEqual(chromosome.get_assignment(),
                         [1, 1, 1, 1, 1, 1, 1, 1, 1])
Пример #8
0
    def test_optimize_moderately_complex(self):
        grid_size = [32, 32]
        node_count = grid_size[0] * grid_size[1]
        edge_count = (grid_size[0] -
                      1) * grid_size[1] + grid_size[0] * (grid_size[1] - 1)

        districts = 2
        assignment = random.choices(range(1, districts + 1), k=node_count)

        master_graph = nx.grid_graph(grid_size)
        nx.set_node_attributes(master_graph, {i: 1
                                              for i in master_graph},
                               name='pop')
        master_graph.graph['districts'] = districts

        Chromosome.objectives = [PopulationEquality(master_graph)]
        chromosome = Chromosome(master_graph, assignment)

        best_state = optimize(chromosome, sample_size=edge_count * 2)
        self.assertTrue(best_state.dominates(chromosome))
Пример #9
0
    def test_find_best_neighbor_complex(self):
        size = 1000

        master_graph = nx.path_graph(size)
        nx.set_node_attributes(master_graph, {i: 1
                                              for i in range(2)},
                               name='pop')
        nx.set_node_attributes(master_graph, {i: 2
                                              for i in range(2, size)},
                               name='pop')
        master_graph.graph['districts'] = 2

        Chromosome.objectives = [PopulationEquality(master_graph)]
        chromosome = Chromosome(master_graph, list(range(size)))

        new_chromosome = find_best_neighbor(chromosome, sample_size=size * 2)

        self.assertEqual(new_chromosome.get_scores(), [0])
        self.assertEqual(new_chromosome.get_assignment(),
                         [1, 1] + list(range(2, size)))