-
Notifications
You must be signed in to change notification settings - Fork 0
/
Individual.py
165 lines (116 loc) · 4.89 KB
/
Individual.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import random
class Individual:
level = "____"
CHROMOSOME_LENGTH = 0
def __init__(self, chromosome):
self.chromosome = chromosome
self.fitness = self.fittness()
@classmethod
def randomMove(cls):
p = random.random()
if p < 0.4 :
return 0
elif p < 0.7:
return 2
return 1
# gene = random.randint(0,2)
# return gene
@classmethod
def create_chromosome(cls):
return [cls.randomMove() for _ in range(cls.CHROMOSOME_LENGTH)]
def mutation(self , child):
p = random.random()
n = random.randint(0,2)
if p < 0.5:
randIndex = random.randint(0, self.CHROMOSOME_LENGTH -1)
child[randIndex] = n
# def mutation(self , child):
# p = random.random()
# # n = random.randint(0,2)
# if p < 0.5:
# randIndex = random.randint(0, self.CHROMOSOME_LENGTH -1)
# child[randIndex] = 0
# def crossover(self , mate):
# child_chromosome1 = []
# child_chromosome2 = []
# size1 =(int)((len(self.chromosome) / 2 ) )- 1
# size2 = len(self.chromosome) - size1
# child_chromosome1.extend(self.chromosome[:size1])
# child_chromosome1.extend(mate.chromosome[-1*size2 :])
# self.mutation(child_chromosome1)
# #
# child_chromosome2.extend(mate.chromosome[:size1])
# child_chromosome2.extend(self.chromosome[-1*size2 :])
# self.mutation(child_chromosome2)
# return Individual(child_chromosome1) , Individual(child_chromosome2)
def crossover(self , mate):
child_chromosome1 = []
child_chromosome2 = []
size1 =(int)((len(self.chromosome) / 3) )
size3 = len(self.chromosome) - size1
child_chromosome1.extend(self.chromosome[:size1])
child_chromosome1.extend(mate.chromosome[size1:size3])
child_chromosome1.extend(self.chromosome[-1*size1 :])
self.mutation(child_chromosome1)
#
child_chromosome2.extend(mate.chromosome[:size1])
child_chromosome2.extend(self.chromosome[size1:size3])
child_chromosome2.extend(mate.chromosome[-1*size1 :])
self.mutation(child_chromosome2)
return Individual(child_chromosome1) , Individual(child_chromosome2)
def fittness(self):
score = 0
longestPath = 0
lenPath =0
for i in range(self.CHROMOSOME_LENGTH):
current_step = self.level[i]
if( self.chromosome[i] == 1 ):
if(i == self.CHROMOSOME_LENGTH - 2):
if(self.level[i + 1] == "_" or self.level[i + 1] == "M"):
score += -2
elif(i < self.CHROMOSOME_LENGTH - 2):
if((self.level[i + 1] == "_" and self.level[i + 2] == "_") or (self.level[i + 1] == "M" and self.level[i + 2] == "M")
or (self.level[i + 1] == "_" and self.level[i + 2] == "M") or (self.level[i + 1] == "M" and self.level[i + 2] == "_")):
score += -2
if( self.chromosome[i] == 2 ):
if i <= self.CHROMOSOME_LENGTH -2 and self.level[i+1] != 'L' :
score += -2
if i > self.CHROMOSOME_LENGTH -2:
score += -2
if (current_step == 'G'):
if (self.chromosome[i-1] != 1 and i >= 1) or (self.chromosome[i-2] != 1 and i >= 2):
if lenPath > longestPath :
longestPath = lenPath
score -= 2
lenPath = 0
if (self.chromosome[i-2] == 1 and i >=2 ):
score +=2
elif (current_step == 'L' ):
if(self.chromosome[i-1] != 2 and i >=1 ):
if lenPath > longestPath :
longestPath = lenPath
score -= 2
lenPath = 0
if self.chromosome[i-2] == 1 and i >=2 :
if lenPath > longestPath :
longestPath = lenPath
lenPath = 0
score -= 2
elif (current_step == 'M' and (i == 0 or self.chromosome[i - 1] != 1) ):
score += 2
lenPath += 1
if lenPath > longestPath :
longestPath = lenPath
lenPath = 0
score += longestPath
if(self.chromosome[-1] == 1):
score += 3
return score
# Individual.level = "____G_ML__G"
# Individual.CHROMOSOME_LENGTH = len("____G_ML__G")
# gg = Individual([1,2,3,4,5,6,7,8,9,10,11])
# gg2 = Individual([-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11])
# (one , two ) = gg.crossover(gg2)
# print(one)
# print(two)
# # print(gg.fitness)