8
8
from math import ceil
9
9
10
10
11
- _password = "Abre. Soy yo! Quién va a ser si no ?"
11
+ _password = "Open up. It's me! Who else ?"
12
12
13
13
###############################################################################
14
- # Opciones de ejecución
14
+ # Command line options
15
15
###############################################################################
16
16
_pop_size = 100
17
17
_elite_rate = 0.2
42
42
if sum ([1 for i in [_elite_rate , _mutate_prob , _pop_size , _max_generations ] if i < 0 ]):
43
43
sys .exit ("Error: Invalid parameters" )
44
44
45
-
46
- ###############################################################################
47
- # Metodos aportados por el profesor (No se modifican)
48
45
###############################################################################
49
46
50
47
def get_password_len ():
@@ -61,9 +58,6 @@ def gene_set():
61
58
""" Return the feasible characters of the password """
62
59
return " 0123456789áéíóúabcdefghijklmnñopqrstuvwxyzÁÉÍÓÚABCDEFGHIJKLMNÑOPQRSTUVWXYZ!\" #$%&\' ()*+,-./:;<=>¿?@[\\ ]^_`{|}"
63
60
64
-
65
- ###############################################################################
66
- # Implementación metodos solicitados por el profesor
67
61
###############################################################################
68
62
69
63
def initial_population (pop_size , chromosome_len ):
@@ -103,14 +97,12 @@ def ga(pop_size=_pop_size, elite_rate=_elite_rate, mutate_prob=_mutate_prob, max
103
97
return False
104
98
105
99
106
- ###############################################################################
107
- # Metodos propios
108
100
###############################################################################
109
101
110
102
def roulette (players , num_winners = 1 ):
111
103
""" Return the winners of the roulette wheel selection """
112
- selection_mode = num_winners < len (players )/ 2 # True si es mejor seleccionar que eliminar
113
- players = players .copy () # Evitar modificar la lista original
104
+ selection_mode = num_winners < len (players )/ 2 # True if it is preferable to select than delete.
105
+ players = players .copy () # Avoid modifying the original list
114
106
players .sort (key = lambda x : x [1 ], reverse = selection_mode )
115
107
winners = [] if selection_mode else players
116
108
for _ in range (num_winners if selection_mode else len (players ) - num_winners ):
@@ -137,7 +129,7 @@ def winner(value):
137
129
138
130
139
131
###############################################################################
140
- # Metodos Impresion
132
+ # Printing methods
141
133
###############################################################################
142
134
143
135
def print_generation (elite_rate , mutate_prob , the_fitest , generation , pop ):
@@ -152,16 +144,19 @@ def print_generation(elite_rate, mutate_prob, the_fitest, generation, pop):
152
144
if _matrix : msg += _print_matrix (pop )
153
145
print (msg )
154
146
147
+
155
148
def _str_print_all (pop ):
156
- lst = "\n \n Listado ordenado de fitness de toda la población :\n \033 [0;0m"
149
+ lst = "\n \n Ordered fitness list of the entire population :\n \033 [0;0m"
157
150
for i in range (len (pop )):
158
151
lst += str (f'\033 [0;31m{ pop [i ][1 ]:4} \033 [0;0m' )
159
152
if (i + 1 ) % 20 == 0 : lst += "\n \033 [0;0m"
160
153
return lst
161
154
155
+
162
156
def _separator_line ():
163
157
return "\n " + "-" * 80 + "\n \033 [0;0m"
164
158
159
+
165
160
def _print_verbose (elite_rate , mutate_prob , the_fitest , generation , pop ):
166
161
ret = "Version: \033 [0K\033 [5;33m" + str (_version ) + "\033 [0;0m"
167
162
ret += "\n Password length: \033 [0K\033 [5;33m" + str (get_password_len ()) + "\033 [0;0m"
@@ -173,12 +168,14 @@ def _print_verbose(elite_rate, mutate_prob, the_fitest, generation, pop):
173
168
ret += "\n \033 [0K\033 [5;32m" + str (the_fitest [0 ]) + "\033 [0;0m"
174
169
return ret
175
170
171
+
176
172
def _print_non_verbose (the_fitest , generation ):
177
173
ret = "\033 [0;0mGeneration: " + "\033 [0K\033 [5;33m" + str (generation ) + "\033 [0;0m"
178
174
ret += "\033 [0;0m best-fitness: " + "\033 [0K\033 [5;33m" + str (the_fitest [1 ]) + "\033 [0;0m"
179
175
ret += " \033 [0K\033 [5;32m" + str (the_fitest [0 ]) + "\033 [0;0m"
180
176
return ret
181
177
178
+
182
179
def _print_matrix (pop ):
183
180
ret = "\n "
184
181
for i in range (40 ):
@@ -188,16 +185,16 @@ def _print_matrix(pop):
188
185
189
186
190
187
###############################################################################
191
- # Selección de versión
188
+ # Version selection
192
189
###############################################################################
193
190
194
191
if os .path .split (__file__ )[- 1 ] == os .path .split (sys .argv [0 ])[- 1 ]:
195
192
if _version :
196
193
if _static_print : print ("\033 [2J\033 [1;1f" )
197
194
exec (open ('ga_v{}.py' .format (_version )).read ())
198
195
else :
199
- print ("\n Ejecute el archivo correspondiente de la versión, o indique la versión que dese ejecutar ." ,
200
- "Ejemplos :" ,
196
+ print ("\n Run the corresponding version file, or indicate the version you want to run ." ,
197
+ "Examples :" ,
201
198
"> python3 ga_v1.py" ,
202
199
"> python3 ga.py --version=1\n " , sep = "\n " )
203
200
exit (0 )
0 commit comments