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 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 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 TestListContains() { int[] s1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; NPuzzleState <int[]> s1State = new NPuzzleState <int[]>(s1); int[] s2 = { 1, 2, 3, 4, 5, 9, 7, 8, 6 }; NPuzzleState <int[]> s2State = new NPuzzleState <int[]>(s2); int[] s3 = { 1, 2, 9, 4, 5, 3, 7, 8, 6 }; NPuzzleState <int[]> s3State = new NPuzzleState <int[]>(s3); int[] s4 = { 1, 2, 9, 4, 5, 3, 7, 8, 6 }; NPuzzleState <int[]> s4State = new NPuzzleState <int[]>(s4); List <NPuzzleState <int[]> > list = new List <NPuzzleState <int[]> >(); list.Add(s1State); Assert.True(list.Contains(s1State)); Assert.False(list.Contains(s2State)); list.Add(s2State); Assert.True(list.Contains(s2State)); Assert.False(list.Contains(s3State)); list.Add(s3State); Assert.True(list.Contains(s3State)); if (!list.Contains(s4State)) { Console.WriteLine("Write Line: s4 not in list"); } }
private Search.PriorityQueue <int, SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > GetPriorityQueue() { // Heuristics.HeuristicFunction<NPuzzleState<int[]>,int,int> handler = Heuristics.NPuzzleHeuristics.ManhattanDistance; 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); //SortedList<int, List<SearchTreeNode.NPuzzleNode<int[],int,int> > > frontier = new SortedList<int, List<SearchTreeNode.NPuzzleNode<int[],int,int> > >(); List <SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > nodes = TestNode.CreateNodesForTesting(); //! md = 12 int[] s7 = { 6, 4, 3, 1, 9, 5, 8, 7, 2 }; NPuzzleState <int[]> s7State = new NPuzzleState <int[]>(s7); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node7 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int> (s7State); nodes.Add(node7); //! 2 + 2 + 1 + 1 + 2 = 8 int[] s8 = { 1, 6, 3, 8, 4, 5, 7, 2, 9 }; NPuzzleState <int[]> s8State = new NPuzzleState <int[]>(s8); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node8 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int> (s8State); nodes.Add(node8); foreach (var node in nodes) { //int md = handler(node); frontier.Append(node); } return(frontier); }
public void TestSolution() { int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal); //! -1 int[] s1 = { 1, 2, 3, 4, 5, 6, 7, 9, 8 }; NPuzzleState <int[]> s1State = new NPuzzleState <int[]>(s1); //! 2 int[] s2 = { 1, 2, 3, 4, 9, 6, 7, 5, 8 }; NPuzzleState <int[]> s2State = new NPuzzleState <int[]>(s2); //! 1 int[] s3 = { 1, 2, 3, 4, 6, 9, 7, 5, 8 }; NPuzzleState <int[]> s3State = new NPuzzleState <int[]>(s3); //! 2 int[] s4 = { 1, 2, 9, 4, 6, 3, 7, 5, 8 }; NPuzzleState <int[]> s4State = new NPuzzleState <int[]>(s4); //! -1 int[] s5 = { 1, 9, 2, 4, 6, 3, 7, 5, 8 }; NPuzzleState <int[]> s5State = new NPuzzleState <int[]>(s5); //! -1 int[] s6 = { 9, 1, 2, 4, 6, 3, 7, 5, 8 }; NPuzzleState <int[]> s6State = new NPuzzleState <int[]>(s6); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> rootNode = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(goalState); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node1 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(new NPuzzleState <int[]>(s1), rootNode, -1, 1); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node2 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(new NPuzzleState <int[]>(s2), node1, 2, 1); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node3 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(new NPuzzleState <int[]>(s3), node2, 1, 1); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node4 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(new NPuzzleState <int[]>(s4), node3, 2, 1); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node5 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(new NPuzzleState <int[]>(s5), node4, -1, 1); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node6 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(new NPuzzleState <int[]>(s6), node5, -1, 1); List <int> solution = node6.Solution(); Assert.AreEqual(solution[0], -1); Assert.AreEqual(solution[1], -1); Assert.AreEqual(solution[2], 2); }
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 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 TestNPuzzleStateEquals() { 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); Assert.False(initialState.Equals(initialState2)); Assert.True(initialState.Equals(initialState)); }
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)); }
private void operatorsView_SelectionChanged(object sender, EventArgs e) { if (operatorsView.SelectedCells.Count == 1 && stateList.Count != 0) { int index = operatorsView.SelectedCells[0].RowIndex; if (index < stateList.Count) { NPuzzleState npState = (NPuzzleState)stateList[index]; FillDGV(npState.Tiles, N); } } }
public void TestPath() { int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //! -1 int[] s1 = { 1, 2, 3, 4, 5, 6, 7, 9, 8 }; //! 2 int[] s2 = { 1, 2, 3, 4, 9, 6, 7, 5, 8 }; //! 1 int[] s3 = { 1, 2, 3, 4, 6, 9, 7, 5, 8 }; //! 2 int[] s4 = { 1, 2, 9, 4, 6, 3, 7, 5, 8 }; //! -1 int[] s5 = { 1, 9, 2, 4, 6, 3, 7, 5, 8 }; NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal); NPuzzleState <int[]> s1State = new NPuzzleState <int[]>(s1); NPuzzleState <int[]> s2State = new NPuzzleState <int[]>(s2); NPuzzleState <int[]> s3State = new NPuzzleState <int[]>(s3); NPuzzleState <int[]> s4State = new NPuzzleState <int[]>(s4); NPuzzleState <int[]> s5State = new NPuzzleState <int[]>(s5); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> rootNode = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(goalState); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node1 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(s1State, rootNode, -1, 1); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node2 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(s2State, node1, 2, 1); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node3 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(s3State, node2, 1, 1); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node4 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(s4State, node3, 2, 1); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node5 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(s5State, node4, -1, 1); List <SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > path = node5.Path(); Assert.AreEqual(s5State, path[0].state); Assert.AreEqual(path[0].action, -1); Assert.AreEqual(s4State, path[1].state); Assert.AreEqual(path[1].action, 2); Assert.AreEqual(s3State, path[2].state); Assert.AreEqual(s2State, path[3].state); }
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); }
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 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 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]); } }
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); }
public void TestManhattanDistance() { //! md = 0 int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal); //! -1, md = 1 int[] s1 = { 1, 2, 3, 4, 5, 6, 7, 9, 8 }; NPuzzleState <int[]> s1State = new NPuzzleState <int[]>(s1); //! 2, md = 2 int[] s2 = { 1, 2, 3, 4, 9, 6, 7, 5, 8 }; NPuzzleState <int[]> s2State = new NPuzzleState <int[]>(s2); //! 1, md 3 int[] s3 = { 1, 2, 3, 4, 6, 9, 7, 5, 8 }; NPuzzleState <int[]> s3State = new NPuzzleState <int[]>(s3); //! 2 md = 4 int[] s4 = { 1, 2, 9, 4, 6, 3, 7, 5, 8 }; NPuzzleState <int[]> s4State = new NPuzzleState <int[]>(s4); //! -1 md = 5 int[] s5 = { 1, 9, 2, 4, 6, 3, 7, 5, 8 }; NPuzzleState <int[]> s5State = new NPuzzleState <int[]>(s5); //! -1 md = 6 int[] s6 = { 9, 1, 2, 4, 6, 3, 7, 5, 8 }; NPuzzleState <int[]> s6State = new NPuzzleState <int[]>(s6); //! 3 + 2 + 1 + 1 + 1 + 1 + 3 int[] s7 = { 6, 4, 3, 1, 9, 5, 8, 7, 2 }; NPuzzleState <int[]> s7State = new NPuzzleState <int[]>(s7); //! 2 + 2 + 1 + 1 + 2 = 8 int[] s8 = { 1, 6, 3, 8, 4, 5, 7, 2, 9 }; NPuzzleState <int[]> s8State = new NPuzzleState <int[]>(s8); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node8 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(s8State); Assert.AreEqual(NPuzzleHeuristics.ManhattanDistance(node8), 8); SearchTreeNode.Node <NPuzzleState <int[]>, int, int> node7 = new SearchTreeNode.Node <NPuzzleState <int[]>, int, int>(s7State); Assert.AreEqual(NPuzzleHeuristics.ManhattanDistance(node7), 12); List <SearchTreeNode.Node <NPuzzleState <int[]>, int, int> > nodes = TestNode.CreateNodesForTesting(); Assert.AreEqual(nodes[0].state.State, goal); Assert.AreEqual(NPuzzleHeuristics.ManhattanDistance(nodes[0]), 0); Assert.AreEqual(nodes[1].state.State, s1); Assert.AreEqual(NPuzzleHeuristics.ManhattanDistance(nodes[1]), 1); Assert.AreEqual(nodes[2].state.State, s2); Assert.AreEqual(NPuzzleHeuristics.ManhattanDistance(nodes[2]), 2); Assert.AreEqual(nodes[3].state.State, s3); Assert.AreEqual(NPuzzleHeuristics.ManhattanDistance(nodes[3]), 3); Assert.AreEqual(nodes[4].state.State, s4); Assert.AreEqual(NPuzzleHeuristics.ManhattanDistance(nodes[4]), 4); Assert.AreEqual(nodes[5].state.State, s5); Assert.AreEqual(NPuzzleHeuristics.ManhattanDistance(nodes[5]), 5); }
public SolutionForm(ISolver A, IEnvironment env, INode start, INode goal) { InitializeComponent(); DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn(); col.HeaderText = "Operators"; col.SortMode = DataGridViewColumnSortMode.NotSortable; operatorsView.Columns.Add(col); operatorsView.RowHeadersWidth = 50; col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; IState currentState = start.State; IPolicy policy = A.getPolicy(); ISearchInfo info = A.GetSearchInfo(); if (policy == null) { MessageBox.Show("No solution found"); } //use policy to find solution int i = 0; int maxi = 3000; double SumQ = 0; DataGridViewRow row; while (policy != null && !currentState.Equals(goal.State) && i < maxi) { row = new DataGridViewRow(); row.HeaderCell.Value = String.Format("{0}", i + 1); IOperator op = policy.action(currentState); operatorsView.Rows.Add(row); operatorsView.Rows[i].Cells[0].Value = op.Name; IOutcome outcome = env.act(currentState, policy.action(currentState)); currentState = outcome.State; stateList.Add(outcome.State); SumQ += outcome.Reward; i += 1; } if (i >= maxi) { MessageBox.Show("No solution found"); stateList.Clear(); } operatorsView.Update(); // print solver parameters + results col = new DataGridViewTextBoxColumn(); col.HeaderText = "Properties"; ResultView.Columns.Add(col); ResultView.RowHeadersWidth = 50; col = new DataGridViewTextBoxColumn(); col.HeaderText = "Values"; ResultView.Columns.Add(col); ResultView.RowHeadersWidth = 50; col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; i = 0; foreach (KeyValuePair <string, string> pair in info.SearchResults.Union(info.SearchParameters)) { row = new DataGridViewRow(); row.HeaderCell.Value = String.Format("{0}", i + 1); ResultView.Rows.Add(row); ResultView.Rows[i].Cells[0].Value = pair.Key; ResultView.Rows[i].Cells[1].Value = pair.Value; i += 1; } //print total reward row = new DataGridViewRow(); row.HeaderCell.Value = String.Format("{0}", i + 1); ResultView.Rows.Add(row); ResultView.Rows[i].Cells[0].Value = "Cummulative reward"; ResultView.Rows[i].Cells[1].Value = SumQ.ToString(); ResultView.Width = col.Width + ResultView.RowHeadersWidth + 20; ResultView.Update(); ConfigureDGV(N); NPuzzleState npState = (NPuzzleState)start.State; FillDGV(npState.Tiles, N); }
public void TestHashSet() { int[] goal = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; NPuzzleState <int[]> goalState = new NPuzzleState <int[]>(goal); //! -1 (from goal), md = 1 int[] s1 = { 1, 2, 3, 4, 5, 6, 7, 9, 8 }; NPuzzleState <int[]> s1State = new NPuzzleState <int[]>(s1); //! md = 1 int[] s7 = { 1, 2, 3, 4, 5, 9, 7, 8, 6 }; NPuzzleState <int[]> s7State = new NPuzzleState <int[]>(s7); //! 2, md = 2 int[] s2 = { 1, 2, 3, 4, 9, 6, 7, 5, 8 }; NPuzzleState <int[]> s2State = new NPuzzleState <int[]>(s2); //! 1, md 3 int[] s3 = { 1, 2, 3, 4, 6, 9, 7, 5, 8 }; NPuzzleState <int[]> s3State = new NPuzzleState <int[]>(s3); //! 2 md = 4 int[] s4 = { 1, 2, 9, 4, 6, 3, 7, 5, 8 }; NPuzzleState <int[]> s4State = new NPuzzleState <int[]>(s4); //! -1 md = 5 int[] s5 = { 1, 9, 2, 4, 6, 3, 7, 5, 8 }; NPuzzleState <int[]> s5State = new NPuzzleState <int[]>(s5); //! -1 md = 6 int[] s6 = { 9, 1, 2, 4, 6, 3, 7, 5, 8 }; NPuzzleState <int[]> s6State = new NPuzzleState <int[]>(s6); int[] s8 = { 9, 1, 2, 4, 6, 3, 7, 5, 8 }; NPuzzleState <int[]> s8State = new NPuzzleState <int[]>(s8); HashSet <NPuzzleState <int[]> > explored = new HashSet <NPuzzleState <int[]> >(); Assert.NotNull(explored.Comparer); Console.WriteLine(explored.Comparer.ToString()); explored.Add(goalState); explored.Add(s1State); explored.Add(s2State); explored.Add(s3State); //explored.Add(s4); //explored.Add(s5State); explored.Add(s6State); explored.Add(s7State); Assert.True(explored.Contains(goalState)); Assert.True(explored.Contains(s1State)); Assert.True(explored.Contains(s7State)); Assert.True(explored.Contains(s2State)); Assert.True(explored.Contains(s3State)); Assert.True(s8State.Equals(s6State)); Assert.AreEqual(s8State.GetHashCode(), s6State.GetHashCode()); Assert.True(explored.Contains(s8State)); Assert.False(explored.Contains(s4State)); Assert.False(explored.Contains(s5State)); }
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); }