Exemplo n.º 1
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])
Exemplo n.º 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)
Exemplo n.º 3
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)
Exemplo n.º 4
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