Skip to content

Commit fab6af8

Browse files
authored
Update ga.py
1 parent ea8c80e commit fab6af8

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

ga.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from math import ceil
99

1010

11-
_password = "Abre. Soy yo! Quién va a ser si no?"
11+
_password = "Open up. It's me! Who else?"
1212

1313
###############################################################################
14-
# Opciones de ejecución
14+
# Command line options
1515
###############################################################################
1616
_pop_size = 100
1717
_elite_rate = 0.2
@@ -42,9 +42,6 @@
4242
if sum([1 for i in [_elite_rate, _mutate_prob, _pop_size, _max_generations] if i < 0]):
4343
sys.exit("Error: Invalid parameters")
4444

45-
46-
###############################################################################
47-
# Metodos aportados por el profesor (No se modifican)
4845
###############################################################################
4946

5047
def get_password_len():
@@ -61,9 +58,6 @@ def gene_set():
6158
""" Return the feasible characters of the password """
6259
return " 0123456789áéíóúabcdefghijklmnñopqrstuvwxyzÁÉÍÓÚABCDEFGHIJKLMNÑOPQRSTUVWXYZ!\"#$%&\'()*+,-./:;<=>¿?@[\\]^_`{|}"
6360

64-
65-
###############################################################################
66-
# Implementación metodos solicitados por el profesor
6761
###############################################################################
6862

6963
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
10397
return False
10498

10599

106-
###############################################################################
107-
# Metodos propios
108100
###############################################################################
109101

110102
def roulette(players, num_winners=1):
111103
""" 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
114106
players.sort(key=lambda x: x[1], reverse=selection_mode)
115107
winners = [] if selection_mode else players
116108
for _ in range(num_winners if selection_mode else len(players) - num_winners):
@@ -137,7 +129,7 @@ def winner(value):
137129

138130

139131
###############################################################################
140-
# Metodos Impresion
132+
# Printing methods
141133
###############################################################################
142134

143135
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):
152144
if _matrix: msg += _print_matrix(pop)
153145
print(msg)
154146

147+
155148
def _str_print_all(pop):
156-
lst = "\n\nListado ordenado de fitness de toda la población:\n\033[0;0m"
149+
lst = "\n\nOrdered fitness list of the entire population:\n\033[0;0m"
157150
for i in range(len(pop)):
158151
lst += str(f'\033[0;31m{pop[i][1]:4}\033[0;0m')
159152
if (i+1) % 20 == 0: lst += "\n\033[0;0m"
160153
return lst
161154

155+
162156
def _separator_line():
163157
return "\n" + "-"*80 + "\n\033[0;0m"
164158

159+
165160
def _print_verbose(elite_rate, mutate_prob, the_fitest, generation, pop):
166161
ret = "Version: \033[0K\033[5;33m" + str(_version) + "\033[0;0m"
167162
ret += "\nPassword 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):
173168
ret += "\n \033[0K\033[5;32m" + str(the_fitest[0]) + "\033[0;0m"
174169
return ret
175170

171+
176172
def _print_non_verbose(the_fitest, generation):
177173
ret = "\033[0;0mGeneration: " + "\033[0K\033[5;33m" + str(generation) + "\033[0;0m"
178174
ret += "\033[0;0m best-fitness: " + "\033[0K\033[5;33m" + str(the_fitest[1]) + "\033[0;0m"
179175
ret += " \033[0K\033[5;32m" + str(the_fitest[0]) + "\033[0;0m"
180176
return ret
181177

178+
182179
def _print_matrix(pop):
183180
ret = "\n"
184181
for i in range(40):
@@ -188,16 +185,16 @@ def _print_matrix(pop):
188185

189186

190187
###############################################################################
191-
# Selección de versión
188+
# Version selection
192189
###############################################################################
193190

194191
if os.path.split(__file__)[-1] == os.path.split(sys.argv[0])[-1]:
195192
if _version:
196193
if _static_print: print("\033[2J\033[1;1f")
197194
exec(open('ga_v{}.py'.format(_version)).read())
198195
else:
199-
print("\nEjecute el archivo correspondiente de la versión, o indique la versión que dese ejecutar.",
200-
"Ejemplos:",
196+
print("\nRun the corresponding version file, or indicate the version you want to run.",
197+
"Examples:",
201198
"> python3 ga_v1.py",
202199
"> python3 ga.py --version=1\n", sep="\n")
203200
exit(0)

0 commit comments

Comments
 (0)