From c530b886046d70e1aea6a70a28765050c5682ca1 Mon Sep 17 00:00:00 2001 From: Lucas Scharenbroich Date: Fri, 24 Feb 2017 23:07:51 -0600 Subject: [PATCH] Fix issue of keeping dirty queue between runs --- AI.Test/SearchTest.cs | 12 +++++++----- SpriteCompiler/AI/AStarSearch.cs | 3 ++- SpriteCompiler/AI/BestFirstSearch.cs | 5 +++-- .../Problem/SpriteGeneratorSearchProblem.cs | 5 +++-- .../Problem/SpriteGeneratorSuccessorFunction.cs | 11 ++++++++++- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/AI.Test/SearchTest.cs b/AI.Test/SearchTest.cs index 0f24f52..67ba621 100644 --- a/AI.Test/SearchTest.cs +++ b/AI.Test/SearchTest.cs @@ -152,10 +152,10 @@ namespace AI.Test public void EightPuzzleTest() { // Number of trials to run - int N = 10; + int N = 100; // 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 // 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(expander); var ids = new IterativeDeepeningSearch(treeSearch, dmax); - var aStarH1 = new AStarSearch(treeSearch, new QueueAdapter()); - var aStarH2 = new IterativeDeepeningAStarSearch(treeSearch, (IntegerCost)dmax); + var aStarH1 = new AStarSearch(treeSearch, () => new QueueAdapter()); + var aStarH2 = new AStarSearch(treeSearch, () => new QueueAdapter()); // Depth runs from 0 to dmax 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"); d[depth, 2] += 1; 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("+--+---------+--------+--------++---------+--------+--------+"); - 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 bf1 = ComputeBranchingFactor((float)n[i, 1] / (float)d[i, 1], i); diff --git a/SpriteCompiler/AI/AStarSearch.cs b/SpriteCompiler/AI/AStarSearch.cs index 6020fbf..6108db4 100644 --- a/SpriteCompiler/AI/AStarSearch.cs +++ b/SpriteCompiler/AI/AStarSearch.cs @@ -1,12 +1,13 @@ namespace SpriteCompiler.AI { using Queue; + using System; public class AStarSearch : BestFirstSearch where T : IHeuristicSearchNode where C : ICost, new() { - public AStarSearch(ISearchStrategy search, IQueue fringe) + public AStarSearch(ISearchStrategy search, Func> fringe) : base(search, fringe) { } diff --git a/SpriteCompiler/AI/BestFirstSearch.cs b/SpriteCompiler/AI/BestFirstSearch.cs index 99f0698..98ca830 100644 --- a/SpriteCompiler/AI/BestFirstSearch.cs +++ b/SpriteCompiler/AI/BestFirstSearch.cs @@ -1,13 +1,14 @@ namespace SpriteCompiler.AI { using Queue; + using System; public class BestFirstSearch : AbstractStateSpaceSearch where T : ISearchNode where C : ICost { - public BestFirstSearch(ISearchStrategy strategy, IQueue fringe) - : base(strategy, () => fringe) + public BestFirstSearch(ISearchStrategy strategy, Func> fringe) + : base(strategy, fringe) { } } diff --git a/SpriteCompiler/Problem/SpriteGeneratorSearchProblem.cs b/SpriteCompiler/Problem/SpriteGeneratorSearchProblem.cs index 463331a..130a394 100644 --- a/SpriteCompiler/Problem/SpriteGeneratorSearchProblem.cs +++ b/SpriteCompiler/Problem/SpriteGeneratorSearchProblem.cs @@ -1,6 +1,8 @@ namespace SpriteCompiler.Problem { using SpriteCompiler.AI; + using SpriteCompiler.AI.Queue; + using System; public sealed class SpriteGeneratorSearchProblem { @@ -18,7 +20,7 @@ { var expander = new SpriteGeneratorNodeExpander(); var strategy = new TreeSearch(expander); - var queue = new Adapters.QueueAdapter(); + Func> queue = () => new Adapters.QueueAdapter(); return new AStarSearch(strategy, queue); } @@ -27,7 +29,6 @@ { var expander = new SpriteGeneratorNodeExpander(); var strategy = new TreeSearch(expander); - var queue = new Adapters.QueueAdapter(); var maxCost = (IntegerCost)maxCycles; return new IterativeDeepeningAStarSearch(strategy, maxCost); diff --git a/SpriteCompiler/Problem/SpriteGeneratorSuccessorFunction.cs b/SpriteCompiler/Problem/SpriteGeneratorSuccessorFunction.cs index f13bb16..478fde8 100644 --- a/SpriteCompiler/Problem/SpriteGeneratorSuccessorFunction.cs +++ b/SpriteCompiler/Problem/SpriteGeneratorSuccessorFunction.cs @@ -138,8 +138,17 @@ var firstByteDistance = firstByte.Offset - state.S.Value; 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 - yield return state.Apply(new MOVE_STACK(firstByteDistance)); + else + { + yield return state.Apply(new MOVE_STACK(firstByteDistance)); + } yield break; }