Skip to content

Commit 022747b

Browse files
committed
First draft of stats usage.
refs #6.
1 parent 0831213 commit 022747b

File tree

5 files changed

+56
-19
lines changed

5 files changed

+56
-19
lines changed

src/AI/AlphaBeta.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ cachedScoreAB var params side dp alpha beta board = do
229229

230230
Nothing -> do
231231
(score, moves) <- Metrics.timed "ai.score.board" $ scoreAB var params side dp alpha beta board
232-
lift $ putAiCache params board dp side score moves var
232+
when (alpha == loose && beta == win) $
233+
lift $ putAiCache params board dp side score moves var
233234
return score
234235

235236
isTargetDepth :: DepthParams -> Bool
@@ -379,7 +380,7 @@ scoreAB var params side dp alpha beta board
379380
beta' = if maximize
380381
then beta
381382
else min beta best
382-
(score,_) <- scoreAB var params (opposite side) dp' alpha' beta' board'
383+
score <- cachedScoreAB var params (opposite side) dp' alpha' beta' board'
383384
$trace "{}| score for side {}: {}" (indent, show side, show score)
384385
pop
385386
best <- getBest

src/AI/AlphaBeta/Cache.hs

+21-6
Original file line numberDiff line numberDiff line change
@@ -176,22 +176,37 @@ lookupAiCache params board depth side handle = do
176176
return cached
177177
-- return $ Just $ CacheItemSide $ fixSign $ cisScore result
178178
Nothing -> do
179-
mbValue <- runStorage handle $ event "file lookup" $ lookupFile board depth side
180-
case mbValue of
181-
Nothing -> do
179+
(mbCached, mbStats) <- runStorage handle $ event "file lookup" $ lookupFile board depth side
180+
let mbStats' = join $ checkStats `fmap` mbStats
181+
case (mbCached, mbStats') of
182+
(Nothing, Nothing) -> do
182183
Metrics.increment "cache.miss"
183184
return Nothing
184-
Just value -> do
185+
(Nothing, Just stats) -> do
186+
Metrics.increment "cache.hit.stats"
187+
return $ Just $ CacheItemSide $ avg stats
188+
(Just score, Nothing) -> do
185189
Metrics.increment "cache.hit.file"
186-
putAiCache' params board depth side value handle
187-
return mbValue
190+
putAiCache' params board depth side (CacheItemSide score) handle
191+
return $ Just $ CacheItemSide score
192+
(Just _, Just stats) -> do
193+
Metrics.increment "cache.hit.stats"
194+
return $ Just $ CacheItemSide $ avg stats
188195

189196
where
190197
queueCleanup bc bk = return ()
191198
-- queueCleanup bc bk = do
192199
-- let key = (bc, bk)
193200
-- now <- liftIO $ getTime Monotonic
194201
-- liftIO $ atomically $ putCleanupQueue (aichCleanupQueue handle) key now
202+
203+
avg :: Stats -> Score
204+
avg s = statsSumScore s `div` statsCount s
205+
206+
checkStats :: Stats -> Maybe Stats
207+
checkStats s
208+
| statsCount s < 10 = Nothing
209+
| otherwise = Just s
195210

196211
lookupMemory :: (BoardCounts, BoardKey) -> Side -> Checkers (Maybe CacheItemSide)
197212
lookupMemory (bc, bk) side = Metrics.timed "cache.lookup.memory" $ do

src/AI/AlphaBeta/Persistent.hs

+30-9
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,21 @@ lookupFileB bstr = do
306306
else loop nextBlockNumber (B.tail bstr)
307307

308308

309-
lookupFile :: Board -> DepthParams -> Side -> Storage (Maybe CacheItemSide)
309+
-- | Returns: (cached result, stats
310+
lookupFile :: Board -> DepthParams -> Side -> Storage (Maybe Score, Maybe Stats)
310311
lookupFile board depth side = Metrics.timed "cache.lookup.file" $ do
311-
mbItem <- lookupFileB (encodeBoard board)
312-
case mbItem of
313-
Nothing -> return Nothing
314-
Just item -> case M.lookup depth (boardScores item) of
315-
Nothing -> return Nothing
316-
Just ci -> case side of
317-
First -> return $ ciFirst ci
318-
Second -> return $ ciSecond ci
312+
mbRecord <- lookupFileB (encodeBoard board)
313+
case mbRecord of
314+
Nothing -> return (Nothing, Nothing)
315+
Just record -> do
316+
let cached =
317+
case M.lookup depth (boardScores record) of
318+
Nothing -> Nothing
319+
Just ci -> case side of
320+
First -> cisScore `fmap` ciFirst ci
321+
Second -> cisScore `fmap` ciSecond ci
322+
stats = boardStats record
323+
return (cached, stats)
319324

320325
lookupStatsFile :: Board -> Storage (Maybe Stats)
321326
lookupStatsFile board = Metrics.timed "stats.lookup.file" $ do
@@ -489,3 +494,19 @@ checkDataFile path = withFile path ReadMode $ \file -> do
489494
record <- Data.Store.decodeIO bstr :: IO PerBoardData
490495
printf "Block #%d: data: %s\n" i (show record)
491496

497+
checkDataFile' :: FilePath -> IO ()
498+
checkDataFile' path = withFile path ReadMode $ \file -> do
499+
nBlocks <- readDataIO file :: IO DataBlockNumber
500+
forM_ [0 .. nBlocks - 1] $ \i -> do
501+
let start = fromIntegral $ calcDataBlockOffset i
502+
hSeek file AbsoluteSeek start
503+
size <- readDataIO file :: IO Word16
504+
when (size > 0) $ do
505+
bstr <- B.hGet file (fromIntegral size)
506+
record <- Data.Store.decodeIO bstr :: IO PerBoardData
507+
case boardStats record of
508+
Nothing -> return ()
509+
Just stats ->
510+
when (statsCount stats > 10) $
511+
printf "Block #%d: data: %s\n" i (show record)
512+

src/Learn.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ learnPdn ai@(AlphaBeta params rules eval) path = do
115115
forM_ (zip [1.. ] pdn) $ \(i, gameRec) -> do
116116
-- liftIO $ print pdn
117117
$info "Processing game {}/{}..." (i :: Int, n)
118-
doLearn' rules eval cache params gameRec
118+
doLearn rules eval cache params gameRec
119119
-- saveAiCache rules params cache
120120
return ()
121121

src/Main.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ main = do
4444
withLogContext (LogContextFrame [] (include defaultLogFilter)) $
4545
learnPdn ai path
4646

47-
["dump", path] -> checkDataFile path
47+
["dump", path] -> checkDataFile' path
4848

4949
-- ["test"] -> do
5050
-- let rules = russian

0 commit comments

Comments
 (0)