def test_selection(self): chromosome2 = Chromosome( [Gene(2, 1, True), Gene(3, 1, True), Gene(4, 1, True)]) chromosome1 = Chromosome( [Gene(1, 2, True), Gene(3, 4, True), Gene(5, 6, True)]) chromosome3 = Chromosome( [Gene(5, 1, True), Gene(8, 2, True), Gene(3, 1, True)]) pop = Population([chromosome1, chromosome2, chromosome3]) self.assertEqual(pop, pop.selection())
def test_selection_with_less_chromosomes(self): chromosome1 = Chromosome( [Gene(2, 1, True), Gene(3, 1, True), Gene(4, 1, True)]) chromosome2 = Chromosome( [Gene(1, 2, True), Gene(3, 4, True), Gene(5, 6, True)]) chromosome3 = Chromosome( [Gene(5, 1, True), Gene(8, 2, True), Gene(3, 1, True)]) pop = Population([chromosome1, chromosome2, chromosome3]) expected_pop = Population([chromosome3, chromosome1]) self.assertEqual(expected_pop, pop.selection(maximum_selection=2))
def test_selection_with_different_fitness_func(self): chromosome1 = Chromosome( [Gene(2, 1, True), Gene(3, 1, True), Gene(4, 1, True)]) chromosome2 = Chromosome( [Gene(1, 2, True), Gene(3, 4, True), Gene(5, 6, True)]) chromosome3 = Chromosome( [Gene(5, 1, True), Gene(8, 2, True), Gene(3, 1, True)]) pop = Population([chromosome1, chromosome2, chromosome3], max_weight=9) expected_pop = Population([chromosome3, chromosome1], max_weight=9) self.assertEqual(expected_pop, pop.selection(maximum_selection=2))
def test_perform_crossover_with_seed(self): chromosome1 = Chromosome( [Gene(2, 1, True), Gene(3, 1, True), Gene(4, 1, True)]) chromosome2 = Chromosome( [Gene(1, 2, True), Gene(3, 4, True), Gene(5, 6, True)]) pop = Population([chromosome1, chromosome2]) crossover_chromosome = pop._perform_crossover(1) expected_crossover_chromosome = Chromosome( [Gene(2, 1, True), Gene(3, 4, True), Gene(5, 6, True)]) self.assertEqual(crossover_chromosome, expected_crossover_chromosome)
def test_crossover_does_not_return_empty_chromosomes(self): population_limit = 10 chromosome1 = Chromosome( [Gene(2, 1, True), Gene(3, 1, True), Gene(4, 1, True)]) chromosome2 = Chromosome( [Gene(1, 2, True), Gene(3, 4, True), Gene(5, 6, True)]) chromosome3 = Chromosome( [Gene(5, 1, True), Gene(8, 2, True), Gene(3, 1, True)]) pop = Population([chromosome1, chromosome2, chromosome3], population_limit=population_limit) new_pop = pop.crossover() for chrom in new_pop.chromosomes: self.assertTrue(len(chrom.genes) > 0)
def test_mutation_does_not_alter_chromosome_length(self): population_limit = 10 chromosome1 = Chromosome( [Gene(2, 1, True), Gene(3, 1, True), Gene(4, 1, True)]) chromosome2 = Chromosome( [Gene(1, 2, True), Gene(3, 4, True), Gene(5, 6, True)]) chromosome3 = Chromosome( [Gene(5, 1, True), Gene(8, 2, True), Gene(3, 1, True)]) expected_chromosome_length = 3 pop = Population([chromosome1, chromosome2, chromosome3], population_limit=population_limit) pop.mutate(0.2) for chromosome in pop.chromosomes: self.assertTrue(len(chromosome.genes), expected_chromosome_length)
def load_from_file(filepath: str) -> List[Gene]: """ Attemps to load the gene data from a file. :param filepath: The path of the file. :return: A list of genes generated from the file specificall for the knapsack problem. """ try: data = np.loadtxt(filepath) genes = [] for row in data: genes.append(Gene(row[0], row[1], True)) return genes except Exception as err: logger.error(err) sys.exit(0)
def test_crossover_returns_same_population_size(self): population_limit = 10 chromosome1 = Chromosome( [Gene(2, 1, True), Gene(3, 1, True), Gene(4, 1, True)]) chromosome2 = Chromosome( [Gene(1, 2, True), Gene(3, 4, True), Gene(5, 6, True)]) chromosome3 = Chromosome( [Gene(5, 1, True), Gene(8, 2, True), Gene(3, 1, True)]) pop = Population([chromosome1, chromosome2, chromosome3], population_limit=population_limit) new_pop = pop.crossover() expected_pop_size = population_limit for chrom in new_pop.chromosomes: self.assertTrue(len(chrom.genes) > 0) self.assertTrue(len(new_pop.chromosomes) == expected_pop_size)
def test_fitness_positive_fitness_high_values(self): genes = [Gene(6, 1, True), Gene(8, 2, True), Gene(4, 3, True)] chromosome = Chromosome(genes) score = chromosome.fitness_score( BoundedKnapsackGA.fitness_func(BoundedKnapsackGA.MAX_WEIGHT)) other_genes = [Gene(2, 1, True), Gene(3, 2, True), Gene(4, 3, True)] other_chromosome = Chromosome(other_genes) other_score = other_chromosome.fitness_score( BoundedKnapsackGA.fitness_func(BoundedKnapsackGA.MAX_WEIGHT)) self.assertTrue(score > 0 and score > other_score)
def _initialize_first_population(self, gene_pool: List[Gene]): """ Helper method that creates a new population with randomly generated gene states. :param gene_pool: The pool of genes that we can use for each chromosome. :return: A population of chromosomes composed of genes. """ from genetic_algorithm.Population import Population population = list() population.append(Chromosome(gene_pool)) for _ in range(self.pop_limit): new_chromosome = [] for gene in gene_pool: toss = bool(random.getrandbits(1)) new_chromosome.append(Gene(gene.value, gene.weight, toss)) population.append(Chromosome(new_chromosome)) return Population(population, population_limit=self.pop_limit)
def test_perform_crossover(self): chromosome1 = Chromosome( [Gene(2, 1, True), Gene(3, 1, True), Gene(4, 1, True)]) chromosome2 = Chromosome( [Gene(1, 2, True), Gene(3, 4, True), Gene(5, 6, True)]) pop = Population([chromosome1, chromosome2]) crossover_pop = pop._perform_crossover() expected_len = 3 self.assertTrue(len(crossover_pop) == expected_len)
def test_fitness_func_bad_fitness(self): genes = [Gene(1, 5, True), Gene(2, 8, True), Gene(3, 2, True)] chromosome = Chromosome(genes) score = chromosome.fitness_score( BoundedKnapsackGA.fitness_func(BoundedKnapsackGA.MAX_WEIGHT)) self.assertTrue(score == 0)
def test_fitness_func_with_reward(self): genes = [Gene(2, 1, True), Gene(3, 2, True), Gene(4, 3, True)] chromosome = Chromosome(genes) score = chromosome.fitness_score( BoundedKnapsackGA.fitness_func(BoundedKnapsackGA.MAX_WEIGHT)) self.assertTrue(score > 0)
def test_inequality_between_chromosomes(self): first_genes = [Gene(1, 2, True), Gene(3, 4, True)] second_genes = [Gene(3, 4, True), Gene(1, 2, True)] first_chromosome = Chromosome(first_genes) second_chromosome = Chromosome(second_genes) self.assertFalse(first_chromosome == second_chromosome)
def test_equality_between_chromosomes(self): genes = [Gene(1, 2, True), Gene(3, 4, True)] first_chromosome = Chromosome(genes) second_chromosome = Chromosome(genes) self.assertTrue(first_chromosome == second_chromosome)
def test_gene_mutation_multiple_choice_pool(self): gene = Gene(1, 2, True) gene.mutate(mutation_chance=0.0) self.assertTrue(gene.is_active)
def test_gene_mutation_with_chance(self): gene = Gene(1, 2, True) gene.mutate(mutation_chance=0.2, mutation_seed=1) self.assertFalse(gene.is_active)
def test_gene_non_mutation(self): gene = Gene(1, 2, True) gene.mutate(mutation_chance=0.2, mutation_seed=2) self.assertTrue(gene.is_active)
def test_mutation_alters_chromosome_length(self): genes = [Gene(6, 1, True), Gene(8, 2, True), Gene(4, 3, True)] expected_length = 3 chromosome = Chromosome(genes) chromosome.mutate(1) self.assertTrue(len(chromosome) == expected_length)
def test_gene_mutation(self): gene = Gene(1, 2, False) gene.mutate(mutation_chance=1.0) self.assertTrue(gene.is_active)