-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_typecombrank.py
158 lines (152 loc) · 5.9 KB
/
task_typecombrank.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
from typebase import *
from utils import *
class handlers:
def handler1(tc, weights0):
tc1 = TypeComb(tc)
weight = 0
for tc2 in weights0:
tc2 = TypeComb(tc2)
id2 = tc2.ID
coeff_def = tc1.getcoeff(tc2)
if coeff_def < -2:
weight += 4 * weights0[id2]
if coeff_def == -2:
weight += 3 * weights0[id2]
if coeff_def == -1:
weight += 2 * weights0[id2]
if coeff_def == 1:
weight -= 2 * weights0[id2]
if coeff_def == 2:
weight -= 4 * weights0[id2]
coeff_off = tc2.getcoeff(tc1)
if coeff_off < -2:
weight -= 3 * weights0[id2]
if coeff_off == -2:
weight -= 2 * weights0[id2]
if coeff_off == -1:
weight -= 1.5 * weights0[id2]
if coeff_off == 1:
weight += 1.5 * weights0[id2]
if coeff_off == 2:
weight += 2 * weights0[id2]
return tc1.ID, weight
def handler2(tc, weights0):
tc1 = TypeComb(tc)
weight_def, weight_off = 0, 0
for tc2 in weights0:
tc2 = TypeComb(tc2)
id2 = tc2.ID
coeff_def = tc1.getcoeff(tc2)
if coeff_def < -2:
weight_def += 4 * weights0[id2][1]
if coeff_def == -2:
weight_def += 3 * weights0[id2][1]
if coeff_def == -1:
weight_def += 2 * weights0[id2][1]
if coeff_def == 1:
weight_def -= 2 * weights0[id2][1]
if coeff_def == 2:
weight_def -= 4 * weights0[id2][1]
coeff_off = tc2.getcoeff(tc1)
if coeff_off < -2:
weight_off -= 3 * weights0[id2][0]
if coeff_off == -2:
weight_off -= 2 * weights0[id2][0]
if coeff_off == -1:
weight_off -= 1.5 * weights0[id2][0]
if coeff_off == 1:
weight_off += 1.5 * weights0[id2][0]
if coeff_off == 2:
weight_off += 2 * weights0[id2][0]
return tc1.ID, (weight_def, weight_off)
_handlers = [handler1, handler2]
def get(version):
return handlers._handlers[version - 1]
class normalizers:
def normalizer1(weights: dict):
weights_min = min(weights.values())
weights = {k: (v - weights_min) for k, v in weights.items()}
weights_mul = len(weights) / sum(weights.values())
weights = {k: (v * weights_mul) for k, v in weights.items()}
return weights
def normalizer2(weights: dict):
weights_def: dict = normalizers.normalizer1({k: v[0] for k, v in weights.items()})
weights_off: dict = normalizers.normalizer1({k: v[1] for k, v in weights.items()})
weights = {}
for k, v in weights_def.items():
weights[k] = (v, weights_off[k])
return weights
_normalizers = [normalizer1, normalizer2]
def get(version):
return normalizers._normalizers[version - 1]
class distancers:
def distancer1(weights0: dict, weights1: dict):
return sum(abs(w0 - w1) for w0, w1 in zip(weights0.values(), weights1.values()))
def distancer2(weights0: dict, weights1: dict):
return sum(abs(w0[0] - w1[0]) + abs(w0[1] - w1[1]) for w0, w1 in zip(weights0.values(), weights1.values()))
_distancers = [distancer1, distancer2]
def get(version):
return distancers._distancers[version - 1]
class postprocessors:
def postprocessor1(res):
return res
def postprocessor2(res: dict):
return {k: (v[0]*v[0] + v[1]*v[1], v[0] + v[1], v[0], v[1]) for k, v in res.items()}
_postprocessors = [postprocessor1, postprocessor2]
def get(version):
return postprocessors._postprocessors[version - 1]
@cache('typecombrank', lambda n, version=2, **kwargs: f"{n}_v{version}")
def BestTypeCombs(n, version=2, multiProcessing=False):
weights0 = {}
if version == 1:
weights0 = {TypeComb(tc).ID: 1 for tc in TYPECOMBS(n)}
if version == 2:
weights0 = {TypeComb(tc).ID: (1, 1) for tc in TYPECOMBS(n)}
for i in range(1000):
weights1 = dict(typecomb_looper(n, handlers.get(version), weights0, multiProcessing=multiProcessing))
weights1 = normalizers.get(version)(weights1)
distance = distancers.get(version)(weights0, weights1)
LOGGER.log(f"iteration {i}, distance = {distance}")
if distance < 0.01:
weights1 = postprocessors.get(version)(weights1)
return weights1
weights0 = weights1.copy()
@cache('typerank', lambda n, BestTypeCombsVersion=2: f"{n}_v{BestTypeCombsVersion}")
def BestType(n, BestTypeCombsVersion=2):
weights: dict[str, any] = BestTypeCombs(n, version=BestTypeCombsVersion)
res = {}
for t in TYPES:
res[t] = 0
for tc, w in weights.items():
if BestTypeCombsVersion == 2:
w = w[0]
tt = tc.split(',')
for t in tt:
res[t] += w
return res
def BestNthMove(*others):
weights = BestTypeCombs(2, version=2)
res = {}
for nth in TYPES:
res[nth] = 0
tc1 = TypeComb((*others, nth))
for tc2 in weights:
coeff = TypeComb(tc2).getcoeff(tc1)
if coeff < -2:
res[nth] -= 3 * weights[tc2][2]
if coeff == -2:
res[nth] -= 2 * weights[tc2][2]
if coeff == -1:
res[nth] -= 1.5 * weights[tc2][2]
if coeff == 1:
res[nth] += 1.5 * weights[tc2][2]
if coeff == 2:
res[nth] += 2 * weights[tc2][2]
return res
def tasks():
printDict(BestTypeCombs(2, version=1))
printDict(BestTypeCombs(2, version=2))
printDict(BestType(2, BestTypeCombsVersion=1))
printDict(BestType(2, BestTypeCombsVersion=2))
if __name__ == '__main__':
printDict(BestNthMove(GROUND, ICE, ROCK))