/** * Get the successors from a node. * * @param parent the parent node. * @param problem the coded problem to solve. * @param heuristic the heuristic used. * @return the list of successors from the parent node. */ private LinkedList<Node> getSuccessors(Node parent, CodedProblem problem, Heuristic heuristic) { final LinkedList<Node> successors = new LinkedList<>(); int index = 0; for (BitOp op : problem.getOperators()) { if (op.isApplicable(parent)) { final BitState nextState = new BitState(parent); nextState.or(op.getCondEffects().get(0).getEffects().getPositive()); nextState.andNot(op.getCondEffects().get(0).getEffects().getNegative()); // Apply the effect of the applicable operator final Node successor = new Node(nextState); this.setCreatedNodes(this.getCreatedNodes() + 1); successor.setCost(parent.getCost() + op.getCost()); successor.setHeuristic(heuristic.estimate(successor, problem.getGoal())); successor.setParent(parent); successor.setOperator(index); successor.setDepth(parent.getDepth() + 1); successors.add(successor); } index++; } return successors; }
/** * Method that tests solution node (cost and size) for Depth First Search search strategy. */ @Test public void testDepthSolutionNode() { System.out.println("StateSpaceStrategy: Test solution node from Depth First Search."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, problemFile); stateSpaceStrategy = new DepthFirstSearch(TIMEOUT * 1000); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == DEPTH_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == DEPTH_SOLUTION_SIZE); }
/** * Method that tests solution node (cost and size) for Enforced Hill Climbing search strategy. */ @Test public void testEnforcedSolutionNode() { System.out.println("StateSpaceStrategy: Test solution node from Enforced Hill Climbing."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, problemFile); stateSpaceStrategy = new EnforcedHillClimbing(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == ENFORCED_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == ENFORCED_SOLUTION_SIZE); }
successor.setCost(parent.getCost() + op.getCost()); successor.setHeuristic(heuristic.estimate(nextState, problem.getGoal())); successor.setParent(parent);
/** * Method that tests solution node (cost and size) for Hill Climbing search strategy. */ @Test public void testHillSolutionNode() { System.out.println("StateSpaceStrategy: Test solution node from Hill Climbing."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, problemFile); stateSpaceStrategy = new HillClimbing(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == HILL_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == HILL_SOLUTION_SIZE); }
/** * Method that tests solution node (cost and size) for Breadth First Search search strategy. */ @Test public void testBreadthSolutionNode() { System.out.println("StateSpaceStrategy: Test solution node from Breadth First Search."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, problemFile); stateSpaceStrategy = new BreadthFirstSearch(TIMEOUT * 1000); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == BREADTH_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == BREADTH_SOLUTION_SIZE); }
/** * Method that tests solution node (cost and size) for AStar search strategy. */ @Test public void testAstarSolutionNode() { System.out.println("StateSpaceStrategy: Test solution node from AStar."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, problemFile); stateSpaceStrategy = new AStar(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == ASTAR_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == ASTAR_SOLUTION_SIZE); }
/** * Method that tests solution node (cost and size) for Greedy Best First Search search strategy. */ @Test public void testGreedySolutionNode() { System.out.println("StateSpaceStrategy: Test solution node from Greedy Best First Search."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, problemFile); stateSpaceStrategy = new GreedyBestFirstSearch(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == GREEDY_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == GREEDY_SOLUTION_SIZE); }
/** * Method that tests solution node (cost and size) for Astar Anytime search strategy. */ @Test public void testAstarAnytimeSolutionNode() { System.out.println("StateSpaceStrategy: Test solution node from Astar Search Anytime."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, problemFile); stateSpaceStrategy = new AStarAnytime(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == ASTAR_ANYTIME_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == ASTAR_ANYTIME_SOLUTION_COST); }
/** * Method that tests solution node (cost and size) for Greedy Best First Search Anytime search strategy. */ @Test public void testGreedyAnytimeSolutionNode() { System.out.println("StateSpaceStrategy: Test solution node from Greedy Best First Search Anytime."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, problemFile); stateSpaceStrategy = new GreedyBestFirstSearchAnytime(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == GREEDY_ANYTIME_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == GREEDY_ANYTIME_SOLUTION_COST); }
/** * Method that tests solution node (cost and size) for Hill Climbing Anytime search strategy. */ @Test public void testHillAnytimeSolutionNode() { System.out.println("StateSpaceStrategy: Test solution node from Hill Climbing Anytime."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, problemFile); stateSpaceStrategy = new HillClimbing(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == HILL_ANYTIME_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == HILL_ANYTIME_SOLUTION_COST); }
/** * Method that tests solution event (cost and size) for EHC search strategy. */ @Test public void testEHCFireSolutionEvent() { System.out.println("SolutionEventAndListener: Test fire solution node from EHC."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, p01ProblemFile); stateSpaceStrategy = new EnforcedHillClimbing(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); stateSpaceStrategy.addSolutionListener(e -> firedSolutionEvent = e); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == ENFORCED_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == ENFORCED_SOLUTION_SIZE); Assert.assertTrue(Math.abs(firedSolutionEvent.getSolutionNode().getCost() - solutionNode.getCost()) < 0.00001); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).size() == stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size()); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).equals(stateSpaceStrategy.extractPlan(solutionNode, codedProblem))); }
/** * Method that tests solution event (cost and size) for HC search strategy. */ @Test public void testHCFireSolutionEvent() { System.out.println("SolutionEventAndListener: Test fire solution node from HC."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, p01ProblemFile); stateSpaceStrategy = new HillClimbing(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); stateSpaceStrategy.addSolutionListener(e -> firedSolutionEvent = e); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == HILL_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == HILL_SOLUTION_SIZE); Assert.assertTrue(Math.abs(firedSolutionEvent.getSolutionNode().getCost() - solutionNode.getCost()) < 0.00001); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).size() == stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size()); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).equals(stateSpaceStrategy.extractPlan(solutionNode, codedProblem))); }
/** * Method that tests solution event (cost and size) for HC search strategy. */ @Test public void testHCAnytimeFireSolutionEvent() { System.out.println("SolutionEventAndListener: Test fire solution node from HC Anytime."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, p01ProblemFile); stateSpaceStrategy = new HillClimbing(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); stateSpaceStrategy.addSolutionListener(e -> firedSolutionEvent = e); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == HILL_ANYTIME_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == HILL_ANYTIME_SOLUTION_SIZE); Assert.assertTrue(Math.abs(firedSolutionEvent.getSolutionNode().getCost() - solutionNode.getCost()) < 0.00001); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).size() == stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size()); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).equals(stateSpaceStrategy.extractPlan(solutionNode, codedProblem))); } }
/** * Method that tests solution event (cost and size) for AStar search strategy. */ @Test public void testAstarFireSolutionEvent() { System.out.println("SolutionEventAndListener: Test fire solution node from AStar."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, p01ProblemFile); stateSpaceStrategy = new AStar(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); stateSpaceStrategy.addSolutionListener(e -> firedSolutionEvent = e); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == ASTAR_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == ASTAR_SOLUTION_SIZE); Assert.assertTrue(Math.abs(firedSolutionEvent.getSolutionNode().getCost() - solutionNode.getCost()) < 0.00001); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).size() == stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size()); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).equals(stateSpaceStrategy.extractPlan(solutionNode, codedProblem))); }
/** * Method that tests solution event (cost and size) for DFS search strategy. */ @Test public void testDFSFireSolutionEvent() { System.out.println("SolutionEventAndListener: Test fire solution node from DFS."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, p01ProblemFile); stateSpaceStrategy = new DepthFirstSearch(TIMEOUT * 1000); stateSpaceStrategy.addSolutionListener(e -> firedSolutionEvent = e); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == DEPTH_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == DEPTH_SOLUTION_SIZE); Assert.assertTrue(Math.abs(firedSolutionEvent.getSolutionNode().getCost() - solutionNode.getCost()) < 0.00001); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).size() == stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size()); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).equals(stateSpaceStrategy.extractPlan(solutionNode, codedProblem))); }
/** * Method that tests solution event (cost and size) for Astar Anytime search strategy. */ @Test public void testAstarAnyFireSolutionEvent() { System.out.println("SolutionEventAndListener: Test fire solution node from Astar Anytime."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, p01ProblemFile); stateSpaceStrategy = new AStarAnytime(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); stateSpaceStrategy.addSolutionListener(e -> firedSolutionEvent = e); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == ASTAR_ANYTIME_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == ASTAR_ANYTIME_SOLUTION_SIZE); Assert.assertTrue(Math.abs(firedSolutionEvent.getSolutionNode().getCost() - solutionNode.getCost()) < 0.00001); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).size() == stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size()); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).equals(stateSpaceStrategy.extractPlan(solutionNode, codedProblem))); }
/** * Method that tests solution event (cost and size) for BFS search strategy. */ @Test public void testBFSFireSolutionEvent() { System.out.println("SolutionEventAndListener: Test fire solution node from BFS."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, p01ProblemFile); stateSpaceStrategy = new BreadthFirstSearch(TIMEOUT * 1000); stateSpaceStrategy.addSolutionListener(e -> firedSolutionEvent = e); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == BREADTH_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == BREADTH_SOLUTION_SIZE); Assert.assertTrue(Math.abs(firedSolutionEvent.getSolutionNode().getCost() - solutionNode.getCost()) < 0.00001); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).size() == stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size()); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).equals(stateSpaceStrategy.extractPlan(solutionNode, codedProblem))); }
/** * Method that tests solution event (cost and size) for GBFS search strategy. */ @Test public void testGbfsFireSolutionEvent() { System.out.println("SolutionEventAndListener: Test fire solution node from GBFS."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, p01ProblemFile); stateSpaceStrategy = new GreedyBestFirstSearch(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); stateSpaceStrategy.addSolutionListener(e -> firedSolutionEvent = e); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == GREEDY_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == GREEDY_SOLUTION_SIZE); Assert.assertTrue(Math.abs(firedSolutionEvent.getSolutionNode().getCost() - solutionNode.getCost()) < 0.00001); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).size() == stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size()); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).equals(stateSpaceStrategy.extractPlan(solutionNode, codedProblem))); }
/** * Method that tests solution event (cost and size) for GBFS Anytime search strategy. */ @Test public void testGbfsAnytimeFireSolutionEvent() { System.out.println("SolutionEventAndListener: Test fire solution node from GBFS Anytime."); final CodedProblem codedProblem = Tools.generateCodedProblem(domainFile, p01ProblemFile); stateSpaceStrategy = new GreedyBestFirstSearchAnytime(TIMEOUT * 1000, HEURISTIC_TYPE, HEURISTIC_WEIGHT); stateSpaceStrategy.addSolutionListener(e -> firedSolutionEvent = e); final Node solutionNode = stateSpaceStrategy.searchSolutionNode(codedProblem); Assert.assertTrue(solutionNode.getCost() == GREEDY_ANYTIME_SOLUTION_COST); Assert.assertTrue(stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size() == GREEDY_ANYTIME_SOLUTION_SIZE); Assert.assertTrue(Math.abs(firedSolutionEvent.getSolutionNode().getCost() - solutionNode.getCost()) < 0.00001); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).size() == stateSpaceStrategy.extractPlan(solutionNode, codedProblem).size()); Assert.assertTrue(stateSpaceStrategy.extractPlan(firedSolutionEvent.getSolutionNode(), codedProblem).equals(stateSpaceStrategy.extractPlan(solutionNode, codedProblem))); }