Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Algorithm for Memory Depth in FSM #1233

Merged
merged 40 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
94ebbee
Clean up docstrings, to match parameter-listing to actual parameters.
Dec 31, 2018
193e8ab
Response to meatballs' feedback.
Jan 3, 2019
b1be726
Merge https://github.com/Axelrod-Python/Axelrod
Jan 4, 2019
0822b5c
Added a get_memory_from_transitions function for FSM.
Jan 6, 2019
395a040
Change default verbosity of get_memory_from_transitions.
Jan 6, 2019
2caf106
Fixed build error, and made less verbose.
Jan 7, 2019
7294426
Change the format of transitions in get_memory_from_transitions to dict.
Jan 7, 2019
080bf8c
Added types to get_memory_from_transitions.
Jan 7, 2019
d0769e8
Make ActionChains faster.
Jan 8, 2019
0e6803e
Replaced get_accessible_transitions with a much faster version.
Jan 9, 2019
ffc319a
Updated tests for new memory classifiers.
Jan 9, 2019
b8e2b44
Fixed mypy and doctest errors.
Jan 11, 2019
b603e7e
Updated metastrategies for new finite set.
Jan 11, 2019
95a1d13
Moved blocks/added comments for readability.
Jan 11, 2019
ebbcf73
More specific typing.
Jan 13, 2019
8d22d95
Responded to Marc's comments.
Jan 14, 2019
f2340ad
Import List from typing.
Jan 14, 2019
2e3c7e4
Change DefaultDict type and added tit_for_five_tat test.
Jan 14, 2019
d5e2b43
Remove type on all_memits
Jan 15, 2019
0a62b74
Remove more typing.
Jan 15, 2019
9df30e3
Fixed type on longest_path argument.
Jan 15, 2019
87792a2
Responding to Marc's comments.
Jan 16, 2019
b4e6c41
Fixed Tuple annotation.
Jan 16, 2019
6763610
Add a memory test to default FSM test.
drvinceknight Jan 16, 2019
f5d6129
Responded to some of drvinceknight comments on memory depth.
Jan 22, 2019
28f90ae
Merge pull request #1 from Axelrod-Python/add-test-of-memory-to-all-fsms
gaffney2010 Jan 22, 2019
222759e
Fixed some errors.
Jan 22, 2019
50e8284
Merge branch 'master' of https://github.com/gaffney2010/Axelrod
Jan 22, 2019
9cae641
Moved FSM memory functions to separate top-level file compute_finite_…
Jan 22, 2019
676b722
Added additional topic documentation for FSM/memory.
Jan 24, 2019
3ad5c16
Fix code in new documentation.
Jan 24, 2019
4ea47e8
Move unit tests for compute FSM memory and add order_memit_tuple.
Jan 25, 2019
1f6a168
Minor changes to meta_strategies doc.
Jan 25, 2019
c4358f9
Update bibliography.rst
gaffney2010 Jan 26, 2019
7f8f246
Undid changes to usually coop/def. Move to different commit.
Jan 27, 2019
a081ad8
Merge branch 'master' of https://github.com/gaffney2010/Axelrod
Jan 27, 2019
a348e2a
Delete old comment.
Jan 27, 2019
2d2a1ea
Remove memory tests from FSM test file; these have already been copied.
Jan 27, 2019
136864c
Add memory library back into FSM test.
Jan 27, 2019
f96fa4e
Minor fixes to compute memory tests.
Jan 28, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
241 changes: 227 additions & 14 deletions axelrod/strategies/finite_state_machines.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,220 @@
from axelrod.action import Action
from axelrod.player import Player
from typing import DefaultDict, Iterator
from collections import defaultdict, namedtuple
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved

C, D = Action.C, Action.D
ALL_ACTIONS = [C, D]
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved


"""
Memit = unit of memory.

This represents the amount of memory that we gain with each new piece of
history. It includes a state, our_response that we make on our way into that
state (in_act), and the opponent's action that makes us move out of that state
(out_act).

For example for this FSM:
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
(0, C, 0, C),
(0, D, 1, C),
(1, C, 0, D),
(1, D, 0, D)

Has the memits:
(C, 0, C),
(C, 0, D),
(D, 0, C),
(D, 0, D),
(C, 1, C),
(C, 1, D)
"""
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
Memit = namedtuple("Memit", ["in_act", "state", "out_act"])

def memits_match(x, y):
"""In action and out actions are the same."""
return x.in_act == y.in_act and x.out_act == y.out_act
def memit_sort(x, y):
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
"""Returns a tuple of x in y, sorted so that (x, y) are viewed as the
same as (y, x).
"""
if repr(x) <= repr(y):
return (x, y)
else:
return (y, x)


def get_accessible_transitions(transitions: dict, initial_state: int) -> dict:
"""Gets all transitions from the list that can be reached from the
initial_state.
"""
edge_dict: DefaultDict[int, list] = defaultdict(list)
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
visited = dict()
for k, v in transitions.items():
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
visited[k[0]] = False
edge_dict[k[0]].append(v[0])
accessible_edges = [initial_state]

edge_queue = [initial_state]
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
visited[initial_state] = True
while len(edge_queue) > 0:
edge = edge_queue.pop()
for successor in edge_dict[edge]:
if not visited[successor]:
visited[successor] = True
edge_queue.append(successor)
accessible_edges.append(successor)

accessible_transitions = dict()
for k, v in transitions.items():
if k[0] in accessible_edges:
accessible_transitions[k] = v

return accessible_transitions


def longest_path(edges: dict, starting_at: Memit) -> int:
"""Returns the number of nodes in the longest path that starts at the given
node. Returns infinity if a loop is encountered.
"""
visited = dict()
for k, v in edges.items():
visited[k] = False
for vi in v:
visited[vi] = False

# This is what we'll recurse on. visited dict is shared between calls.
def recurse(at_node):
visited[at_node] = True
record = 1 # Count the nodes, not the edges.
for successor in edges[at_node]:
if visited[successor]:
return float("inf")
successor_length = recurse(successor)
if successor_length == float("inf"):
return float("inf")
if record < successor_length + 1:
record = successor_length + 1
return record

return recurse(starting_at)


Transition = namedtuple("Transition", ["state", "last_opponent_action",
"next_state", "next_action"])
def transition_iterator(transitions: dict) -> Iterator[Transition]:
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
"""Changes the transition dictionary into a iterator on namedtuples, because
we use repeatedly.
"""
for k, v in transitions.items():
yield Transition(k[0], k[1], v[0], v[1])


def get_memory_from_transitions(transitions: dict,
initial_state: int = None) -> int:
"""This function calculates the memory of an FSM from the transitions.

Assume that transitions are a dict with entries like
(state, last_opponent_action): (next_state, next_action)

We first break down the transitions into memits (see above). We also create
a graph of memits, where the successor to a given memit are all possible
memits that could occur in the memory immediately before the given memit.

Then we pair up memits with different states, but same in and out actions.
These represent points in time that we can't determine which state we're in.
We also create a graph of memit-pairs, where memit-pair, Y, succedes a
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
memit-pair, X, if the two memits in X are succeded by the two memits in Y.
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
These edges reperesent consecutive points in time that we can't determine
which state we're in.

Then for all memit-pairs that disagree, in the sense that they imply
different next_action, we find the longest chain starting at that
memit-pair. [If a loop is encountered then this will be infinite.] We take
the maximum over all sugh memit-pairs. This represents the longest possible
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
chain of memory for which we wouldn't know what to do next. We return this.
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
"""
# If initial_state is set, use this to determine which transitions are
# reachable from the initial_state and restrict to those.
if initial_state is not None:
transitions = get_accessible_transitions(transitions, initial_state)

# Get the incoming actions for each state.
incoming_action_by_state: DefaultDict[int, set] = defaultdict(set)
for trans in transition_iterator(transitions):
incoming_action_by_state[trans.next_state].add(trans.next_action)

# Keys are starting memit, and values are all possible terminal memit.
# Will walk backwards through the graph.
memit_edges: DefaultDict[Memit, set] = defaultdict(set)
for trans in transition_iterator(transitions):
# Since all actions are out-paths for each state, add all of these.
# That is to say that your opponent could do anything
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
for out_action in ALL_ACTIONS:
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
# More recent in action history
starting_node = Memit(trans.next_action, trans.next_state,
out_action)
# All incoming paths to current state
for in_action in incoming_action_by_state[trans.state]:
# Less recent in action history
ending_node = Memit(in_action, trans.state,
trans.last_opponent_action)
memit_edges[starting_node].add(ending_node)

all_memits = memit_edges.keys()

pair_nodes = set()
pair_edges: DefaultDict[tuple, set] = defaultdict(set)
# Loop through all pairs of memits.
for x, y in [(x, y) for x in all_memits for y in all_memits]:
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
if x == y:
continue
if not memits_match(x, y):
continue

# If the memits match, then the strategy can't tell the difference
# between the states. We call this a pair of matched memits (or just a
# pair).
pair_nodes.add(memit_sort(x, y))
# When two memits in matched pair have successors that are also matched,
# then we draw an edge. This represents consecutive historical times
# that we can't tell which state we're in.
for x_successor in memit_edges[x]:
for y_successor in memit_edges[y]:
if memits_match(x_successor, y_successor):
pair_edges[memit_sort(x, y)].add(memit_sort(x_successor,
y_successor))

if len(pair_nodes) == 0:
# If there are no pair of tied memits, then either no memits are needed
# to break a tie (i.e. all next_actions are the same) or the first memit
# breaks a tie (i.e. memory 1)
next_action_set = set()
for trans in transition_iterator(transitions):
next_action_set.add(trans.next_action)
if len(next_action_set) == 1:
return 0
return 1

# Get next_action for each memit. Used to decide if they are in conflict,
# because we only have undecidability if next_action doesn't match.
next_action_by_memit = dict()
for trans in transition_iterator(transitions):
for in_action in incoming_action_by_state[trans.state]:
memit_key = Memit(in_action, trans.state,
trans.last_opponent_action)
next_action_by_memit[memit_key] = trans.next_action

# Calculate the longest path.
record = 0
for pair in pair_nodes:
if next_action_by_memit[pair[0]] != next_action_by_memit[pair[1]]:
# longest_path is the longest chain of tied states. We add one to
# get the memory length needed to break all ties.
path_length = longest_path(pair_edges, pair) + 1
if record < path_length:
record = path_length
return record


class SimpleFSM(object):
Expand Down Expand Up @@ -124,7 +337,7 @@ class Fortress3(FSMPlayer):

name = "Fortress3"
classifier = {
"memory_depth": 3,
"memory_depth": 2,
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand Down Expand Up @@ -161,7 +374,7 @@ class Fortress4(FSMPlayer):

name = "Fortress4"
classifier = {
"memory_depth": 4,
"memory_depth": 3,
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand Down Expand Up @@ -197,7 +410,7 @@ class Predator(FSMPlayer):

name = "Predator"
classifier = {
"memory_depth": 9,
"memory_depth": float("inf"),
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand Down Expand Up @@ -241,7 +454,7 @@ class Pun1(FSMPlayer):

name = "Pun1"
classifier = {
"memory_depth": 2,
"memory_depth": float("inf"),
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand All @@ -268,7 +481,7 @@ class Raider(FSMPlayer):

name = "Raider"
classifier = {
"memory_depth": 3,
"memory_depth": float("inf"),
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand Down Expand Up @@ -303,7 +516,7 @@ class Ripoff(FSMPlayer):

name = "Ripoff"
classifier = {
"memory_depth": 2,
"memory_depth": 3,
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand Down Expand Up @@ -336,7 +549,7 @@ class UsuallyCooperates(FSMPlayer):

name = "UsuallyCooperates"
classifier = {
"memory_depth": 2,
"memory_depth": float("inf"),
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand Down Expand Up @@ -367,7 +580,7 @@ class UsuallyDefects(FSMPlayer):

name = "UsuallyDefects"
classifier = {
"memory_depth": 2,
"memory_depth": float("inf"),
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand Down Expand Up @@ -398,7 +611,7 @@ class SolutionB1(FSMPlayer):

name = "SolutionB1"
classifier = {
"memory_depth": 3,
"memory_depth": 2,
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand Down Expand Up @@ -432,7 +645,7 @@ class SolutionB5(FSMPlayer):

name = "SolutionB5"
classifier = {
"memory_depth": 5,
"memory_depth": float("inf"),
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand Down Expand Up @@ -471,7 +684,7 @@ class Thumper(FSMPlayer):

name = "Thumper"
classifier = {
"memory_depth": 2,
"memory_depth": float("inf"),
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand All @@ -497,7 +710,7 @@ class EvolvedFSM4(FSMPlayer):

name = "Evolved FSM 4"
classifier = {
"memory_depth": 4,
"memory_depth": float("inf"),
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand Down Expand Up @@ -533,7 +746,7 @@ class EvolvedFSM16(FSMPlayer):

name = "Evolved FSM 16"
classifier = {
"memory_depth": 16, # At most
"memory_depth": float("inf"), # At most
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand Down Expand Up @@ -589,7 +802,7 @@ class EvolvedFSM16Noise05(FSMPlayer):

name = "Evolved FSM 16 Noise 05"
classifier = {
"memory_depth": 16, # At most
"memory_depth": float("inf"), # At most
gaffney2010 marked this conversation as resolved.
Show resolved Hide resolved
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand Down
Loading