Change the success function to allow the order of the succesors to be controlled

This commit is contained in:
Lucas Scharenbroich 2016-11-30 16:13:43 -06:00
parent b7a3af9437
commit 5fc5a254ab
5 changed files with 10 additions and 8 deletions

View File

@ -6,7 +6,7 @@
public interface ISearchProblem<A, S, C> public interface ISearchProblem<A, S, C>
where C : IComparable<C> where C : IComparable<C>
{ {
IDictionary<A, S> Successors(S state); IEnumerable<Tuple<A, S>> Successors(S state);
bool IsGoal(S state); bool IsGoal(S state);
C StepCost(S fromState, A action, S toState); C StepCost(S fromState, A action, S toState);
C Heuristic(S state); C Heuristic(S state);

View File

@ -1,9 +1,10 @@
namespace SpriteCompiler.AI namespace SpriteCompiler.AI
{ {
using System;
using System.Collections.Generic; using System.Collections.Generic;
public interface ISuccessorFunction<A, S> public interface ISuccessorFunction<A, S>
{ {
IDictionary<A, S> Successors(S state); IEnumerable<Tuple<A, S>> Successors(S state);
} }
} }

View File

@ -13,8 +13,8 @@ namespace SpriteCompiler.AI
{ {
foreach (var successor in problem.Successors(node.State)) foreach (var successor in problem.Successors(node.State))
{ {
var action = successor.Key; var action = successor.Item1;
var state = successor.Value; var state = successor.Item2;
var next = CreateNode(node, state); var next = CreateNode(node, state);
next.Action = action; next.Action = action;

View File

@ -22,7 +22,7 @@ namespace SpriteCompiler.AI
this.heuristicFn = heuristicFn; this.heuristicFn = heuristicFn;
} }
public IDictionary<A, S> Successors(S state) public IEnumerable<Tuple<A, S>> Successors(S state)
{ {
return successorFn.Successors(state); return successorFn.Successors(state);
} }

View File

@ -7,7 +7,7 @@
public sealed class SpriteGeneratorSuccessorFunction : ISuccessorFunction<CodeSequence, SpriteGeneratorState> public sealed class SpriteGeneratorSuccessorFunction : ISuccessorFunction<CodeSequence, SpriteGeneratorState>
{ {
public IDictionary<CodeSequence, SpriteGeneratorState> Successors(SpriteGeneratorState state) public IEnumerable<Tuple<CodeSequence, SpriteGeneratorState>> Successors(SpriteGeneratorState state)
{ {
// This is the work-horse of the compiler. For a given state we need to enumerate all of the // This is the work-horse of the compiler. For a given state we need to enumerate all of the
// potential next operations. // potential next operations.
@ -26,7 +26,8 @@
// 3. Single-byte at the end of a solid run // 3. Single-byte at the end of a solid run
// a. If no registers are 8-bit, LDA #Imm/STA 0,s (8 cycles, sets Acc) // a. If no registers are 8-bit, LDA #Imm/STA 0,s (8 cycles, sets Acc)
// b. If any reg is already 8-bit, LDA #imm/PHA (6 cycles) // b. If any reg is already 8-bit, LDA #imm/PHA (6 cycles)
//
// We al
var actions = new List<CodeSequence>(); var actions = new List<CodeSequence>();
var bytes = state.Bytes.ToDictionary(x => x.Offset, x => x); var bytes = state.Bytes.ToDictionary(x => x.Offset, x => x);
@ -117,7 +118,7 @@
} }
// Run through the actions to create a dictionary // Run through the actions to create a dictionary
return actions.ToDictionary(x => x, x => x.Apply(state)); return actions.Select(x => Tuple.Create(x, x.Apply(state)));
} }
private Func<SpriteByte, bool> WithinRangeOf(int addr, int range) private Func<SpriteByte, bool> WithinRangeOf(int addr, int range)