Ejemplo n.º 1
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()
Ejemplo n.º 2
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()
Ejemplo n.º 3
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()
Ejemplo n.º 4
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()
Ejemplo n.º 5
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()
Ejemplo n.º 6
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()
Ejemplo n.º 7
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()
Ejemplo n.º 8
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()
Ejemplo n.º 9
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])
Ejemplo n.º 10
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()
Ejemplo n.º 11
0
 def test_begin_test(self):
     self.assertFalse(Random.test)
     Random.begin_test()
     self.assertTrue(Random.test)
     Random.end_test()