/
main.py
215 lines (193 loc) · 7.33 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
207
208
209
210
211
212
213
214
215
import copy
import distribution
import numpy
from config import *
# Optimize for minimal number time in expectation
class Optimizer(object):
def __init__(self, n, desired):
n -= 2
self.n = n
self.INF = 10000
self.calculator = distribution.Calculator()
self.distribution = {}
for slot in range(10):
self.distribution[slot] = {}
for t in range(n):
self.distribution[slot][t] = self.calculator.distribution(slot, t+3)
self.costs = [self.INF] * 10
self.desired = desired
for x in self.desired:
self.costs[x] = 0
self.p_encounter = ENCOUNTER_RATE / 256.0
self.base_distribution = [(ENCOUNTER_SLOTS[i+1] - ENCOUNTER_SLOTS[i]) / 256.0 for i in range(10)]
def eval(self, strats):
costs = []
coefficients = []
p_encounter = self.p_encounter
if len(strats) != 10:
raise Exception("Need to provide strats for all 10 encounter slots")
for i in range(10):
x = [0] * 10
cost = 1 / p_encounter
for j in range(10):
if j in self.desired:
continue
cost += self.base_distribution[j] * ENCOUNTER_TIME
x[j] += self.base_distribution[j]
strat = strats[i]
n = len(strat)
if n > self.n:
raise Exception("Strat %d with %d elements is too long, preprocessing only goes up to %d." % (i, n, self.n))
for k in reversed(range(n)):
if strat[k] == 0:
cost += 1
continue
dist = self.distribution[i][k]
s = sum(ENCOUNTER_TIME * dist[j] for j in range(10) if j not in self.desired)
cost = 1 + p_encounter * s + (1 - p_encounter) * cost
for j in range(10):
if j in self.desired:
continue
x[j] = p_encounter * dist[j] + (1 - p_encounter) * x[j]
costs.append(cost)
coefficients.append(x)
x = numpy.linalg.inv(numpy.identity(10) - numpy.matrix(coefficients))
out = [sum(x.item((i, j)) * costs[j] for j in range(10)) for i in range(10)]
return out
# Expected cost of an encounter from given distribution
def expected_cost(self, dist):
costs = self.costs
x = sum(dist[i] * (costs[i] + ENCOUNTER_TIME) for i in range(10) if i not in self.desired)
return x
def optimize_once(self, slot):
expected_costs = [self.expected_cost(self.distribution[slot][k]) for k in range(self.n)]
p_encounter = self.p_encounter
failure_cost = 1 / self.p_encounter + self.expected_cost(self.base_distribution)
strat = []
for k in reversed(range(self.n)):
no_encounter_cost = 1 + failure_cost
encounter_cost = 1 + p_encounter * expected_costs[k] + (1 - p_encounter) * failure_cost
failure_cost = min(no_encounter_cost, encounter_cost)
strat.append(int(encounter_cost < no_encounter_cost))
strat.reverse()
return strat, failure_cost
def optimize(self, n_iterations = 200):
strats, costs = [], []
for i in range(n_iterations):
strats, costs = [], []
last_costs = self.costs
for slot in range(10):
strat, cost = self.optimize_once(slot)
strats.append(strat)
costs.append(cost)
self.costs = copy.copy(costs)
for slot in self.desired:
self.costs[slot] = 0
dcosts = sum([(last_costs[i] - self.costs[i]) ** 2 for i in range(len(costs))])
if dcosts < 1e-6:
break
return strats, costs
# Optimize for maximum probability within a time threshold
class Maximizer(object):
def __init__(self, n, desired):
n -= 2
self.n = n
self.calculator = distribution.Calculator()
self.distribution = {}
for slot in range(10):
self.distribution[slot] = {}
for t in range(n):
self.distribution[slot][t] = self.calculator.distribution(slot, t)
self.desired = desired
self.p_encounter = ENCOUNTER_RATE / 256.0
self.probabilities = {}
self.base_distribution = [(ENCOUNTER_SLOTS[i+1] - ENCOUNTER_SLOTS[i]) / 256.0 for i in range(10)]
def probability(self, dist, steps_left):
if steps_left >= 0:
x = sum(dist[i] * (1 if i in self.desired else self.probabilities[steps_left][i]) for i in range(10))
else:
x = sum(dist[i] for i in self.desired)
return x
def maximize(self, steps_left):
n = self.n
p_encounter = self.p_encounter
out = []
p_desired = sum([ENCOUNTER_SLOTS[i+1] - ENCOUNTER_SLOTS[i] for i in self.desired]) / 256.0
boundary_p = 0
for s in range(steps_left + 1):
# Assume that after n steps, we know nothing about distribution and take every step in the grass
if s > n:
boundary_p = p_encounter * self.probability(self.base_distribution, s - n - 1 - ENCOUNTER_TIME) + (1 - p_encounter) * boundary_p
self.probabilities[s] = [0] * 10
strats = []
for i in range(10):
p = boundary_p
strat = []
for k in reversed(range(n)):
if k >= s:
strat.append(0)
continue
t = p_encounter * self.probability(self.distribution[i][k], s - k - 1 - ENCOUNTER_TIME) + (1 - p_encounter) * p
strat.append(int(t > p))
p = max(t, p)
self.probabilities[s][i] = p
strat.reverse()
strats.append(strat)
out.append(strats)
return out, self.probabilities[steps_left]
def print_array(arr):
def collapse(arr):
arr.append(-1)
first = None
last = None
out = []
s = 0
for x in arr:
if x != last:
if last is not None:
out.append(s)
if last is None:
s = 3
first = x
else:
s = 1
last = x
else:
s += 1
return (out, first)
arr, p = collapse(arr)
s = []
from termcolor import colored
for i in range(len(arr)):
if i % 2 == p:
s.append(colored(arr[i], 'blue', attrs=['bold']))
else:
s.append(colored(arr[i], 'red', attrs=['bold']))
s = ', '.join(s)
print '[%s]' % s
def test(steps):
steps -= 2
maximizer = Maximizer(100, DESIRED_SLOTS)
strats, p = maximizer.maximize(steps)
strats = strats[-1]
for i in range(10):
print ENCOUNTER_NAMES[i]
print_array(strats[i])
print ''
def main():
n = 100
if IS_YELLOW:
n = 150
optimizer = Optimizer(n, DESIRED_SLOTS)
strats, costs = optimizer.optimize()
for i in range(10):
print ENCOUNTER_NAMES[i]
print_array(strats[i])
print ''
if __name__ == '__main__':
import sys
import colorama
colorama.init()
if len(sys.argv) > 1:
DESIRED_SLOTS = map(int, sys.argv[1].split(','))
main()