This repository was archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenetic.py
More file actions
295 lines (270 loc) · 10.6 KB
/
Genetic.py
File metadata and controls
295 lines (270 loc) · 10.6 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
##Optimization Genetic Approch
import math as mat
import numpy as np
import time
from numpy.linalg import norm
from numpy.random import randint, rand
from MPCMain import MPC, Order
##resulotion = 1/(2^n-1)*range ---> the resulotion of the argument
##Generation ---> number of optimization iterations
##controlarguments ---> the number of control efforts type
##fitness ---> the probability of the candidate to be choosen
##Code ---> the binary code for the candidate
class Candidate:
def __init__(self):
self.fitness = []
self.relative_fitness = []
self.Target_Value = []
self.Generation = []
self.Code = 0
self.Value = []
@property
def Code(self):
return self.__Code
@Code.setter
def Code(self, Code):
self.__Code = np.asarray(Code)
class DNA(Candidate):
def __init__(
self, Number_of_Candidate, Val_max, Val_min, resulotion, controlarguments,
):
super().__init__()
self.iteration = 0
self.Number_of_Candidate = Number_of_Candidate
self.Candidate_List = []
self.Val_max = Val_max
self.Val_min = Val_min
self.Resulotion_init = resulotion
self.Resulotion = []
self.Controlarguments = controlarguments
self.argument_bits = []
self.NumberofBits = 0
self.Fitsum = 0
self.Parent_List = []
@property
def Number_of_Candidate(self):
return self.__Number_of_Candidate
@property
def NumberofBits(self):
return self.__NumberofBits
@Number_of_Candidate.setter
def Number_of_Candidate(self, Number_of_Candidate):
self.__Number_of_Candidate = Number_of_Candidate
@NumberofBits.setter
def NumberofBits(self, NumberofBits):
self.__NumberofBits = NumberofBits
def initial_Parent_List(self):
self.Parent_List = np.zeros([self.Number_of_Candidate, self.NumberofBits])
def Calculate_NumberofBits(self):
for i in range(self.Controlarguments):
self.NumberofBits = self.NumberofBits + mat.ceil(
(
mat.log2(
np.abs(self.Val_max[i] - self.Val_min[i] + self.Resulotion_init)
/ self.Resulotion_init
)
)
)
self.argument_bits.append(
mat.ceil(
(
mat.log2(
np.abs(
self.Val_max[i] - self.Val_min[i] + self.Resulotion_init
)
/ self.Resulotion_init
)
)
)
)
self.Resulotion.append(
(self.Val_max[i] - self.Val_min[i]) / (2 ** (self.argument_bits[i]) - 1)
)
def Initialize_Population(self):
for i in range(self.Number_of_Candidate):
self.Candidate_List.append(Candidate())
self.Candidate_List[i].Code = self.Code_init()
self.Candidate_List[i].Value = self.Calculate_Value(
self.Candidate_List[i].Code,
self.Controlarguments,
self.argument_bits,
self.Resulotion,
self.Val_min,
)
while not self.Constraint(self.Candidate_List[i].Value):
self.Candidate_List[i].Code = self.Code_init()
self.Candidate_List[i].Value = self.Calculate_Value(
self.Candidate_List[i].Code,
self.Controlarguments,
self.argument_bits,
self.Resulotion,
self.Val_min,
)
def Code_init(self):
Code = []
for _ in range(self.NumberofBits):
Code.append(randint(2))
return Code
def DNA_fitness(self):
M_value = 0
Max = 0
self.Fitsum = 0
for i in range(self.Number_of_Candidate):
self.Candidate_List[i].Target_Value = self.Calculate_Target(
self.Candidate_List[i].Value
)
if i == 0 or M_value > self.Candidate_List[i].Target_Value:
M_value = self.Candidate_List[i].Target_Value
if M_value < 0:
for i in range(self.Number_of_Candidate):
self.Candidate_List[i].fitness = 1 / (
1 + self.Candidate_List[i].Target_Value - M_value
)
self.Fitsum = self.Fitsum + self.Candidate_List[i].fitness
if i == 0 or Max < self.Candidate_List[i].fitness:
j = i
Max = self.Candidate_List[i].fitness
else:
for i in range(self.Number_of_Candidate):
self.Candidate_List[i].fitness = 1 / (
1 + self.Candidate_List[i].Target_Value
)
self.Fitsum = self.Fitsum + self.Candidate_List[i].fitness
if i == 0 or Max < self.Candidate_List[i].fitness:
j = i
Max = self.Candidate_List[i].fitness
self.Parent_List[0] = self.Candidate_List[j].Code
def Parent_Update(self):
val = 0
for i in range(self.Number_of_Candidate):
self.Candidate_List[i].relative_fitness = (
self.Candidate_List[i].fitness / self.Fitsum
) + val
val = self.Candidate_List[i].relative_fitness
for i in range(1, self.Number_of_Candidate):
val = rand()
j = 0
while self.Candidate_List[j].relative_fitness < val:
j = j + 1
self.Parent_List[i] = self.Candidate_List[j].Code
def CroosoverandMutation(self):
# TODO: ID Check
self.Candidate_List[0].Code = self.Parent_List[0]
self.Candidate_List[0].Value = self.Calculate_Value(
self.Candidate_List[0].Code,
self.Controlarguments,
self.argument_bits,
self.Resulotion,
self.Val_min,
)
for i in range(1, self.Number_of_Candidate):
val = randint(9)
num1 = int(randint(self.Number_of_Candidate))
num2 = int(randint(self.Number_of_Candidate))
if val < 7:
V = randint(np.sum(self.argument_bits))
self.Candidate_List[i].Code = np.concatenate(
(self.Parent_List[num1][:V], self.Parent_List[num2][V:]), axis=None,
)
self.Candidate_List[i].Value = self.Calculate_Value(
self.Candidate_List[i].Code,
self.Controlarguments,
self.argument_bits,
self.Resulotion,
self.Val_min,
)
while not self.Constraint(self.Candidate_List[i].Value):
V = randint(self.NumberofBits)
self.Candidate_List[i].Code = np.concatenate(
(self.Parent_List[num1, :V], self.Parent_List[num2, V:],),
axis=None,
)
self.Candidate_List[i].Value = self.Calculate_Value(
self.Candidate_List[i].Code,
self.Controlarguments,
self.argument_bits,
self.Resulotion,
self.Val_min,
)
else:
V = randint(np.sum(self.argument_bits))
self.Candidate_List[i].Code = self.Parent_List[num1]
if self.Candidate_List[i].Code[V]:
self.Candidate_List[i].Code[V] = 0
else:
self.Candidate_List[i].Code[V] = 1
self.Candidate_List[i].Value = self.Calculate_Value(
self.Candidate_List[i].Code,
self.Controlarguments,
self.argument_bits,
self.Resulotion,
self.Val_min,
)
while not self.Constraint(self.Candidate_List[i].Value):
if self.Candidate_List[i].Code[V]:
self.Candidate_List[i].Code[V] = 0
else:
self.Candidate_List[i].Code[V] = 1
V = randint(np.sum(self.argument_bits))
if self.Candidate_List[i].Code[V]:
self.Candidate_List[i].Code[V] = 0
else:
self.Candidate_List[i].Code[V] = 1
self.Candidate_List[i].Value = self.Calculate_Value(
self.Candidate_List[i].Code,
self.Controlarguments,
self.argument_bits,
self.Resulotion,
self.Val_min,
)
@staticmethod
def Calculate_Value(Code, Number_of_arguments, argument_bits, Resulotion, Val_min):
Value = np.zeros([Number_of_arguments,])
Running = 0
for i in range(Number_of_arguments):
Multi = 2 ** (argument_bits[i] - 1)
for j in range(Running, argument_bits[i] + Running):
Value[i] = Value[i] + Code[j] * Multi
Multi = Multi / 2
Running = Running + argument_bits[i]
Value[i] = (Value[i]) * Resulotion[i] + Val_min[i]
return Value
# @staticmethod
# def Constraint(Control_efforts):
# if norm(Control_efforts) > 8:
# return 1
# else:
# return 0
# @staticmethod
# def Calculate_Target(Control_efforts):
# return norm(Control_efforts) ** 2 + Control_efforts[1] ** 3
##RunningFunciton
Rmax = 0.5
Initial_Position = np.zeros([5, 1])
# Optimal_path = ......!!
# For first running we will check Path_center = Optimal_path
Optimal_path = np.array([[5, 10, 15, 20], [5, 10, 15, 20]])
Path_center = Optimal_path
Weights = np.ones([6, 1])
Time_Delta = 1
##Genetic initialization
Number_of_Candidate = 10
# control efforts
Val_max = np.array([4, mat.pi / 4, 4, mat.pi / 4, 4, mat.pi / 4])
Val_min = np.array([-2, -mat.pi / 4, -2, -mat.pi / 4, -2, -mat.pi / 4])
resulotion = 0.01
# control arguments gas and steering
controlarguments = 6
K = DNA(Number_of_Candidate, Val_max, Val_min, resulotion, controlarguments,)
K2 = MPC(Rmax, Initial_Position, Optimal_path, Path_center, Weights, Time_Delta)
K.Calculate_NumberofBits()
K.initial_Parent_List()
K.Initialize_Population()
K.DNA_fitness()
K.CroosoverandMutation()
for i in range(100):
print(K.Candidate_List[0].Target_Value)
print(K.Candidate_List[0].Value)
K.DNA_fitness()
K.Parent_Update()
K.CroosoverandMutation()