From 6e0d2416e070f22a35c1650834d80ce6381e6c2b Mon Sep 17 00:00:00 2001 From: handcraftsman Date: Thu, 2 May 2019 17:16:34 -0500 Subject: [PATCH 1/6] fix inefficiency reported by Piotr.Lewczuk@uk-erlangen.de --- ch08/genetic.py | 3 +-- ch12/genetic.py | 3 +-- ch13/genetic.py | 3 +-- ch16/genetic.py | 3 +-- ch18/genetic.py | 3 +-- es/ch08/genetic.py | 3 +-- es/ch12/genetic.py | 3 +-- es/ch13/genetic.py | 3 +-- es/ch15/genetic.py | 3 +-- es/ch16/genetic.py | 3 +-- es/ch18/genetic.py | 3 +-- 11 files changed, 11 insertions(+), 22 deletions(-) diff --git a/ch08/genetic.py b/ch08/genetic.py index 875bbd7..189107d 100644 --- a/ch08/genetic.py +++ b/ch08/genetic.py @@ -85,8 +85,7 @@ def _get_improvement(new_child, generate_parent, maxAge): continue index = bisect_left(historicalFitnesses, child.Fitness, 0, len(historicalFitnesses)) - difference = len(historicalFitnesses) - index - proportionSimilar = difference / len(historicalFitnesses) + proportionSimilar = index / len(historicalFitnesses) if random.random() < exp(-proportionSimilar): parent = child continue diff --git a/ch12/genetic.py b/ch12/genetic.py index dc5c89c..5ee0ea9 100644 --- a/ch12/genetic.py +++ b/ch12/genetic.py @@ -134,8 +134,7 @@ def _get_improvement(new_child, generate_parent, maxAge, poolSize): continue index = bisect_left(historicalFitnesses, child.Fitness, 0, len(historicalFitnesses)) - difference = len(historicalFitnesses) - index - proportionSimilar = difference / len(historicalFitnesses) + proportionSimilar = index / len(historicalFitnesses) if random.random() < exp(-proportionSimilar): parents[pindex] = child continue diff --git a/ch13/genetic.py b/ch13/genetic.py index d040846..550b7c2 100644 --- a/ch13/genetic.py +++ b/ch13/genetic.py @@ -143,8 +143,7 @@ def _get_improvement(new_child, generate_parent, maxAge, poolSize, maxSeconds): continue index = bisect_left(historicalFitnesses, child.Fitness, 0, len(historicalFitnesses)) - difference = len(historicalFitnesses) - index - proportionSimilar = difference / len(historicalFitnesses) + proportionSimilar = index / len(historicalFitnesses) if random.random() < exp(-proportionSimilar): parents[pindex] = child continue diff --git a/ch16/genetic.py b/ch16/genetic.py index 16a9841..99c9a3b 100644 --- a/ch16/genetic.py +++ b/ch16/genetic.py @@ -144,8 +144,7 @@ def _get_improvement(new_child, generate_parent, maxAge, poolSize, continue index = bisect_left(historicalFitnesses, child.Fitness, 0, len(historicalFitnesses)) - difference = len(historicalFitnesses) - index - proportionSimilar = difference / len(historicalFitnesses) + proportionSimilar = index / len(historicalFitnesses) if random.random() < exp(-proportionSimilar): parents[pindex] = child continue diff --git a/ch18/genetic.py b/ch18/genetic.py index 7c6d815..01a29de 100644 --- a/ch18/genetic.py +++ b/ch18/genetic.py @@ -145,8 +145,7 @@ def _get_improvement(new_child, generate_parent, maxAge, poolSize, continue index = bisect_left(historicalFitnesses, child.Fitness, 0, len(historicalFitnesses)) - difference = len(historicalFitnesses) - index - proportionSimilar = difference / len(historicalFitnesses) + proportionSimilar = index / len(historicalFitnesses) if random.random() < exp(-proportionSimilar): parents[pindex] = child continue diff --git a/es/ch08/genetic.py b/es/ch08/genetic.py index 1b78da4..059d452 100644 --- a/es/ch08/genetic.py +++ b/es/ch08/genetic.py @@ -88,8 +88,7 @@ def _obtener_mejoras(nuevo_niño, generar_padre, edadMáxima): continue índice = bisect_left(aptitudesHistóricas, niño.Aptitud, 0, len(aptitudesHistóricas)) - diferencia = len(aptitudesHistóricas) - índice - proporciónSimilar = diferencia / len(aptitudesHistóricas) + proporciónSimilar = índice / len(aptitudesHistóricas) if random.random() < exp(-proporciónSimilar): padre = niño continue diff --git a/es/ch12/genetic.py b/es/ch12/genetic.py index c9b249e..d564f8b 100644 --- a/es/ch12/genetic.py +++ b/es/ch12/genetic.py @@ -138,8 +138,7 @@ def _obtener_mejoras(nuevo_niño, generar_padre, edadMáxima, tamañoDePiscina): continue índice = bisect_left(aptitudesHistóricas, niño.Aptitud, 0, len(aptitudesHistóricas)) - diferencia = len(aptitudesHistóricas) - índice - proporciónSimilar = diferencia / len(aptitudesHistóricas) + proporciónSimilar = índice / len(aptitudesHistóricas) if random.random() < exp(-proporciónSimilar): padres[pÍndice] = niño continue diff --git a/es/ch13/genetic.py b/es/ch13/genetic.py index b439082..3591c1c 100644 --- a/es/ch13/genetic.py +++ b/es/ch13/genetic.py @@ -150,8 +150,7 @@ def _obtener_mejoras(nuevo_niño, generar_padre, edadMáxima, tamañoDePiscina, continue índice = bisect_left(aptitudesHistóricas, niño.Aptitud, 0, len(aptitudesHistóricas)) - diferencia = len(aptitudesHistóricas) - índice - proporciónSimilar = diferencia / len(aptitudesHistóricas) + proporciónSimilar = índice / len(aptitudesHistóricas) if random.random() < exp(-proporciónSimilar): padres[pÍndice] = niño continue diff --git a/es/ch15/genetic.py b/es/ch15/genetic.py index 6a00549..9659892 100644 --- a/es/ch15/genetic.py +++ b/es/ch15/genetic.py @@ -150,8 +150,7 @@ def _obtener_mejoras(nuevo_niño, generar_padre, edadMáxima, tamañoDePiscina, continue índice = bisect_left(aptitudesHistóricas, niño.Aptitud, 0, len(aptitudesHistóricas)) - diferencia = len(aptitudesHistóricas) - índice - proporciónSimilar = diferencia / len(aptitudesHistóricas) + proporciónSimilar = índice / len(aptitudesHistóricas) if random.random() < exp(-proporciónSimilar): padres[pÍndice] = niño continue diff --git a/es/ch16/genetic.py b/es/ch16/genetic.py index a0af7fc..e912871 100644 --- a/es/ch16/genetic.py +++ b/es/ch16/genetic.py @@ -150,8 +150,7 @@ def _obtener_mejoras(nuevo_niño, generar_padre, edadMáxima, tamañoDePiscina, continue índice = bisect_left(aptitudesHistóricas, niño.Aptitud, 0, len(aptitudesHistóricas)) - diferencia = len(aptitudesHistóricas) - índice - proporciónSimilar = diferencia / len(aptitudesHistóricas) + proporciónSimilar = índice / len(aptitudesHistóricas) if random.random() < exp(-proporciónSimilar): padres[pÍndice] = niño continue diff --git a/es/ch18/genetic.py b/es/ch18/genetic.py index 321ca6d..4d7506b 100644 --- a/es/ch18/genetic.py +++ b/es/ch18/genetic.py @@ -151,8 +151,7 @@ def _obtener_mejoras(nuevo_niño, generar_padre, edadMáxima, tamañoDePiscina, continue índice = bisect_left(aptitudesHistóricas, niño.Aptitud, 0, len(aptitudesHistóricas)) - diferencia = len(aptitudesHistóricas) - índice - proporciónSimilar = diferencia / len(aptitudesHistóricas) + proporciónSimilar = índice / len(aptitudesHistóricas) if random.random() < exp(-proporciónSimilar): padres[pÍndice] = niño continue From 514b80b44b98df34457e564989598811d986c567 Mon Sep 17 00:00:00 2001 From: handcraftsman Date: Fri, 31 May 2019 18:23:52 -0500 Subject: [PATCH 2/6] fix tabs vs spaces in merged code segment reported by Piotr.Lewczuk@uk-erlangen.de --- ch09/knapsackTests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ch09/knapsackTests.py b/ch09/knapsackTests.py index b589088..a1c1e53 100644 --- a/ch09/knapsackTests.py +++ b/ch09/knapsackTests.py @@ -255,8 +255,8 @@ def __init__(self, totalWeight, totalVolume, totalValue): def __gt__(self, other): if self.TotalValue != other.TotalValue: return self.TotalValue > other.TotalValue - if self.TotalWeight != other.TotalWeight: - return self.TotalWeight < other.TotalWeight + if self.TotalWeight != other.TotalWeight: + return self.TotalWeight < other.TotalWeight return self.TotalValue < other.TotalValue def __str__(self): From 322eda59dc9f0acf2e145abaf82fef7dbed7d556 Mon Sep 17 00:00:00 2001 From: handcraftsman Date: Fri, 31 May 2019 19:13:47 -0500 Subject: [PATCH 3/6] change knapsack Fitness to compare Volume last --- ch09/knapsackTests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ch09/knapsackTests.py b/ch09/knapsackTests.py index a1c1e53..2f7ac5f 100644 --- a/ch09/knapsackTests.py +++ b/ch09/knapsackTests.py @@ -254,10 +254,10 @@ def __init__(self, totalWeight, totalVolume, totalValue): def __gt__(self, other): if self.TotalValue != other.TotalValue: - return self.TotalValue > other.TotalValue + return self.TotalValue > other.TotalValue if self.TotalWeight != other.TotalWeight: return self.TotalWeight < other.TotalWeight - return self.TotalValue < other.TotalValue + return self.TotalVolume < other.TotalVolume def __str__(self): return "wt: {:0.2f} vol: {:0.2f} value: {}".format( From 2a71f2882b0ecad1ba692d3bf9bc74ce9d02b37d Mon Sep 17 00:00:00 2001 From: handcraftsman Date: Fri, 21 Jun 2019 08:58:22 -0500 Subject: [PATCH 4/6] prevent FutureWarning about || inside a [..] regex --- ch17/regexTests.py | 2 ++ es/ch17/regex.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/ch17/regexTests.py b/ch17/regexTests.py index 39d654d..63bc94b 100644 --- a/ch17/regexTests.py +++ b/ch17/regexTests.py @@ -96,6 +96,8 @@ def repair_in_character_set(token, result, finals): return repair_ignore_repeat_metas_following_repeat_or_start_metas elif token == '[': pass + elif token == '|' and result[-1] == '|': + pass # suppresses FutureWarning about || else: result.append(token) return repair_in_character_set diff --git a/es/ch17/regex.py b/es/ch17/regex.py index f29d61f..8e5cd61 100644 --- a/es/ch17/regex.py +++ b/es/ch17/regex.py @@ -95,6 +95,8 @@ def reparar_in_character_set(símbolo, resultado, finales): return manejar_metas_de_repetición_que_siguen_metas_de_repetición_o_inicio elif símbolo == '[': pass + elif símbolo == '|' and resultado[-1] == '|': + pass # suprimir FutureWarning sobre || else: resultado.append(símbolo) return reparar_in_character_set From 565e405ad0df8d3c360cfc9c335dbacfcc3041c0 Mon Sep 17 00:00:00 2001 From: ErfanThinker Date: Sat, 4 Jul 2020 11:32:32 +0430 Subject: [PATCH 5/6] Removed redundant condition --- ch16/circuitTests.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ch16/circuitTests.py b/ch16/circuitTests.py index 5e139b6..1f53295 100644 --- a/ch16/circuitTests.py +++ b/ch16/circuitTests.py @@ -53,8 +53,7 @@ def create_gene(index, gates, sources): if gateType[1].input_count() > 0: indexA = random.randint(0, index) if gateType[1].input_count() > 1: - indexB = random.randint(0, index) \ - if index > 1 and index >= len(sources) else 0 + indexB = random.randint(0, index) if indexB == indexA: indexB = random.randint(0, index) return Node(gateType[0], indexA, indexB) From 2359440659559fae56b156fe30320b5f5f9a77ee Mon Sep 17 00:00:00 2001 From: handcraftsman Date: Tue, 7 Jul 2020 07:36:56 -0500 Subject: [PATCH 6/6] make ErfanThinker's change in the Spanish version --- es/ch16/pruebas.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/es/ch16/pruebas.py b/es/ch16/pruebas.py index d11f6a8..03a1a8d 100644 --- a/es/ch16/pruebas.py +++ b/es/ch16/pruebas.py @@ -53,8 +53,7 @@ def crear_gen(índice, puertas, fuentes): if tipoDePuerta[1].recuento_de_entradas() > 0: índiceA = random.randint(0, índice) if tipoDePuerta[1].recuento_de_entradas() > 1: - índiceB = random.randint(0, índice) \ - if índice > 1 and índice >= len(fuentes) else 0 + índiceB = random.randint(0, índice) if índiceB == índiceA: índiceB = random.randint(0, índice) return Nodo(tipoDePuerta[0], índiceA, índiceB)