public void TestAcceptableState() { int[] state1 = { 1, 2, 3, 4, 5, 6, 8, 7, 9 }; Assert.AreEqual(NPuzzleUtils.AcceptableState(state1), false); int[] state2 = { 1, 2, 3, 4, 8, 5, 6, 7, 9 }; Assert.AreEqual(NPuzzleUtils.AcceptableState(state2), false); int[] state3 = { 2, 1, 4, 3, 5, 6, 7, 8, 9 }; Assert.AreEqual(NPuzzleUtils.AcceptableState(state3), true); }
public void TestAStarGraphSearch() { int size = 9; int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal); NPuzzleState <int[]> initial = NPuzzleUtils.GenerateInitState(size); Assert.True(NPuzzleUtils.AcceptableState(initial.State)); Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> npuzzle = new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(goalState, initial); Assert.AreEqual(npuzzle.InitialState, initial); Assert.AreEqual(npuzzle.GoalState, goal); //Heuristics.HeuristicFunction<NPuzzleState<int[]>,int, int> handler = Heuristics.NPuzzleHeuristics.ManhattanDistance; Heuristics.Heurfun <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > handler = Heuristics.NPuzzleHeuristics.AStarManhattanDistance; try { Assert.AreEqual(npuzzle.InitialState, initial); // Search.BestFirstGraphSearch<int[],int,int> bfgs = new Search.BestFirstGraphSearch<int[],int,int>(npuzzle, handler); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> initialNode = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(npuzzle.InitialState); // Search.PriorityQueue<int, SearchTreeNode.Node<NPuzzleState<int[]>,int,int>> frontier = new Search.PriorityQueue<int, SearchTreeNode.Node<NPuzzleState<int[]>,int,int>>(); Search.AStarGraphSearch <NPuzzleState <int[]>, int, int> asgs = new Search.AStarGraphSearch <NPuzzleState <int[]>, int, int>(npuzzle, handler); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node = asgs.Search(); List <int> solution = node.Solution(); Console.WriteLine("Printing solution to AStar:"); solution.ForEach(delegate(int a) { Console.Write("{0} ", a); }); Console.WriteLine(""); } catch (NPuzzleUtils.InvalidProblemException ex) { System.Console.WriteLine("There is an InvalidProblemException here doode"); System.Console.WriteLine(ex.Message); throw ex; } catch (NPuzzleUtils.InvalidProblemPropertyException ex) { throw ex; } catch (System.NullReferenceException ex) { Console.WriteLine(ex); } }
/*! * Generate a random acceptable initial state * */ public static Problem.NPuzzleState <int[]> GenerateInitState(int size) { int[] state = new int[size]; for (int i = 0; i < size; i++) { state[i] = i + 1; } NPuzzleUtils.Shuffle(state); while (!NPuzzleUtils.AcceptableState(state)) { NPuzzleUtils.Shuffle(state); } return(new Problem.NPuzzleState <int[]>(state)); }
public void GenerateInitStateTest() { NPuzzleState <int[]> state; int size = 9; state = NPuzzleUtils.GenerateInitState(size); Assert.That(state.State, Has.Exactly(1).EqualTo(size)); Assert.That(state.State, Has.Exactly(1).EqualTo(size - 4)); Assert.That(state.State, Has.Exactly(size - 1).LessThan(size)); Assert.That(state.State, Has.Exactly(size - 1).GreaterThan(1)); Assert.That(state.State, Has.Exactly(size - 4).GreaterThan(4)); Assert.AreEqual(NPuzzleUtils.AcceptableState(state.State), true); Console.WriteLine(state.ToString()); }
private bool ValidInitialState(int[] initial) { return(NPuzzleUtils.AcceptableState(initial)); }