Skip to content

Commit

Permalink
Trained new network, updated Zobrist hashing to use standard random n…
Browse files Browse the repository at this point in the history
…umbers, released v1.1.1
  • Loading branch information
Timmoth committed Sep 24, 2024
1 parent de44a8d commit 8b72924
Show file tree
Hide file tree
Showing 13 changed files with 577 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
working-directory: Sapling
id: get_version
run: |
VERSION=1.1.0
VERSION=1.1.1
echo "Application version: $VERSION"
echo "::set-output name=version::$VERSION"
Expand Down
2 changes: 1 addition & 1 deletion Sapling.Engine/OutputHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public static unsafe string CreateDiagram(this GameState gameState, bool blackAt

if (includeZobristKey)
{
output.AppendLine($"Zobrist Key : {gameState.Board.Data.Hash}");
output.AppendLine($"Zobrist Key : {gameState.Board.Data.Hash.ToString("X")}");
}
}

Expand Down
Binary file not shown.
10 changes: 9 additions & 1 deletion Sapling.Engine/Resources/WeightsHistory/log.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,12 @@ SuperBatches: 180
Data: 1bn positions
WDL: 0.4
LR: CosineDecayLR 0.001 * 0.3 * 0.3 * 0.3
SuperBatches: 180
SuperBatches: 180

----------------------------------
16_hl1024.bin
----------------------------------
Data: 1.5bn positions
WDL: 0.4
LR: CosineDecayLR 0.001 * 0.3 * 0.3 * 0.3
SuperBatches: 200
Binary file modified Sapling.Engine/Resources/sapling.nnue
Binary file not shown.
33 changes: 32 additions & 1 deletion Sapling.Engine/Sapling.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,30 @@
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<TieredPGO>true</TieredPGO>
<TieredCompilation>true</TieredCompilation>

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>SaplingEngine</PackageId>
<PackageVersion>1.1.0</PackageVersion>
<Authors>Tim Jones</Authors>
<Company>Aptacode</Company>
<Description>A strong UCI chess engine</Description>
<PackageProjectUrl>https://github.com/Timmoth/Sapling</PackageProjectUrl>
<RepositoryUrl>https://github.com/Timmoth/Sapling</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>Chess Engine</PackageTags>
<Title>Sapling</Title>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<IncludeSymbols>true</IncludeSymbols>
<ApplicationIcon>logo.ico</ApplicationIcon>
<PackageIcon>logo.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<Content Include="logo.ico" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentResults" Version="3.16.0" />
</ItemGroup>
Expand All @@ -18,4 +38,15 @@
<EmbeddedResource Include="Resources\sapling.nnue" />
</ItemGroup>

<ItemGroup>
<None Include="..\logo.png">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

</Project>
312 changes: 288 additions & 24 deletions Sapling.Engine/Zobrist.cs

Large diffs are not rendered by default.

Binary file added Sapling.Engine/logo.ico
Binary file not shown.
Binary file added Sapling.Engine/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
223 changes: 223 additions & 0 deletions Sapling.Utils/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
using System.Buffers.Binary;
using System.Text;
using Sapling.Engine;
using Sapling.Engine.MoveGen;

namespace Sapling.Utils
{
internal class Program
{
public static string PolyGlotToUciMove(ushort move)
{
// Extract components using bit-shifting and masking

// To file (0-2 bits)
int toFile = (move & 0b0000000000000111);

// To rank (3-5 bits)
int toRank = (move >> 3) & 0b0000000000000111;

// From file (6-8 bits)
int fromFile = (move >> 6) & 0b0000000000000111;

// From rank (9-11 bits)
int fromRank = (move >> 9) & 0b0000000000000111;

// Promotion piece (12-14 bits)
int promotionPiece = (move >> 12) & 0b0000000000000111;

// Convert the file (0-7) to a letter ('a' = 0, 'b' = 1, ..., 'h' = 7)
char fromFileChar = (char)('a' + fromFile);
char toFileChar = (char)('a' + toFile);

// Convert the rank (0-7) to a number ('1' = 0, '2' = 1, ..., '8' = 7)
char fromRankChar = (char)('1' + fromRank);
char toRankChar = (char)('1' + toRank);

// Create the basic move string (like "e2e4")
string moveStr = $"{fromFileChar}{fromRankChar}{toFileChar}{toRankChar}";

// Handle promotion if present (promotionPiece > 0 means promotion)
if (promotionPiece > 0)
{
// Convert promotion piece (1=q, 2=r, 3=b, 4=n)
char promotionChar = promotionPiece switch
{
1 => 'q', // Queen
2 => 'r', // Rook
3 => 'b', // Bishop
4 => 'n', // Knight
_ => throw new InvalidOperationException("Invalid promotion piece")
};

// Append the promotion character to the move string
moveStr += promotionChar;
}

return moveStr;
}

public static ushort ToOpeningMove(uint move)
{
var moveType = move.GetMoveType();

var promotion = 0;
if (moveType >= Constants.PawnKnightPromotion)
{
promotion = moveType - 3;
}

var from = move.GetFromSquare();
var to = move.GetToSquare();

return (ushort)(to.GetFileIndex() |
to.GetRankIndex() << 3 |
from.GetFileIndex() << 6 |
from.GetRankIndex() << 9 |
promotion << 12
);
}

static void Main(string[] args)
{
var fileName = "./Human.bin";
var entrySize = sizeof(ulong) + sizeof(ushort) + sizeof(ushort) + sizeof(uint);
var entryCount = (int)new FileInfo(fileName).Length / entrySize;

var openingMoves = new Dictionary<ulong, List<ushort>>();
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
using var reader = new BinaryReader(fs, Encoding.UTF8, false);
for (var i = 0; i < entryCount; i++)
{
ulong hash;
ushort move;

if (BitConverter.IsLittleEndian)
{
hash = BinaryPrimitives.ReverseEndianness(reader.ReadUInt64());
move = BinaryPrimitives.ReverseEndianness(reader.ReadUInt16());
}
else
{
hash = reader.ReadUInt64();
move = reader.ReadUInt16();
}

// Skip weight & learn
reader.ReadUInt16();
reader.ReadUInt32();

if (!openingMoves.TryGetValue(hash, out var moveList))
{
openingMoves[hash] = moveList = new List<ushort>();
}

moveList.Add(move);
}
}

Console.WriteLine("Size: " + entryCount);

var gameHashes = new HashSet<ulong>();
var openingBookBulder = new StringBuilder();

var validFirstMoves = new Dictionary<string, int>
{
// Pawn moves
{"a2a3", 0}, {"a2a4", 0},
{"b2b3", 0}, {"b2b4", 0},
{"c2c3", 0}, {"c2c4", 0},
{"d2d3", 0}, {"d2d4", 0},
{"e2e3", 0}, {"e2e4", 0},
{"f2f3", 0}, {"f2f4", 0},
{"g2g3", 0}, {"g2g4", 0},
{"h2h3", 0}, {"h2h4", 0},

// Knight moves
{"b1a3", 0}, {"b1c3", 0},
{"g1f3", 0}, {"g1h3", 0}
};

var initialGameState = new GameState(BoardStateExtensions.CreateBoardFromArray(Constants.InitialState));
var gameState = new GameState(BoardStateExtensions.CreateBoardFromArray(Constants.InitialState));
for (var i = 0; i < 5000000; i++)
{
gameState.ResetTo(initialGameState.Board);
var firstMove = gameState.Moves[Random.Shared.Next(0, gameState.Moves.Count)];
gameState.Apply(firstMove);

var openingMovesBuilder = new StringBuilder();
openingMovesBuilder.Append(firstMove.ToUciMoveName());
var gameOk = true;
var j = 1;

for (j = 1; j < 12; j++)
{
var hash = Zobrist.CalculatePolyGlotKey(ref gameState.Board.Data);
if (!openingMoves.TryGetValue(hash, out var openingMoveList))
{
gameOk = false;
break;
}

var randomOpeningMove = openingMoveList[Random.Shared.Next(0, openingMoveList.Count)];
var mv = gameState.Moves.FirstOrDefault(m => ToOpeningMove(m) == randomOpeningMove);
if (mv == default)
{
var uciMove = PolyGlotToUciMove(randomOpeningMove);
if (uciMove == "e1h1")
{
mv = gameState.Moves.FirstOrDefault(m => m.ToUciMoveName() == "e1g1");
}
else if (uciMove == "e1a1")
{
mv = gameState.Moves.FirstOrDefault(m => m.ToUciMoveName() == "e1b1");
}
else if (uciMove == "e8h8")
{
mv = gameState.Moves.FirstOrDefault(m => m.ToUciMoveName() == "e8g8");
}
else if (uciMove == "e8a8")
{
mv = gameState.Moves.FirstOrDefault(m => m.ToUciMoveName() == "e8b8");
}
}

if (mv == default)
{
if (j < 8)
{
gameOk = false;
}

break;
}

openingMovesBuilder.Append(" ");
openingMovesBuilder.Append(mv.ToUciMoveName());
gameState.Apply(mv);
}

if (!gameHashes.Contains(gameState.Board.Data.Hash) && gameOk)
{
gameHashes.Add(gameState.Board.Data.Hash);
validFirstMoves[firstMove.ToUciMoveName()]++;
openingBookBulder.AppendLine(openingMovesBuilder.ToString());
}
}

var total = 0;
foreach (var (move, count) in validFirstMoves)
{
total += count;
Console.WriteLine($"move: {move} count: {count}");
}

Console.WriteLine($"Total games: {total}");

File.WriteAllText("./book.csv",openingBookBulder.ToString());
Console.WriteLine("Fin");
}
}
}
14 changes: 14 additions & 0 deletions Sapling.Utils/Sapling.Utils.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Sapling.Engine\Sapling.Engine.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions Sapling.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sapling.Engine.Tests", "Sap
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sapling", "Sapling\Sapling.csproj", "{F889F19A-4106-4765-A531-C9F96D966DE8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sapling.Utils", "Sapling.Utils\Sapling.Utils.csproj", "{F0836F77-0512-4AF6-B0D1-4AF34B6CAE91}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{F889F19A-4106-4765-A531-C9F96D966DE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F889F19A-4106-4765-A531-C9F96D966DE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F889F19A-4106-4765-A531-C9F96D966DE8}.Release|Any CPU.Build.0 = Release|Any CPU
{F0836F77-0512-4AF6-B0D1-4AF34B6CAE91}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0836F77-0512-4AF6-B0D1-4AF34B6CAE91}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0836F77-0512-4AF6-B0D1-4AF34B6CAE91}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0836F77-0512-4AF6-B0D1-4AF34B6CAE91}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 3 additions & 3 deletions Sapling/Sapling.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<Nullable>enable</Nullable>
<ApplicationIcon>logo.ico</ApplicationIcon>
<Title>Sapling</Title>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<Version>1.1.0.0</Version>
<AssemblyVersion>1.1.1.0</AssemblyVersion>
<FileVersion>1.1.1.0</FileVersion>
<Version>1.1.1.0</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

Expand Down

0 comments on commit 8b72924

Please sign in to comment.