-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot_quadratic.pyx
69 lines (61 loc) · 2.61 KB
/
bot_quadratic.pyx
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
"""
Calculates a (multi-variable) quadratic polynomial over the state for each
action and chooses the one giving the highest value.
Assumes 4 actions.
"""
from cython import ccall, cclass, locals, returns
from bot_base cimport BaseBot
from interface cimport c_do_action, c_get_state
@cclass
class Bot(BaseBot):
@staticmethod
def shapes(steps, actions, features):
return {
'free': (actions,),
'state0l': (actions, features),
'state0q': (actions, features, features)
}
@ccall
@returns('void')
@locals(steps='int', step='int', action='int',
features='int', feature0='int', feature1='int',
free='float[4]', state0l='float[:, ::1]',
state0q='float[:, :, ::1]',
state0q0='float[:, :]', state0q1='float[:, :]',
state0q2='float[:, :]', state0q3='float[:, :]',
values='float[4]',
state0='float*', state0f0='float', state0f01='float')
def act(self, steps):
features = self.level['features']
free = self.params['free']
state0l = self.params['state0l']
state0q = self.params['state0q']
state0q0 = state0q[0]
state0q1 = state0q[1]
state0q2 = state0q[2]
state0q3 = state0q[3]
action = -1
for step in range(steps):
values = free[:]
state0 = c_get_state()
for feature0 in range(features):
state0f0 = state0[feature0]
values[0] += state0l[0, feature0] * state0f0
values[1] += state0l[1, feature0] * state0f0
values[2] += state0l[2, feature0] * state0f0
values[3] += state0l[3, feature0] * state0f0
for feature1 in range(features):
state0f01 = state0f0 * state0[feature1]
values[0] += state0q0[feature0, feature1] * state0f01
values[1] += state0q1[feature0, feature1] * state0f01
values[2] += state0q2[feature0, feature1] * state0f01
values[3] += state0q3[feature0, feature1] * state0f01
action = (((0 if values[0] > values[3] else 3)
if values[0] > values[2] else
(2 if values[2] > values[3] else 3))
if values[0] > values[1] else
((1 if values[1] > values[3] else 3)
if values[1] > values[2] else
(2 if values[2] > values[3] else 3)))
c_do_action(action)
self.last_action = action