-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7_searchbasedtesting.py
126 lines (88 loc) · 3.09 KB
/
7_searchbasedtesting.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import random
def test_me(x, y):
if x == 2 * (y + 1):
return True
else:
return False
def get_fitness(test):
return abs(test[0] - (2 * (test[1] + 1)))
num_parameters = 2
MAX = 1000000
MIN = -MAX
def get_random_test():
return [random.randint(MIN, MAX) for _ in range(num_parameters)]
def get_neighbours(individual):
neighbours = []
for p in range(len(individual)):
if individual[p] > MIN:
neighbour = individual[:]
neighbour[p] = individual[p] - 1
neighbours.append(neighbour)
if individual[p] < MAX:
neighbour = individual[:]
neighbour[p] = individual[p] + 1
neighbours.append(neighbour)
return neighbours
def hillclimbing():
current = get_random_test()
for i in range(10000):
neighbours = get_neighbours(current)
best_neighbour = min(neighbours, key=lambda x: get_fitness(x))
if get_fitness(best_neighbour) < get_fitness(current):
current = best_neighbour
else:
# Random restart
current = get_random_test()
print(f"Current fitness: {get_fitness(current)}")
return current
test = hillclimbing()
print(test)
if test_me(*test):
print("Target found")
def mutate(individual):
P_mutate = 1/len(individual)
mutated = individual[:]
for position in range(len(individual)):
if random.random() < P_mutate:
mutated[position] = int(random.gauss(mutated[position], 20))
return mutated
def crossover(parent1, parent2):
pos = random.randint(0, len(parent1))
offspring1 = parent1[:pos] + parent2[pos:]
offspring2 = parent2[:pos] + parent1[pos:]
return offspring1, offspring2
tournament_size = 3
def selection(population):
candidates = random.sample(population, tournament_size)
winner = min(candidates, key=lambda x: get_fitness(x))
return winner
elite_size = 2
def elitism(population):
population.sort(key = lambda x: get_fitness(x))
return population[:elite_size]
population_size = 30
max_gen = 100
P_xover = 0.7
def ga():
population = [get_random_test() for _ in range(population_size)]
best_individual = min(population, key = lambda x: get_fitness(x))
best_fitness = get_fitness(best_individual)
for i in range(max_gen):
new_population = elitism(population)
while len(new_population) < len(population):
parent1 = selection(population)
parent2 = selection(population)
if random.random() < P_xover:
offspring1, offspring2 = crossover(parent1, parent2)
else:
offspring1, offspring2 = parent1, parent2
offspring1 = mutate(offspring1)
offspring2 = mutate(offspring2)
new_population.append(offspring1)
new_population.append(offspring2)
population = new_population
best_individual = min(population, key=lambda x: get_fitness(x))
best_fitness = get_fitness(best_individual)
print(f"Generation {i}: Fitness {best_fitness}")
return best_individual
print(ga())