-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeManagedSearch.nim
143 lines (125 loc) · 5.23 KB
/
timeManagedSearch.nim
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
import
types,
move,
position,
hashTable,
rootSearch,
evaluation,
utils,
anarchyParameters
import std/[
atomics,
threadpool,
times
]
type MoveTime = object
maxTime, approxTime: Duration
func calculateMoveTime(moveTime, timeLeft, incPerMove: Duration, movesToGo, halfmovesPlayed: int16): MoveTime =
doAssert movesToGo >= 0
let estimatedGameLength = 70
let estimatedMovesToGo = max(10, estimatedGameLength - halfmovesPlayed div 2)
var newMovesToGo = max(2, min(movesToGo, estimatedMovesToGo))
result.maxTime = min(initDuration(milliseconds = timeLeft.inMilliseconds div 5), moveTime)
result.approxTime = initDuration(milliseconds = clamp(
timeLeft.inMilliseconds div newMovesToGo, 0, int.high div 2) +
clamp(incPerMove.inMilliseconds, 0, int.high div 2))
if incPerMove.inSeconds >= 2 or timeLeft > initDuration(minutes = 2):
result.approxTime = (15 * result.approxTime) div 10
elif incPerMove.inMilliseconds < 200 and timeLeft < initDuration(seconds = 30):
result.approxTime = (8 * result.approxTime) div 10
if movesToGo > 2:
result.maxTime = min(initDuration(milliseconds = timeLeft.inMilliseconds div 5), moveTime)
result.maxTime = min(result.maxTime, result.approxTime*2)
iterator iterativeTimeManagedSearch*(
position: Position,
hashTable: var HashTable,
positionHistory: seq[Position] = newSeq[Position](0),
targetDepth: Ply = Ply.high,
stop: ptr Atomic[bool] = nil,
movesToGo: int16 = int16.high,
increment = [white: DurationZero, black: DurationZero],
timeLeft = [white: initDuration(milliseconds = int64.high), black: initDuration(milliseconds = int64.high)],
moveTime = initDuration(milliseconds = int64.high),
numThreads: int,
maxNodes: uint64 = uint64.high,
difficultyLevel: DifficultyLevel,
evaluation: proc(position: Position): Value {.noSideEffect.} = evaluate
): tuple[value: Value, pv: seq[Move], nodes: uint64, passedTime: Duration] =
var stopFlag: Atomic[bool]
var stop = if stop == nil: addr stopFlag else: stop
stop[].store(false)
let calculatedMoveTime = calculateMoveTime(
moveTime, timeLeft[position.us], increment[position.us], movesToGo, position.halfmovesPlayed)
var stopwatchResult = spawn stopwatch(stop, calculatedMoveTime.maxTime)
let start = now()
var
startLastIteration = now()
branchingFactors = newSeq[float](targetDepth.int)
lastNumNodes = uint64.high
var iteration = -1
for (value, pv, nodes, canStop) in iterativeDeepeningSearch(
position = position,
hashTable = hashTable,
stop = stop,
positionHistory = positionHistory,
targetDepth = targetDepth,
numThreads = numThreads,
maxNodes = maxNodes,
evaluation = evaluation,
approxTotalTime = calculatedMoveTime.approxTime,
difficultyLevel = difficultyLevel
):
iteration += 1
let totalPassedTime = now() - start
let iterationPassedTime = (now() - startLastIteration)
startLastIteration = now()
yield (value: value, pv: pv, nodes: nodes, passedTime: iterationPassedTime)
assert calculatedMoveTime.approxTime >= DurationZero
branchingFactors[iteration] = nodes.float / lastNumNodes.float;
lastNumNodes = if nodes <= 100_000: uint64.high else: nodes
var averageBranchingFactor = branchingFactors[iteration]
if iteration >= 4:
averageBranchingFactor =
(branchingFactors[iteration] +
branchingFactors[iteration - 1] +
branchingFactors[iteration - 2] +
branchingFactors[iteration - 3])/4.0
let estimatedTimeNextIteration =
initDuration(milliseconds = (iterationPassedTime.inMilliseconds.float * averageBranchingFactor).int64)
if estimatedTimeNextIteration + totalPassedTime > calculatedMoveTime.approxTime and iteration >= 4:
break;
if timeLeft[position.us] < initDuration(milliseconds = int64.high) and canStop:
break
stop[].store(true)
discard ^stopwatchResult
proc timeManagedSearch*(
position: Position,
hashTable: var HashTable,
positionHistory: seq[Position] = newSeq[Position](0),
targetDepth: Ply = Ply.high,
stop: ptr Atomic[bool] = nil,
movesToGo: int16 = int16.high,
increment = [white: DurationZero, black: DurationZero],
timeLeft = [white: initDuration(milliseconds = int64.high), black: initDuration(milliseconds = int64.high)],
moveTime = initDuration(milliseconds = int64.high),
numThreads = 1,
maxNodes: uint64 = uint64.high,
difficultyLevel: DifficultyLevel,
evaluation: proc(position: Position): Value {.noSideEffect.} = evaluate
): tuple[value: Value, pv: seq[Move]] =
for (value, pv, nodes, passedTime) in iterativeTimeManagedSearch(
position,
hashTable,
positionHistory,
targetDepth,
stop,
movesToGo = movesToGo,
increment = increment,
timeLeft = timeLeft,
moveTime = moveTime,
numThreads = numThreads,
maxNodes = maxNodes,
difficultyLevel = difficultyLevel,
evaluation
):
result = (value: value, pv: pv)