public void TestNPuzzleProblemAcceptableAction() { int[] state = { 5, 6, 2, 8, 9, 3, 1, 4, 7 }; NPuzzleState <int[]> initialState = new NPuzzleState <int[]>(state); Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = CreateProblem(initialState); Assert.True(problem.AcceptableAction(initialState.State, 4, -1)); Assert.True(problem.AcceptableAction(initialState.State, 4, 1)); Assert.True(problem.AcceptableAction(initialState.State, 4, -2)); Assert.True(problem.AcceptableAction(initialState.State, 4, 2)); Assert.False(problem.AcceptableAction(initialState.State, 4, 3)); Assert.False(problem.AcceptableAction(initialState.State, 4, 0)); int[] state2 = { 4, 9, 8, 2, 3, 5, 6, 7, 1 }; NPuzzleState <int[]> state2State = new NPuzzleState <int[]>(state2); problem = CreateProblem(state2State); NPuzzleState <int[]> initialState2 = new NPuzzleState <int[]>(state2); Assert.True(problem.AcceptableAction(initialState2.State, 1, -1)); Assert.True(problem.AcceptableAction(initialState2.State, 1, 1)); Assert.True(problem.AcceptableAction(initialState2.State, 1, -2)); }
public void TestNPuzzleInitialState() { int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int[] initial = { 1, 2, 3, 4, 5, 6, 7, 9, 8 }; NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal); NPuzzleState <int[]> initialState = new NPuzzleState <int[]>(initial); try { Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(goalState, initialState); Assert.NotNull(problem); Assert.NotNull(problem.InitialState); Assert.AreEqual(problem.InitialState, initialState); Problem.AbstractProblem <NPuzzleState <int[]>, int, int> p2 = new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(goalState, initialState); Assert.NotNull(p2); Assert.True(p2.GoalTest(goalState)); Assert.AreEqual(p2.Result(initialState, 1), goalState); List <int> results = new List <int>(); results.Add(1); results.Add(-1); results.Add(2); Assert.AreEqual(p2.Actions(initialState), results); } catch (NPuzzleUtils.InvalidNPuzzleStatesException ex) { } }
public void TestSearchClassCreation() { int size = 9; Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = NPuzzleUtils.CreateProblem(size); Heuristics.HeuristicFunction <NPuzzleState <int[]>, int, int> handler = Heuristics.NPuzzleHeuristics.ManhattanDistance; //Search.BestFirstGraphSearch<int[], int, int, int> bfgs = new Search.BestFirstGraphSearch<int[], int, int, int>(problem, handler); }
public void TestExpand() { int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> rootNode = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(goalState); List <SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > expandedNodes; Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = NPuzzleUtils.CreateProblem(9); expandedNodes = rootNode.Expand(problem); List <SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > testNodes = new List <SearchTreeNode.Node <NPuzzleState <int[]>, int, int> >(); //! actions for goal are -1 and 2 //! state resulting from action 2 int[] s1 = { 1, 2, 3, 4, 5, 9, 7, 8, 6 }; NPuzzleState <int[]> s1State = new NPuzzleState <int[]>(s1); //! state resulting from -1 int[] s2 = { 1, 2, 3, 4, 5, 6, 7, 9, 8 }; NPuzzleState <int[]> s2State = new NPuzzleState <int[]>(s2); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node2 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(s1State, rootNode, 2, 1); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node3 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(s2State, rootNode, -1, 1); testNodes.Add(node3); testNodes.Add(node2); Assert.AreEqual(testNodes.Count, expandedNodes.Count); Assert.AreEqual(expandedNodes[0].state, node3.state); // Assert.True(expandedNodes[0].state.Equals( node3.state)); Assert.AreEqual(expandedNodes[0].parent, node3.parent); Assert.True(expandedNodes[0].parent.Equals(node3.parent)); Assert.AreEqual(expandedNodes[0].action, node3.action); Assert.True(expandedNodes[0].action.Equals(node3.action)); Assert.AreEqual(expandedNodes[0].depth, node3.depth); Assert.True(expandedNodes[0].depth.Equals(node3.depth)); Assert.AreEqual(expandedNodes[0].pathCost, node3.pathCost); Assert.True(expandedNodes[0].pathCost.Equals(node3.pathCost)); // Assert.True(expandedNodes[0].Equals(node3)); // Assert.AreEqual(expandedNodes[0], node3); // Assert.AreEqual(expandedNodes[1], node2); // CollectionAssert.AreEqual(testNodes, expandedNodes); // Assert.That(testNodes, Is.EquivalentTo(expandedNodes)); }
public void TestNPuzzleProblemCreation() { int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int[] initial = { 1, 2, 3, 4, 5, 6, 7, 9, 8 }; NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal); NPuzzleState <int[]> initialState = new NPuzzleState <int[]>(initial); Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(goalState, initialState); problem.Actions(goalState); Assert.Throws <NPuzzleUtils.InvalidNPuzzleStatesException>(() => new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(initialState, goalState)); }
public void TestNPuzzleProblemResult() { int[] state = { 7, 6, 4, 9, 8, 2, 5, 3, 1 }; NPuzzleState <int[]> initialState = new NPuzzleState <int[]>(state); //! acceptable actions are -2, 2, 1 //! -2 int[] r1 = { 7, 6, 4, 5, 8, 2, 9, 3, 1 }; NPuzzleState <int[]> r1State = new NPuzzleState <int[]>(r1); int[] r2 = { 9, 6, 4, 7, 8, 2, 5, 3, 1 }; NPuzzleState <int[]> r2State = new NPuzzleState <int[]>(r2); int[] r3 = { 7, 6, 4, 8, 9, 2, 5, 3, 1 }; NPuzzleState <int[]> r3State = new NPuzzleState <int[]>(r3); int[] s2 = { 8, 5, 2, 9, 3, 7, 4, 6, 1 }; NPuzzleState <int[]> s2State = new NPuzzleState <int[]>(s2); // 2 int[] s2r1 = { 9, 5, 2, 8, 3, 7, 4, 6, 1 }; NPuzzleState <int[]> s2r1State = new NPuzzleState <int[]>(s2r1); // -2 int[] s2r2 = { 8, 5, 2, 4, 3, 7, 9, 6, 1 }; NPuzzleState <int[]> s2r2State = new NPuzzleState <int[]>(s2r2); // 1 int[] s2r3 = { 8, 5, 2, 3, 9, 7, 4, 6, 1 }; NPuzzleState <int[]> s2r3State = new NPuzzleState <int[]>(s2r3); Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = CreateProblem(initialState); CollectionAssert.AreEquivalent(r1State, problem.Result(initialState, -2)); CollectionAssert.AreEquivalent(r2State, problem.Result(initialState, 2)); CollectionAssert.AreEquivalent(r3State, problem.Result(initialState, 2)); CollectionAssert.AreEquivalent(s2r1State, problem.Result(s2State, 2)); CollectionAssert.AreEquivalent(s2r2State, problem.Result(s2State, -2)); CollectionAssert.AreEquivalent(s2r3State, problem.Result(s2State, 1)); Assert.Throws <NPuzzleUtils.ResultAcceptableActionException>(() => problem.Result(initialState, 0)); }
public void TestNPuzzleProblemGoalTest() { int[] state = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; NPuzzleState <int[]> initialState = new NPuzzleState <int[]>(state); int[] state2 = { 1, 2, 3, 4, 5, 6, 7, 9, 8 }; NPuzzleState <int[]> initialState2 = new NPuzzleState <int[]>(state2); Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = CreateProblem(initialState); Assert.False(problem.GoalTest(initialState2)); Assert.True(problem.GoalTest(initialState)); }
public void TestSomething() { 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); Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> npuzzle = new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(goalState, initial); HashSet <NPuzzleState <int[]> > explored = new HashSet <NPuzzleState <int[]> >(); //! first node in the frontier is the initialState SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(npuzzle.InitialState); }
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); } }
public void TestNPuzzleProblemActions() { int[] initial = { 1, 2, 3, 4, 5, 6, 7, 9, 8 }; NPuzzleState <int[]> initialState = new NPuzzleState <int[]>(initial); Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = CreateProblem(initialState); List <int> actions = problem.Actions(initialState); List <int> expected = new List <int>(); expected.Add(-1); expected.Add(1); expected.Add(2); CollectionAssert.AreEquivalent(expected, actions); int[] initial2 = { 9, 7, 4, 2, 6, 3, 5, 8, 1 }; NPuzzleState <int[]> initialState2 = new NPuzzleState <int[]>(initial2); problem = CreateProblem(initialState2); actions = problem.Actions(initialState2); expected = new List <int>(); expected.Add(-2); expected.Add(1); CollectionAssert.AreEquivalent(expected, actions); int[] initial3 = { 2, 4, 8, 6, 3, 9, 7, 1, 5 }; NPuzzleState <int[]> initialState3 = new NPuzzleState <int[]>(initial3); problem = CreateProblem(initialState3); actions = problem.Actions(initialState3); expected = new List <int>(); expected.Add(-2); expected.Add(2); expected.Add(-1); CollectionAssert.AreEquivalent(expected, actions); int[] initial4 = { 2, 4, 8, 6, 9, 3, 7, 1, 5 }; NPuzzleState <int[]> initialState4 = new NPuzzleState <int[]>(initial4); problem = CreateProblem(initialState4); actions = problem.Actions(initialState4); expected = new List <int>(); expected.Add(-2); expected.Add(2); expected.Add(-1); expected.Add(1); CollectionAssert.AreEquivalent(expected, actions); }
public void TestNPuzzleProblemGetEmptyIndex() { int[] state = { 9, 7, 3, 6, 5, 2, 8, 1, 4 }; NPuzzleState <int[]> initialState = new NPuzzleState <int[]>(state); Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = CreateProblem(initialState); Assert.AreEqual(problem.GetEmptyIndex(initialState.State), 0); int[] state2 = { 1, 7, 6, 5, 4, 3, 8, 2, 9 }; NPuzzleState <int[]> initialState2 = new NPuzzleState <int[]>(state2); Assert.AreEqual(problem.GetEmptyIndex(initialState2.State), 8); int[] state3 = { 1, 2, 3, 4, 5, 6, 7, 8, 8 }; NPuzzleState <int[]> initialState3 = new NPuzzleState <int[]>(state3); // Assert.Throws<NPuzzleUtils.MissingEmptyElementException>(() =>problem.GetEmptyIndex(state3)); Assert.AreEqual(problem.GetEmptyIndex(initialState3.State), -1); }
public void TestChildNode() { int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal); //! action -1 int[] s1 = { 1, 2, 3, 4, 5, 9, 7, 8, 6 }; NPuzzleState <int[]> s1State = new NPuzzleState <int[]>(s1); int[] s2 = { 1, 2, 3, 4, 5, 6, 7, 9, 8 }; NPuzzleState <int[]> s2State = new NPuzzleState <int[]>(s2); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(goalState); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> childNode = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(s2State, node, -1, 1); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> childNode2 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(s1State, node, 2, 1); Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = NPuzzleUtils.CreateProblem(9); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> expectedNode = node.ChildNode(problem, -1); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> expectedNode2 = node.ChildNode(problem, 2); //! Tests Assert.AreEqual(node.depth, 0); Assert.AreEqual(expectedNode.parent, childNode.parent); Assert.AreEqual(expectedNode.pathCost, childNode.pathCost); Assert.AreEqual(expectedNode.state, childNode.state); Assert.AreEqual(expectedNode.action, -1); Assert.AreEqual(expectedNode.depth, 1); Assert.AreEqual(expectedNode2.parent, childNode2.parent); Assert.AreEqual(expectedNode2.pathCost, childNode2.pathCost); Assert.AreEqual(expectedNode2.state, childNode2.state); Assert.AreEqual(expectedNode2.action, 2); Assert.AreEqual(expectedNode2.depth, 1); }
/*! * Create a random instance of a problem */ public static Problem.NPuzzleProblem <Problem.NPuzzleState <int[]>, int, int> CreateProblem(int size) { if (!AcceptableLength(size)) { string msg = String.Format("Length given: {0}", size); UnacceptableLengthException ex = new UnacceptableLengthException(msg); throw ex; } int[] goalState = new int[size]; NPuzzleState <int[]> initial; for (int i = 0; i < size; i++) { goalState[i] = i + 1; } NPuzzleState <int[]> goal = new NPuzzleState <int[]>(goalState); initial = GenerateInitState(size); Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(goal, initial); return(problem); }
private Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> CreateProblem(NPuzzleState <int[]> initial) { int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int> problem = new Problem.NPuzzleProblem <NPuzzleState <int[]>, int, int>(new NPuzzleState <int[]>(goal), initial); return(problem); }