-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
206 lines (164 loc) · 6.24 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import kivy
# kivy requires this statement to be before other kivy imports
kivy.require('1.10.0')
from kivy.app import App
from kivy.clock import Clock
from kivy.graphics import Mesh, Color
from kivy.graphics.instructions import InstructionGroup
from kivy.properties import ObjectProperty, NumericProperty
from kivy.uix.scatter import ScatterPlane
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from itertools import chain
import numpy as np
import pickle
from env import BaseGrid, Env
from sarsa import Sarsa
class AgentWidget(Widget):
'''
Widget responsible for drawing an agent.
'''
pass
# TODO: Remove this class. For now only represent approximate plan for a
# project.
class Agent(object):
'''
A programmable agent to simulate individuals in the swarm.
An Agent should act in accordance with some goal, as collecting the most
food.
'''
def __init__(self, all_actions):
''' Initiates agent. '''
self.all_actions = all_actions
def act(self, state):
'''
Make a turn. Returns an action that agent wants to perform.
state should convey information about location and neighbors.
All agents move at the same time.
During the turn they can:
1) read information from the cell they are in (maybe, also from
adjacent cells)
2) put information into the cell they are in (maybe, also adjacent
cell)
3) move to the adjacent cell
4) pick up food from cell
5) put food into cell
The information they read and put should be simple.
'''
pass
class Grid(BaseGrid):
def __init__(self, canvas, mesh_mode, color, cell_size, pix_origin=None,
*args, **kwargs):
super().__init__(*args, **kwargs)
self.canvas = canvas
self.mesh_mode = mesh_mode
self.cell_size = cell_size
self.pix_origin = pix_origin
self.canvas_groups = {}
self.color = color
def _clean_key(self, key):
super()._clean_key(key)
self.canvas.remove(self.canvas_groups[key])
def __setitem__(self, key, cell):
ret = super().__setitem__(key, cell)
group = InstructionGroup()
if cell.food == 0:
group.add(self.color)
group.add(Mesh(vertices=self._mesh_vertices(cell),
indices=list(range(6)),
mode=self.mesh_mode))
else:
group.add(Color(0, 1, 0, 1))
group.add(Mesh(vertices=self._mesh_vertices(cell),
indices=list(range(6)),
mode='triangle_fan'))
self.canvas_groups[key] = group
self.canvas.add(group)
return ret
def _mesh_vertices(self, cell):
x, y = self.pixcenter(cell.q, cell.r)
return list(chain(*[
(float(x + np.cos(np.pi * (i + 0.5) / 3) * self.cell_size),
float(y + np.sin(np.pi * (i + 0.5) / 3) * self.cell_size),
0, 0) for i in range(6)]))
def pixel_to_hex(self, x, y):
'''
Converts pos in pixel coordinates with offset to (q, r) pair in
Axial coordinates.
'''
x -= self.pix_origin[0]
y -= self.pix_origin[1]
q = (x * np.sqrt(3) / 3 - y / 3) / self.cell_size
r = y * 2 / 3 / self.cell_size
return Grid.axial_round(q, r)
@staticmethod
def axial_round(q, r):
'''
Converting to Cube coordinates, rounding up to nearest integers and
resetting the component with biggest change so that x+y+z=0 stands.
'''
ccs = [q, -q - r, r]
ind = np.argmax([np.abs(np.rint(x) - x) for x in ccs])
ccs = [int(np.rint(x)) for x in ccs]
ccs[ind] = - ccs[(ind + 1) % 3] - ccs[(ind + 2) % 3]
return ccs[0], ccs[2]
def pixcenter(self, q, r):
''' Returns pixel coordinates of the center. '''
cx, cy = self.pix_origin
x = cx + self.cell_size * np.sqrt(3) * (q + r / 2)
y = cy + self.cell_size * 3 / 2 * r
return float(x), float(y)
class Field(ScatterPlane):
'''
This is the Field which will contain cells.
'''
agent_widget = ObjectProperty(None)
total_reward = NumericProperty(0)
def __init__(self, cell_size=25, *args, **kwargs):
super().__init__(*args, **kwargs)
self.cell_size = cell_size
# At the __init__ height and width, and consecutively center may be not
# established, yet due to layout logic.
Clock.schedule_once(self._init_after)
Clock.schedule_interval(self.update, 0.1)
def _init_after(self, dt):
''' Perform initializations after the layout is finalized. '''
self.env = Env()
# TODO: Move params to config file
with open('sarsa.pickle', 'rb') as fd:
self.sarsa = pickle.load(fd)
self.grid = Grid(self.canvas, 'line_loop', Color(), self.cell_size,
self.to_local(*self.center))
self.state = self.env.reset(self.grid)
self._place_agent(self.state.cell)
def _place_agent(self, cell):
self.agent_widget.center = self.grid.pixcenter(cell.q, cell.r)
# FIXME
for _ in self.grid.neighbors(cell.q, cell.r):
pass
def on_touch_down(self, touch):
super().on_touch_down(touch)
x, y = self.to_local(touch.x, touch.y)
q, r = self.grid.pixel_to_hex(x, y)
if (q, r) in self.grid:
print("Touched ({}, {}) in {}.".format(q, r, (x, y)))
print("env tvisited", self.env.tvisited[q, r])
print("state food", self.state.food)
else:
self.grid.init(q, r)
for _ in self.grid.neighbors(q, r):
pass
return True
# TODO: Shouldn't this feel better in SwarmApp?
def update(self, dt):
action = self.sarsa.policy(self.state, explore=False)
next_state, reward, done = self.env.step(action)
self.sarsa.adapt_policy(self.state, action, next_state, reward)
self.state = next_state
self.total_reward += int(reward)
self._place_agent(self.state.cell)
class SwarmApp(App):
def build(self):
return BoxLayout()
if __name__ == '__main__':
SwarmApp().run()