-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneticAlgorithm.py
More file actions
116 lines (104 loc) · 3.83 KB
/
GeneticAlgorithm.py
File metadata and controls
116 lines (104 loc) · 3.83 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import random
import sys
import time
class GeneticAlgorithm:
# Config Options
def __init__(self,
genotype,
function,
generations=25,
population_size=50,
mutate_chance=0.25,
tournamentSize=6,
elitism=True):
self.elitism = elitism
self.generations = generations
self.population_size = population_size
self.mutate_chance = mutate_chance
self.tournamentSize = tournamentSize
self.genotype = genotype
self.function = function
def InitialGeneration(self):
initGen = []
for i in range(self.population_size):
initGen.append(self.genotype.generate())
return initGen
def Fitness(self, chromosome):
fitnessVal = self.function(chromosome)
self.curTime = fitnessVal[1]
return fitnessVal[0]
def PopFitness(self, pop):
fitnesspop = []
popNum = 0
self.bestTime = np.inf
for chromosome in pop:
popNum += 1
print "Pop: ",
print popNum
fitnesspop.append(self.Fitness(chromosome))
if self.curTime < self.bestTime:
self.bestTime = self.curTime
return fitnesspop
def Mutate(self, chromosome):
return self.genotype.mutate(chromosome)
def Crossover(self, chromosome1, chromosome2):
if self.genotype.size == 1:
return chromosome1
index = random.randint(1, self.genotype.size -1)
index2 = random.randint(index, self.genotype.size)
result = []
result.extend(chromosome1[:index])
result.extend(chromosome2[index:index2])
result.extend(chromosome1[index2:])
return result
def TournamentSelection(self, pop, bestIndex):
parentList = []
parents = np.random.choice(self.population_size, self.tournamentSize)
for i in range(self.tournamentSize):
parentList.append(bestIndex.index(parents[i]))
parentList = np.argsort(np.array(parentList))
parent1 = pop[parents[parentList[0]]]
parent2 = pop[parents[parentList[1]]]
child = self.Crossover(parent1,parent2)
if random.random() > self.mutate_chance:
child = self.Mutate(child)
return child
def Evolve(self, pop):
newpop = []
popValues = self.PopFitness(pop)
bestIndex = np.argsort(popValues, axis=0)[::-1]
self.popBestVal = popValues[bestIndex[0]]
self.popBest = pop[bestIndex[0]]
if self.elitism:
newpop.append(self.popBest)
while len(newpop) != len(pop):
newpop.append(self.TournamentSelection(pop, bestIndex.tolist()))
return newpop
def NewGeneration(self, pop):
#fbest = np.inf
genNum = 0
with open("GAValues.csv", 'w') as f:
f.write("New Run\n")
f.write("Generation,Best Reward,Best Time\n")
for gen in range(self.generations):
print "Generation: ",
print genNum
pop = self.Evolve(pop)
with open("GAValues.csv", 'a') as f:
f.write(str(genNum) + "," + str(self.popBestVal) + "," + str(self.bestTime) + "\n")
with open("NNMalmoBest.txt", 'w') as f:
f.write(str(self.popBest))
genNum += 1
fvalues = self.PopFitness(pop)
idx = np.argsort(fvalues, axis=0)[::-1]
self.popBest = pop[idx[0]]
self.popBestVal = fvalues[idx[0]]
with open("GAValues.csv", 'a') as f:
f.write(str(genNum) + "," + str(self.popBestVal) + "," + str(self.bestTime) + "\n")
return self.popBest
def Run(self):
pop = self.InitialGeneration()
return self.NewGeneration(pop)