Skip to content

Commit 8bc822d

Browse files
committed
Add ability to pass shared library name to Player
I have also added a warning when a self interaction is being played. Not that the warning only happens once (as indicated by the test). I have also removed the tournament in our README. Closes #65
1 parent d1981db commit 8bc822d

File tree

3 files changed

+53
-21
lines changed

3 files changed

+53
-21
lines changed

README.rst

-17
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,6 @@ Running a match:
3333
>>> match.play()
3434
[(C, C), (C, C), (C, D), (C, D), (C, C)]
3535
36-
Running an instance of Axelrod's second tournament:
37-
38-
.. code-block:: python
39-
40-
>>> import axelrod_fortran as axlf
41-
>>> import axelrod as axl
42-
>>> players = [axlf.Player(name) for name in axlf.second_tournament_strategies]
43-
>>> print(len(players), "players")
44-
63 players
45-
>>> tournament = axl.Tournament(players, repetitions=1,
46-
... turns=63, match_attributes={"length": -1})
47-
>>> results = tournament.play()
48-
>>> results.write_summary('summary.csv')
49-
>>> plot = axl.Plot(results)
50-
>>> plot.save_all_plots("second_tournament")
51-
52-
5336
Contributing
5437
============
5538

src/axelrod_fortran/player.py

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import random
2+
import warnings
23

34
import axelrod as axl
45
from axelrod.interaction_utils import compute_final_score
@@ -7,7 +8,6 @@
78
from .strategies import characteristics
89

910
C, D = Action.C, Action.D
10-
strategies = cdll.LoadLibrary('libstrategies.so')
1111
actions = {0: C, 1: D}
1212
original_actions = {C: 0, D: 1}
1313

@@ -16,7 +16,8 @@ class Player(axl.Player):
1616

1717
classifier = {"stochastic": True}
1818

19-
def __init__(self, original_name):
19+
def __init__(self, original_name,
20+
shared_library_name='libstrategies.so'):
2021
"""
2122
Parameters
2223
----------
@@ -27,6 +28,8 @@ def __init__(self, original_name):
2728
A instance of an axelrod Game
2829
"""
2930
super().__init__()
31+
self.shared_library_name = shared_library_name
32+
self.shared_library = cdll.LoadLibrary(shared_library_name)
3033
self.original_name = original_name
3134
self.original_function = self.original_name
3235
is_stochastic = characteristics[self.original_name]['stochastic']
@@ -56,7 +59,7 @@ def original_function(self):
5659

5760
@original_function.setter
5861
def original_function(self, value):
59-
self.__original_function = strategies[(value + '_').lower()]
62+
self.__original_function = self.shared_library[(value + '_').lower()]
6063
self.__original_function.argtypes = (
6164
POINTER(c_int), POINTER(c_int), POINTER(c_int), POINTER(c_int),
6265
POINTER(c_float))
@@ -72,6 +75,17 @@ def original_strategy(
7275
return self.original_function(*[byref(arg) for arg in args])
7376

7477
def strategy(self, opponent):
78+
if type(opponent) is Player\
79+
and (opponent.original_name == self.original_name) \
80+
and (opponent.shared_library_name == self.shared_library_name):
81+
82+
message = """
83+
You are playing a match with two copies of the same player.
84+
However the axelrod fortran players share memory.
85+
You can initialise an instance of an Axelrod_fortran player with a `strategies`
86+
variable that points to a copy of the shared library."""
87+
warnings.warn(message=message)
88+
7589
if not self.history:
7690
their_last_move = 0
7791
scores = (0, 0)

tests/test_player.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from axelrod import (Alternator, Cooperator, Defector,
33
Match, Game, basic_strategies, seed)
44
from axelrod.action import Action
5-
from ctypes import c_int, c_float, POINTER
5+
from ctypes import c_int, c_float, POINTER, CDLL
66

77
import itertools
88
import pytest
@@ -22,6 +22,15 @@ def test_init():
2222
assert player.original_function.restype == c_int
2323
with pytest.raises(ValueError):
2424
player = Player('test')
25+
assert "libstrategies.so" == player.shared_library_name
26+
assert type(player.shared_library) is CDLL
27+
assert "libstrategies.so" in str(player.shared_library)
28+
29+
def test_init_with_shared():
30+
player = Player("k42r", shared_library_name="libstrategies.so")
31+
assert "libstrategies.so" == player.shared_library_name
32+
assert type(player.shared_library) is CDLL
33+
assert "libstrategies.so" in str(player.shared_library)
2534

2635

2736
def test_matches():
@@ -145,3 +154,29 @@ def test_champion_v_alternator():
145154

146155
seed(0)
147156
assert interactions == match.play()
157+
158+
def test_warning_for_self_interaction(recwarn):
159+
"""
160+
Test that a warning is given for a self interaction.
161+
"""
162+
player = Player("k42r")
163+
opponent = Player("k42r")
164+
165+
match = Match((player, opponent))
166+
167+
interactions = match.play()
168+
assert len(recwarn) == 1
169+
170+
def test_no_warning_for_normal_interaction(recwarn):
171+
"""
172+
Test that a warning is not given for a normal interaction
173+
"""
174+
player = Player("k42r")
175+
opponent = Alternator()
176+
for players in [(Player("k42r"), Alternator()),
177+
(Player("k42r"), Player("k41r"))]:
178+
179+
match = Match(players)
180+
181+
interactions = match.play()
182+
assert len(recwarn) == 0

0 commit comments

Comments
 (0)