def test_ga_uses_one_iteration_for_trivially_satisfiable_problem(self): v1 = Variable('a') c1 = Clause([Literal(v1)]) maxsat = MAXSAT([v1], [c1]) ga = GA(maxsat) _, _, iteration = ga.run() self.assertEqual(1, iteration)
def test_ga_uses_one_iteration_for_unsatisfiable_problem_if_not_below_threshold(self): v1 = Variable('a') c1 = Clause([Literal(v1, positive=True)]) c2 = Clause([Literal(v1, positive=False)]) maxsat = MAXSAT([v1], [c1, c2]) ga = GA(maxsat, fitness_threshold=0.5) _, _, iteration = ga.run() self.assertEqual(1, iteration)
def test_ga_achieves_fitness_of_one(self): v1 = Variable('a') c1 = Clause([Literal(v1)]) maxsat = MAXSAT([v1], [c1]) ga = GA(maxsat) _, fitness, _ = ga.run() self.assertEqual(1, fitness)
def test_ga_uses_all_iterations_for_unsatisfiable_problem_if_below_threshold(self): v1 = Variable('a') c1 = Clause([Literal(v1, positive=True)]) c2 = Clause([Literal(v1, positive=False)]) maxsat = MAXSAT([v1], [c1, c2]) ga = GA(maxsat) _, _, iteration = ga.run() self.assertEqual(iteration, ga.max_iterations)
def test_get_candidate_fitness_returns_half(self): v1 = Variable('a') c1 = Clause([Literal(v1, positive=True)]) c2 = Clause([Literal(v1, positive=False)]) maxsat = MAXSAT([v1], [c1, c2]) valuation = Valuation.init_random_from_variables([v1]) ga = GA(maxsat) self.assertEqual(0.5, ga.get_candidate_fitness(valuation))
def test_ga_achieves_fitness_of_half(self): v1 = Variable('a') c1 = Clause([Literal(v1, positive=True)]) c2 = Clause([Literal(v1, positive=False)]) maxsat = MAXSAT([v1], [c1, c2]) ga = GA(maxsat) _, fitness, _ = ga.run() self.assertEqual(0.5, fitness)
def test_get_candidate_fitness_returns_one(self): v1 = Variable('a') v2 = Variable('b') c1 = Clause([Literal(v1, positive=True)]) c2 = Clause([Literal(v2, positive=False)]) maxsat = MAXSAT([v1, v2], [c1, c2]) valuation = Valuation({v1: True, v2: False}) ga = GA(maxsat) self.assertEqual(1, ga.get_candidate_fitness(valuation))
def compute(day): get_whole_trunk() trunk_max_order = get_trunk_max_order() data = get_orders_trunk_can_take(trunk_max_order) # print data ga = GA() log.info('start to compute') ga.GA_main(data, trunk_max_order) log.info('ga down.start to get best gene') best_gene = ga.selectBest() gene_data = best_gene.gene_to_data(ga.order, ga.key_order) log.info('start to modify_model') modify_model(gene_data) all_scheduling[day] = gene_data
def compute(day): get_whole_trunk() trunk_max_order = get_trunk_max_order() data = get_orders_trunk_can_take(trunk_max_order) trunk_data, order_list = get_orders_list(trunk_max_order, data) gene_len = 0 gene_len += len(order_list) * gene_bits print 'gene length: ', len(order_list) * gene_bits, len(order_list) ga = GA() log.info('start to compute') # ga.GA_main(data, trunk_max_order) ga.GA_main2(trunk_data, order_list) log.info('ga down.start to get best gene') best_gene = ga.selectBest() best_gene.gene_to_data(ga.gene_bits, ga.order_list) log.info('start to modify_model') all_scheduling[day] = modify_model(best_gene.gene_data, trunk_data)
def test_generate_population_generates_population_of_correct_size(self): population_size = 32 ga = GA(self.rand_maxsat, population_size=population_size) population = ga.generate_population() self.assertEqual(population_size, len(population))
def setUp(self): self.rand_maxsat = ProblemGenerator().generate_problem() self.rand_ga = GA(self.rand_maxsat)
class TestGA(unittest.TestCase): def setUp(self): self.rand_maxsat = ProblemGenerator().generate_problem() self.rand_ga = GA(self.rand_maxsat) def test_create_ga_without_problem_fails(self): self.assertRaises(TypeError, lambda l: GA()) def test_ga_achieves_fitness_of_half(self): v1 = Variable('a') c1 = Clause([Literal(v1, positive=True)]) c2 = Clause([Literal(v1, positive=False)]) maxsat = MAXSAT([v1], [c1, c2]) ga = GA(maxsat) _, fitness, _ = ga.run() self.assertEqual(0.5, fitness) def test_ga_achieves_fitness_of_one(self): v1 = Variable('a') c1 = Clause([Literal(v1)]) maxsat = MAXSAT([v1], [c1]) ga = GA(maxsat) _, fitness, _ = ga.run() self.assertEqual(1, fitness) def test_ga_uses_all_iterations_for_unsatisfiable_problem_if_below_threshold(self): v1 = Variable('a') c1 = Clause([Literal(v1, positive=True)]) c2 = Clause([Literal(v1, positive=False)]) maxsat = MAXSAT([v1], [c1, c2]) ga = GA(maxsat) _, _, iteration = ga.run() self.assertEqual(iteration, ga.max_iterations) def test_ga_uses_one_iteration_for_unsatisfiable_problem_if_not_below_threshold(self): v1 = Variable('a') c1 = Clause([Literal(v1, positive=True)]) c2 = Clause([Literal(v1, positive=False)]) maxsat = MAXSAT([v1], [c1, c2]) ga = GA(maxsat, fitness_threshold=0.5) _, _, iteration = ga.run() self.assertEqual(1, iteration) def test_ga_uses_one_iteration_for_trivially_satisfiable_problem(self): v1 = Variable('a') c1 = Clause([Literal(v1)]) maxsat = MAXSAT([v1], [c1]) ga = GA(maxsat) _, _, iteration = ga.run() self.assertEqual(1, iteration) def test_generate_population_returns_list(self): population = self.rand_ga.generate_population() self.assertTrue(isinstance(population, list)) def test_generate_population_generates_population_of_correct_size(self): population_size = 32 ga = GA(self.rand_maxsat, population_size=population_size) population = ga.generate_population() self.assertEqual(population_size, len(population)) def test_generate_population_generates_population_of_valuation_instances(self): population = self.rand_ga.generate_population() self.assertTrue(all(isinstance(p, Valuation) for p in population)) def test_get_candidate_fitness_returns_half(self): v1 = Variable('a') c1 = Clause([Literal(v1, positive=True)]) c2 = Clause([Literal(v1, positive=False)]) maxsat = MAXSAT([v1], [c1, c2]) valuation = Valuation.init_random_from_variables([v1]) ga = GA(maxsat) self.assertEqual(0.5, ga.get_candidate_fitness(valuation)) def test_get_candidate_fitness_returns_one(self): v1 = Variable('a') v2 = Variable('b') c1 = Clause([Literal(v1, positive=True)]) c2 = Clause([Literal(v2, positive=False)]) maxsat = MAXSAT([v1, v2], [c1, c2]) valuation = Valuation({v1: True, v2: False}) ga = GA(maxsat) self.assertEqual(1, ga.get_candidate_fitness(valuation)) def test_get_population_fitness_returns_list(self): population = self.rand_ga.generate_population() self.rand_ga.population = population candidate_fitnesses = self.rand_ga.get_population_fitness() self.assertEqual(list, type(candidate_fitnesses)) def test_get_population_fitness_returns_list_containing_valuations(self): population = self.rand_ga.generate_population() self.rand_ga.population = population candidate_fitnesses = self.rand_ga.get_population_fitness() self.assertTrue(all(isinstance(k[0], Valuation) for k in candidate_fitnesses)) def test_get_population_fitness_returns_list_containing_floats(self): population = self.rand_ga.generate_population() self.rand_ga.population = population candidate_fitnesses = self.rand_ga.get_population_fitness() self.assertTrue(all(isinstance(k[1], float) for k in candidate_fitnesses)) def test_create_offspring_creates_two_children(self): valuation1 = Valuation.init_random_from_variables(self.rand_maxsat.variables) valuation2 = Valuation.init_random_from_variables(self.rand_maxsat.variables) offspring = self.rand_ga.create_offspring(valuation1, valuation2) self.assertEqual(2, len(offspring)) def test_create_offspring_creates_first_child_correctly_until_crossover_point(self): variables = self.rand_maxsat.variables crossover_index = int(len(variables) / 2) valuation1 = Valuation.init_random_from_variables(variables) valuation2 = Valuation.init_random_from_variables(variables) offspring = self.rand_ga.create_offspring(valuation1, valuation2, crossover_index=crossover_index) child1 = offspring[0] self.assertTrue( all(child1.get_value_for_variable(v) == valuation1.get_value_for_variable(v) for v in variables[:crossover_index + 1]) ) def test_create_offspring_creates_first_child_correctly_after_crossover_point(self): variables = self.rand_maxsat.variables crossover_index = int(len(variables) / 2) valuation1 = Valuation.init_random_from_variables(variables) valuation2 = Valuation.init_random_from_variables(variables) offspring = self.rand_ga.create_offspring(valuation1, valuation2, crossover_index=crossover_index) child1 = offspring[0] self.assertTrue( all(child1.get_value_for_variable(v) == valuation2.get_value_for_variable(v) for v in variables[crossover_index + 1:]) ) def test_create_offspring_creates_second_child_correctly_until_crossover_point(self): variables = self.rand_maxsat.variables crossover_index = int(len(variables) / 2) valuation1 = Valuation.init_random_from_variables(variables) valuation2 = Valuation.init_random_from_variables(variables) offspring = self.rand_ga.create_offspring(valuation1, valuation2, crossover_index=crossover_index) child2 = offspring[1] self.assertTrue( all(child2.get_value_for_variable(v) == valuation2.get_value_for_variable(v) for v in variables[:crossover_index + 1]) ) def test_create_offspring_creates_second_child_correctly_after_crossover_point(self): variables = self.rand_maxsat.variables crossover_index = int(len(variables) / 2) valuation1 = Valuation.init_random_from_variables(variables) valuation2 = Valuation.init_random_from_variables(variables) offspring = self.rand_ga.create_offspring(valuation1, valuation2, crossover_index=crossover_index) child2 = offspring[1] self.assertTrue( all(child2.get_value_for_variable(v) == valuation1.get_value_for_variable(v) for v in variables[crossover_index + 1:]) )