namespace OECS.PlayTest;
///
/// A pool of agents selected by weight. Higher weight = more likely to be picked.
///
public class WeightedAgentPool
{
private readonly List<(IAgent Agent, int Weight)> _agents = new();
private int _totalWeight;
private static readonly Random _rng = new();
public void Add(IAgent agent, int weight)
{
_agents.Add((agent, weight));
_totalWeight += weight;
}
public IAgent Pick()
{
if (_agents.Count == 0)
throw new InvalidOperationException("WeightedAgentPool is empty");
int roll = _rng.Next(_totalWeight);
int cumulative = 0;
foreach (var (agent, weight) in _agents)
{
cumulative += weight;
if (roll < cumulative)
return agent;
}
return _agents[^1].Agent;
}
}