Smaller minor optimizations

This commit is contained in:
Asriel Camora
2023-07-06 19:45:59 +02:00
parent 1f5da66bc6
commit 2894867195
4 changed files with 13 additions and 21 deletions
+2 -11
View File
@@ -1,5 +1,4 @@
using Craftimizer.Simulator.Actions;
using System;
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Runtime.CompilerServices;
@@ -52,11 +51,9 @@ public struct ActionSet
public readonly bool IsEmpty => bits == 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
//public readonly ActionType SelectRandom(Random random) => ElementAt(random.Next(Count));
public readonly ActionType SelectRandom(Random random) => First();
public readonly ActionType SelectRandom(Random random) => ElementAt(random.Next(Count));
[MethodImpl(MethodImplOptions.AggressiveInlining)]
//public ActionType? PopRandom(Random random) => PopFirst();
public ActionType? PopRandom(Random random)
{
uint snapshot;
@@ -84,22 +81,16 @@ public struct ActionSet
uint snapshot;
uint newValue;
ActionType action;
int i = 0;
do
{
++i;
snapshot = bits;
if (snapshot == 0)
return null;
var index = 0;
action = ToAction(Intrinsics.NthBitSet(snapshot, index) - 1);
action = ToAction(Intrinsics.NthBitSet(snapshot, 0) - 1);
newValue = snapshot & ~ToMask(action);
}
while (Interlocked.CompareExchange(ref bits, newValue, snapshot) != snapshot);
//if (i != 1)
//Console.WriteLine($"Retried {i-1} times");
return action;
}
+2 -2
View File
@@ -30,9 +30,9 @@ public sealed class ArenaNode<T> where T : struct
if (Data == null)
Interlocked.CompareExchange(ref Data, new ArenaNode<T>[BatchCount][], null);
var index = Interlocked.Increment(ref this.index) - 1;
var idx = Interlocked.Increment(ref this.index) - 1;
var (arrayIdx, subIdx) = GetArrayIndex(index);
var (arrayIdx, subIdx) = GetArrayIndex(idx);
if (Data[arrayIdx] == null)
Interlocked.CompareExchange(ref Data[arrayIdx], new ArenaNode<T>[BatchSize], null);
+4 -4
View File
@@ -7,12 +7,12 @@ public struct NodeScores
{
public float ScoreSum;
public float MaxScore;
public float Visits;
public int Visits;
public void Visit(float score)
{
ScoreSum += score;
MaxScore = Math.Max(MaxScore, score);
Visits++;
Intrinsics.CASAdd(ref ScoreSum, score);
Intrinsics.CASMax(ref MaxScore, score);
Interlocked.Increment(ref Visits);
}
}
+5 -4
View File
@@ -96,7 +96,7 @@ public sealed class Solver
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Node? EvalBestChild(float parentVisits, ref Node.ChildBuffer children)
private Node? EvalBestChild(int parentVisits, ref Node.ChildBuffer children)
{
if (parentVisits == 0)
return null;
@@ -110,7 +110,7 @@ public sealed class Solver
var CVector = new Vector<float>(C);
Span<float> scoreSums = stackalloc float[vecLength];
Span<float> visits = stackalloc float[vecLength];
Span<int> visits = stackalloc int[vecLength];
Span<float> maxScores = stackalloc float[vecLength];
var max = (0, 0);
@@ -130,8 +130,9 @@ public sealed class Solver
var s = new Vector<float>(scoreSums);
var m = new Vector<float>(maxScores);
var v = new Vector<float>(visits);
v = Vector.Max(v, Vector<float>.One);
var vInt = new Vector<int>(visits);
vInt = Vector.Max(vInt, Vector<int>.One);
var v = Vector.ConvertToSingle(vInt);
var exploitation = (W * (s / v)) + (w * m);
var exploration = CVector * Intrinsics.ReciprocalSqrt(v);
var evalScores = exploitation + exploration;