feat(backend): PolicyRunner ONNX inference wrapper
This commit is contained in:
55
Backend.Tests/PolicyRunnerTests.cs
Normal file
55
Backend.Tests/PolicyRunnerTests.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System.IO;
|
||||
using Backend;
|
||||
using Xunit;
|
||||
|
||||
namespace Backend.Tests;
|
||||
|
||||
public class PolicyRunnerTests
|
||||
{
|
||||
private static string LocateOnnxModel()
|
||||
{
|
||||
var dir = AppContext.BaseDirectory;
|
||||
while (dir is not null)
|
||||
{
|
||||
var candidate = Path.Combine(dir, "models", "ppo_lander.onnx");
|
||||
if (File.Exists(candidate)) return candidate;
|
||||
var parent = Directory.GetParent(dir);
|
||||
if (parent is null) break;
|
||||
dir = parent.FullName;
|
||||
}
|
||||
throw new FileNotFoundException(
|
||||
"models/ppo_lander.onnx not found — run `python Training/export_onnx.py ...`");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LoadsWithoutError()
|
||||
{
|
||||
using var runner = new PolicyRunner(LocateOnnxModel());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectAction_ReturnsValidActionForZeroObs()
|
||||
{
|
||||
using var runner = new PolicyRunner(LocateOnnxModel());
|
||||
var obs = new float[7]; // all zeros
|
||||
int action = runner.SelectAction(obs);
|
||||
Assert.InRange(action, 0, 3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectAction_IsDeterministicForSameInput()
|
||||
{
|
||||
using var runner = new PolicyRunner(LocateOnnxModel());
|
||||
var obs = new float[] { 0.1f, -0.2f, 0.05f, 0.0f, 0.0f, 1.0f, 0.0f };
|
||||
int a1 = runner.SelectAction(obs);
|
||||
int a2 = runner.SelectAction(obs);
|
||||
Assert.Equal(a1, a2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SelectAction_ThrowsIfObsLengthWrong()
|
||||
{
|
||||
using var runner = new PolicyRunner(LocateOnnxModel());
|
||||
Assert.Throws<ArgumentException>(() => runner.SelectAction(new float[3]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user