-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathga.py
More file actions
executable file
·138 lines (103 loc) · 3.65 KB
/
Copy pathga.py
File metadata and controls
executable file
·138 lines (103 loc) · 3.65 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
import random
import time
import json
import os
GENE_SIZE = 50
POPULATION_SIZE = 50
GENERATIONS = 50
CROSSOVER_PROBABILITY = 0.9
MUTATION_PROBABILITY = 0.0
def initial_population_setup():
population = []
for i in range(POPULATION_SIZE):
population.append([[0] * GENE_SIZE, 0])
for i in range(POPULATION_SIZE):
for j in range(GENE_SIZE):
population[i][0][j] = random.randint(0, 1)
return population
def calculate_fitness(population):
for i in range(POPULATION_SIZE):
population[i][1] = 0
for j in range(GENE_SIZE):
if population[i][0][j] == 1:
population[i][1] += 1
return population
def calculate_total_and_highest_fitness(population):
total = 0
highest = 0
for item in population:
total += item[1]
if item[1] > highest:
highest = item[1]
return [total, highest]
def shuffle(population):
population = random.sample(population, len(population))
return population
def tournament_selection(population):
offspring = []
for i in range(POPULATION_SIZE):
parent1 = random.randint(0, POPULATION_SIZE - 1)
parent2 = random.randint(0, POPULATION_SIZE - 1)
if population[parent1][1] >= population[parent2][1]:
offspring.append(population[parent1])
else:
offspring.append(population[parent2])
return offspring
def crossover(population):
offspring = []
probability = random.random()
for i in range(0, POPULATION_SIZE, 2):
first_parent = population[i][0]
second_parent = population[i + 1][0]
if probability >= CROSSOVER_PROBABILITY:
offspring.append([first_parent, 0])
offspring.append([second_parent, 0])
else:
crossover_point = random.randint(0, 9)
first_child = first_parent[:crossover_point] + second_parent[crossover_point:]
second_child = first_parent[crossover_point:] + second_parent[:crossover_point]
offspring.append([first_child, 0])
offspring.append([second_child, 0])
return offspring
def mutation(population):
for i in range(POPULATION_SIZE):
for j in range(GENE_SIZE):
mutation_chance = random.random()
if mutation_chance <= MUTATION_PROBABILITY:
if population[i][0][j] == 0:
population[i][0][j] = 1
else:
population[i][0][j] = 0
return population
start = time.clock()
json_list = []
population = initial_population_setup()
population = calculate_fitness(population)
initial_fitness_stats = calculate_total_and_highest_fitness(population)
print("Initial average fitness:")
print(initial_fitness_stats[0] / POPULATION_SIZE)
print("Initial highest fitness:")
print(initial_fitness_stats[1])
offspring = population
for i in range(GENERATIONS):
offspring = shuffle(offspring)
offspring = tournament_selection(offspring)
offspring = crossover(offspring)
offspring = mutation(offspring)
offspring = calculate_fitness(offspring)
fitness_stats = calculate_total_and_highest_fitness(offspring)
print("Total fitness:")
print(fitness_stats[0])
print("Average fitness:")
average_fitness = fitness_stats[0] / POPULATION_SIZE
print(average_fitness)
print("Highest fitness:")
print(fitness_stats[1])
json_list.append([fitness_stats[0], average_fitness, fitness_stats[1]])
stop = time.clock()
if os.path.isfile('results.json'):
os.remove('results.json')
file = open('results.json', 'w')
file.write(json.dumps(json_list))
print("Number of milliseconds to execute:")
print((stop - start) * 1000)