-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.py
199 lines (173 loc) · 6.07 KB
/
kernel.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
import math
import numba
import sqlite3
import numpy as np
#import functools
#for nXn=>mXm where n>m
patience = 0.8
FIRST_HAND = 1
LAST_HAND = -1
SELF=1
ENEMY=-1
INIT_MAX_UTIL = -100
INIT_MIN_UTIL = 100
INIT_POLICY = -100
def pick_slice(target,index,sliceSize):
result = []
shift = 0
targetSize = int(math.sqrt(len(target)))
for _ in range(sliceSize):
result+=target[index+shift:index+shift+sliceSize]
shift+=targetSize
return result
def num_of_slice_in_1D(target,slice):
targetSize = int(math.sqrt(len(target)))
return targetSize - slice +1
def size_of_target(target):
return int(math.sqrt(len(target)))
def is_target_empty(target):
return sum([0 if elem == 0 else 1 for elem in target]) == 0
def is_target_full(target):
return (0 in target)
def check_row(target):
step = size_of_target(target)
temp = [sum(target[i: i+step]) for i in range(0,len(target),step)]
result = (False,None)
if (size_of_target(target) in temp):
result = (False,1)
elif (-size_of_target(target) in temp):
result = (False,-1)
elif (0 in target):
result = (True,0)
else:
result = (False,0)
return result
def check_column(target):
step = size_of_target(target)
[temp,*rest] = [target[i: i+step] for i in range(0,len(target),step)]
for i in range(len(temp)):
for line in rest:
temp[i]+=line[i]
result = (False,0)
if (size_of_target(target) in temp):
result = (False,1)
elif (-size_of_target(target) in temp):
result = (False,-1)
elif (0 in target):
result = (True,0)
else:
result = (False,0)
return result
def check_diagnal(target):
size = size_of_target(target)
temp = sum([target[i*size_of_target(target)+i] for i in range(size)])
result = (False,0)
if temp == size:
result = (False,1)
elif temp == -size:
result = (False,-1)
elif 0 in target:
result = (True, 0)
else:
result = (False,0)
return result
def check_rev_diagnal(target):
size = size_of_target(target)
temp = sum([target[i*size_of_target(target)+(size - i - 1)] for i in range(size)])
result = (False,0)
if temp == size:
result = (False,1)
elif temp == -size:
result = (False,-1)
elif 0 in target:
result = (True, 0)
else:
result = (False,0)
return result
def terminal_state_check(target):
(is_playable,winner) = check_row(target)
if winner == 0 and is_playable:
(is_playable,winner) = check_column(target)
elif winner == 0 and is_playable:
(is_playable, winner) = check_diagnal(target)
elif winner == 0 and is_playable:
(is_playable, winner) = check_rev_diagnal(target)
return (is_playable,winner)
#r=0
def is_action_avalaible(state,action):
return state[action] == 0
def apply_policy(state,policy,label):
result = [s for s in state]
if is_action_avalaible(state,policy):
result[policy] = label
return result
def generate_policy(target):
return [policy for policy in range(len(target)) if target[policy] == 0]
#@numba.jit(nopython=True)
def best_policy_and_util(board, label,depth = 1,alpha = -math.inf,beta = math.inf,synmmatric_optimization = None):
possible_policies = generate_policy(board)
best_policy = INIT_POLICY
#print(depth,"invoked best")
state_pool =
(has_empty, winner) = terminal_state_check(board)
max_util = INIT_MAX_UTIL
if (has_empty and winner == 0):
for policy in possible_policies:
if (alpha<beta):
image_board = apply_policy(board, policy, label)
(_, r) = worst_policy_and_util(image_board, -label, depth + 1,alpha,beta)
alpha = r if alpha<r else alpha
if (r > max_util):
best_policy = policy
max_util = r
else:
max_util = winner * 10
return (best_policy, max_util)
"""
Min method
"""
#@numba.jit(nopython=True)
def worst_policy_and_util(board, label,depth = 1,alpha = -math.inf,beta = math.inf,synmmatric_optimization = None):
possible_policies = generate_policy(board)
worst_policy = INIT_POLICY
#print(depth,"invoked worst")
(has_empty, winner) = terminal_state_check(board)
min_util = INIT_MIN_UTIL
if (has_empty and winner == 0):
for policy in possible_policies:
if (alpha<beta):
image_board = apply_policy(board, policy, label)
(_, temp_util) = best_policy_and_util(image_board, -label, depth + 1,alpha,beta)
beta = temp_util if temp_util<beta else beta
if (temp_util < min_util):
worst_policy = policy
min_util = temp_util
else:
min_util = winner * 10
return (worst_policy, min_util)
def policy_generalization(size_of_source,size_of_target,global_index,local_index):
return (int(local_index/size_of_target)+int(global_index/size_of_source))*size_of_source+(local_index%size_of_target+global_index%size_of_source)
def policy2D(index,size):
return (int(index/size),index%size)
def policy1D(policy,size):
(dim1,dim2) = policy
return dim1*size+dim2
def policy_rotate_90_deg(policy,size):#90deg
(dim2,dim1) = policy2D(policy,size)
return dim1*size+dim2
def policy_rotate_180_deg(policy,size):#90deg
(dim1,dim2) = policy2D(policy,size)
return (dim1)*size+size-dim2-1
def policy_rotate_270_deg(policy,size):#90deg
(dim2,dim1) = policy2D(policy,size)
return (size-dim1-1)*size+dim2
def symatric_state_inclusive(state):
size = int(len(state)**0.5)
return {0:state,
90:list(np.rot90(np.reshape(state,(size,size)),k=1).flatten()),
180:list(np.rot90(np.reshape(state,(size,size)),k=2).flatten()),
270:list(np.rot90(np.reshape(state,(size,size)),k=3).flatten())}
#print (best_policy_and_util([1,1,1,0,0,-1,0,0,-1,0,0,0,-1,0,0,-1,0,0,0,0,0,0,0,0,0],1))
#print (policy_generalization(5,3,12,4))
#print (policy_rotate_180_deg(6,5))
print (symatric_state_inclusive([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]))