Skip to content

Commit 8b17e01

Browse files
bing-jmarcharper
authored andcommitted
style changes in docstrings and comments, renamed file to bandits, updated doc test, and changed value of UNIFORM constant to -1.
1 parent 32605e3 commit 8b17e01

File tree

5 files changed

+10
-44
lines changed

5 files changed

+10
-44
lines changed

axelrod/strategies/_strategies.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
from .dbs import DBS
9191
from .defector import Defector, TrickyDefector
9292
from .doubler import Doubler
93-
from .armed_bandits import Greedy, EpsilonGreedy
93+
from .bandits import Greedy, EpsilonGreedy
9494
from .finite_state_machines import (
9595
TF1,
9696
TF2,

axelrod/strategies/armed_bandits.py renamed to axelrod/strategies/bandits.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Greedy(Player):
2828
"manipulates_state": False,
2929
}
3030

31-
UNIFORM = np.inf # constant that replaces weight when rewards aren't weighted
31+
UNIFORM = -1.0 # constant that replaces weight when rewards aren't weighted
3232

3333
def __init__(
3434
self,
@@ -46,16 +46,16 @@ def __init__(
4646
recency_weight
4747
0.0 <= recency_weight <= 1.0
4848
The exponential recency weight used in calculating the average reward.
49-
If this argument is not provided, the player will not weigh rewards based on recency.
49+
If this argument is equal to -1 or is not provided, the player will not weigh rewards based on recency.
5050
"""
5151
super().__init__()
5252
self._rewards = {C: init_c_reward, D: init_d_reward}
5353
self.weight = recency_weight
5454

55-
# treat out of range values as extremes
56-
if self.weight <= 0:
55+
# limit parameter value range
56+
if (self.weight != self.UNIFORM) and self.weight <= 0:
5757
self.weight = 0.0
58-
if (not np.isinf(self.weight)) and (self.weight >= 1):
58+
if self.weight >= 1:
5959
self.weight = 1.0
6060

6161
def update_rewards(self, opponent: Player):
@@ -66,7 +66,7 @@ def update_rewards(self, opponent: Player):
6666
last_score = game.score(last_round)[0]
6767

6868
# if UNIFORM, use 1 / total number of times the updated action was taken previously
69-
if np.isinf(self.weight):
69+
if self.weight == self.UNIFORM:
7070
weight = 1 / (
7171
self.history.cooperations if last_play == C else self.history.defections
7272
)
@@ -78,7 +78,6 @@ def update_rewards(self, opponent: Player):
7878
)
7979

8080
def strategy(self, opponent: Player) -> Action:
81-
"""Actual strategy definition that determines player's action."""
8281
# if not the first turn
8382
if len(self.history) != 0:
8483
self.update_rewards(opponent)
@@ -89,7 +88,7 @@ def strategy(self, opponent: Player) -> Action:
8988

9089
class EpsilonGreedy(Greedy):
9190
"""
92-
Has a 1 - epsilon probability of behaving like Greedy(), and plays randomly otherwise.
91+
Has a 1 - epsilon probability of behaving like Greedy; otherwise, randomly choose to cooperate or defect.
9392
9493
Names:
9594
@@ -144,7 +143,6 @@ def _post_init(self):
144143
self.classifier["stochastic"] = False
145144

146145
def strategy(self, opponent: Player) -> Action:
147-
"""Actual strategy definition that determines player's action."""
148146
# this will also update the reward appropriately
149147
greedy_action = super().strategy(opponent)
150148

axelrod/tests/strategies/test_armed_bandits.py

-32
Original file line numberDiff line numberDiff line change
@@ -89,35 +89,3 @@ def test_strategy(self):
8989
attrs={"_rewards": {C: 3, D: 0}},
9090
seed=1,
9191
)
92-
93-
# temporary overriding function used to search for seeds
94-
# def versus_test(
95-
# self,
96-
# opponent,
97-
# expected_actions,
98-
# turns=None,
99-
# noise=None,
100-
# seed=None,
101-
# match_attributes=None,
102-
# attrs=None,
103-
# init_kwargs=None,
104-
# ):
105-
#
106-
# if init_kwargs is None:
107-
# init_kwargs = dict()
108-
#
109-
# player = self.player(**init_kwargs)
110-
#
111-
# test_match = TestMatch()
112-
# seed = test_match.search_seeds(
113-
# player,
114-
# opponent,
115-
# [x for (x, y) in expected_actions],
116-
# [y for (x, y) in expected_actions],
117-
# turns=turns,
118-
# noise=noise,
119-
# seed=seed,
120-
# attrs=attrs,
121-
# match_attributes=match_attributes,
122-
# )
123-
# self.assertIsNotNone(seed)

docs/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Count the number of available players::
5353

5454
>>> import axelrod as axl
5555
>>> len(axl.strategies)
56-
240
56+
242
5757

5858
Create matches between two players::
5959

docs/reference/strategy_index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Here are the docstrings of all the strategies in the library.
1818
:members:
1919
.. automodule:: axelrod.strategies.appeaser
2020
:members:
21-
.. automodule:: axelrod.strategies.armed_bandits
21+
.. automodule:: axelrod.strategies.bandits
2222
:members:
2323
.. automodule:: axelrod.strategies.averagecopier
2424
:members:

0 commit comments

Comments
 (0)