Compare commits

...

7 Commits
dev ... time

Author SHA1 Message Date
Calcitem d1cb31a501 test 2021-04-23 01:47:42 +08:00
Calcitem 657706613d Add timeman from Stockfish 2021-04-23 01:20:35 +08:00
Calcitem bf0558ebf1 search: ids: Do not clear hash to speedup
Self play cut from 10s to 7s
2021-04-22 01:23:01 +08:00
Calcitem 5465232b14 search: Do not call TranspositionTable::save() when reach leaf
Like morris v0.3
2021-04-20 23:12:04 +08:00
Calcitem 7edc9a42f3 search: TranspositionTable::save param use oldAlpha instead of alpha
Same with morris 0.2

self play move history not change.
2021-04-20 23:08:02 +08:00
Calcitem 786a1e55fd search: Adjust TranspositionTable::save params
like morris 0.2, but not same.

Use:
TranspositionTable::boundType(bestValue, alpha, beta),
Not use oldAlpha

self play change last few moves, white win more quickly.

From:
(3,5)->(2,5)
(1,5)->(1,4)
(2,5)->(1,5)
(1,6)->(1,7)
(1,5)->(1,6)
(1,7)->(2,7)
-(2,4)
(1,6)->(1,5)
(2,7)->(1,7)
(1,5)->(1,6)
(1,7)->(2,7)
-(1,2)
Player2 win!

To:
(3,5)->(2,5)
(1,6)->(1,7)
(2,5)->(3,5)
(1,7)->(2,7)
-(2,4)
(3,5)->(2,5)
(1,5)->(1,6)
-(1,2)
Player2 win!
2021-04-20 23:03:42 +08:00
Calcitem 6d195992bf test 2021-04-20 22:48:26 +08:00
10 changed files with 242 additions and 17 deletions

View File

@ -476,6 +476,7 @@
<ClInclude Include="src\perfect\threadManager.h" />
<ClInclude Include="src\search.h" />
<ClInclude Include="src\thread_win32_osx.h" />
<ClInclude Include="src\timeman.h" />
<ClInclude Include="src\tt.h" />
<ClInclude Include="src\debug.h" />
<ClInclude Include="src\hashmap.h" />
@ -762,6 +763,7 @@
<ClCompile Include="src\perfect\strLib.cpp" />
<ClCompile Include="src\perfect\threadManager.cpp" />
<ClCompile Include="src\search.cpp" />
<ClCompile Include="src\timeman.cpp" />
<ClCompile Include="src\tt.cpp" />
<ClCompile Include="src\misc.cpp" />
<ClCompile Include="src\thread.cpp" />

View File

@ -165,6 +165,9 @@
<ClInclude Include="src\perfect\perfect.h">
<Filter>Perfect AI Files</Filter>
</ClInclude>
<ClInclude Include="src\timeman.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="debug\moc_predefs.h.cbt">
@ -455,6 +458,9 @@
<ClCompile Include="src\perfect\perfect_test.cpp">
<Filter>Perfect AI Files</Filter>
</ClCompile>
<ClCompile Include="src\timeman.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="millgame.rc">

View File

@ -30,6 +30,7 @@
#include "position.h"
#include "search.h"
#include "thread.h"
#include "timeman.h"
#include "tt.h"
#include "uci.h"
@ -42,6 +43,12 @@
using std::string;
using Eval::evaluate;
using namespace Search;
using namespace Stockfish;
namespace Search
{
LimitsType Limits;
}
Value MTDF(Position *pos, Sanmill::Stack<Position> &ss, Value firstguess, Depth depth, Depth originDepth, Move &bestMove);
@ -75,6 +82,9 @@ void Search::clear()
int Thread::search()
{
us = rootPos->side_to_move();
//Stockfish::Time.init(Limits, us, rootPos->game_ply());
Sanmill::Stack<Position> ss;
Value value = VALUE_ZERO;
@ -153,6 +163,9 @@ int Thread::search()
const Depth depthBegin = 2;
Value lastValue = VALUE_ZERO;
auto startTime = now();// limits.startTime;
double totalTime = 1000.0;
loggerDebug("\n==============================\n");
loggerDebug("==============================\n");
loggerDebug("==============================\n");
@ -160,7 +173,7 @@ int Thread::search()
for (Depth i = depthBegin; i < originDepth; i += 1) {
#ifdef TRANSPOSITION_TABLE_ENABLE
#ifdef CLEAR_TRANSPOSITION_TABLE
TranspositionTable::clear();
//TranspositionTable::clear();
#endif
#endif
@ -173,6 +186,13 @@ int Thread::search()
loggerDebug("%d(%d) ", value, value - lastValue);
lastValue = value;
// Stop the search if we have exceeded the totalTime
if (now() - startTime > totalTime) {
//Threads.stop = true;
loggerDebug("timeout");
goto out;
}
}
#ifdef TIME_STAT
@ -200,6 +220,8 @@ int Thread::search()
value = search(rootPos, ss, d, originDepth, alpha, beta, bestMove);
#endif
out:
#ifdef TIME_STAT
timeEnd = chrono::steady_clock::now();
loggerDebug("Total Time: %llus\n", chrono::duration_cast<chrono::seconds>(timeEnd - timeStart).count());
@ -278,7 +300,14 @@ Value search(Position *pos, Sanmill::Stack<Position> &ss, Depth depth, Depth ori
}
#endif /* ENDGAME_LEARNING */
const bool atRoot = (originDepth == depth);
#ifdef TRANSPOSITION_TABLE_ENABLE
// check transposition-table
const Value oldAlpha = alpha;
Bound type = BOUND_NONE;
const Value probeVal = TranspositionTable::probe(posKey, depth, alpha, beta, type
@ -325,6 +354,8 @@ Value search(Position *pos, Sanmill::Stack<Position> &ss, Depth depth, Depth ori
}
#endif
// process leaves
// Check for aborted search
// TODO: and immediate draw
if (unlikely(pos->phase == Phase::gameOver) || // TODO: Deal with hash
@ -339,17 +370,6 @@ Value search(Position *pos, Sanmill::Stack<Position> &ss, Depth depth, Depth ori
bestValue -= depth;
}
#ifdef TRANSPOSITION_TABLE_ENABLE
TranspositionTable::save(bestValue,
depth,
BOUND_EXACT,
posKey
#ifdef TT_MOVE_ENABLE
, MOVE_NONE
#endif // TT_MOVE_ENABLE
);
#endif
return bestValue;
}
@ -363,6 +383,8 @@ Value search(Position *pos, Sanmill::Stack<Position> &ss, Depth depth, Depth ori
}
#endif // THREEFOLD_REPETITION
// recurse
MovePicker mp(*pos);
Move nextMove = mp.next_move();
const int moveCount = mp.move_count();
@ -373,6 +395,11 @@ Value search(Position *pos, Sanmill::Stack<Position> &ss, Depth depth, Depth ori
return bestValue;
}
// Follow morris, do not save to TT
if (moveCount == 0) {
return -VALUE_INFINITE;
}
#ifdef TRANSPOSITION_TABLE_ENABLE
#ifndef DISABLE_PREFETCH
for (int i = 0; i < moveCount; i++) {
@ -443,6 +470,7 @@ Value search(Position *pos, Sanmill::Stack<Position> &ss, Depth depth, Depth ori
if (Threads.stop.load(std::memory_order_relaxed))
return VALUE_ZERO;
// TODO: Not follow morris
if (value >= bestValue) {
bestValue = value;
@ -459,8 +487,7 @@ Value search(Position *pos, Sanmill::Stack<Position> &ss, Depth depth, Depth ori
#ifdef TRANSPOSITION_TABLE_ENABLE
TranspositionTable::save(bestValue,
depth,
bestValue >= beta ? BOUND_LOWER :
BOUND_UPPER,
TranspositionTable::boundType(bestValue, oldAlpha, beta),
posKey
#ifdef TT_MOVE_ENABLE
, bestMove

View File

@ -35,6 +35,30 @@ using namespace std;
namespace Search
{
/// LimitsType struct stores information sent by GUI about available time to
/// search the current move, maximum depth/time, or if we are in analysis mode.
struct LimitsType
{
LimitsType()
{ // Init explicitly due to broken value-initialization of non POD in MSVC
time[WHITE] = time[BLACK] = inc[WHITE] = inc[BLACK] = npmsec = movetime = TimePoint(0);
movestogo = depth = mate = perft = infinite = 0;
nodes = 0;
}
bool use_time_management() const
{
return time[WHITE] || time[BLACK];
}
std::vector<Move> searchmoves;
TimePoint time[COLOR_NB], inc[COLOR_NB], npmsec, movetime, startTime;
int movestogo, depth, mate, perft, infinite;
int64_t nodes;
};
extern LimitsType Limits;
void init() noexcept;
void clear();

View File

@ -546,13 +546,14 @@ void ThreadPool::clear()
/// ThreadPool::start_thinking() wakes up main thread waiting in idle_loop() and
/// returns immediately. Main thread will wake up other threads and start the search.
void ThreadPool::start_thinking(Position *pos, bool ponderMode)
void ThreadPool::start_thinking(Position *pos, const Search::LimitsType& limits, bool ponderMode)
{
main()->wait_for_search_finished();
main()->stopOnPonderhit = stop = false;
increaseDepth = true;
main()->ponder = ponderMode;
Search::Limits = limits;
// We use Position::set() to set root position across threads. But there are
// some StateInfo fields (previous, pliesFromNull, capturedPiece) that cannot

View File

@ -63,6 +63,8 @@ public:
void start_searching();
void wait_for_search_finished();
std::atomic<uint64_t> nodes;
Position *rootPos { nullptr };
// Mill Game
@ -160,7 +162,7 @@ struct MainThread : public Thread
struct ThreadPool : public std::vector<Thread *>
{
void start_thinking(Position *, bool = false);
void start_thinking(Position *, const Search::LimitsType &, bool = false);
void clear();
void set(size_t);
@ -169,6 +171,11 @@ struct ThreadPool : public std::vector<Thread *>
return static_cast<MainThread *>(front());
}
uint64_t nodes_searched() const
{
return accumulate(&Thread::nodes);
}
std::atomic_bool stop, increaseDepth;
private:

101
src/timeman.cpp Normal file
View File

@ -0,0 +1,101 @@
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
Stockfish 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.
Stockfish 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 <algorithm>
#include <cfloat>
#include <cmath>
#include "search.h"
#include "timeman.h"
#include "uci.h"
namespace Stockfish {
TimeManagement Time; // Our global time management object
/// TimeManagement::init() is called at the beginning of the search and calculates
/// the bounds of time allowed for the current game ply. We currently support:
// 1) x basetime (+ z increment)
// 2) x moves in y seconds (+ z increment)
void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) {
TimePoint moveOverhead = TimePoint(Options["Move Overhead"]);
TimePoint slowMover = TimePoint(Options["Slow Mover"]);
TimePoint npmsec = TimePoint(Options["nodestime"]);
// optScale is a percentage of available time to use for the current move.
// maxScale is a multiplier applied to optimumTime.
double optScale, maxScale;
// If we have to play in 'nodes as time' mode, then convert from time
// to nodes, and use resulting values in time management formulas.
// WARNING: to avoid time losses, the given npmsec (nodes per millisecond)
// must be much lower than the real engine speed.
if (npmsec)
{
if (!availableNodes) // Only once at game start
availableNodes = npmsec * limits.time[us]; // Time is in msec
// Convert from milliseconds to nodes
limits.time[us] = TimePoint(availableNodes);
limits.inc[us] *= npmsec;
limits.npmsec = npmsec;
}
startTime = now();// limits.startTime;
// Maximum move horizon of 50 moves
int mtg = limits.movestogo ? std::min(limits.movestogo, 50) : 50;
// Make sure timeLeft is > 0 since we may use it as a divisor
TimePoint timeLeft = std::max(TimePoint(1),
limits.time[us] + limits.inc[us] * (mtg - 1) - moveOverhead * (2 + mtg));
// A user may scale time usage by setting UCI option "Slow Mover"
// Default is 100 and changing this value will probably lose elo.
timeLeft = slowMover * timeLeft / 100;
// x basetime (+ z increment)
// If there is a healthy increment, timeLeft can exceed actual available
// game time for the current move, so also cap to 20% of available game time.
if (limits.movestogo == 0)
{
optScale = std::min(0.0084 + std::pow(ply + 3.0, 0.5) * 0.0042,
0.2 * limits.time[us] / double(timeLeft));
maxScale = std::min(7.0, 4.0 + ply / 12.0);
}
// x moves in y seconds (+ z increment)
else
{
optScale = std::min((0.8 + ply / 128.0) / mtg,
0.8 * limits.time[us] / double(timeLeft));
maxScale = std::min(6.3, 1.5 + 0.11 * mtg);
}
// Never use more than 80% of the available time for this move
optimumTime = TimePoint(optScale * timeLeft);
maximumTime = TimePoint(std::min(0.8 * limits.time[us] - moveOverhead, maxScale * optimumTime));
if (Options["Ponder"])
optimumTime += optimumTime / 4;
}
} // namespace Stockfish

51
src/timeman.h Normal file
View File

@ -0,0 +1,51 @@
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2021 The Stockfish developers (see AUTHORS file)
Stockfish 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.
Stockfish 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/>.
*/
#ifndef TIMEMAN_H_INCLUDED
#define TIMEMAN_H_INCLUDED
#include "misc.h"
#include "search.h"
#include "thread.h"
namespace Stockfish {
/// The TimeManagement class computes the optimal time to think depending on
/// the maximum available time, the game move number and other parameters.
class TimeManagement {
public:
void init(Search::LimitsType& limits, Color us, int ply);
TimePoint optimum() const { return optimumTime; }
TimePoint maximum() const { return maximumTime; }
TimePoint elapsed() const { return Search::Limits.npmsec ?
TimePoint(Threads.nodes_searched()) : now() - startTime; }
int64_t availableNodes; // When in 'nodes as time' mode
private:
TimePoint startTime;
TimePoint optimumTime;
TimePoint maximumTime;
};
extern TimeManagement Time;
} // namespace Stockfish
#endif // #ifndef TIMEMAN_H_INCLUDED

View File

@ -121,13 +121,17 @@ void setoption(istringstream &is)
void go(Position *pos)
{
Search::LimitsType limits;
#ifdef UCI_AUTO_RE_GO
begin:
#endif
limits.startTime = now(); // As early as possible!
repetition = 0;
Threads.start_thinking(pos);
Threads.start_thinking(pos, limits);
if (pos->get_phase() == Phase::gameOver)
{

View File

@ -20,6 +20,7 @@
#include "misc.h"
#include "bitboard.h"
#include "position.h"
#include "uci.h"
QString APP_FILENAME_DEFAULT = "MillGame";
@ -44,6 +45,7 @@ QString getAppFileName()
#ifndef PERFECT_AI_TEST
int main(int argc, char *argv[])
{
UCI::init(Options);
Bitboards::init();
Position::init();