Skip to content

Commit

Permalink
TranspositionTable tests
Browse files Browse the repository at this point in the history
* Added tests for TranspositionTable and memory tweaks
  • Loading branch information
jonthysell committed May 7, 2017
1 parent bd52354 commit fd91ed7
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Mzinga.Core/AI/GameAI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public GameAI()
_transpositionTable = new TranspositionTable();
}

public GameAI(MetricWeights metricWeights) : base()
public GameAI(MetricWeights metricWeights) : this()
{
if (null == metricWeights)
{
Expand Down
24 changes: 13 additions & 11 deletions Mzinga.Core/AI/TranspositionTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static int GetCapacity(long sizeInBytes)
throw new ArgumentOutOfRangeException("sizeInBytes");
}

return 1 + (int)Math.Round(sizeInBytes / (double)EntrySizeInBytes);
return 1 + (int)Math.Round(FillFactor * sizeInBytes / EntrySizeInBytes);
}

private static bool TranspostionTableReplaceEntryPredicate(TranspositionTableEntry existingEntry, TranspositionTableEntry entry)
Expand Down Expand Up @@ -73,12 +73,14 @@ private static bool TranspostionTableReplaceEntryPredicate(TranspositionTableEnt
return false;
}

private const long EntrySizeInBytes = (2 * sizeof(int)) // Key pointers x2
+ ((2 * 8 + 4 * 11 + 16 * 9) * sizeof(char)) // Key length (2x Q, 4x B, 16x other)
+ sizeof(int) // Entry pointer
+ TranspositionTableEntry.SizeInBytes; // Entry object
private static readonly long EntrySizeInBytes = (2 * IntPtr.Size) // Key pointers x2
+ ((2 * 8 + 4 * 11 + 16 * 9) * sizeof(char)) // Key length (2x Q, 4x B, 16x other)
+ IntPtr.Size // Entry pointer
+ TranspositionTableEntry.SizeInBytes; // Entry object

private const long DefaultSizeInBytes = 16 * 1024 * 1024;
public const long DefaultSizeInBytes = 32 * 1024 * 1024;

private const double FillFactor = 0.92; // To leave room for unaccounted for overhead and unused dictionary capcacity
}

public class TranspositionTableEntry
Expand All @@ -88,11 +90,11 @@ public class TranspositionTableEntry
public int Depth;
public string BestMove;

public const long SizeInBytes = sizeof(TranspositionTableEntryType)
+ sizeof(double) // Value
+ sizeof(int) // Depth
+ sizeof(int) // BestMove pointer
+ (12 * sizeof(char)); // BestMove length
public static readonly long SizeInBytes = sizeof(TranspositionTableEntryType)
+ sizeof(double) // Value
+ sizeof(int) // Depth
+ IntPtr.Size // BestMove pointer
+ (14 * sizeof(char)); // BestMove length
}

public enum TranspositionTableEntryType : byte
Expand Down
1 change: 1 addition & 0 deletions Mzinga.CoreTest/Mzinga.CoreTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<Compile Include="..\Mzinga.Core\Properties\SharedInfo.cs">
<Link>Properties\SharedInfo.cs</Link>
</Compile>
<Compile Include="TranspositionTableTests.cs" />
<Compile Include="MetricWeightsTests.cs" />
<Compile Include="GameAITests.cs" />
<Compile Include="MoveSetTests.cs" />
Expand Down
86 changes: 86 additions & 0 deletions Mzinga.CoreTest/TranspositionTableTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// TranspositionTableTests.cs
//
// Author:
// Jon Thysell <[email protected]>
//
// Copyright (c) 2017 Jon Thysell <http://jonthysell.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Diagnostics;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Mzinga.Core;
using Mzinga.Core.AI;

namespace Mzinga.CoreTest
{
[TestClass]
public class TranspositionTableTests
{
[TestMethod]
public void TranspositionTable_NewTest()
{
TranspositionTable tt = new TranspositionTable();
Assert.IsNotNull(tt);
}

[TestMethod]
public void TranspositionTable_MaxMemoryTest()
{
long expectedSizeInBytes =TranspositionTable.DefaultSizeInBytes;

TranspositionTable tt = new TranspositionTable(expectedSizeInBytes);
Assert.IsNotNull(tt);

int count = tt.Capacity;

long startMemoryUsage = GC.GetTotalMemory(true);
for (int i = 0; i < tt.Capacity; i++)
{
string key = i.ToString().PadLeft(204);
tt.Store(key, CreateMaxEntry(i));
}
long endMemoryUsage = GC.GetTotalMemory(true);

Assert.AreEqual(tt.Capacity, tt.Count);

long actualSizeInBytes = endMemoryUsage - startMemoryUsage;

double usageRatio = actualSizeInBytes / (double)expectedSizeInBytes;
Trace.WriteLine(string.Format("Usage: {0} ({1:P2})", actualSizeInBytes, usageRatio));

Assert.IsTrue(usageRatio - 1.0 <= 0.01, string.Format("Table is too big {0:P2}: {1} bytes (~{2} bytes per entry)", usageRatio, actualSizeInBytes - expectedSizeInBytes, (actualSizeInBytes - expectedSizeInBytes) / tt.Count));
}


private TranspositionTableEntry CreateMaxEntry(int id)
{
TranspositionTableEntry te = new TranspositionTableEntry();
te.Depth = 0;
te.Type = TranspositionTableEntryType.Exact;
te.Value = 0;
te.BestMove = id.ToString().PadLeft(14);
return te;
}
}
}

0 comments on commit fd91ed7

Please sign in to comment.