-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradius_one_agent_state.py
99 lines (86 loc) · 3.38 KB
/
radius_one_agent_state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import picking_cans_board
import termcolor
class RadiusOneAgentState():
def __init__(self, state):
self._state = state
@staticmethod
def AgentStateForCell(board, r, c):
# Contets of the 3x3 matrix returned to represent the current state of
# the agent. The current position of the agent is the cell labeled "4".
# | 0 | 1 | 2 |
# | 3 | 4 | 5 |
# | 6 | 7 | 8 |
radius = RadiusOneAgentState.VisibleRadius()
diameter = RadiusOneAgentState.VisibleDiameter()
state = [[None for _ in range(diameter)] for _ in range(diameter)]
for i in range(diameter):
for j in range(diameter):
state[i][j] = board.GetContents((r - radius) + i,
(c - radius) + j)
return RadiusOneAgentState(state)
@staticmethod
def AgentStateForBoardPosition(position):
len_cells = len(picking_cans_board.CELLS)
# Exponent of 2 for each board position relative to current position
# (where 4 is).
# | 0 | 1 | 2 |
# | 3 | 4 | 5 |
# | 6 | 7 | 8 |
diameter = RadiusOneAgentState.VisibleDiameter()
state = [[None for _ in range(diameter)] for _ in range(diameter)]
for i in range(diameter):
for j in range(diameter):
exponent = (diameter * i) + j
state[i][j] = (position / (len_cells ** exponent)) % len_cells
return RadiusOneAgentState(state)
@staticmethod
def VisibleRadius():
return 1
@staticmethod
def VisibleDiameter():
return (2 * RadiusOneAgentState.VisibleRadius()) + 1
@staticmethod
def NumberOfVisibleCells():
# The agent can view the numbered cells relative to its current
# location (cell labeled 4).
# | 0 | 1 | 2 |
# | 3 | 4 | 5 |
# | 6 | 7 | 8 |
return RadiusOneAgentState.VisibleDiameter()**2
@staticmethod
def NumberOfStates():
return (len(picking_cans_board.CELLS)**
RadiusOneAgentState.NumberOfVisibleCells())
def __str__(self):
output = []
color_for_board_contents_function = (
picking_cans_board.Board.ColorForBoardContents)
for row in self._state:
output.append([])
for cell in row:
if cell is not None:
output[-1].append(termcolor.colored(
" ", on_color=color_for_board_contents_function(cell)))
else:
output[-1].append(" ")
# Handle placing the separators.
output[-1] = (("|" if row[0] is not None else " ") +
"|".join(output[-1]) +
("|" if row[-1] is not None else " "))
return "\n".join(output)
def __int__(self):
# Exponent of 2 for each board cell relative to current cell
# (where 4 is).
# | 0 | 1 | 2 |
# | 3 | 4 | 5 |
# | 6 | 7 | 8 |
diameter = RadiusOneAgentState.VisibleDiameter()
output = 0
for i in range(diameter):
for j in range(diameter):
exponent = (diameter * i) + j
output += ((len(picking_cans_board.CELLS) ** exponent) *
self._state[i][j])
return output
def GetContents(self, r, c):
return self._state[r][c]