Skip to content

Commit 47c9c03

Browse files
committed
Improve table copying and function name change
1 parent 63aa9a0 commit 47c9c03

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

ann_evolve.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
-g GENERATIONS how many generations to run the program for [default: 1000]
1515
-u MUTATION_RATE mutation rate i.e. probability that a given value will flip [default: 0.4]
1616
-d MUTATION_DISTANCE amount of change a mutation will cause [default: 5]
17-
-b BOTTLENECK number of individuals to keep from each generation [default: 5]
17+
-b BOTTLENECK number of individuals to keep from each generation [default: 10]
1818
-i PROCESSORS number of processors to use [default: 4]
1919
-o OUTPUT_FILE file to write statistics to [default: weights.csv]
2020
-k STARTING_POPULATION starting population size for the simulation [default: 10]
@@ -24,6 +24,7 @@
2424
import csv
2525
from itertools import repeat
2626
from multiprocessing import Pool
27+
from operator import itemgetter
2728
import os
2829
import random
2930
from statistics import mean, pstdev
@@ -33,7 +34,7 @@
3334

3435
import axelrod as axl
3536
from axelrod.strategies.ann import ANN, split_weights
36-
from axelrod_utils import score_for, objective_match_score, objective_match_moran_win
37+
from axelrod_utils import score_for, objective_match_score, objective_moran_win
3738

3839
## Neural network specifics
3940

axelrod_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def objective_match_score_difference(me, other, turns, noise):
3333
scores_for_this_opponent.append(score_diff)
3434
return scores_for_this_opponent
3535

36-
def objective_match_moran_win(me, other, turns, noise=0, repetitions=100):
36+
def objective_moran_win(me, other, turns, noise=0, repetitions=100):
3737
"""Objective function to maximize Moran fixations over N=4 matches"""
3838
assert(noise == 0)
3939
# N = 4 population

fsm_evolve.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
from axelrod import flip_action
3232
from axelrod.strategies.finite_state_machines import FSMPlayer
33-
from axelrod_utils import score_for, objective_match_score, objective_match_moran_win
33+
from axelrod_utils import score_for, objective_match_score, objective_moran_win
3434

3535

3636
## FSM Tables
@@ -66,12 +66,16 @@ def table_from_representation(rep):
6666
table.append(row)
6767
return table
6868

69+
def copy_table(table):
70+
new = list(map(list, table))
71+
return new
72+
6973
def score_table(table, noise):
7074
"""
7175
Take a lookup table dict and return a tuple of the score and the table.
7276
"""
7377
return (score_for(FSMPlayer, args=[table, 0, 'C'], noise=noise,
74-
# objective=objective_match_moran_win), table)
78+
# objective=objective_moran_win), table)
7579
objective=objective_match_score), table)
7680

7781

@@ -93,8 +97,8 @@ def crossover(tables):
9397
continue
9498
# For reproduction, pick a random crossover point
9599
crosspoint = 2 * randrange(num_states)
96-
new_table = t1[:crosspoint]
97-
new_table += t2[crosspoint:]
100+
new_table = list(map(list, t1[:crosspoint]))
101+
new_table += list(map(list, t2[crosspoint:]))
98102
copies.append(new_table)
99103
return copies
100104

@@ -147,7 +151,7 @@ def evolve(starting_tables, mutation_rate, generations, bottleneck, pool,
147151
# the previous generation (i.e. the second element of each tuple)
148152
# plus a bunch of random ones
149153
random_tables = get_random_tables(num_states, 4)
150-
tables_to_copy = [list(x[1]) for x in current_bests]
154+
tables_to_copy = [copy_table(x[1]) for x in current_bests]
151155
tables_to_copy += random_tables
152156

153157
# Crossover
@@ -157,7 +161,7 @@ def evolve(starting_tables, mutation_rate, generations, bottleneck, pool,
157161

158162
# The population of tables we want to consider includes the
159163
# recombined, mutated copies, plus the originals
160-
population = copies + [list(x[1]) for x in current_bests]
164+
population = copies + [copy_table(x[1]) for x in current_bests]
161165
# Map the population to get a list of (score, table) tuples
162166
# This list will be sorted by score, best tables first
163167
results = score_all_tables(population, pool, noise)

lookup_evolve.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
from axelrod import flip_action
3535
from axelrod.strategies.lookerup import LookerUp, create_lookup_table_keys
36-
from axelrod_utils import score_for, objective_match_score, objective_match_moran_win
36+
from axelrod_utils import score_for, objective_match_score, objective_moran_win
3737
import analyze_data
3838

3939

@@ -67,7 +67,8 @@ def score_table(table, noise):
6767
Take a lookup table dict and return a tuple of the score and the table.
6868
"""
6969
return (score_for(LookerUp, args=[table], noise=noise,
70-
objective=objective_match_score), table)
70+
# objective=objective_match_score), table)
71+
objective = objective_moran_win), table)
7172

7273
def score_all_tables(tables, pool, noise):
7374
"""Use a multiprocessing Pool to take a bunch of tables and score them"""

pso_evolve.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from axelrod import Gambler
2525
from axelrod.strategies.lookerup import (
2626
create_lookup_table_keys, create_lookup_table_from_pattern)
27-
from axelrod_utils import score_for, objective_match_score, objective_match_moran_win
27+
from axelrod_utils import score_for, objective_match_score, objective_moran_win
2828

2929

3030
def optimizepso(plays, opp_plays, opp_start_plays, noise=0):

0 commit comments

Comments
 (0)