-
Notifications
You must be signed in to change notification settings - Fork 10
/
cog_types.py
289 lines (233 loc) · 9.83 KB
/
cog_types.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
Cogstruction: Optimizing cog arrays in Legends of Idleon
Copyright (C) 2021 Michael P. Lane
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
"""
import itertools
import numpy as np
from constants import ONE_SIG_PROB, NUM_COGS_HORI, NUM_COGS_VERT
from coords import Coords
"""
- All cogs are instances of `Cog'.
- `Cog' has two immediate subclasses, `Character' and `Boost_Cog'. Characters are instances of `Character' and cogs with
adjacency bonuses are instances of `Boost_Cog'. All other cogs are instances of `Cog', but not of any subclass of `Cog'.
- Each `Cog' has an numpy ndarray of `strengths'. This ndarray estimates how much a `Cog' prefers a given coordinate. For
example, a `Right_Cog' might have low strength for coordinates on the far right side of the `Cog_Array'.
- The `strengths' always add to 1.
- The ndarray `strengths' is always instantiated uniformly (over non-empty coords) via the method
`Cog.instantiate_strengths(cog_array)'.
"""
class Cog:
def __init__(self, build_rate, flaggy_rate, exp_mult):
self.build_rate = build_rate
self.flaggy_rate = flaggy_rate
self.exp_mult = exp_mult
self.strengths = None
self.strength_start_value = None
self.average_obj = None
self.std_obj = None
def __str__(self):
return (
("Type: %s\n" % self.__class__.__name__) +
(("Build rate: %d\n" % self.build_rate) if self.build_rate > 0 else "") +
(("Flaggy rate: %d\n" % self.flaggy_rate) if self.flaggy_rate > 0 else "") +
(("Exp mult: %d%%\n" % (self.exp_mult * 100)) if self.exp_mult > 0 else "")
).strip()
"""
- Let X denote the random variable `obj_fxn(cog_array.instantiate_randomly())'.
- Let Y denote how much of X is due to `self'.
- This method estimates the expectation of Y, which is the first return value.
- It also estimates the standard deviation of Y, which is the second return value.
"""
def get_average_std_obj(self, cog_array=None, obj_fxn=None, samples_per_coord=1):
if not self.average_obj:
if not cog_array or not obj_fxn:
raise RuntimeError
objs = []
for coords,_ in cog_array:
for _ in range(samples_per_coord):
cog_array.move_all_to_spares().move_cog_from_spares(coords,self).randomize()
obj1 = obj_fxn(cog_array.move_all_to_spares().move_cog_from_spares(coords, self).randomize())
obj2 = obj_fxn(cog_array.move_cog_to_spares(coords))
objs.append(obj1-obj2)
self.average_obj = np.median(objs)
self.std_obj = (np.percentile(objs,100*(0.50+ONE_SIG_PROB)) - np.percentile(objs,100*(0.50-ONE_SIG_PROB)))/2
return self.average_obj, self.std_obj
"""
- Set each non-empty coordinate strength to 1/N, where N is the total number of non-empty coords of `cog_array'.
"""
def instantiate_strengths(self,cog_array):
self.strengths = np.zeros((NUM_COGS_HORI,NUM_COGS_VERT))
for coords,_ in cog_array:
self.strengths[coords.x,coords.y] = 1.0
self.strength_start_value = 1/np.sum(self.strengths)
self.strengths /= np.sum(self.strengths)
"""
- Multiply `self.strengths[coords.x,coords.y]' by `factor` and renormalize.
- If `max_factor is not None', then `factor' is set to `max_factor' if the former is larger and likewise `factor' is
set to `1/max_factor' if the former is smaller. This is to prevent spuriously large or small factors from dominating
strengths.
- If `max_multiplier is not None', then no non-zero entry of `self.strengths' is smaller than
`self.strength_start_value / max_multiplier' nor larger than `self.strength_start_value * max_multiplier'. This is to
prevent accumulations of spuriously large or small factors.
"""
def update_strength(self,coords,factor,max_factor=None,max_multiplier=None):
if max_factor:
if factor > max_factor:
factor = max_factor
elif factor < 1/max_factor:
factor = 1/max_factor
if max_multiplier:
max_str = max_multiplier * self.strength_start_value
min_str = self.strength_start_value / max_multiplier
stre = self.strengths[coords.x,coords.y]
if factor*stre > max_str:
self.strengths[coords.x, coords.y] = max_str
elif factor*stre < min_str:
self.strengths[coords.x, coords.y] = min_str
else:
self.strengths[coords.x, coords.y] = stre*factor
else:
self.strengths[coords.x, coords.y] *= factor
self.strengths /= np.sum(self.strengths)
def get_strength(self,coords):
return self.strengths[coords.x,coords.y]
def get_abbr(self):
return "O"
def get_max_oob_neighbors(self):
return 0
class Character(Cog):
def __init__(self, build_rate, flaggy_rate, exp_rate, name ="hahahaha"):
super().__init__(build_rate,flaggy_rate,0.0)
self.exp_rate = exp_rate
self.name = name
def get_abbr(self):
return "C"
def __str__(self):
return (
super().__str__() + "\n" +
("Name: %s\n" % self.name) +
("Exp rate: %d\n" % self.exp_rate)
).strip()
class Boost_Cog(Cog):
def __init__(self, build_rate, flaggy_rate, exp_mult, build_rate_boost, flaggy_rate_boost, flaggy_speed_boost, exp_boost):
super().__init__(build_rate, flaggy_rate, exp_mult)
self.build_rate_boost = build_rate_boost
self.flaggy_rate_boost = flaggy_rate_boost
self.flaggy_speed_boost = flaggy_speed_boost
self.exp_boost = exp_boost
def __str__(self):
return (
super().__str__() + "\n" +
(("Build rate boost: %d%%\n" % int(self.build_rate_boost*100)) if self.build_rate_boost > 0 else "") +
(("Flaggy rate boost: %d%%\n" % int(self.flaggy_rate_boost*100)) if self.flaggy_rate_boost > 0 else "") +
(("Flaggy speed boost: %d%%\n" % int(self.flaggy_speed_boost * 100)) if self.flaggy_speed_boost > 0 else "") +
(("Exp boost: %d%%\n" % int(self.exp_boost * 100)) if self.exp_boost > 0 else "")
).strip()
class Yang_Cog(Boost_Cog):
def get_influence(self,coords):
return map(
lambda t: coords + Coords(t[0], t[1]),
[(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1), (-2, 0), (2, 0), (0, -2), (0, 2)]
)
def get_abbr(self):
return "Y"
def get_max_oob_neighbors(self):
return 4
class X_Cog(Boost_Cog):
def get_influence(self,coords):
return map(
lambda t: coords + Coords(t[0], t[1]),
[(-1, -1), (-1, 1), (1, -1), (1, 1)]
)
def get_abbr(self):
return "X"
def get_max_oob_neighbors(self):
return 2
class Plus_Cog(Boost_Cog):
def get_influence(self,coords):
return map(
lambda t: coords + Coords(t[0], t[1]),
[(-1, 0), (1, 0), (0, -1), (0, 1)]
)
def get_abbr(self):
return "+"
def get_max_oob_neighbors(self):
return 2
class Left_Cog(Boost_Cog):
def get_influence(self,coords):
return map(
lambda t: coords + Coords(t[0], t[1]),
[(-2, -1), (-2, 0), (-2, 1), (-1, -1), (-1, 0), (-1, 1)]
)
def get_abbr(self):
return "<"
def get_max_oob_neighbors(self):
return 3
class Right_Cog(Boost_Cog):
def get_influence(self,coords):
return map(
lambda t: coords + Coords(t[0], t[1]),
[(2, -1), (2, 0), (2, 1), (1, -1), (1, 0), (1, 1)]
)
def get_abbr(self):
return ">"
def get_max_oob_neighbors(self):
return 3
class Up_Cog(Boost_Cog):
def get_influence(self,coords):
return map(
lambda t: coords + Coords(t[0], t[1]),
[(-1, 1), (0, 1), (1, 1), (-1, 2), (0, 2), (1, 2)]
)
def get_abbr(self):
return "^"
def get_max_oob_neighbors(self):
return 3
class Down_Cog(Boost_Cog):
def get_influence(self,coords):
return map(
lambda t: coords + Coords(t[0], t[1]),
[(-1, -1), (0, -1), (1, -1), (-1, -2), (0, -2), (1, -2)]
)
def get_abbr(self):
return "v"
def get_max_oob_neighbors(self):
return 3
class Row_Cog(Boost_Cog):
def get_influence(self,coords):
return map(
lambda t: coords + Coords(t[0],t[1]),
[(x,0) for x in itertools.chain(range(-NUM_COGS_HORI,0),range(1,NUM_COGS_HORI+1))]
)
def get_abbr(self):
return "-"
def get_max_oob_neighbors(self):
return NUM_COGS_HORI
class Col_Cog(Boost_Cog):
def get_influence(self,coords):
return map(
lambda t: coords + Coords(t[0],t[1]),
[(0,y) for y in itertools.chain(range(-NUM_COGS_VERT,0),range(1,NUM_COGS_VERT+1))]
)
def get_abbr(self):
return "|"
def get_max_oob_neighbors(self):
return NUM_COGS_VERT
class Omni_Cog(Boost_Cog):
def get_influence(self,coords):
return map(
lambda t: coords + Coords(t[0], t[1]),
[(-2, -2), (-2, 2), (2, -2), (2, 2)]
)
def get_abbr(self):
return "*"
def get_max_oob_neighbors(self):
return 2