Initial setup for testing AI performance, random fixes
All checks were successful
Build / Build (push) Successful in 54s
All checks were successful
Build / Build (push) Successful in 54s
This commit is contained in:
89
AI/AIRunner/Program.cs
Normal file
89
AI/AIRunner/Program.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System.CommandLine;
|
||||
using System.CommandLine.Parsing;
|
||||
using System.Reflection;
|
||||
using PkmnLib.Dynamic.AI;
|
||||
using Serilog;
|
||||
|
||||
namespace AIRunner;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
private static List<PokemonAI>? _availableAIs;
|
||||
|
||||
private static Task<int> Main(string[] args)
|
||||
{
|
||||
Log.Logger = new LoggerConfiguration().MinimumLevel.Debug().WriteTo.Console().CreateLogger();
|
||||
Log.Information("Starting AI Runner...");
|
||||
_availableAIs = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes())
|
||||
.Where(type => type.IsSubclassOf(typeof(PokemonAI)) && !type.IsAbstract).Select(Activator.CreateInstance)
|
||||
.Cast<PokemonAI>().ToList();
|
||||
|
||||
var testCommand = new Command("test", "Run two AIs against each other")
|
||||
{
|
||||
new Option<string>("--ai1")
|
||||
{
|
||||
Description = "The name of the first AI script to run against the second AI.",
|
||||
Required = true,
|
||||
Validators = { ValidateAI },
|
||||
},
|
||||
new Option<string>("--ai2")
|
||||
{
|
||||
Description = "The name of the second AI script to run against the first AI.",
|
||||
Required = true,
|
||||
Validators = { ValidateAI },
|
||||
},
|
||||
new Option<int>("--battles")
|
||||
{
|
||||
Description = "The number of battles to run between the two AIs.",
|
||||
Required = false,
|
||||
DefaultValueFactory = _ => 100,
|
||||
Validators =
|
||||
{
|
||||
result =>
|
||||
{
|
||||
if (result.GetValueOrDefault<int>() <= 0)
|
||||
{
|
||||
result.AddError("--battles must be a positive integer.");
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
testCommand.SetAction(result =>
|
||||
{
|
||||
var ai1Name = result.GetRequiredValue<string>("--ai1");
|
||||
var ai2Name = result.GetRequiredValue<string>("--ai2");
|
||||
var ai1 = _availableAIs!.First(a =>
|
||||
string.Equals(a.Name, ai1Name, StringComparison.InvariantCultureIgnoreCase));
|
||||
var ai2 = _availableAIs!.First(a =>
|
||||
string.Equals(a.Name, ai2Name, StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
return TestCommandRunner.RunTestCommand(ai1, ai2, result.GetRequiredValue<int>("--battles"));
|
||||
});
|
||||
|
||||
var rootCommand = new RootCommand("PkmnLib.NET AI Runner")
|
||||
{
|
||||
testCommand,
|
||||
};
|
||||
rootCommand.Description = "A tool to run AI scripts against each other in Pokémon battles.";
|
||||
var parseResult = rootCommand.Parse(args);
|
||||
return parseResult.InvokeAsync();
|
||||
|
||||
static void ValidateAI(OptionResult result)
|
||||
{
|
||||
var aiName = result.GetValueOrDefault<string>();
|
||||
if (string.IsNullOrWhiteSpace(aiName))
|
||||
{
|
||||
result.AddError("must be a non-empty string.");
|
||||
return;
|
||||
}
|
||||
|
||||
var ai = _availableAIs!.FirstOrDefault(a => a.Name == aiName);
|
||||
if (ai == null)
|
||||
{
|
||||
result.AddError(
|
||||
$"AI '{aiName}' not found. Available AIs: {string.Join(", ", _availableAIs!.Select(a => a.Name))}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user