Skip to content

Commit

Permalink
Threading and stop command
Browse files Browse the repository at this point in the history
  • Loading branch information
SnowballSH committed Jan 8, 2022
1 parent b3bd7d0 commit 7267a8c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 16 deletions.
12 changes: 9 additions & 3 deletions GoneuraOu/Commands/Go.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Diagnostics;
using System.Threading;
using GoneuraOu.Board;
using GoneuraOu.Search;

namespace GoneuraOu.Commands
{
public static class Go
{
public static void DoGo(this Protocol proto, string[] tokens)
public static Thread? DoGo(this Protocol proto, string[] tokens)
{
Debug.Assert(tokens[0] == "go");

Expand All @@ -28,7 +29,7 @@ public static void DoGo(this Protocol proto, string[] tokens)

case "perft":
Perft.PerftRootPrint(proto.CurrentPosition, uint.Parse(tokens[index]));
return;
return null;

case "wtime":
if (proto.CurrentPosition.CurrentTurn == Turn.Sente)
Expand Down Expand Up @@ -56,11 +57,16 @@ public static void DoGo(this Protocol proto, string[] tokens)
}
}

StopSearch.Stop = false;
var searcher = new Searcher();

searcher.DoSearch(proto.CurrentPosition, proto.Limit);
var th = new Thread(() => searcher.DoSearch(proto.CurrentPosition, proto.Limit));

th.Start();

GC.Collect();

return th;
}
}
}
27 changes: 24 additions & 3 deletions GoneuraOu/Commands/Protocol.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using GoneuraOu.Evaluation;

namespace GoneuraOu.Commands
Expand All @@ -11,6 +12,11 @@ public struct SearchLimit
public ulong? MoveTime;
}

public static class StopSearch
{
public static bool Stop;
}

public class Protocol
{
public Board.Board CurrentPosition;
Expand All @@ -26,17 +32,24 @@ public void StartProtocol()
{
Console.Out.Flush();

Console.WriteLine("GoneuraOu Version 0.1a");
Console.WriteLine("GoneuraOu Version 0.1");

TranspositionTable.TranspositionTable.Init(16);

Thread? mainThread = null;
var initAttacks = true;

while (true)
{
var input = Console.In.ReadLine()?.Trim();
if (input == null) continue;
var tokens = input.Split(' ');

if (mainThread is {IsAlive: false})
{
mainThread = null;
}

switch (tokens[0])
{
case "isready":
Expand Down Expand Up @@ -70,8 +83,16 @@ public void StartProtocol()
break;

case "go":
this.DoGo(tokens);
GC.Collect();
if (mainThread == null)
{
mainThread = this.DoGo(tokens);
GC.Collect();
}

break;

case "stop":
StopSearch.Stop = true;
break;

case "eval":
Expand Down
16 changes: 8 additions & 8 deletions GoneuraOu/Search/Searcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void IterativeDeepening(Board.Board board, uint maxDepth)

var newScore = Negamax(board, alpha, beta, depth);

if (_maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
if (StopSearch.Stop || _maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
{
break;
}
Expand Down Expand Up @@ -300,7 +300,7 @@ public int Negamax(Board.Board board, int alpha, int beta, uint depth, bool doNu
board.UndoNullMove();
Ply--;

if (_maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
if (StopSearch.Stop || _maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
{
return score;
}
Expand All @@ -316,7 +316,7 @@ public int Negamax(Board.Board board, int alpha, int beta, uint depth, bool doNu
{
var score = -Negamax(board, alpha, beta,
depth - 7, doNull);
if (_maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
if (StopSearch.Stop || _maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
{
return score;
}
Expand Down Expand Up @@ -378,7 +378,7 @@ public int Negamax(Board.Board board, int alpha, int beta, uint depth, bool doNu
: depth / 2
: depth - 2;
score = -Negamax(board, -alpha - 1, -alpha, dp);
if (_maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
if (StopSearch.Stop || _maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
{
board.UndoMove(move);
Ply--;
Expand All @@ -393,7 +393,7 @@ public int Negamax(Board.Board board, int alpha, int beta, uint depth, bool doNu
if (score > alpha)
{
score = -Negamax(board, -alpha - 1, -alpha, depth - 1);
if (_maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
if (StopSearch.Stop || _maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
{
board.UndoMove(move);
Ply--;
Expand All @@ -404,7 +404,7 @@ public int Negamax(Board.Board board, int alpha, int beta, uint depth, bool doNu
if (score > alpha && score < beta)
{
score = -Negamax(board, -beta, -alpha, depth - 1);
if (_maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
if (StopSearch.Stop || _maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
{
board.UndoMove(move);
Ply--;
Expand All @@ -416,7 +416,7 @@ public int Negamax(Board.Board board, int alpha, int beta, uint depth, bool doNu
else
{
score = -Negamax(board, -beta, -alpha, depth - 1);
if (_maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
if (StopSearch.Stop || _maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
{
board.UndoMove(move);
Ply--;
Expand Down Expand Up @@ -540,7 +540,7 @@ public int Quiescence(Board.Board board, int alpha, int beta)
board.UndoMove(move);
Ply--;

if (_maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
if (StopSearch.Stop || _maxTime.HasValue && (ulong)_timer.ElapsedMilliseconds > _maxTime.Value)
{
return score;
}
Expand Down
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Decent UCI Minishogi (5x5 shogi) Engine

Current Strength: Around **2100 ELO** (Blitz & up), 1800 ELO (Bullet)
Current Strength: Around **2150 ELO**

## Technology

Expand Down Expand Up @@ -54,7 +54,42 @@ Technologies used to build this engine: (crossed-out means in the future)

Games during development progress:

Defeats Fairy-Stockfish 1900 ELO (From now on, evaluation will be provided.):
Jan 8 2022

Beats Fairy-Stockfish 2000 ELO in bullet (bullet 0.5+0)

```
[White "GoneuraOu"]
[Black "Fairy-Stockfish 2000 ELO"]
[Result "1-0"]
[TimeControl "40/30"]
[Variant "minishogi"]
1. Sb2 {+0.08/13 1.5s} Sb4 {+0.76/18 3.5s} 2. Bc2 {+1.76/13 1.4s}
Bc4 {+0.51/17 1.9s} 3. Rd1 {+1.76/11 1.3s} Be2 {+0.53/18 2.1s}
4. Rd2 {+1.65/12 1.3s} Bc4 {-0.23/17 2.8s} 5. Rd1 {+1.76/12 1.2s}
Be2 {+0.07/18 2.5s} 6. Rd2 {+1.65/12 1.1s} Bc4 {-0.38/16 0.87s}
7. Rd1 {+1.76/12 1.1s} Rc5 {0.00/18 2.2s} 8. Ba4 {+1.37/11 1.0s}
Be2 {+0.76/18 1.9s} 9. Rd2 {+1.57/13 0.97s} Pe3 {+0.23/16 0.63s}
10. Bc2 {+1.96/11 0.92s} Gc4 {-1.61/16 1.7s} 11. Gc1 {+3.43/12 0.88s}
Rd5 {-1.61/15 1.5s} 12. Rxd5 {+6.09/10 0.83s} Kxd5 {-2.07/15 0.17s}
13. R@e4 {+5.59/10 0.79s} R@e1 {-1.61/15 1.3s} 14. Rxe3 {+4.44/10 0.75s}
Gd4 {-1.07/14 0.86s} 15. Rxe2 {+3.88/10 0.71s} Rxe2 {-0.46/12 0.98s}
16. P@d3 {+5.93/8 0.68s} Gc3 {-1.69/14 0.84s} 17. Sxc3 {+7.75/9 0.64s}
Sxc3 {-7.30/11 0.24s} 18. G@d4 {+7.85/8 0.61s} Sxd4 {-9.46/11 0.35s}
19. Pxd4 {+8.90/8 0.58s} Ke5 {-10.76/11 0.65s} 20. B@c4 {+10.57/9 0.55s}
R@c5 {-8.30/10 0.54s} 21. Bxe2 {+18.50/9 0.52s} S@e4 {-22.84/13 0.45s}
22. S@c4 {+22.62/9 0.50s} Rxc4 {-M22/10 0.37s} 23. R@a5 {+22.61/9 0.47s}
S@c5 {-17.23/9 0.15s} 24. Bxc4 {+29.62/10 0.45s} Kxd4 {-M18/15 0.17s}
25. Bb5+ {+28.16/8 0.43s} G@c4 {-M10/13 0.090s} 26. R@e1 {+29.77/8 0.41s}
Kd5 {-M10/13 0.071s} 27. +Bxc4 {+M7/9 0.070s} Kxc4 {-M6/32 0.027s}
28. Rxe4 {+M5/7 0.30s} Kd5 {-M4/157 0.037s} 29. G@d4 {+M1/3 0.002s, Sente mates}
1-0
```

Jan 7 2022

Defeats Fairy-Stockfish 1900 ELO (blitz 2+1):

```
[White "Fairy-Stockfish 1900 ELO"]
Expand Down

0 comments on commit 7267a8c

Please sign in to comment.