-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
229 lines (194 loc) · 7.38 KB
/
functions.py
File metadata and controls
229 lines (194 loc) · 7.38 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
# ------------- IMPORTS ------------------
import random
from math import sin, cos
# ----------------------------------------
# ------------- FUNCTIONS ----------------
def genSudoBinaryCode(n):
n = int(n)
intSquares = [2**i for i in range(-20,100) if 2**i < n]
for i in range(len(intSquares)):
if sum(intSquares[0:i+1]) > n:
intSquares.pop(i)
sobrante = n - (intSquares[len(intSquares)-1] + intSquares[len(intSquares)-1]-1)
intSquares.append(sobrante)
intSquares = sorted(intSquares)
intSquares.append('+/-')
return intSquares[::-1]
def genNArraysOf(x,y,u1):
arrs = []
for i in range(x):
arr = []
for j in range(y):
if random.uniform(0,1) > u1:
arr.append(1)
else:
arr.append(0)
arrs.append(arr)
return arrs
def getValuesOfXandY(randVals, sudoBinaryCode):
values = []
for randVal in randVals:
xs = []
ys = []
signX = 1
signY = 1
for i, j in zip(randVal[:30], sudoBinaryCode):
if j == '+/-':
if i == 0:
signX = -1
xs.append(i)
else:
xs.append(i*j)
for i, j in zip(randVal[30:], sudoBinaryCode):
if j == '+/-':
if i == 0:
signY = -1
ys.append(i)
else:
ys.append(i*j)
xadd = sum(xs)*signX
yadd = sum(ys)*signY
values.append([randVal, xadd, yadd])
return values
def f(x,y):
return -(x * sin(x)**2 * cos(x)**3 + (y * sin(y)**2 * cos(y)**3))
# def f(x,y):
# return x + y
def getPofEach(arr):
arr = [abs(i) for i in arr]
total = sum(arr)
ps = []
for element in arr:
ps.append(element/total)
return ps
def getPA(arr):
pa = []
s = 0
for element in arr:
s += element
pa.append(s)
return pa
def bestK(arrOfArrs,k):
return sorted(arrOfArrs, key=lambda x: x[3], reverse=True)[:k]
def crossElements(e1,e2):
xe1 = e1[:15]+e2[15:30]+e1[30:45]+e2[45:60]
xe2 = e2[:15]+e1[15:30]+e2[30:45]+e1[45:60]
return xe1, xe2
def mutate(arr,uM):
newArr = []
for i in range(len(arr)):
numeroAleatorio = random.uniform(0,1)
if numeroAleatorio > uM:
newArr.append(1 if arr[i] == 0 else 0)
else:
newArr.append(arr[i])
return newArr
# ----------------------------------------
# Inputs ( Esto se va a cambiar de STDIN a lo que se reciba de un request HTTP)
poblacion, numKdeElitismo, cantidadGeneraciones = [int(x) for x in input().split(' ')]
umbral1, umbralX, umbralM = [float(x) for x in input().split(' ')]
sudoBinCode = genSudoBinaryCode(358)
print("Codificación Pseudo binaria: ")
print(sudoBinCode)
print("-"*50)
print("Población: ",poblacion)
print("K de Elitismo: ",numKdeElitismo)
print("Numero de generaciones: ",cantidadGeneraciones)
print("Umbral 1: ",umbral1)
print("Umbral Cruzamiento: ",umbralX)
print("Umbral Mutación: ",umbralM)
# ----------------------------------------
# ----------------------------------------
# ----------------------------------------
# -----------FIRST GENERATION-------------
# ----------------------------------------
# ----------------------------------------
# Generar los N arreglos de 1's y 0's y su valor de X y Y dado el umbral1
firstGeneration = (
getValuesOfXandY(
genNArraysOf(poblacion,60,umbral1),
sudoBinCode
)
)
# Generar Valores de f(x,y) para cada uno de los arreglos de 1's y 0's
for element in firstGeneration:
element.append(f(element[1],element[2]))
# Agregar P(f(x,y)) y PA(f(x,y)) a cada Arreglo de 1's y 0's
probabilidades = getPofEach([element[3] for element in firstGeneration])
probabilidadesAcumuladas = getPA(probabilidades)
for element, i in zip(firstGeneration, range(len(probabilidades))):
element.append(probabilidades[i])
element.append(probabilidadesAcumuladas[i])
print("-"*50)
print("-> GENERACION #0")
for element in firstGeneration:
print("X: ",element[1]," Y: ",element[2]," f(x,y): ", element[3])
bG = bestK(firstGeneration,1)
print("Best X: ",bG[0][1]," Best Y: ",bG[0][2]," Best f(x,y): ",bG[0][3])
# ----------------------------------------
# ----------------------------------------
# ----------------------------------------
# ------SUBSEQUENT GENERATIONS------------
# ----------------------------------------
# ----------------------------------------
previousGeneration = firstGeneration
for generacion in range(cantidadGeneraciones):
print(numKdeElitismo)
# Obtener los mejores K elementos de la generacion anterior
bestOfPreviousGeneration = (bestK(previousGeneration, numKdeElitismo))
bestOfPreviousGeneration = [element[0] for element in bestOfPreviousGeneration]
# Pasar esos K elementos a a la siguiente generación y el resto (N-K) Cruzar si pasan Umbral o no Cruzar si no pasan y Mutar si pasan
nextGeneration = bestOfPreviousGeneration
while len(nextGeneration) < poblacion:
numeroAleatorio = random.uniform(0,.98)
for element in previousGeneration:
# print(element[5]," > ", numeroAleatorio, " ? ")
if element[5] >= numeroAleatorio:
father = element[0]
break
numeroAleatorio = random.uniform(0,.98)
for element in previousGeneration:
if element[5] >= numeroAleatorio:
mother = element[0]
break
# Si la madre y el padre son el mismo, buscar hasta encontrar otra madre
if father == mother:
while father == mother:
numeroAleatorio = random.uniform(0,.98)
for element in previousGeneration:
if element[5] >= numeroAleatorio:
mother = element[0]
break
# Cruzar
numeroAleatorio = random.uniform(0,1)
if numeroAleatorio > umbralX:
child1, child2 = crossElements(father,mother)
else:
child1, child2 = father, mother
nextGeneration.append(child1)
nextGeneration.append(child2)
for i in range(numKdeElitismo,len(nextGeneration)):
nextGeneration[i] = mutate(nextGeneration[i],umbralM)
# Generar valores de X y Y para todos los elementos de la nueva generacion
nextGeneration = (
getValuesOfXandY(
nextGeneration,
sudoBinCode
)
)
# Generar Valores de f(x,y) para cada uno de los arreglos de 1's y 0's de siguiente generación
for element in nextGeneration:
element.append(f(element[1],element[2]))
# Agregar P(f(x,y)) y PA(f(x,y)) a cada Arreglo de 1's y 0's a siguiente generacion
probabilidades = getPofEach([element[3] for element in nextGeneration])
probabilidadesAcumuladas = getPA(probabilidades)
for element, i in zip(nextGeneration, range(len(probabilidades))):
element.append(probabilidades[i])
element.append(probabilidadesAcumuladas[i])
previousGeneration = nextGeneration
print("-"*50)
print("-> GENERACION #", generacion+1)
for element in nextGeneration:
print("X: ",element[1]," Y: ",element[2]," f(x,y): ", element[3])
bG = bestK(nextGeneration,1)
print("Best X: ",bG[0][1]," Best Y: ",bG[0][2]," Best f(x,y): ",bG[0][3])