public void TestHashSet() { HashSet <NPuzzleState <int[]> > explored = new HashSet <NPuzzleState <int[]> >(); int size = 9, num = 100; NPuzzleState <int[]>[] states = new NPuzzleState <int[]> [num]; NPuzzleState <int[]> state; int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal); for (int i = 0; i < num; i++) { state = NPuzzleUtils.GenerateInitState(size); states[i] = state; explored.Add(state); } Assert.False(explored.Contains(goalState)); explored.Add(goalState); for (int i = 0; i < num; i++) { Assert.True(explored.Contains(states[i])); } Assert.True(explored.Contains(goalState)); }
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 TestNodeCreation() { int size = 9; //TODO: change NPuzzleUtils.GenerateInitState NPuzzleState <int[]> state = NPuzzleUtils.GenerateInitState(size); System.Diagnostics.Debug.WriteLine("State: "); System.Diagnostics.Debug.WriteLine(state.State.ToString()); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(state); }
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); }
/*! * Shuffle an array. Found on https://dotnetperls.com/fisher-yates-shuffle */ public static void Shuffle <T>(T[] state) { int l = state.Length; for (int i = 0; i < l; i++) { //! NextDouble returns a double from //! 0 to 1 int r = i + (int)(_random.NextDouble() * (l - i)); NPuzzleUtils.Swap(state, i, r); } }
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 TestCorrectElements() { int size = 9; int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = i + 1; } for (int j = 0; j < 10; j++) { NPuzzleUtils.Shuffle(array); Assert.AreEqual(NPuzzleUtils.CorrectElements(array), true); } }
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); } }
/*! * Don't test for inversions with the largest * element in the array */ public void TestCountInversions() { int[] s1 = { 9, 8, 7, 6, 5, 4, 3, 2, 1 }; Assert.AreEqual(NPuzzleUtils.CountInversions(s1), 28); NPuzzleUtils.Swap(s1, 1, 2); Assert.AreEqual(NPuzzleUtils.CountInversions(s1), 27); int[] s2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Assert.AreEqual(NPuzzleUtils.CountInversions(s2), 0); NPuzzleUtils.Swap(s2, 7, 8); Assert.AreEqual(NPuzzleUtils.CountInversions(s2), 0); NPuzzleUtils.Swap(s2, 6, 7); Assert.AreEqual(NPuzzleUtils.CountInversions(s2), 0); }
/*! * 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)); }
private Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > GetPriorityQueueToo() { Heuristics.Heurfun <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > handler = Heuristics.NPuzzleHeuristics.ManhattanDistance; Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > frontier = new Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> >(handler); int size = 9; SearchTreeNode.Node <NPuzzleState <int[]>, int, int>[] nodeArray = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int> [100]; SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node; for (int i = 0; i < 100; i++) { NPuzzleState <int[]> istate = NPuzzleUtils.GenerateInitState(size); node = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(istate); nodeArray[i] = node; //int heur = NPuzzleHeuristics.ManhattanDistance(node); frontier.Append(node); } return(frontier); }
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()); }
// TODO: again, NPuzzleState return type instead of int[] public override NPuzzleState <int[]> Result(NPuzzleState <int[]> s, int action) { int[] state = s.State; int[] newState = new int[state.Length]; int emptyIndex; state.CopyTo(newState, 0); emptyIndex = GetEmptyIndex(state); if (emptyIndex == -1) { NPuzzleUtils.MissingEmptyElementException ex = new NPuzzleUtils.MissingEmptyElementException(state.Length.ToString()); throw ex; } if (AcceptableAction(state, emptyIndex, action)) { if (action == 1) { NPuzzleUtils.Swap(newState, emptyIndex, emptyIndex + 1); } else if (action == -1) { NPuzzleUtils.Swap(newState, emptyIndex, emptyIndex - 1); } else if (action == -2) { NPuzzleUtils.Swap(newState, emptyIndex, emptyIndex + dimension); } else { NPuzzleUtils.Swap(newState, emptyIndex, emptyIndex - dimension); } } else { String msg = String.Format("You entered an invalid action: {0} for the state {1}", action, state.ToString()); NPuzzleUtils.ResultAcceptableActionException ex = new NPuzzleUtils.ResultAcceptableActionException(msg); throw ex; } return(new NPuzzleState <int[]>(newState)); }
public void TestGetIncumbent() { Heuristics.Heurfun <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > handler = Heuristics.NPuzzleHeuristics.ManhattanDistance; Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > frontier = new Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> >(handler); int size = 9; SearchTreeNode.Node <NPuzzleState <int[]>, int, int>[] nodeArray = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int> [100]; SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node; for (int i = 0; i < 100; i++) { NPuzzleState <int[]> istate = NPuzzleUtils.GenerateInitState(size); node = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(istate); nodeArray[i] = node; int heur = NPuzzleHeuristics.ManhattanDistance(node); frontier.Append(node); } for (int i = 0; i < 100; i++) { Assert.AreEqual(nodeArray[i], frontier.GetIncumbent(nodeArray[i])); } }
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); }
public void TestAppendToo() { Heuristics.Heurfun <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > handler = Heuristics.NPuzzleHeuristics.ManhattanDistance; Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > frontier = new Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> >(handler); int size = 9; SearchTreeNode.Node <NPuzzleState <int[]>, int, int>[] nodeArray = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int> [100]; SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node; for (int i = 0; i < 100; i++) { NPuzzleState <int[]> istate = NPuzzleUtils.GenerateInitState(size); node = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(istate); nodeArray[i] = node; //int heur = NPuzzleHeuristics.ManhattanDistance(node); frontier.Append(node); } for (int i = 0; i < 100; i++) { int heur = NPuzzleHeuristics.ManhattanDistance(nodeArray[i]); Assert.True(frontier.InPriorityQueue(nodeArray[i])); } int j = 0; int[] heurArray = new int[100]; while (frontier.Count() > 0) { node = frontier.Pop(); heurArray[j] = NPuzzleHeuristics.ManhattanDistance(node); j++; } for (j = 0; j < 99; j++) { Assert.True(heurArray[j] <= heurArray[j + 1]); } }
private bool ValidInitialState(int[] initial) { return(NPuzzleUtils.AcceptableState(initial)); }