public void Evaluate( IPolicy <TState, TAction> policy, IRewarder <TState, TAction> rewarder, int sweepLimit = -1) { var numSweeps = 0; var largestValueChange = 0.0; do { largestValueChange = 0.0; foreach (var state in _problem.AllStates()) { var originalValue = Value(state); var newValue = CalculateValue(state, policy, rewarder); _values[state] = newValue; var valueChange = Math.Abs(originalValue - newValue); if (valueChange > largestValueChange) { largestValueChange = valueChange; } } if (sweepLimit > 0 && ++numSweeps == sweepLimit) { break; } } while (largestValueChange > 0.000001); }
public static GreedyPolicy <TState, TAction> Create( IProblem <TState, TAction> problem, ValueTable <TState, TAction> valueTable, IRewarder <TState, TAction> rewarder) { var greedyPolicy = new GreedyPolicy <TState, TAction>(problem); foreach (var state in problem.AllStates()) { var bestAction = FindBestAction(problem, state, valueTable, rewarder); greedyPolicy._actions[state] = bestAction; } return(greedyPolicy); }
private static bool AllValuesAreEqual( IProblem <GamblersWorldState, GamblersWorldAction> problem, ValueTable <GamblersWorldState, GamblersWorldAction> genericValues, GamblersValueTable gamblersValues) { foreach (var state in problem.AllStates()) { var genericValue = genericValues.Value(state); var gamblerValue = gamblersValues.Value(state); Assert.AreEqual(genericValue, gamblerValue, 0.01, $"values not equal for state {state}. " + $"generic: {genericValue}, gambler: {gamblerValue}"); } return(true); }
private static bool AllValuesAreEqual( IProblem <GridWorldState, GridWorldAction> problem, ValueTable <GridWorldState, GridWorldAction> genericValues, GridWorldValueTable gridValues) { foreach (var state in problem.AllStates()) { var genericValue = genericValues.Value(state); var gridValue = gridValues.Value(state); Console.WriteLine($"generic: {genericValue}, grid {gridValue}"); // Assert.AreEqual(genericValue, gridValue, 0.01, // $"values not equal for state {state}. " + // $"generic: {genericValue}, grid: {gridValue}"); } return(true); }
public ValueTable(IProblem <TState, TAction> problem) { _problem = problem; _values = problem.AllStates().ToDictionary(s => s, s => 0.0); }