-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
129 lines (95 loc) · 3.07 KB
/
main.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
127
128
129
from random import randint
from listOperator import *
import copy
from chromosome import *
import Test
import view
initPopulationSize = 30
breakPoint = 4
mutationRatio = 0.25
generation = 100
selectionRatioOnGeneration = 0.15
Gens_Length = 8
population = Population() #Test.makePopulation()
gens = Chromosome()
def init():
global initPopulationSize
global population
global Gens_Length
global gens
for i in range(0, Gens_Length):
gens.append(i)
for i in range(0, initPopulationSize):
new_chromosome = generate_random_chromosome()
population.append(new_chromosome)
def prob(chromosome=Chromosome()):
return chromosome.fitness()/population.Efitness()
def mutation_pool():
pool = []
for item in population:
CHNumber = int(prob(item) * 100)
for i in range(0, CHNumber):
pool.append(copy.deepcopy(item))
return pool
def selection(pool=[]):
parent1_I = randint(0, pool.__len__() - 1)
parent2_I = randint(0, pool.__len__() - 1)
return pool[parent1_I], pool[parent2_I]
def crossover(chromosome1=Chromosome(), chromosome2=Chromosome()):
global gens
gens_copy = copy.deepcopy(gens)
child = Chromosome(copy.deepcopy(gens))
for i in range(0 , breakPoint):
child[i] = chromosome1[i]
j = breakPoint
for i in range(breakPoint, gens_copy.__len__()):
if chromosome2[i] not in child[:breakPoint]:
child[j] = chromosome2[i]
j = j + 1
gencopy = copy.deepcopy(gens)
availabeGens = subList(gencopy, child[:j])
for i in range(j, child.__len__()):
child[i] = availabeGens[i-j]
return child
def generate_random_chromosome():
global gens
chromosome = Chromosome(copy.deepcopy(gens))
gens_copy = copy.deepcopy(gens)
for i in range(0,gens_copy.__len__()):
randGenIndex = randint(0,gens_copy.__len__() - 1)
randGen = gens_copy[randGenIndex]
gens_copy.remove(randGen)
chromosome[i] = randGen
return chromosome
def main():
global population
global mutationRatio
global generation
global gens
bests = Population()
bests.append(population.bestChromosome())
fitAnswer = 0
generation_counter = 0
while fitAnswer != gens.fullScore() and generation_counter < generation:
population_childs = Population()
for j in range(0, initPopulationSize):
parent1, parent2 = selection(mutation_pool())
child = crossover(parent1, parent2)
population_childs.append(child)
population.mutating(mutationRatio)
population = population.NextGeneration(selectionRatioOnGeneration, population_childs)
nextGenerationBestAnswer = population.bestChromosome()
bests.append(nextGenerationBestAnswer)
fitAnswer = nextGenerationBestAnswer.fitness()
print("Generation " + str(generation_counter) + ": " + str(nextGenerationBestAnswer) + " fitness: " + str(
nextGenerationBestAnswer.fitness()))
generation_counter = generation_counter + 1
# for item in bests:
answer = bests.bestChromosome()
print("-----------------------------------------------------")
print("| Best answer: " + str(answer) + " fitness:" + str(answer.fitness()) + " |")
print("-----------------------------------------------------")
view.showChromosome(answer, Gens_Length)
print(answer)
init()
main()