Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4f6621f

Browse files
author
Greg Eakin
committed
Clarify and fix which age gets reset in the Get Improvement function.
1 parent 54078b4 commit 4f6621f

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

‎ch08/genetic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,20 @@ def _get_improvement(new_child, generate_parent, maxAge):
9090
if random.random() < exp(-proportionSimilar):
9191
parent = child
9292
continue
93+
bestParent.Age = 0
9394
parent = bestParent
94-
parent.Age = 0
9595
continue
9696
if not child.Fitness > parent.Fitness:
9797
# same fitness
9898
child.Age = parent.Age + 1
9999
parent = child
100100
continue
101+
child.Age = 0
101102
parent = child
102-
parent.Age = 0
103103
if child.Fitness > bestParent.Fitness:
104-
yield child
105104
bestParent = child
106-
historicalFitnesses.append(child.Fitness)
105+
yield bestParent
106+
historicalFitnesses.append(bestParent.Fitness)
107107

108108

109109
class Chromosome:

‎ch12/genetic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,20 @@ def _get_improvement(new_child, generate_parent, maxAge, poolSize):
139139
if random.random() < exp(-proportionSimilar):
140140
parents[pindex] = child
141141
continue
142+
bestParent.Age = 0
142143
parents[pindex] = bestParent
143-
parent.Age = 0
144144
continue
145145
if not child.Fitness > parent.Fitness:
146146
# same fitness
147147
child.Age = parent.Age + 1
148148
parents[pindex] = child
149149
continue
150+
child.Age = 0
150151
parents[pindex] = child
151-
parent.Age = 0
152152
if child.Fitness > bestParent.Fitness:
153-
yield child
154153
bestParent = child
155-
historicalFitnesses.append(child.Fitness)
154+
yield bestParent
155+
historicalFitnesses.append(bestParent.Fitness)
156156

157157

158158
class Chromosome:

‎ch13/genetic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,20 @@ def _get_improvement(new_child, generate_parent, maxAge, poolSize, maxSeconds):
148148
if random.random() < exp(-proportionSimilar):
149149
parents[pindex] = child
150150
continue
151+
bestParent.Age = 0
151152
parents[pindex] = bestParent
152-
parent.Age = 0
153153
continue
154154
if not child.Fitness > parent.Fitness:
155155
# same fitness
156156
child.Age = parent.Age + 1
157157
parents[pindex] = child
158158
continue
159+
child.Age = 0
159160
parents[pindex] = child
160-
parent.Age = 0
161161
if child.Fitness > bestParent.Fitness:
162-
yield False, child
163162
bestParent = child
164-
historicalFitnesses.append(child.Fitness)
163+
yield False, bestParent
164+
historicalFitnesses.append(bestParent.Fitness)
165165

166166

167167
class Chromosome:

‎ch16/genetic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,20 +149,20 @@ def _get_improvement(new_child, generate_parent, maxAge, poolSize,
149149
if random.random() < exp(-proportionSimilar):
150150
parents[pindex] = child
151151
continue
152+
bestParent.Age = 0
152153
parents[pindex] = bestParent
153-
parent.Age = 0
154154
continue
155155
if not child.Fitness > parent.Fitness:
156156
# same fitness
157157
child.Age = parent.Age + 1
158158
parents[pindex] = child
159159
continue
160+
child.Age = 0
160161
parents[pindex] = child
161-
parent.Age = 0
162162
if child.Fitness > bestParent.Fitness:
163-
yield False, child
164163
bestParent = child
165-
historicalFitnesses.append(child.Fitness)
164+
yield False, bestParent
165+
historicalFitnesses.append(bestParent.Fitness)
166166

167167

168168
def hill_climbing(optimizationFunction, is_improvement, is_optimal,

‎ch18/genetic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,20 +150,20 @@ def _get_improvement(new_child, generate_parent, maxAge, poolSize,
150150
if random.random() < exp(-proportionSimilar):
151151
parents[pindex] = child
152152
continue
153+
bestParent.Age = 0
153154
parents[pindex] = bestParent
154-
parent.Age = 0
155155
continue
156156
if not child.Fitness > parent.Fitness:
157157
# same fitness
158158
child.Age = parent.Age + 1
159159
parents[pindex] = child
160160
continue
161+
child.Age = 0
161162
parents[pindex] = child
162-
parent.Age = 0
163163
if child.Fitness > bestParent.Fitness:
164-
yield False, child
165164
bestParent = child
166-
historicalFitnesses.append(child.Fitness)
165+
yield False, bestParent
166+
historicalFitnesses.append(bestParent.Fitness)
167167

168168

169169
def hill_climbing(optimizationFunction, is_improvement, is_optimal,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /