-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_evolutionary.py
88 lines (67 loc) · 2.61 KB
/
test_evolutionary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import numpy as np
from evolutionary import *
from utils.utilities import generate_population
def test_generate_population_one_dimention():
population = generate_population(1, 20, 100)
assert population.shape == (20, 1)
def test_generate_population_n_dimentions():
population = generate_population(2, 31, 100)
assert population.shape == (31, 2)
population = generate_population(10, 112, 100)
assert population.shape == (112, 10)
def test_evaluate_fitness():
population = np.array([
np.array([2]),
np.array([-5]),
np.array([-17]),
np.array([0]),
np.array([121])
])
q = lambda x: x**2
fitness = evaluate_fitness(q, population)
assert fitness == [4, 25, 289, 0, 14641]
def test_find_best():
population = np.array([
np.array([2]),
np.array([-5]),
np.array([-17]),
np.array([0]),
np.array([121])
])
q = lambda x: x**2
fitness = evaluate_fitness(q, population)
best_individual, best_fitness = find_best(population, fitness)
assert best_individual == np.array([0])
assert best_fitness == 0
def test_reproduction():
population = generate_population(1, 20, 50)
q = lambda x: x**2
fitness = evaluate_fitness(q, population)
offspring = reproduction(population, fitness, 20, 2)
assert offspring.shape == population.shape
offspring = reproduction(population, fitness, 8, 2)
assert offspring.shape[0] == 8
assert offspring.shape[1] == 1
def test_crossover():
population = generate_population(2, 30, 20)
offspring = crossover(population, 0.3, 0.1)
assert offspring.shape == population.shape
population = generate_population(3, 15, 5)
offspring = crossover(population, 0.3, 0.1)
assert offspring.shape == population.shape
def test_mutation():
population = generate_population(3, 24, 12)
offspring = mutation(population, 0.4, 1)
assert offspring.shape == population.shape
def test_succession():
population = generate_population(2, 6, 100)
q = lambda x, y: x**2 + y
fitness = evaluate_fitness(q, population)
offspring = reproduction(population, fitness, 6, 2)
offspring_fitness = evaluate_fitness(q, offspring)
successors, successors_fitness = succession(population, fitness, offspring, offspring_fitness, 1)
assert successors.shape == population.shape
assert len(successors_fitness) == successors.shape[0]
successors, successors_fitness = succession(population, fitness, offspring, offspring_fitness, 4)
assert successors.shape == population.shape
assert len(successors_fitness) == successors.shape[0]