Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.grudge_memory = 0
self.grudged = False
if self.grudged:
self.grudge_memory += 1
return D
elif D in opponent.history[-1:]:
self.mem_length = (opponent.defections * 20) // len(opponent.history)
self.grudged = True
return D
return C
class InversePunisher(Player):
"""
An inverted version of Punisher. The player starts by cooperating however
will defect if at any point the opponent has defected, and forgets after
mem_length matches, with 1 <= mem_length <= 20. This time mem_length is
proportional to the amount of time the opponent has played C.
Names:
- Inverse Punisher: Original name by Geraint Palmer
"""
name = "Inverse Punisher"
classifier = {
"memory_depth": float("inf"), # Long memory
"stochastic": False,
"makes_use_of": set(),
import axelrod as axl
import random
import unittest
from axelrod_dojo import GamblerParams
from axelrod.strategies.lookerup import Plays
from axelrod.strategies.lookerup import create_lookup_table_keys
C, D = axl.Action.C, axl.Action.D
class TestGamblerParams(unittest.TestCase):
def test_init(self):
plays = 2
op_plays = 2
op_start_plays = 1
gambler_params = GamblerParams(plays=plays, op_plays=op_plays,
op_start_plays=op_start_plays)
self.assertEqual(gambler_params.PlayerClass, axl.Gambler)
self.assertEqual(gambler_params.plays, plays)
self.assertEqual(gambler_params.op_plays, op_plays)
self.assertEqual(gambler_params.op_start_plays, op_start_plays)
def test_score(self):
axl.seed(0)
opponents_information = [utils.PlayerInfo(s, {})
for s in axl.demo_strategies]
objective = utils.prepare_objective()
params = DummyParams()
score = utils.score_params(params,
objective=objective,
opponents_information=opponents_information)
expected_score = 2.0949
self.assertEqual(score, expected_score)
turns=turns,
noise=noise,
repetitions=repetitions)
population = dojo.Population(params_class=dojo.HMMParams,
params_kwargs={"num_states": num_states},
size=size,
objective=objective,
output_filename=self.temporary_file.name,
opponents=opponents,
bottleneck=2,
mutation_probability = .01,
processes=0)
generations = 4
axl.seed(0)
population.run(generations, print_output=False)
self.assertEqual(population.generation, 4)
def test_creation_seqLen(self):
axl.seed(0)
test_length = 10
self.instance = CyclerParams(sequence_length=test_length)
self.assertEqual(self.instance.sequence, [C, D, D, C, D, D, D, D, D, D])
self.assertEqual(self.instance.sequence_length, test_length)
self.assertEqual(len(self.instance.sequence), test_length)
population = dojo.Population(params_class=dojo.FSMParams,
params_kwargs={"num_states": num_states,
"mutation_probability": .5},
size=size,
objective=objective,
output_filename=self.temporary_file.name,
opponents=opponents,
bottleneck=2,
mutation_probability = .01,
processes=1)
for p in population.population:
self.assertEqual(p.mutation_probability, .5)
generations = 1
axl.seed(0)
population.run(generations, print_output=False)
self.assertEqual(population.generation, 1)
noise=noise,
repetitions=repetitions)
population = dojo.Population(params_class=dojo.HMMParams,
params_kwargs={"num_states": num_states},
size=size,
objective=objective,
output_filename=self.temporary_file.name,
opponents=opponents,
sample_count=2, # Randomly sample 2 opponents at each step
bottleneck=2,
mutation_probability = .01,
processes=1)
generations = 4
axl.seed(0)
population.run(generations, print_output=False)
self.assertEqual(population.generation, 4)
# Manually read from temp file to find best strategy
best_score, best_params = 0, None
with open(self.temporary_file.name, "r") as f:
reader = csv.reader(f)
for row in reader:
_, mean_score, sd_score, max_score, arg_max = row
if float(max_score) > best_score:
best_score = float(max_score)
best_params = arg_max
# Test the load params function
for num in range(1, 4 + 1):
best = dojo.load_params(params_class=dojo.HMMParams,
population = dojo.Population(params_class=dojo.HMMParams,
params_kwargs={"num_states": num_states,
"mutation_probability": .5},
size=size,
objective=objective,
output_filename=self.temporary_file.name,
opponents=opponents,
bottleneck=2,
mutation_probability = .01,
processes=1)
for p in population.population:
self.assertEqual(p.mutation_probability, .5)
generations = 1
axl.seed(0)
population.run(generations, print_output=False)
self.assertEqual(population.generation, 1)
noise=noise,
repetitions=repetitions)
population = dojo.Population(params_class=dojo.FSMParams,
params_kwargs={"num_states": num_states},
size=size,
objective=objective,
output_filename=self.temporary_file.name,
opponents=opponents,
sample_count=2, # Randomly sample 2 opponents at each step
bottleneck=2,
mutation_probability = .01,
processes=1)
generations = 4
axl.seed(0)
population.run(generations, print_output=False)
self.assertEqual(population.generation, 4)
# Manually read from tempo file to find best strategy
best_score, best_params = 0, None
with open(self.temporary_file.name, "r") as f:
reader = csv.reader(f)
for row in reader:
_, mean_score, sd_score, max_score, arg_max = row
if float(max_score) > best_score:
best_score = float(max_score)
best_params = arg_max
# Test the load params function
for num in range(1, 4 + 1):
best = dojo.load_params(params_class=dojo.FSMParams,
def test_init_without_defaults(self):
"""
Check that by default have random rows.
"""
num_states = 2
transitions_Cs, transitions_Ds = [], []
for seed in range(2):
axl.seed(seed)
hmm_params = HMMParams(num_states=num_states)
transitions_Cs.append(hmm_params.transitions_C)
transitions_Ds.append(hmm_params.transitions_D)
self.assertNotEqual(*transitions_Cs)
self.assertNotEqual(*transitions_Ds)