Skip to content

Commit 6b6031a

Browse files
committed
Simplify moran mutate function
1 parent 42ecb93 commit 6b6031a

File tree

1 file changed

+9
-24
lines changed

1 file changed

+9
-24
lines changed

axelrod/moran.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,6 @@ def __init__(
122122
self._random = RandomGenerator(seed=seed)
123123
self._bulk_random = BulkRandomGenerator(self._random.random_seed_int())
124124
self.set_players()
125-
# Build the set of mutation targets
126-
# Determine the number of unique types (players)
127-
keys = set([str(p) for p in players])
128-
# Create a dictionary mapping each type to a set of representatives
129-
# of the other types
130-
d = dict()
131-
for p in players:
132-
d[str(p)] = p
133-
mutation_targets = dict()
134-
for key in sorted(keys):
135-
mutation_targets[key] = [
136-
v for (k, v) in sorted(d.items()) if k != key
137-
]
138-
self.mutation_targets = mutation_targets
139125

140126
if interaction_graph is None:
141127
interaction_graph = complete_graph(len(players), loops=False)
@@ -218,16 +204,15 @@ def mutate(self, index: int) -> Player:
218204
return self.players[index].mutate()
219205

220206
# Assuming mutation_method == "transition"
221-
if self.mutation_rate > 0:
222-
# Choose another strategy at random from the initial population
223-
r = self._random.random()
224-
if r < self.mutation_rate:
225-
s = str(self.players[index])
226-
j = self._random.randrange(0, len(self.mutation_targets[s]))
227-
p = self.mutation_targets[s][j]
228-
return p.clone()
229-
# Just clone the player
230-
return self.players[index].clone()
207+
if self._random.random() > self.mutation_rate:
208+
# Just clone the player
209+
return self.players[index].clone()
210+
211+
# Choose another strategy at random from the initial population
212+
player = None
213+
while player is None or str(player) == str(self.players[index]):
214+
player = self._random.choice(self.initial_players)
215+
return player.clone()
231216

232217
def death(self, index: int = None) -> int:
233218
"""

0 commit comments

Comments
 (0)