Smaller minor optimizations
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
using Craftimizer.Simulator.Actions;
|
using Craftimizer.Simulator.Actions;
|
||||||
using System;
|
|
||||||
using System.Diagnostics.Contracts;
|
using System.Diagnostics.Contracts;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
@@ -52,11 +51,9 @@ public struct ActionSet
|
|||||||
public readonly bool IsEmpty => bits == 0;
|
public readonly bool IsEmpty => bits == 0;
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
//public readonly ActionType SelectRandom(Random random) => ElementAt(random.Next(Count));
|
public readonly ActionType SelectRandom(Random random) => ElementAt(random.Next(Count));
|
||||||
public readonly ActionType SelectRandom(Random random) => First();
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
//public ActionType? PopRandom(Random random) => PopFirst();
|
|
||||||
public ActionType? PopRandom(Random random)
|
public ActionType? PopRandom(Random random)
|
||||||
{
|
{
|
||||||
uint snapshot;
|
uint snapshot;
|
||||||
@@ -84,22 +81,16 @@ public struct ActionSet
|
|||||||
uint snapshot;
|
uint snapshot;
|
||||||
uint newValue;
|
uint newValue;
|
||||||
ActionType action;
|
ActionType action;
|
||||||
int i = 0;
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
++i;
|
|
||||||
snapshot = bits;
|
snapshot = bits;
|
||||||
if (snapshot == 0)
|
if (snapshot == 0)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var index = 0;
|
action = ToAction(Intrinsics.NthBitSet(snapshot, 0) - 1);
|
||||||
|
|
||||||
action = ToAction(Intrinsics.NthBitSet(snapshot, index) - 1);
|
|
||||||
newValue = snapshot & ~ToMask(action);
|
newValue = snapshot & ~ToMask(action);
|
||||||
}
|
}
|
||||||
while (Interlocked.CompareExchange(ref bits, newValue, snapshot) != snapshot);
|
while (Interlocked.CompareExchange(ref bits, newValue, snapshot) != snapshot);
|
||||||
//if (i != 1)
|
|
||||||
//Console.WriteLine($"Retried {i-1} times");
|
|
||||||
return action;
|
return action;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,9 +30,9 @@ public sealed class ArenaNode<T> where T : struct
|
|||||||
if (Data == null)
|
if (Data == null)
|
||||||
Interlocked.CompareExchange(ref Data, new ArenaNode<T>[BatchCount][], 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)
|
if (Data[arrayIdx] == null)
|
||||||
Interlocked.CompareExchange(ref Data[arrayIdx], new ArenaNode<T>[BatchSize], null);
|
Interlocked.CompareExchange(ref Data[arrayIdx], new ArenaNode<T>[BatchSize], null);
|
||||||
|
|||||||
@@ -7,12 +7,12 @@ public struct NodeScores
|
|||||||
{
|
{
|
||||||
public float ScoreSum;
|
public float ScoreSum;
|
||||||
public float MaxScore;
|
public float MaxScore;
|
||||||
public float Visits;
|
public int Visits;
|
||||||
|
|
||||||
public void Visit(float score)
|
public void Visit(float score)
|
||||||
{
|
{
|
||||||
ScoreSum += score;
|
Intrinsics.CASAdd(ref ScoreSum, score);
|
||||||
MaxScore = Math.Max(MaxScore, score);
|
Intrinsics.CASMax(ref MaxScore, score);
|
||||||
Visits++;
|
Interlocked.Increment(ref Visits);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ public sealed class Solver
|
|||||||
|
|
||||||
[Pure]
|
[Pure]
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
private Node? EvalBestChild(float parentVisits, ref Node.ChildBuffer children)
|
private Node? EvalBestChild(int parentVisits, ref Node.ChildBuffer children)
|
||||||
{
|
{
|
||||||
if (parentVisits == 0)
|
if (parentVisits == 0)
|
||||||
return null;
|
return null;
|
||||||
@@ -110,7 +110,7 @@ public sealed class Solver
|
|||||||
var CVector = new Vector<float>(C);
|
var CVector = new Vector<float>(C);
|
||||||
|
|
||||||
Span<float> scoreSums = stackalloc float[vecLength];
|
Span<float> scoreSums = stackalloc float[vecLength];
|
||||||
Span<float> visits = stackalloc float[vecLength];
|
Span<int> visits = stackalloc int[vecLength];
|
||||||
Span<float> maxScores = stackalloc float[vecLength];
|
Span<float> maxScores = stackalloc float[vecLength];
|
||||||
|
|
||||||
var max = (0, 0);
|
var max = (0, 0);
|
||||||
@@ -130,8 +130,9 @@ public sealed class Solver
|
|||||||
|
|
||||||
var s = new Vector<float>(scoreSums);
|
var s = new Vector<float>(scoreSums);
|
||||||
var m = new Vector<float>(maxScores);
|
var m = new Vector<float>(maxScores);
|
||||||
var v = new Vector<float>(visits);
|
var vInt = new Vector<int>(visits);
|
||||||
v = Vector.Max(v, Vector<float>.One);
|
vInt = Vector.Max(vInt, Vector<int>.One);
|
||||||
|
var v = Vector.ConvertToSingle(vInt);
|
||||||
var exploitation = (W * (s / v)) + (w * m);
|
var exploitation = (W * (s / v)) + (w * m);
|
||||||
var exploration = CVector * Intrinsics.ReciprocalSqrt(v);
|
var exploration = CVector * Intrinsics.ReciprocalSqrt(v);
|
||||||
var evalScores = exploitation + exploration;
|
var evalScores = exploitation + exploration;
|
||||||
|
|||||||
Reference in New Issue
Block a user