Skip to content

Commit 204d498

Browse files
committed
SteinAndRapoport Strategy
1 parent 4702455 commit 204d498

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
from .selfsteem import SelfSteem
7373
from .shortmem import ShortMem
7474
from .stalker import Stalker
75+
from .axelrod_first import SteinAndRapoport
7576
from .titfortat import (
7677
TitForTat, TitFor2Tats, TwoTitsForTat, Bully, SneakyTitForTat,
7778
SuspiciousTitForTat, AntiTitForTat, HardTitForTat, HardTitFor2Tats,
@@ -232,6 +233,7 @@
232233
SolutionB5,
233234
SpitefulTitForTat,
234235
Stalker,
236+
SteinAndRapoport,
235237
StochasticCooperator,
236238
StochasticWSLS,
237239
SuspiciousTitForTat,

axelrod/strategies/axelrod_first.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from axelrod.player import Player
99
from axelrod.random_ import random_choice
1010
from.memoryone import MemoryOnePlayer
11+
from axelrod.strategy_transformers import FinalTransformer
12+
from scipy.stats import chisquare
1113

1214
from typing import List, Dict, Tuple
1315

@@ -487,3 +489,69 @@ class UnnamedStrategy(Player):
487489
def strategy(opponent: Player) -> Action:
488490
r = random.uniform(3, 7) / 10
489491
return random_choice(r)
492+
493+
@FinalTransformer((D, D), name_prefix=None)
494+
class SteinAndRapoport(Player):
495+
"""
496+
A player who plays according to statistic methods.
497+
Begins by playing C for the first four (4) rounds , then it plays
498+
tit for tat and at the last 2 round it Defects. Every 15 turns it
499+
run a chi-squared test to check whether the opponent behaves randomly
500+
or not . In case the opponent behaves randomly then Stein_and_Rapoport
501+
Defects untill the next 15 round (where we check again), otherwise he
502+
still plays TitForTat.
503+
"""
504+
505+
name = 'Stein and Rapoport'
506+
classifier = {
507+
'memory_depth': 15,
508+
'stochastic': False,
509+
'makes_use_of': set(),
510+
'long_run_time': False,
511+
'inspects_source': False,
512+
'manipulates_source': False,
513+
'manipulates_state': False
514+
}
515+
516+
def __init__(self, alpha: float=0.05) -> None:
517+
"""
518+
Parameters
519+
----------
520+
alpha, float
521+
The significant level of pvalue from chi-squared test
522+
0.05 by default according to literature
523+
"""
524+
super().__init__()
525+
self.alpha = alpha
526+
if (self.alpha > 1) or (self.alpha < 0):
527+
self.alpha = 0.05
528+
529+
def strategy(self , opponent: Player , chi_tests = [0]) -> Action:
530+
# chi-tests == 0 then we play TitForTat
531+
round_number = len(self.history) + 1
532+
533+
# First 4 moves
534+
if round_number < 5 :
535+
return C
536+
537+
# For first 15 rounds tit for tat as we dont know opponents strategy
538+
if round_number < 16 :
539+
if opponent.history[-1] == 'D' :
540+
return D
541+
else :
542+
return C
543+
544+
if len(self.history) % 15 == 0 :
545+
if chisquare([opponent.cooperations, opponent.defections]).pvalue >= self.alpha :
546+
chi_tests.append(1)
547+
else :
548+
chi_tests.append(0)
549+
550+
if chi_tests[-1] == 1 :
551+
# Defect if opponent plays randomly
552+
return D
553+
else : # TitForTatat if opponent plays not randomly
554+
if opponent.history[-1] == 'D' :
555+
return D
556+
else :
557+
return C

axelrod/tests/strategies/test_axelrod_first.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,41 @@ def test_strategy(self):
387387
actions = [(C, C), (C, C), (D, C), (C, C), (C, C), (D, C)]
388388
self.versus_test(axelrod.Cooperator(), expected_actions=actions,
389389
seed=10)
390+
391+
392+
class SteinAndRapoport(TestPlayer):
393+
394+
name = "SteinAndRapoport"
395+
player = axelrod.SteinAndRapoport
396+
expected_classifier = {
397+
'memory_depth': 15,
398+
'long_run_time': False,
399+
'stochastic': False,
400+
'makes_use_of': set(),
401+
'inspects_source': False,
402+
'manipulates_source': False,
403+
'manipulates_state': False
404+
}
405+
406+
def test_strategy(self):
407+
self.first_play_test(C)
408+
409+
# Our Player (SteinAndRapoport) vs Cooperator
410+
# After 15th round (pvalue < alpha) still plays titfortat
411+
opponent = axelrod.Cooperator()
412+
actions = [(C, C)] * 17 + [(D, C)] * 2
413+
self.versus_test(opponent, expected_actions=actions)
414+
415+
# Our Player (SteinAndRapoport) vs Defector
416+
# After 15th round (pvalue < alpha) still plays titfortat
417+
opponent = axelrod.Cooperator()
418+
actions = [(C, D)] * 4 + [(D, D)] * 15
419+
self.versus_test(opponent, expected_actions=actions)
420+
421+
# Our Player (SteinAndRapoport) vs Alternator
422+
# After 15th round (pvalue > alpha) starts defect
423+
opponent = axelrod.Alternator()
424+
actions = [(C, C), (C, D), (C, C), (C, D), (D, C), (C, D), (D, C),
425+
(C, D), (D, C), (C, D), (D, C), (C, D),(D, C), (C, D),
426+
(D, C), (D, D), (D, C), (D, D), (D, C)]
427+
self.versus_test(opponent, expected_actions=actions)

0 commit comments

Comments
 (0)