forked from liamt19/Lizard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added data generation functionality (liamt19#63)
bench: 4909216
- Loading branch information
Showing
25 changed files
with
1,261 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ | |
/Properties/launchSettings.json | ||
/*.exe | ||
/*.pdb | ||
/data | ||
/.vs/ProjectEvaluation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
| ||
using System.Runtime.InteropServices; | ||
|
||
namespace Lizard.Logic.Datagen | ||
{ | ||
[StructLayout(LayoutKind.Explicit)] | ||
public unsafe struct BulletDataFormat : TOutputFormat | ||
{ | ||
// STM here is used to fix the game result, which is dependent on the STM: | ||
// If black is to move, the result is flipped around WhiteWin <-> BlackWin. | ||
// WE don't know the result when creating the entries, and STM isn't stored within them anywhere, | ||
// So manually place the STM in the last byte of padding of the entries. | ||
[FieldOffset( 0)] BulletFormatEntry BFE; | ||
[FieldOffset(29)] Move BestMove; | ||
[FieldOffset(31)] byte STM; | ||
|
||
public int Score | ||
{ | ||
get => BFE.score; | ||
set => BFE.score = (short)value; | ||
} | ||
|
||
public GameResult Result | ||
{ | ||
get => (GameResult)BFE.result; | ||
set => BFE.result = (byte)value; | ||
} | ||
|
||
public void SetSTM(int stm) { STM = (byte)stm; } | ||
public void SetResult(GameResult gr) | ||
{ | ||
if (STM == Black) | ||
{ | ||
gr = (GameResult)(2 - gr); | ||
} | ||
|
||
Result = gr; | ||
} | ||
|
||
public string GetWritableTextData() | ||
{ | ||
return ""; | ||
} | ||
|
||
public byte[] GetWritableData() | ||
{ | ||
int len = Marshal.SizeOf<BulletFormatEntry>(); | ||
IntPtr ptr = Marshal.AllocHGlobal(len); | ||
byte[] myBuffer = new byte[len]; | ||
|
||
Marshal.StructureToPtr(BFE, ptr, false); | ||
Marshal.Copy(ptr, myBuffer, 0, len); | ||
Marshal.FreeHGlobal(ptr); | ||
|
||
return myBuffer; | ||
} | ||
|
||
public void Fill(Position pos, Move bestMove, int score) | ||
{ | ||
BFE = BulletFormatEntry.FromBitboard(ref pos.bb, pos.ToMove, (short)score, GameResult.Draw); | ||
STM = (byte)pos.ToMove; | ||
BestMove = bestMove; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
| ||
using System.Buffers.Binary; | ||
using System.Numerics; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Lizard.Logic.Datagen | ||
{ | ||
[StructLayout(LayoutKind.Explicit, Size = 32)] | ||
public unsafe struct BulletFormatEntry | ||
{ | ||
public const int Size = 32; | ||
|
||
[FieldOffset(0)] public ulong occ; | ||
[FieldOffset(8)] public fixed byte pcs[16]; | ||
[FieldOffset(24)] public short score; | ||
[FieldOffset(26)] public byte result; | ||
[FieldOffset(27)] public byte ksq; | ||
[FieldOffset(28)] public byte opp_ksq; | ||
[FieldOffset(29)] public fixed byte _pad[3]; | ||
|
||
|
||
|
||
public void FillBitboard(ref Bitboard bb) | ||
{ | ||
bb.Reset(); | ||
|
||
bb.Occupancy = occ; | ||
|
||
ulong temp = occ; | ||
int idx = 0; | ||
while (temp != 0) | ||
{ | ||
int sq = poplsb(&temp); | ||
int piece = (pcs[idx / 2] >> (4 * (idx & 1))) & 0b1111; | ||
|
||
bb.AddPiece(sq, piece / 8, piece % 8); | ||
|
||
idx++; | ||
} | ||
} | ||
|
||
|
||
|
||
public static BulletFormatEntry FromBitboard(ref Bitboard bb, int stm, short score, GameResult result) | ||
{ | ||
Span<ulong> bbs = [ | ||
bb.Colors[White], bb.Colors[Black], | ||
bb.Pieces[0], bb.Pieces[1], bb.Pieces[2], | ||
bb.Pieces[3], bb.Pieces[4], bb.Pieces[5], | ||
]; | ||
|
||
if (stm == Black) | ||
{ | ||
for (int i = 0; i < bbs.Length; i++) | ||
bbs[i] = BinaryPrimitives.ReverseEndianness(bbs[i]); | ||
|
||
(bbs[White], bbs[Black]) = (bbs[Black], bbs[White]); | ||
|
||
score = (short)-score; | ||
result = 1 - result; | ||
} | ||
|
||
ulong occ = bbs[0] | bbs[1]; | ||
|
||
BulletFormatEntry bfe = new BulletFormatEntry | ||
{ | ||
score = score, | ||
occ = occ, | ||
result = (byte)(2 * (int)result), | ||
ksq = (byte) BitOperations.TrailingZeroCount(bbs[0] & bbs[7]), | ||
opp_ksq = (byte)(BitOperations.TrailingZeroCount(bbs[1] & bbs[7]) ^ 56) | ||
}; | ||
|
||
Span<byte> pieces = stackalloc byte[16]; | ||
|
||
int idx = 0; | ||
ulong occ2 = occ; | ||
int piece = 0; | ||
while (occ2 > 0) | ||
{ | ||
int sq = BitOperations.TrailingZeroCount(occ2); | ||
ulong bit = 1UL << sq; | ||
occ2 &= occ2 - 1; | ||
|
||
byte colour = (byte)(((bit & bbs[1]) > 0 ? 1 : 0) << 3); | ||
for (int i = 2; i < 8; i++) | ||
if ((bit & bbs[i]) > 0) | ||
{ | ||
piece = i - 2; | ||
break; | ||
} | ||
|
||
byte pc = (byte)(colour | (byte)piece); | ||
|
||
bfe.pcs[idx / 2] |= (byte)(pc << (4 * (idx & 1))); | ||
|
||
idx += 1; | ||
} | ||
|
||
return bfe; | ||
} | ||
|
||
|
||
|
||
public void WriteToBuffer(Span<byte> buff) | ||
{ | ||
fixed (void* buffPtr = &buff[0], thisPtr = &this) | ||
{ | ||
Unsafe.CopyBlock(buffPtr, thisPtr, BulletFormatEntry.Size); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
| ||
namespace Lizard.Logic.Datagen | ||
{ | ||
public static class DatagenParameters | ||
{ | ||
public const int HashSize = 8; | ||
|
||
public const int MinOpeningPly = 8; | ||
public const int MaxOpeningPly = 9; | ||
|
||
public const int SoftNodeLimit = 5000; | ||
public const int DepthLimit = 24; | ||
|
||
public const int WritableDataLimit = 512; | ||
|
||
public const int AdjudicateMoves = 4; | ||
public const int AdjudicateScore = 3000; | ||
public const int MaxFilteringScore = 6000; | ||
|
||
public const int MaxOpeningScore = 1200; | ||
public const int MaxScrambledOpeningScore = 600; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
| ||
namespace Lizard.Logic.Datagen | ||
{ | ||
public enum GameResult | ||
{ | ||
WhiteWin = 2, | ||
Draw = 1, | ||
BlackWin = 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace Lizard.Logic.Datagen | ||
{ | ||
public interface TOutputFormat | ||
{ | ||
public int Score { get; set; } | ||
public GameResult Result { get; set; } | ||
public void SetResult(GameResult gr); | ||
public void SetSTM(int stm); | ||
public string GetWritableTextData(); | ||
public void Fill(Position pos, Move bestMove, int score); | ||
|
||
public static string ResultToMarlin(GameResult gr) | ||
{ | ||
return gr switch | ||
{ | ||
GameResult.WhiteWin => "1.0", | ||
GameResult.Draw => "0.5", | ||
GameResult.BlackWin => "0.0", | ||
}; | ||
} | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.