Skip to content

Commit c115ae1

Browse files
committed
Some documenttion.
refs #1.
1 parent 0d72ad5 commit c115ae1

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

src/AI/AlphaBeta.hs

+22-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
{-# LANGUAGE DeriveDataTypeable #-}
55
{-# LANGUAGE TemplateHaskell #-}
66
{-# LANGUAGE FlexibleInstances #-}
7+
8+
{-
9+
- This module contains an implementation of alpha-beta-pruning algorithm
10+
- with small improvements.
11+
-}
12+
713
module AI.AlphaBeta where
814

915
import Control.Monad
@@ -101,6 +107,7 @@ putMoves First board moves memo =
101107
putMoves Second board moves memo =
102108
memo {mmSecond = putBoardMap board moves (mmSecond memo)}
103109

110+
-- | Calculate score of the board
104111
doScore :: (GameRules rules, Evaluator eval) => rules -> eval -> AICacheHandle rules -> AlphaBetaParams -> Side -> DepthParams -> Board -> Checkers Score
105112
doScore rules eval var params side dp board =
106113
fixSign <$> evalStateT (cachedScoreAB var params side dp (-max_score) max_score board) initState
@@ -150,13 +157,15 @@ instance HasLogContext (StateT (ScoreState rules eval) Checkers) where
150157
put st'
151158
return result
152159

160+
-- | Calculate score of the board.
161+
-- This uses the cache. It is called in the recursive call also.
153162
cachedScoreAB :: forall rules eval. (GameRules rules, Evaluator eval)
154163
=> AICacheHandle rules
155164
-> AlphaBetaParams
156165
-> Side
157166
-> DepthParams
158-
-> Score
159-
-> Score
167+
-> Score -- ^ Alpha
168+
-> Score -- ^ Beta
160169
-> Board
161170
-> ScoreM rules eval Score
162171
cachedScoreAB var params side dp alpha beta board = do
@@ -179,6 +188,12 @@ cachedScoreAB var params side dp alpha beta board = do
179188
isTargetDepth :: DepthParams -> Bool
180189
isTargetDepth dp = dpCurrent dp >= dpTarget dp
181190

191+
-- | Increase current depth as necessary.
192+
-- If there is only 1 move currently possible, this can increase
193+
-- the target depth, up to dpMax.
194+
-- If there are a lot of moves possible, this can decrease the
195+
-- target depth, down to dpMin.
196+
-- Otherwise, this just increases dpCurrent by 1.
182197
updateDepth :: (Monad m, HasLogging m, MonadIO m) => Int -> DepthParams -> m DepthParams
183198
updateDepth nMoves dp
184199
| nMoves == 1 = do
@@ -195,17 +210,19 @@ updateDepth nMoves dp
195210
return $ dp {dpCurrent = dpCurrent dp + 1, dpTarget = target}
196211
| otherwise = return $ dp {dpCurrent = dpCurrent dp + 1}
197212

213+
-- | Calculate score for the board
198214
scoreAB :: forall rules eval. (GameRules rules, Evaluator eval)
199215
=> AICacheHandle rules
200216
-> AlphaBetaParams
201217
-> Side
202218
-> DepthParams
203-
-> Score
204-
-> Score
219+
-> Score -- ^ Alpha
220+
-> Score -- ^ Beta
205221
-> Board
206222
-> ScoreM rules eval (Score, [Move])
207223
scoreAB var params side dp alpha beta board
208224
| isTargetDepth dp = do
225+
-- target depth is achieved, calculate score of current board directly
209226
evaluator <- gets ssEvaluator
210227
let score0 = evalBoard evaluator First side board
211228
$trace " X Side: {}, A = {}, B = {}, score0 = {}" (show side, alpha, beta, score0)
@@ -216,6 +233,7 @@ scoreAB var params side dp alpha beta board
216233
$trace "{}V Side: {}, A = {}, B = {}" (indent, show side, alpha, beta)
217234
moves <- possibleMoves' board
218235

236+
-- this actually means that corresponding side lost.
219237
when (null moves) $
220238
$trace "{}| No moves left." (Single indent)
221239

src/AI/AlphaBeta/Cache.hs

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import Core.Parallel
3333
import AI.AlphaBeta.Types
3434
import AI.AlphaBeta.Persistent
3535

36+
-- | Prepare AI storage instance.
37+
-- This also contains Processor instance with several threads.
3638
loadAiCache :: GameRules rules
3739
=> (ScoreMoveInput rules -> Checkers (Move, Score))
3840
-> AlphaBeta rules
@@ -164,6 +166,8 @@ normalize bsize (bc,bk,side) =
164166
then (bc', bk', opposite side)
165167
else (bc, bk, side)
166168

169+
-- | Look up for item in the cache. First lookup in the memory,
170+
-- then in the file (if it is open).
167171
lookupAiCache :: GameRules rules => AlphaBetaParams -> Board -> Int -> Side -> AICacheHandle rules -> Checkers (Maybe CacheItemSide)
168172
lookupAiCache params board depth side handle = do
169173
-- let bsize = boardSize (aichRules handle)
@@ -215,6 +219,9 @@ lookupAiCache params board depth side handle = do
215219
Second -> return $ ciSecond item
216220
else return Nothing
217221

222+
-- | Put an item to the cache.
223+
-- It is always writen to the memory,
224+
-- and it is writen to the file if it is open.
218225
putAiCache' :: GameRules rules => AlphaBetaParams -> (BoardCounts,BoardKey) -> Int -> Side -> StorageValue -> AICacheHandle rules -> Checkers ()
219226
putAiCache' params (bc,bk) depth side sideItem handle = do
220227
let bsize = boardSize (aichRules handle)

src/AI/AlphaBeta/Types.hs

+8-4
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ instance Default AlphaBetaParams where
4747
, abCombinationDepth = 8
4848
}
4949

50+
-- Calculation depth parameters
5051
data DepthParams = DepthParams {
51-
dpTarget :: Int
52-
, dpCurrent :: Int
53-
, dpMax :: Int
54-
, dpMin :: Int
52+
dpTarget :: Int -- ^ Target depth: how deep we currently want to calculate the tree
53+
, dpCurrent :: Int -- ^ Currently achieved depth
54+
, dpMax :: Int -- ^ Maximum allowed depth
55+
, dpMin :: Int -- ^ Minimum allowed depth
5556
}
5657
deriving (Eq, Show)
5758

@@ -104,6 +105,8 @@ data Locks = Locks {
104105
, lBlockLocks :: TVar (M.Map IndexBlockNumber RWL.RWLock)
105106
}
106107

108+
-- | Handle to the instance of AI storage
109+
-- and related structures
107110
data AICacheHandle rules = AICacheHandle {
108111
aichRules :: rules
109112
, aichData :: TVar (AICache rules)
@@ -118,6 +121,7 @@ type WriteQueue = TChan (BoardKey, Int, Side, StorageValue)
118121

119122
type CleanupQueue = TVar (PQ.HashPSQ QueueKey TimeSpec ())
120123

124+
-- | File handle
121125
data FHandle = FHandle {
122126
fhOffset :: FileOffset
123127
, fhHandle :: Fd

0 commit comments

Comments
 (0)