Exemple #1
0
 def test_solve(self):
     Constants.NSGA2_NUM_GENERATIONS = 3
     Constants.NSGA2_NUM_GENES_MUTATING = 1
     p1 = default_consistent_problem()
     p1.set_value(0, 0)
     p1.set_value(1, 2)
     p1.set_value(2, 5)
     p2 = default_consistent_problem()
     p2.set_value(0, 5)
     p2.set_value(1, 3)
     p2.set_value(2, 5)
     p3 = default_consistent_problem()
     p3.set_value(0, 3)
     p3.set_value(1, 0)
     p3.set_value(2, 4)
     p4 = default_consistent_problem()
     p4.set_value(0, 4)
     p4.set_value(1, 1)
     p4.set_value(2, 2)
     p5 = default_consistent_problem()
     p5.set_value(0, 1)
     p5.set_value(1, 3)
     p5.set_value(2, 1)
     nsga2 = Nsga2([p1, p2, p3, p4, p5])
     solutions = nsga2.solve()
     self.assertTrue(True)
     Random.end_test()
Exemple #2
0
 def emo_phase(self):
     for _ in range(Constants.NSGA2_NUM_GENES_MUTATING):
         random_variable = Random.random_int_between_a_and_b(
             0,
             self.problem.num_variables() - 1)
         new_value = Random.random_choice(
             self.problem.variables[random_variable].domain.values)
         self.problem.set_value(random_variable, new_value)
Exemple #3
0
 def test_get_parents(self):
     Random.begin_test()
     individuals = default_individuals()
     tournament_pool = get_tournament_pool(individuals)
     Random.set_test_value_for("random_choice", tournament_pool[0])
     Random.set_test_value_for("random_choice", tournament_pool[-1])
     mum, dad = get_parents(individuals, False)
     self.assertEqual(mum, tournament_pool[-1])
     self.assertEqual(dad, tournament_pool[0])
     Random.end_test()
Exemple #4
0
 def test_emo_phase(self):
     Random.begin_test()
     Constants.NSGA2_NUM_GENES_MUTATING = 1
     Random.set_test_value_for("random_int_between_a_and_b", 2)
     Random.set_test_value_for("random_choice", 3)
     child = default_dominating_individual()
     child.emo_phase()
     self.assertEqual(child.problem.get_value(0), 1)
     self.assertEqual(child.problem.get_value(1), 2)
     self.assertEqual(child.problem.get_value(2), 3)
     Random.end_test()
Exemple #5
0
 def test_swap_half_genes(self):
     Random.begin_test()
     Random.set_test_value_for("random_int_between_a_and_b", 2)
     parent = default_dominating_individual()
     child = default_other_dominating_individual()
     child.swap_half_genes(parent)
     values = [child.problem.get_value(i) for i in range(child.problem.num_variables())]
     self.assertEqual(values, [1, 3, 1])
     Random.end_test()
Exemple #6
0
def generate_solutions_discrete_domain(problem, population_size):
    solutions = set()
    while len(solutions) < population_size:
        solution = deepcopy(problem)
        current_budget = Constants.BUDGET
        possible_variables = [i for i in range(solution.num_variables())]
        while len(possible_variables) > 0:
            rand_variable_index = Random.random_choice(possible_variables)
            possible_variables.remove(rand_variable_index)
            d = get_max_domain(
                solution.variables[rand_variable_index].domain.values,
                solution.variables[rand_variable_index].objective_info.price,
                current_budget
            )
            if len(d) == 0:
                break
            new_value = Random.random_choice(d)
            current_budget -= new_value * solution.variables[rand_variable_index].objective_info.price
            solution.set_value(
                rand_variable_index,
                new_value
            )
        solutions.add(solution)
    return list(solutions)
Exemple #7
0
 def swap_half_genes(self, other):
     variables = [
         Random.random_int_between_a_and_b(0,
                                           self.problem.num_variables() - 1)
         for _ in range(floor(self.problem.num_variables() / 2))
     ]
     for v in variables:
         if self.problem.get_value(v) != other.problem.get_value(v):
             original = self.problem.get_value(v)
             self.problem.set_value(v, other.problem.get_value(v))
             if not self.problem.consistent():
                 self.problem.set_value(v, original)
             original = other.problem.get_value(v)
             other.problem.set_value(v, self.problem.get_value(v))
             if not other.problem.consistent():
                 other.problem.set_value(v, original)
Exemple #8
0
 def test_get_children(self):
     Random.begin_test()
     for _ in range(4):
         Random.set_test_value_for("random_int_between_a_and_b", 1)
         Random.set_test_value_for("random_int_between_a_and_b", 2)
     individuals = default_individuals()
     son, daughter = get_children(individuals[0], individuals[1])
     son_values = [
         son.problem.get_value(i)
         for i in range(son.problem.num_variables())
     ]
     daughter_values = [
         daughter.problem.get_value(i)
         for i in range(daughter.problem.num_variables())
     ]
     self.assertEqual(son_values, [1, 2, 1])
     self.assertEqual(daughter_values, [0, 2, 1])
     Random.end_test()
Exemple #9
0
def get_parents(parent_population, improved):
    tournament_pool = get_tournament_pool(parent_population)
    if improved:
        gf1 = Random.random_choice(tournament_pool)
        gf2 = Random.random_choice(tournament_pool)
        mum = compare_partners(gf1, gf2)
        bf1 = Random.random_choice(tournament_pool)
        bf2 = Random.random_choice(tournament_pool)
        dad = compare_partners(bf1, bf2)
    else:
        mum = Random.random_choice(tournament_pool)
        dad = Random.random_choice(tournament_pool)
    return mum, dad
Exemple #10
0
 def test_random_int_between_a_and_b(self):
     true_rando = Random.random_int_between_a_and_b(1, 5)
     self.assertLessEqual(true_rando, 5)
     self.assertLessEqual(1, true_rando)
     Random.begin_test()
     Random.set_test_value_for("random_int_between_a_and_b", 1)
     Random.set_test_value_for("random_int_between_a_and_b", 2)
     Random.set_test_value_for("random_int_between_a_and_b", 3)
     self.assertEqual(Random.random_int_between_a_and_b(1, 5), 3)
     self.assertEqual(Random.random_int_between_a_and_b(1, 5), 2)
     self.assertEqual(Random.random_int_between_a_and_b(1, 5), 1)
     Random.end_test()
Exemple #11
0
 def test_random_float_between_a_and_b(self):
     true_rando = Random.random_float_between_a_and_b(2.4142, 3.1416)
     self.assertLessEqual(true_rando, 3.1416)
     self.assertLessEqual(2.4142, true_rando)
     Random.begin_test()
     Random.set_test_value_for("random_float_between_0_and_1", 2.5)
     Random.set_test_value_for("random_float_between_0_and_1", 2.9)
     Random.set_test_value_for("random_float_between_0_and_1", 3.1)
     self.assertEqual(Random.random_float_between_0_and_1(), 3.1)
     self.assertEqual(Random.random_float_between_0_and_1(), 2.9)
     self.assertEqual(Random.random_float_between_0_and_1(), 2.5)
     Random.end_test()
Exemple #12
0
 def test_random_float_between_0_and_1(self):
     true_rando = Random.random_float_between_0_and_1()
     self.assertLessEqual(true_rando, 1.0)
     self.assertLessEqual(0.0, true_rando)
     Random.begin_test()
     Random.set_test_value_for("random_float_between_0_and_1", 0.1)
     Random.set_test_value_for("random_float_between_0_and_1", 0.2)
     Random.set_test_value_for("random_float_between_0_and_1", 0.3)
     self.assertEqual(Random.random_float_between_0_and_1(), 0.3)
     self.assertEqual(Random.random_float_between_0_and_1(), 0.2)
     self.assertEqual(Random.random_float_between_0_and_1(), 0.1)
     Random.end_test()
Exemple #13
0
 def test_begin_test(self):
     self.assertFalse(Random.test)
     Random.begin_test()
     self.assertTrue(Random.test)
     Random.end_test()
Exemple #14
0
 def test_random_choice(self):
     true_rando = Random.random_choice([0, 1, 2, 3, 4, 5])
     self.assertIn(true_rando, [0, 1, 2, 3, 4, 5])
     Random.begin_test()
     Random.set_test_value_for("random_choice", 1)
     Random.set_test_value_for("random_choice", 2)
     Random.set_test_value_for("random_choice", 3)
     self.assertEqual(Random.random_choice([0, 1, 2, 3, 4, 5]), 3)
     self.assertEqual(Random.random_choice([0, 1, 2, 3, 4, 5]), 2)
     self.assertEqual(Random.random_choice([0, 1, 2, 3, 4, 5]), 1)
     Random.end_test()
     true_rando = Random.random_choice([0, 4, 5])
     self.assertNotIn(true_rando, [1, 2, 3])
Exemple #15
0
 def test_set_test_value_for(self):
     Random.begin_test()
     self.assertEqual(
         Random.non_random_values["random_float_between_0_and_1"], [])
     self.assertEqual(
         Random.non_random_values["random_int_between_a_and_b"], [])
     self.assertEqual(
         Random.non_random_values["random_float_between_a_and_b"], [])
     self.assertEqual(Random.non_random_values["random_choice"], [])
     Random.set_test_value_for("random_float_between_0_and_1", 0.123)
     Random.set_test_value_for("random_float_between_0_and_1", 0.456)
     Random.set_test_value_for("random_int_between_a_and_b", 0)
     Random.set_test_value_for("random_int_between_a_and_b", 1)
     Random.set_test_value_for("random_int_between_a_and_b", 2)
     Random.set_test_value_for("random_float_between_a_and_b", 0.000)
     Random.set_test_value_for("random_float_between_a_and_b", 4.123)
     Random.set_test_value_for("random_float_between_a_and_b", 5.432)
     Random.set_test_value_for("random_choice", 3)
     Random.set_test_value_for("random_choice", 5)
     Random.set_test_value_for("random_choice", 2)
     self.assertEqual(
         Random.non_random_values["random_float_between_0_and_1"], [0.123, 0.456])
     self.assertEqual(
         Random.non_random_values["random_int_between_a_and_b"], [0, 1, 2])
     self.assertEqual(Random.non_random_values["random_float_between_a_and_b"], [
         0.000, 4.123, 5.432])
     self.assertEqual(Random.non_random_values["random_choice"], [3, 5, 2])
     Random.end_test()