-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.go
290 lines (267 loc) · 6.55 KB
/
kernel.go
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
290
package gmk
import (
"math"
)
const (
FIRSTHAND = 1
LASTHAND = -1
TIE = 0
SELF = 1
ENEMY = -1
)
/********************************board structure**********************************/
// the game Board
type Board struct {
board []int
size int
metaSize int
metaPosition int
}
//a piece of game board
type MetaBoard struct {
super *Board
board []int
size int
position int
}
/********************************help function**********************************/
func IndexDimIncrease(position int, size int) (int, int) {
verticalIndex := position / size
horizontalIndex := position % size
return verticalIndex, horizontalIndex
}
func IndexDimReduction(dim1 int, dim2 int, metric int) int {
return dim1*metric + dim2
}
func IsPolicyLigit(game Board, policy int) bool {
result := policy < len(game.board)
result = result && game.IsPolicyAvailable(policy)
return result
}
/********************************board function**********************************/
func BoardInit(size, metaSize int) Board {
b := Board{nil, size, metaSize, 0}
b.board = make([]int, size*size)
for index := range b.board {
b.board[index] = 0
}
return b
}
func (board *Board) PosiReshape() (int, int) {
verticalIndex := board.metaPosition / board.size
horizontalIndex := board.metaPosition % board.size
return verticalIndex, horizontalIndex
}
func (board *Board) NumOfMeta() int {
return (board.size - board.metaSize + 1) * (board.size - board.metaSize + 1)
}
func (board *Board) NextMeta() (bool, MetaBoard) {
columnIndex, rowIndex := board.PosiReshape()
var m MetaBoard
canProceed := true
if board.size-columnIndex >= board.metaSize {
m = MetaBoard{board,
make([]int, board.metaSize*board.metaSize),
board.metaSize,
board.metaPosition}
globalIndex := board.metaPosition
for i := 0; i < board.metaSize; i++ {
oldIndex := globalIndex
for j := 0; j < board.metaSize; j++ {
m.board[i*board.metaSize+j] = board.board[globalIndex]
globalIndex++
}
globalIndex = oldIndex + board.size
}
if board.size-rowIndex >= board.metaSize {
board.metaPosition++
} else {
board.metaPosition += board.metaSize
}
} else {
canProceed = false
}
return canProceed, m
}
func (this *Board) Apply(policy int, flag int) {
this.board[policy] = flag
}
func (this *Board) Cancel(policy int) {
this.board[policy] = 0
}
func (this *Board) IsPlaybale() (bool, int) {
canContinue := true
winner := 0
for i := 0; i < this.NumOfMeta() && canContinue; i++ {
_, m := this.NextMeta()
canContinue, winner = m.State()
}
return canContinue, winner
}
func (this *Board) IsEmpty() bool {
result := true
for i := 0; i < len(this.board); i++ {
result = (this.board[i] == 0)
}
return result
}
func (this *Board) Globalize(localPolicy int) int {
vertical, horizontal := IndexDimIncrease(localPolicy, this.metaSize)
metaVertical, metaHorizontal := IndexDimIncrease(this.metaPosition, this.size)
return (vertical+metaVertical)*this.size + (horizontal + metaHorizontal)
}
func (b *Board) ToString() string {
result := ""
for i := 0; i < b.size; i++ {
for j := 0; j < b.size; j++ {
if b.board[i*b.size+j] == 1 {
result += "O "
} else if b.board[i*b.size+j] == 1 {
result += "X "
} else {
result += "- "
}
}
result += "\n"
}
result += "\n"
return result
}
func (this *Board) IsPolicyAvailable(policy int) bool {
return this.board[policy] == 0
}
/********************************meta board function**********************************/
func (b *MetaBoard) EmptyPositions() []int {
result := make([]int, 0, b.size*b.size)
nextPosition := 0
for i := 0; i < len(b.board); i++ {
if b.board[i] == 0 {
result = append(result, i)
nextPosition++
}
}
return result
}
func (b *MetaBoard) State() (bool, int) {
hasEmpty := false
winner := 0
//check empty
for _, elem := range b.board {
if elem == 0 {
hasEmpty = true
}
}
for i := 0; i < b.size; i++ {
winner += b.board[i*b.size+i]
}
winner /= b.size
if winner == 0 {
for i := 0; i < b.size; i++ {
winner += b.board[i*b.size+(b.size-i-1)]
}
winner /= b.size
} else {
for i := 0; i < b.size && (winner == 0); i++ {
for j := 0; j < b.size; j++ {
winner += b.board[i*b.size+j]
}
winner /= b.size
}
if winner == 0 {
for i := 0; i < b.size && (winner == 0); i++ {
for j := 0; j < b.size; j++ {
winner += b.board[j*b.size+i]
}
winner /= b.size
}
}
}
return hasEmpty, winner
}
func (this *MetaBoard) Flip(flag int) {
if flag == -1 {
for i := 0; i < len(this.board); i++ {
this.board[i] /= flag
}
}
}
func (this *MetaBoard) Apply(policy int, flag int) {
this.board[policy] = flag
}
func (this *MetaBoard) Cancel(policy int) {
this.board[policy] = 0
}
func (this *MetaBoard) Localize(globalPolicy int) int {
vertical, horizontal := IndexDimIncrease(globalPolicy, this.super.size)
metaVertical, metaHorizontal := IndexDimIncrease(this.super.metaPosition, this.super.size)
return (vertical-metaVertical)*this.size + (horizontal - metaHorizontal)
}
func (this *MetaBoard) IsPolicyAvailable(policy int) bool {
return this.board[policy] == 0
}
//has to flip before call this method
func (this *MetaBoard) BestLocalPolicyAndUtil(flag int, decayRate float64) (int, float64) {
maxUtil := math.Inf(-1)
var bestPolicy int
var util float64
hasEmpty, winner := this.State()
if hasEmpty && winner == 0 {
policies := this.EmptyPositions()
var p int
var u float64
for _, policy := range policies {
this.Apply(policy, flag)
p, u = this.WorstLocalPolicyAndUtil(-flag, decayRate)
this.Cancel(policy)
}
if maxUtil < u {
bestPolicy = p
maxUtil = u
util = decayRate * u
}
} else {
util = float64(winner * 10)
}
return bestPolicy, util
}
func (this *MetaBoard) WorstLocalPolicyAndUtil(flag int, decayRate float64) (int, float64) {
minUtil := math.Inf(1)
var worstPolicy int
var util float64
hasEmpty, winner := this.State()
if hasEmpty && winner == 0 {
policies := this.EmptyPositions()
var p int
var u float64
for _, policy := range policies {
this.Apply(policy, flag)
p, u = this.BestLocalPolicyAndUtil(-flag, decayRate)
this.Cancel(policy)
}
if minUtil > u {
worstPolicy = p
minUtil = u
util = decayRate * u
}
} else {
util = float64(winner * 10)
}
return worstPolicy, util
}
func (b *MetaBoard) ToString() string {
result := ""
for i := 0; i < b.size; i++ {
for j := 0; j < b.size; j++ {
if b.board[i*b.size+j] == 1 {
result += "O "
} else if b.board[i*b.size+j] == 1 {
result += "X "
} else {
result += "- "
}
}
result += "\n"
}
//result += "\n"
return result
}