-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCTS.hs
454 lines (377 loc) · 13.8 KB
/
MCTS.hs
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
#ifdef ENGINE
module Main (main) where
import AEI
#else
module MCTS
( MMTree(..)
, MCTSTables
, TreeNode(..)
, newSearch -- :: Int -> IO SearchEngine
, constructMove -- :: MCTSTables -> MMTree -> Int -> IO DMove
, improveTree -- :: MCTSTables -> MMTree -> Int -> IO Double
, descendByUCB1 -- :: MCTSTables -> MMTree -> Int -> IO MMTree
, nodeValue -- :: MMTree -> IO Double
, nodeVisitCount -- :: MMTree -> IO Int
, nodeTreeNode -- :: MMTree -> IO TreeNode
, newHTables -- :: Int -> IO MCTSTables
, leaf -- :: TreeNode
, isLeaf -- :: Int -> TreeNode -> Bool
) where
import AEI (SearchEngine)
#endif
import Control.Applicative ((<$>))
import Control.Arrow ((***))
import Control.Concurrent.MVar
import Control.Monad (foldM, void, when)
import Data.Bits
import Data.Int (Int32)
import Data.List (sortBy)
import Data.Maybe (fromMaybe)
import Bits.BitRepresentation
import Eval.BitEval
import Eval.MonteCarloEval
import Hash
import Helpers (changeMVar)
import Computation
#ifdef VERBOSE
import System.IO (hFlush, stdout)
#endif
#ifdef ENGINE
main :: IO ()
main = runAEIInterface newSearch
#endif
-- | Mini-Max Tree representation
data MMTree = MT { board :: !Board
, movePhase :: !MovePhase
, treeNode :: MVar TreeNode
, step :: DStep
}
data TreeNode = Node { children :: ![MMTree] -- ^ steps from this
, value :: !Double -- ^ actual value of node
, visitCount :: !Int
}
iNFINITY' :: Double
iNFINITY' = 0.9
thresholdMaternity, thresholdCacheing, cachedCount, virtualVisits :: Int
thresholdMaternity = 5
thresholdCacheing = 50
cachedCount = 6
virtualVisits = 4
newSearch :: Int -> IO SearchEngine
newSearch = return . search
-- |
search :: Int -- ^ table size
-> Board -- ^ starting position
-> MVar (DMove, String) -- ^ best results to store here
-> IO ComputationToken
search tableSize b mv = do
newLeaf <- newMVar leaf
tables <- newHTables tableSize
let tree = MT { board = b
, movePhase = (mySide b, 0)
, treeNode = newLeaf
, step = dPass
}
#if CORES > 1
let c = CORES
newMVar =<< mapM (\_ -> (forkIO $ search' tree tables mv))
[1 .. c :: Int]
#else
search' tree tables mv
#endif
search' :: MMTree -> MCTSTables -> MVar (DMove, String) -> IO ()
search' mt tables mvar = do
void $ improveTree tables mt 0
count <- nodeVisitCount mt
when (count `mod` 10 == 0) $ do
move <- constructMove tables mt 0
score <- (/ fromIntegral count) <$> nodeValue mt
changeMVar mvar (const (move, show score ++ " " ++ show count))
#ifdef VERBOSE
when (count `mod` VERBOSE == 0) $
putStrLn ("info actual " ++ show (move,score))
>> hFlush stdout
#endif
search' mt tables mvar
constructMove :: MCTSTables -> MMTree -> Int -> IO DMove
constructMove _ _ 4 = return []
constructMove tables mt n = do
tn <- nodeTreeNode mt
if player mt /= mySide (board mt) || isLeaf n tn
then return []
else do
chs <- mapM vcPair (children tn)
let best = fst . head $ sortBy cmp chs
(step best :) <$> constructMove tables best (n+1)
where
cmp x y = compare (snd y) (snd x)
vcPair mt' = do vc <- nodeVisitCount mt'
return (mt', vc)
leaf :: TreeNode
leaf = Node { children = []
, value = 0
, visitCount = virtualVisits
}
-- Depending on visitCount a Node could be mature, old or an ordinary leaf:
isLeaf :: Int -> TreeNode -> Bool
isLeaf depth (Node { visitCount = vc }) = vc < thresholdMaternity + depth
isMature :: Int -> TreeNode -> Bool
isMature depth tn = not $ isLeaf (depth-1) tn
isOld :: Int -> TreeNode -> Bool
isOld _ (Node { visitCount = vc }) = vc >= thresholdCacheing
-- | Run one iteration of the algorithm.
improveTree :: MCTSTables
-> MMTree
-> Int -- ^ depth
-> IO Double -- ^ value given by playout simulation
improveTree tables mt !depth = do
tn <- nodeTreeNode mt
if isLeaf depth tn
then do
-- Value given by Monte Carlo simulation
val <- normaliseValue
<$> getValueByMC (board mt) (movePhase mt) (step mt)
modifyMVar_ (treeNode mt) $ \tn' ->
if isMature depth tn' && isLeaf depth tn'
then -- leaf expansion
expandNode tables mt val depth tn'
else return
$ improveTreeNode val 0 tn'
improveStep tables (step mt) val
return val
else do
if null $ children tn
-- immobilization
then return $ value tn
else do
-- worsten node, to not be used by other threads
changeMVar (treeNode mt) worstenTreeNode
-- Find best node
node <- descendByUCB1 tables mt depth
-- And traverse throught it
impr <- improveTree tables node (depth + 1)
-- Backpropagate result to node and history heuristic
changeMVar (treeNode mt) $ improveTreeNode impr wORSTEN
improveStep tables (step mt) impr
return impr
-- TODO delete worsting or tune this value
wORSTEN :: Int
wORSTEN = 15
improveTreeNode :: Double -> Int -> TreeNode -> TreeNode
improveTreeNode impr w tn@(Node { value = val, visitCount = vc }) =
tn { value = val + impr
, visitCount = vc + 1 - w
}
worstenTreeNode :: TreeNode -> TreeNode
worstenTreeNode tn@(Node { visitCount = vc }) =
tn { visitCount = vc + wORSTEN }
expandNode :: MCTSTables -> MMTree -> Double -> Int -> TreeNode
-> IO TreeNode
expandNode tables mt impr depth (Node { value = val, visitCount = vc }) = do
-- generate steps
chls <- mapM (leafFromStep tables brd mp depth) steps
-- if is immobilised evaluate node
newVal <- if null steps
then normaliseValue <$> evalImmobilised (board mt) pl
else return $ val + impr
return Node { children = chls
, value = newVal
, visitCount = vc + 1
}
where
steps = generateSteps brd mp
brd = board mt
mp@(pl,_) = movePhase mt
leafFromStep :: MCTSTables -> Board -> MovePhase -> Int -> DStep
-> IO MMTree
leafFromStep tables brd mp depth s = do
fromTT <- getHash tt index
-- Do we have this node in transposition tables?
tn <- case fromTT of
Just tn -> return tn
Nothing -> do
-- insert new item to TT
tn <- newMVar leaf
addHash tt index tn
return tn
return $ MT
{ board = brd'
, movePhase = mp'
, treeNode = tn
, step = s
}
where
index@(brd', _, mp') =
(makeDStep' brd s, depth, stepInMove mp s)
tt = ttTable tables
-- U C T f o r m u l a
type ParentToken = (MCTSTables, Int, Int, Int, MovePhase, DStep, Board)
-- | Immobilised position cause fail.
-- Third argument is depth of the node.
descendByUCB1 :: MCTSTables
-> MMTree -- ^ the node
-> Int -- ^ depth
-> IO MMTree -- ^ nodes 'ucb'-best child
descendByUCB1 tables mt depth = do
tn <- nodeTreeNode mt
let chs = children tn
quant = player mt <#> Gold
mp@(_,sc) = movePhase mt
vc = visitCount tn
token = ( tables
, vc
, quant
, depth+1
, mp
, if sc == 0 then dPass
else step mt
, board mt
)
if isOld depth tn
then cachedByUCB1 token vc mt chs
else descendByUCB1' token chs
-- | If node is old, we order its children by their UCB values and then when
-- we want find its best child, we look only in first 'cachedCount'
-- children.
cachedByUCB1 :: ParentToken
-> Int -- ^ visitCount of the node
-> MMTree -- ^ the node
-> [MMTree] -- ^ nodes children
-> IO MMTree
cachedByUCB1 token count mt mss
| count `mod` thresholdCacheing == 0 = do
-- recache children nodes
mssVals <- mapM (valueUCB token) mss
let mssSorted = map fst $ sortBy cmp $ zip mss mssVals
changeMVar (treeNode mt) $ \tn -> tn { children = mssSorted }
-- in head is the actual best
return $ head mssSorted
| otherwise = descendByUCB1' token $ take cachedCount mss
where
-- reverse compare for sortBy to create decreasing list
cmp x y = compare (snd y) (snd x)
-- | second argument have to be not empty
descendByUCB1' :: ParentToken -> [MMTree] -> IO MMTree
descendByUCB1' token (hMms:mms) = do
valHMms <- valueUCB token hMms
fst <$> foldM (accumUCB token) (hMms, valHMms) mms
descendByUCB1' _ _ = error "Given list is empty in descendByUCB1'."
accumUCB :: ParentToken -> (MMTree, Double) -> MMTree
-> IO (MMTree, Double)
accumUCB token (best, bestValue) mt = do
nodeVal <- valueUCB token mt
if nodeVal > bestValue
then return (mt, nodeVal)
else return (best, bestValue)
valueUCB :: ParentToken -> MMTree -> IO Double
valueUCB (tables,count,quant,depth,mp,st,brd) mt = do
tn <- nodeTreeNode mt
let nb = fromIntegral $ visitCount tn
let vl = value tn
let stepVal = evalStep brd mp st (step mt)
#ifndef noHH
histValPair <- getHash (hhTable tables) $ step mt
let histVal = fromMaybe 0 $ uncurry (/) <$> histValPair
#endif
case tn of
Node { visitCount = 0 } ->
return $ iNFINITY' + stepVal/(nb+1)
_ | null (children tn) && not (isLeaf depth tn) ->
nodeValue mt -- immobilised
_ -> return $ (quant' * vl / nb) + 0.01 * sqrt (log cn / nb)
+ stepVal / nb
#ifndef noHH
+ (quant' * histVal * 1.1) / nb
#endif
where
cn = fromIntegral count
quant' = fromIntegral quant
normaliseValue :: Int -> Double
normaliseValue v = 2 / (1 + exp (-0.0003 * fromIntegral v)) - 1
player :: MMTree -> Player
player = fst . movePhase
nodeValue :: MMTree -> IO Double
nodeValue mt = value <$> readMVar (treeNode mt)
nodeVisitCount :: MMTree -> IO Int
nodeVisitCount mt = visitCount <$> readMVar (treeNode mt)
nodeTreeNode :: MMTree -> IO TreeNode
nodeTreeNode = readMVar . treeNode
-- Implementation of Transposition table
type TTable = HTable (MVar TreeNode) TTObject (Board, Int, MovePhase)
data TTObject = TTO { board0 :: Board
, treeNode0 :: MVar TreeNode
, depth0 :: Int
, phase :: MovePhase
}
newTT :: Int -> IO TTable
newTT tableSize = do
ht <- newHT id
return HT
{ table = ht
, getEntry = treeNode0
, isValid = ttIsValid
, key = ttKey ts
, saveEntry = ttSaveEntry
}
where
ts = (fromIntegral tableSize) * (500000 `div` 200)
ttIsValid :: (Board, Int, MovePhase) -> TTObject -> Bool
ttIsValid (b,d,mp) e = phase e == mp
&& depth0 e == d
&& hash (board0 e) == hash b
&& board0 e == b
ttKey :: Int32 -> (Board, Int, MovePhase) -> Int32
ttKey tableSize (b, de, (pl,s)) = fromIntegral . (`mod` tableSize) $
fromIntegral (hash b) `xor` fromIntegral (playerToInt pl)
`xor` (fromIntegral s `shift` 4) `xor` (fromIntegral de `shift` 6)
ttSaveEntry :: (MVar TreeNode) -> (Board, Int, MovePhase) -> TTObject
ttSaveEntry tn (b, d, mp) =
TTO { board0 = b
, treeNode0 = tn
, depth0 = d
, phase = mp
}
-- Implementation of History heuristics table
type HHTable = HTable (Double,Double) HHObject DStep
data HHObject = HHO { hhValue :: (Double, Double)
, step0 :: DStep
}
newHH :: IO HHTable
newHH = do
ht <- newHT id
return HT
{ table = ht
, getEntry = hhValue
, isValid = \_ _ -> True
, key = dStepToInt
, saveEntry = hhSaveEntry
}
hhSaveEntry :: (Double, Double) -> DStep -> HHObject
hhSaveEntry val st = HHO { hhValue = val
, step0 = st
}
-- | Updates statistics for given DStep.
improveStep :: MCTSTables -> DStep -> Double -> IO ()
#ifndef noHH
improveStep tables ss val = do
ho <- getHash hhT ss
addHash hhT ss $
case ho of
Nothing -> (val, 1)
Just hho -> (+ val) *** (+ 1) $ hho
where
hhT = hhTable tables
#else
improveStep _ _ _ = return ()
#endif
-- Structure of Hash Tables
data MCTSTables = MCTSTables { ttTable :: TTable
, hhTable :: HHTable
}
newHTables :: Int -> IO MCTSTables
newHTables size = do
tt <- newTT size
hh <- newHH
return MCTSTables { ttTable = tt, hhTable = hh }