def test_partially_match_crossover(): np.random.seed(42) i1 = pg.Individual(genotype=np.array([4, 3, 5, 7, 8, 1, 2, 0, 9, 6])) i2 = pg.Individual(genotype=np.array([8, 9, 1, 3, 6, 5, 7, 4, 2, 0])) o1, o2 = pg.partially_match_crossover(i1, i2) assert np.array_equal(o1.genotype, np.array([4, 7, 8, 3, 6, 5, 1, 2, 0, 9])) assert np.array_equal(o2.genotype, np.array([9, 3, 6, 7, 8, 1, 5, 4, 2, 0]))
def test_equal_individual(): ind1 = pg.Individual(fitness=pg.Fitness(value=3), genotype=[0, 1, 0, 1, 1]) ind2 = ind1.clone() assert ind1.equal(ind2) is True ind3 = pg.Individual(fitness=pg.Fitness(value=3), genotype=[0, 1, 1, 1, 1]) assert ind1.equal(ind3) is False ind4 = pg.Individual(fitness=pg.Fitness(value=2), genotype=[0, 1, 0, 1, 1]) assert ind1.equal(ind4) is False ind5 = pg.Individual(fitness=pg.Fitness(value=3), genotype=[0, 1, 0, 1, 1]) ind5.run_eval = False assert ind1.equal(ind5) is False
def test_uniform_crossover(): np.random.seed(42) i1 = pg.Individual(genotype=np.array( [0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1])) i2 = pg.Individual(genotype=np.array( [1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1])) o1, o2 = pg.uniform_crossover(i1, i2) assert np.array_equal( o1.genotype, np.array([0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1])) assert np.array_equal( o2.genotype, np.array([1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1]))
def test_flip_mutation3(): np.random.seed(42) i1 = pg.Individual(genotype=np.array([4, 3, 5, 7, 8, 1, 2, 0, 9, 6])) m1 = pg.flip_mutation(i1, gene_rate=None, low=0, high=9) assert m1.run_eval is True assert np.array_equal(m1.genotype, np.array([4, 3, 5, 7, 8, 1, 7, 0, 9, 6]))
def discrete_crossover_adaptive(i1, i2): ''' Discrete Crossover Args: i1 (Individual): indivdiual 1 fixed genome i2 (Individual): indivdiual 2 fixed genome Returns: single offspring from 2 parents ''' o1 = pg.Individual( genotype=np.empty(i1.genotype.size, dtype=i1.genotype.dtype)) o1.parameters = np.empty(i1.parameters.size, dtype=i1.parameters.dtype) for i in range(o1.genotype.size): if np.random.uniform() < 0.5: o1.genotype[i] = i1.genotype[i] else: o1.genotype[i] = i2.genotype[i] for i in range(o1.parameters.size): if np.random.uniform() < 0.5: o1.parameters[i] = i1.parameters[i] else: o1.parameters[i] = i2.parameters[i] return o1
def test_swap_mutation2(): np.random.seed(42) i1 = pg.Individual(genotype=np.array([4, 3, 5, 7, 8, 1, 2, 0, 9, 6])) m1 = pg.swap_mutation(i1, gene_rate=1.0) assert m1.run_eval is True assert np.array_equal(m1.genotype, np.array([0, 2, 3, 4, 9, 5, 8, 1, 7, 6]))
def test_disrete_crossover(): np.random.seed(42) i1 = pg.Individual(genotype=np.array([ 0.61185289, 0.13949386, 0.29214465, 0.36636184, 0.45606998, 0.78517596, 0.19967378, 0.51423444, 0.59241457, 0.04645041 ])) i2 = pg.Individual(genotype=np.array([ 0.60754485, 0.17052412, 0.06505159, 0.94888554, 0.96563203, 0.80839735, 0.30461377, 0.09767211, 0.68423303, 0.44015249 ])) o1 = pg.discrete_crossover(i1, i2) assert np.isclose( o1.genotype.all(), np.array([ 0.61185289, 0.17052412, 0.06505159, 0.94888554, 0.45606998, 0.78517596, 0.19967378, 0.09767211, 0.68423303, 0.44015249 ]).all())
def test_intermediary_crossover(): np.random.seed(42) i1 = pg.Individual(genotype=np.array([ 0.61185289, 0.13949386, 0.29214465, 0.36636184, 0.45606998, 0.78517596, 0.19967378, 0.51423444, 0.59241457, 0.04645041 ])) i2 = pg.Individual(genotype=np.array([ 0.60754485, 0.17052412, 0.06505159, 0.94888554, 0.96563203, 0.80839735, 0.30461377, 0.09767211, 0.68423303, 0.44015249 ])) o1 = pg.intermediary_crossover(i1, i2) assert np.isclose( o1.genotype.all(), np.array([ 0.60969887, 0.15500899, 0.17859812, 0.65762369, 0.710851, 0.79678666, 0.25214378, 0.30595327, 0.6383238, 0.24330145 ]).all())
def test_binary_flip_mutation1(): np.random.seed(42) i1 = pg.Individual(genotype=np.array( [0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1])) m1 = pg.binary_flip_mutation(i1, gene_rate=0.0) assert m1.run_eval is True assert np.array_equal( m1.genotype, np.array([0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1]))
def test_uncorrelated_one_step_mutation(): np.random.seed(42) i1 = pg.Individual(genotype=np.array([ 0.61185289, 0.13949386, 0.29214465, 0.36636184, 0.45606998, 0.78517596, 0.19967378, 0.51423444, 0.59241457, 0.04645041 ])) m1 = pg.uncorrelated_one_step_mutation(i1) assert np.isclose( m1.genotype.all(), np.array([ 0.60427399, 0.17499665, 0.37562893, 0.35352682, 0.44323586, 0.87173989, 0.24174041, 0.48850039, 0.62215478, 0.05481461 ]).all())
def test_clone_individual(): ind1 = pg.Individual(fitness=pg.Fitness(value=3), genotype=[0, 1, 0, 1, 1]) ind2 = ind1.clone() assert ind1.fitness.equal(ind2.fitness) is True assert ind1.genotype == ind2.genotype assert ind1.run_eval == ind2.run_eval ind1.fitness = 2 ind1.genotype = [1, 0, 1, 0, 0] ind1.run_eval = False assert ind2.fitness.value == 3 assert ind2.genotype == [0, 1, 0, 1, 1] assert ind2.run_eval is True
def test_uncorrelated_n_steps_mutation(): np.random.seed(42) i1 = pg.Individual(genotype=np.array([ 0.61185289, 0.13949386, 0.29214465, 0.36636184, 0.45606998, 0.78517596, 0.19967378, 0.51423444, 0.59241457, 0.04645041 ])) m1 = pg.uncorrelated_n_steps_mutation(i1) assert np.isclose( m1.genotype.all(), np.array([ 1.97088495, 0.38304596, -0.28830838, 0.70303098, 0.43352264, 0.86057563, 0.31735872, 1.23638915, 0.6205196, 0.04865446 ]).all())
def test_uniform_mutation3(): np.random.seed(42) i1 = pg.Individual(genotype=np.array([ 0.61185289, 0.13949386, 0.29214465, 0.36636184, 0.45606998, 0.78517596, 0.19967378, 0.51423444, 0.59241457, 0.04645041 ])) m1 = pg.uniform_mutation(i1, gene_rate=None, low=0.0, high=1.0) assert m1.run_eval is True assert np.isclose( m1.genotype.all(), np.array([ 0.61185289, 0.13949386, 0.29214465, 0.36636184, 0.45606998, 0.78517596, 0.86617615, 0.51423444, 0.59241457, 0.96990985 ]).all())
def test_uniform_mutation2(): np.random.seed(42) i1 = pg.Individual(genotype=np.array([ 0.61185289, 0.13949386, 0.29214465, 0.36636184, 0.45606998, 0.78517596, 0.19967378, 0.51423444, 0.59241457, 0.04645041 ])) m1 = pg.uniform_mutation(i1, gene_rate=1.0, low=0.0, high=1.0) assert m1.run_eval is True assert np.isclose( m1.genotype.all(), np.array([ 0.95071431, 0.59865848, 0.15599452, 0.86617615, 0.70807258, 0.96990985, 0.21233911, 0.18340451, 0.52475643, 0.29122914 ]).all())
def test_negative_tournament_selection(): np.random.seed(42) individuals = [ pg.Individual(fitness=pg.Fitness(value=np.random.randint(10))) for _ in range(10) ] pop = pg.Population(individuals=np.array(individuals)) # [6, 3, 7, 4, 6, 9, 2, 6, 7, 4] np.random.seed(42) inds1 = [pg.negative_tournament_selection(pop, size=5) for _ in range(10)] assert [inds1[i].fitness.value for i in range(10)] == [6, 7, 6, 9, 9, 9, 7, 7, 7, 7] np.random.seed(24) inds2 = [pg.negative_tournament_selection(pop, size=5) for _ in range(10)] assert [inds2[i].fitness.value for i in range(10)] == [7, 6, 7, 7, 6, 6, 3, 4, 6, 7]
def test_random_selection(): np.random.seed(42) individuals = [ pg.Individual(fitness=pg.Fitness(value=np.random.randint(10))) for _ in range(10) ] pop = pg.Population(individuals=np.array(individuals)) # [6, 3, 7, 4, 6, 9, 2, 6, 7, 4] np.random.seed(42) inds1 = [pg.random_selection(pop) for _ in range(10)] assert [inds1[i].fitness.value for i in range(10)] == [2, 4, 6, 6, 2, 4, 7, 2, 6, 6] np.random.seed(24) inds2 = [pg.random_selection(pop) for _ in range(10)] assert [inds2[i].fitness.value for i in range(10)] == [7, 4, 6, 6, 3, 3, 3, 6, 6, 4]
def intermediary_crossover(i1, i2): ''' Intermediary Crossover Args: i1 (Individual): indivdiual 1 fixed genome i2 (Individual): indivdiual 2 fixed genome Returns: single offspring from 2 parents ''' o1 = pg.Individual( genotype=np.empty(i1.genotype.size, dtype=i1.genotype.dtype)) for i in range(o1.genotype.size): o1.genotype[i] = (i1.genotype[i] + i2.genotype[i]) / 2.0 return o1
def make_generic_population(size, make_individual_fn, *args, **kargs): ''' Make Generic Population Args: size (int): number of individuals in the population make_individual_fn (function): function that returns a random individual Return: random population of fixed size without fitness evaluation ''' pop = make_empty_population(size) for i in range(size): pop.individuals[i] = pg.Individual() pop.individuals[i].genotype = make_individual_fn(*args, **kargs) return pop
def apply_global_mutation(pop, pool_size, mt_op, **kargs): ''' Apply Global Mutation Args: pop (Population): individuals to apply mutation, randomly picked until pool_size pool_size (int): number of individuals that will be applied mutation mt_op (function): mutation operator **kargs: keyword arguments for the mutation oeprator Return: population of mutated individuals ''' new_pop = pg.make_empty_population(pool_size) for i in range(0, pool_size): index = np.random.randint(pop.size) new_pop.individuals[i] = pg.Individual() new_pop.individuals[i] = mt_op(pop.individuals[index], **kargs) new_pop.individuals[i].run_eval = True return new_pop
def test_make_individual1(): ind = pg.Individual() assert type(ind) is pg.Individual assert ind.fitness is None assert ind.genotype is None assert ind.run_eval is True
def test_make_individual2(): ind = pg.Individual(fitness=pg.Fitness(value=3), genotype=[0, 1, 0, 1, 1]) assert type(ind) is pg.Individual assert ind.fitness.value == 3 assert ind.genotype == [0, 1, 0, 1, 1] assert ind.run_eval is True