-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fitness.py
175 lines (131 loc) · 3.21 KB
/
Fitness.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
166
167
168
169
170
171
172
173
174
175
import random
import Assist
# <editor-fold desc="Fitness Function Definitions">
def evalcandonemax(elem):
fit = 0
for vect in elem:
fit = fit + vect
return fit
def evalcandTrap5(elem):
fit = 0
i = 0
while i < len(elem):
u = elem[i]
i += 1
u += elem[i]
i += 1
u += elem[i]
i += 1
u += elem[i]
i += 1
u += elem[i]
i += 1
if u < 5:
fit += 4 - u
else:
fit += 5
return fit
def evalcandTrap5Inv(elem):
fit = 0
i = 0
while i < len(elem):
u = elem[i]
i += 1
u += elem[i]
i += 1
u += elem[i]
i += 1
u += elem[i]
i += 1
u += elem[i]
i += 1
if u > 0:
fit += u
else:
fit += 5
return fit
def evalcandTrap7(elem):
fit = 0
i = 0
while i < len(elem):
u = elem[i]
i += 1
u += elem[i]
i += 1
u += elem[i]
i += 1
u += elem[i]
i += 1
u += elem[i]
i += 1
u += elem[i]
i += 1
u += elem[i]
i += 1
if u < 7:
fit += 6 - u
else:
fit += 7
return fit
# </editor-fold>
# <editor-fold desc=" Get Fitness Function Names">
def evalcandonemaxName():
return "One Max"
def evalcandTrap5Name():
return "Trap 5"
def evalcandTrap7Name():
return "Trap 7"
def evalcandTrap5InvName():
return "Inverted Trap 5"
# </editor-fold>
getfit = {0: evalcandonemax,
1: evalcandTrap5,
2: evalcandTrap7,
3: evalcandTrap5Inv,
}
getfitName = {0: evalcandonemaxName,
1: evalcandTrap5Name,
2: evalcandTrap7Name,
3: evalcandTrap5InvName,
}
# <editor-fold desc="Assistive Functions">
def evalpop(population):
for elem in population:
fit = 0
for vect in elem:
fit = fit + vect
print(elem, " Fitness", fit)
def tournament(population, k, n, fitnessfunc):
best = []
for i in range(1, k+1):
ind = population[random.randint(1, n)]
if (len(best) == 0) or getfit[fitnessfunc](ind) > getfit[fitnessfunc](best):
best = ind
return best
def tournamentprob(population, k, n, fitnessFunc):
# print("#####TOUR######")
best = []
final = []
for i in range(1, k+1):
ind = population[random.randint(1, n)]
best.append(ind)
sorted(best, key=getfit[fitnessFunc], reverse=True)[0]
toss = random.randint(1, 100)
# print("toss val ", toss)
#
# print("\n")
#
# print("best0 ", best[0])
# print(" Fitness ", getfit[fitnessFunc](best[0]))
# print("\n")
# print("best1 ", best[1])
# print(" Fitness ", getfit[fitnessFunc](best[1]))
# print("\n")
if toss < 90:
final = best[0]
else:
final = best[1]
#print("final ", final)
#input("Press Enter to continue...")
return final
# </editor-fold>