Fix issue of keeping dirty queue between runs

This commit is contained in:
Lucas Scharenbroich 2017-02-24 23:07:51 -06:00
parent fda5e268d7
commit c530b88604
5 changed files with 25 additions and 11 deletions

View File

@ -152,10 +152,10 @@ namespace AI.Test
public void EightPuzzleTest() public void EightPuzzleTest()
{ {
// Number of trials to run // Number of trials to run
int N = 10; int N = 100;
// Maximum depth of the solution // Maximum depth of the solution
int dmax = 6; int dmax = 10;
// Now we define the search algorithm and the type of node expansion. Russell & Norvig discuss // Now we define the search algorithm and the type of node expansion. Russell & Norvig discuss
// two type of expansion strategies: tree search and graph search. One will avoid cycles in the search // two type of expansion strategies: tree search and graph search. One will avoid cycles in the search
@ -166,8 +166,8 @@ namespace AI.Test
var treeSearch = new TreeSearch<Direction, EightPuzzleBoard, EightPuzzleNode, IntegerCost>(expander); var treeSearch = new TreeSearch<Direction, EightPuzzleBoard, EightPuzzleNode, IntegerCost>(expander);
var ids = new IterativeDeepeningSearch<Direction, EightPuzzleBoard, EightPuzzleNode, IntegerCost>(treeSearch, dmax); var ids = new IterativeDeepeningSearch<Direction, EightPuzzleBoard, EightPuzzleNode, IntegerCost>(treeSearch, dmax);
var aStarH1 = new AStarSearch<Direction, EightPuzzleBoard, EightPuzzleNode, IntegerCost>(treeSearch, new QueueAdapter<EightPuzzleNode, IntegerCost>()); var aStarH1 = new AStarSearch<Direction, EightPuzzleBoard, EightPuzzleNode, IntegerCost>(treeSearch, () => new QueueAdapter<EightPuzzleNode, IntegerCost>());
var aStarH2 = new IterativeDeepeningAStarSearch<Direction, EightPuzzleBoard, EightPuzzleNode, IntegerCost>(treeSearch, (IntegerCost)dmax); var aStarH2 = new AStarSearch<Direction, EightPuzzleBoard, EightPuzzleNode, IntegerCost>(treeSearch, () => new QueueAdapter<EightPuzzleNode, IntegerCost>());
// Depth runs from 0 to dmax // Depth runs from 0 to dmax
int[,] d = new int[dmax + 1, 3]; int[,] d = new int[dmax + 1, 3];
@ -210,6 +210,8 @@ namespace AI.Test
System.Diagnostics.Trace.WriteLine("A* (h2) Solution has depth " + depth + " nodes and expanded " + expander[IntrumentedParameters.NODES_EXPANDED] + " nodes"); System.Diagnostics.Trace.WriteLine("A* (h2) Solution has depth " + depth + " nodes and expanded " + expander[IntrumentedParameters.NODES_EXPANDED] + " nodes");
d[depth, 2] += 1; d[depth, 2] += 1;
n[depth, 2] += expander[IntrumentedParameters.NODES_EXPANDED]; n[depth, 2] += expander[IntrumentedParameters.NODES_EXPANDED];
// PrintSolution(solution);
} }
} }
} }
@ -218,7 +220,7 @@ namespace AI.Test
Trace.WriteLine("| d| IDS | A*(h1) | A*(h2) || IDS | A*(h1) | A*(h2) |"); Trace.WriteLine("| d| IDS | A*(h1) | A*(h2) || IDS | A*(h1) | A*(h2) |");
Trace.WriteLine("+--+---------+--------+--------++---------+--------+--------+"); Trace.WriteLine("+--+---------+--------+--------++---------+--------+--------+");
for (int i = 0; i <= dmax; i++) for (int i = 1; i <= dmax; i++)
{ {
var bf0 = ComputeBranchingFactor((float)n[i, 0] / (float)d[i, 0], i); var bf0 = ComputeBranchingFactor((float)n[i, 0] / (float)d[i, 0], i);
var bf1 = ComputeBranchingFactor((float)n[i, 1] / (float)d[i, 1], i); var bf1 = ComputeBranchingFactor((float)n[i, 1] / (float)d[i, 1], i);

View File

@ -1,12 +1,13 @@
namespace SpriteCompiler.AI namespace SpriteCompiler.AI
{ {
using Queue; using Queue;
using System;
public class AStarSearch<A, S, T, C> : BestFirstSearch<A, S, T, C> public class AStarSearch<A, S, T, C> : BestFirstSearch<A, S, T, C>
where T : IHeuristicSearchNode<A, S, T, C> where T : IHeuristicSearchNode<A, S, T, C>
where C : ICost<C>, new() where C : ICost<C>, new()
{ {
public AStarSearch(ISearchStrategy<A, S, T, C> search, IQueue<T> fringe) public AStarSearch(ISearchStrategy<A, S, T, C> search, Func<IQueue<T>> fringe)
: base(search, fringe) : base(search, fringe)
{ {
} }

View File

@ -1,13 +1,14 @@
namespace SpriteCompiler.AI namespace SpriteCompiler.AI
{ {
using Queue; using Queue;
using System;
public class BestFirstSearch<A, S, T, C> : AbstractStateSpaceSearch<A, S, T, C> public class BestFirstSearch<A, S, T, C> : AbstractStateSpaceSearch<A, S, T, C>
where T : ISearchNode<A, S, T, C> where T : ISearchNode<A, S, T, C>
where C : ICost<C> where C : ICost<C>
{ {
public BestFirstSearch(ISearchStrategy<A, S, T, C> strategy, IQueue<T> fringe) public BestFirstSearch(ISearchStrategy<A, S, T, C> strategy, Func<IQueue<T>> fringe)
: base(strategy, () => fringe) : base(strategy, fringe)
{ {
} }
} }

View File

@ -1,6 +1,8 @@
namespace SpriteCompiler.Problem namespace SpriteCompiler.Problem
{ {
using SpriteCompiler.AI; using SpriteCompiler.AI;
using SpriteCompiler.AI.Queue;
using System;
public sealed class SpriteGeneratorSearchProblem public sealed class SpriteGeneratorSearchProblem
{ {
@ -18,7 +20,7 @@
{ {
var expander = new SpriteGeneratorNodeExpander(); var expander = new SpriteGeneratorNodeExpander();
var strategy = new TreeSearch<CodeSequence, SpriteGeneratorState, SpriteGeneratorSearchNode, IntegerCost>(expander); var strategy = new TreeSearch<CodeSequence, SpriteGeneratorState, SpriteGeneratorSearchNode, IntegerCost>(expander);
var queue = new Adapters.QueueAdapter<SpriteGeneratorSearchNode, IntegerCost>(); Func<IQueue<SpriteGeneratorSearchNode>> queue = () => new Adapters.QueueAdapter<SpriteGeneratorSearchNode, IntegerCost>();
return new AStarSearch<CodeSequence, SpriteGeneratorState, SpriteGeneratorSearchNode, IntegerCost>(strategy, queue); return new AStarSearch<CodeSequence, SpriteGeneratorState, SpriteGeneratorSearchNode, IntegerCost>(strategy, queue);
} }
@ -27,7 +29,6 @@
{ {
var expander = new SpriteGeneratorNodeExpander(); var expander = new SpriteGeneratorNodeExpander();
var strategy = new TreeSearch<CodeSequence, SpriteGeneratorState, SpriteGeneratorSearchNode, IntegerCost>(expander); var strategy = new TreeSearch<CodeSequence, SpriteGeneratorState, SpriteGeneratorSearchNode, IntegerCost>(expander);
var queue = new Adapters.QueueAdapter<SpriteGeneratorSearchNode, IntegerCost>();
var maxCost = (IntegerCost)maxCycles; var maxCost = (IntegerCost)maxCycles;
return new IterativeDeepeningAStarSearch<CodeSequence, SpriteGeneratorState, SpriteGeneratorSearchNode, IntegerCost>(strategy, maxCost); return new IterativeDeepeningAStarSearch<CodeSequence, SpriteGeneratorState, SpriteGeneratorSearchNode, IntegerCost>(strategy, maxCost);

View File

@ -138,8 +138,17 @@
var firstByteDistance = firstByte.Offset - state.S.Value; var firstByteDistance = firstByte.Offset - state.S.Value;
if (state.S.IsScreenOffset && firstByteDistance >= 256 && state.LongA) if (state.S.IsScreenOffset && firstByteDistance >= 256 && state.LongA)
{ {
// If the accumulator is not set to the stack
if (!state.A.IsScreenOffset)
{
yield return state.Apply(new TSC());
}
// Go to the next byte, or the first solid edge // Go to the next byte, or the first solid edge
yield return state.Apply(new MOVE_STACK(firstByteDistance)); else
{
yield return state.Apply(new MOVE_STACK(firstByteDistance));
}
yield break; yield break;
} }