-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtimeallocator.cpp
80 lines (61 loc) · 2.13 KB
/
timeallocator.cpp
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
/*
Copyright (C) 2015 Matthew Lai
Giraffe is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Giraffe is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "timeallocator.h"
#include <iostream>
#include <cstdint>
namespace
{
static const int32_t SUDDEN_DEATH_DIVISOR = 80;
static const double DIVISOR_MAX_RATIO = 2.0;
static const double MIN_TIME_PER_MOVE = 0.0;
// this number controls how much more time it uses in the beginning vs the end
// higher number means more time in the beginning
static const double TIME_SCALE = 2.0f;
}
Search::TimeAllocation AllocateTime(const ChessClock &cc)
{
Search::TimeAllocation tAlloc;
if (cc.GetMode() == ChessClock::EXACT_MODE)
{
tAlloc.normalTime = cc.GetInc();
tAlloc.maxTime = cc.GetInc();
}
else if (cc.GetMode() == ChessClock::CONVENTIONAL_INCREMENTAL_MODE)
{
int32_t divisor = cc.GetMovesUntilNextPeriod();
if (divisor == 0)
{
// sudden death mode
divisor = SUDDEN_DEATH_DIVISOR;
}
tAlloc.normalTime = (cc.GetInc() + cc.GetReading() / divisor);
tAlloc.maxTime = cc.GetInc() + cc.GetReading() / divisor * DIVISOR_MAX_RATIO;
if ((tAlloc.normalTime * TIME_SCALE) < (cc.GetReading() / 3.0f))
{
tAlloc.normalTime *= TIME_SCALE;
if ((tAlloc.maxTime * TIME_SCALE) < (cc.GetReading() / 3.0f))
{
tAlloc.maxTime *= TIME_SCALE;
}
}
tAlloc.normalTime = std::max(tAlloc.normalTime, MIN_TIME_PER_MOVE);
tAlloc.maxTime = std::max(tAlloc.maxTime, MIN_TIME_PER_MOVE);
std::cout << "# Allocated " << tAlloc.normalTime << ", " << tAlloc.maxTime << " seconds" << std::endl;
}
else
{
std::cout << "Error (unknown time control mode)" << std::endl;
}
return tAlloc;
}