nextState.andNot(op.getCondEffects().get(0).getEffects().getNegative()); final Node successor = new Node(nextState); successor.setCost(current.getCost() + op.getCost()); successor.setParent(current); successor.setOperator(index); successor.setDepth(current.getDepth() + 1); if (nextNode.satisfy(goal)) { solution = nextNode; } else { return solution.getCost();
Node root = new Node(init, null, 0, 0, heuristic.estimate(init, problem.getGoal())); root.setHeuristic(heuristic.estimate(root, problem.getGoal())); restartList.add(root); Node solution = null; if (returnedSolution != null && returnedSolution.getCost() < bound) { this.getSolutionNodes().add(new Node(returnedSolution, returnedSolution.getParent(), 0, returnedSolution.getCost(), returnedSolution.getDepth(), returnedSolution.getHeuristic())); bound = returnedSolution.getCost(); solution = returnedSolution; logger.trace("* " + this.getSolutionNodes().size() + " solutions found. Best cost: " + bound + "\n");
/** * Extract a plan from a solution node for the specified planning problem. * * @param node the solution node. * @param problem the problem to be solved. * @return the solution plan or null is no solution was found. */ @Override public SequentialPlan extractPlan(final Node node, final CodedProblem problem) { if (node != null) { Node n = node; final SequentialPlan plan = new SequentialPlan(); while (n.getParent() != null) { final BitOp op = problem.getOperators().get(n.getOperator()); plan.add(0, op); n = n.getParent(); } return plan; } else { return null; } }
/** * 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); }
if (node == null) { BitState init = new BitState(problem.getInit()); root = new Node(init, null, 0, 0, heuristic.estimate(init, problem.getGoal())); root.setHeuristic(heuristic.estimate(root, problem.getGoal())); openList.add(root); } else { final Node successor = popBestNode(successors); this.setExploredNodes(this.getExploredNodes() + 1); if (successor.satisfy(problem.getGoal())) { solution = successor; fireSolution(new SolutionEvent(this, solution, problem));
Node root = new Node(init, null, 0, 0, heuristic.estimate(init, codedProblem.getGoal())); openList.add(root); double bestHeuristic = root.getHeuristic(); final Node successor = successors.pop(); this.setExploredNodes(this.getExploredNodes() + 1); final double heuristicSuccessor = successor.getHeuristic(); if (heuristicSuccessor == 0.0) { solution = successor;
Node root = new Node(init, null, 0, 0, heuristic.estimate(init, codedProblem.getGoal())); openList.add(root); final Node successor = popBestNode(successors); this.setExploredNodes(this.getExploredNodes() + 1); if (successor.satisfy(codedProblem.getGoal())) { solution = successor; fireSolution(new SolutionEvent(this, solution, codedProblem));
/** * Return the best node from a list according to the heuristic value. * * @param nodes the list containing nodes. * @return the best node from the nodes' list. */ private Node popBestNode(Collection<Node> nodes) { Node node = null; if (!nodes.isEmpty()) { final Iterator<Node> i = nodes.iterator(); node = i.next(); while (i.hasNext()) { final Node next = i.next(); if (next.getHeuristic() < node.getHeuristic()) { node = next; } else if (Math.abs(next.getHeuristic() - node.getHeuristic()) < .001 && !restartList.contains(next)) { restartList.addLast(next); } } nodes.remove(node); } return node; } }
/** * Return the estimated distance to the goal to reach the specified state. If the return value is * <code>Integer.MAX_VALUE</code>, it means that the goal is unreachable from the specified * state. * * @param state the state from which the distance to the goal must be estimated. * @param goal the goal expression. * @return the distance to the goal state from the specified state. * @throws NullPointerException if <code>state == null && goal == null</code>. */ @Override public int estimate(final BitState state, final BitExp goal) { return (int) estimateCost(new Node(state), goal); }
/** * 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); }
/** * Get a node from a list of nodes. * * @param states the list of nodes (successors). * @return the node from the list. */ private Node popPriorityNode(Collection<Node> states) { Node state = null; if (!states.isEmpty()) { final Iterator<Node> i = states.iterator(); state = i.next(); while (i.hasNext()) { final Node next = i.next(); if (next.getHeuristic() < state.getHeuristic()) { state = next; } } states.remove(state); } return state; } }
final Node root = new Node(init, null, -1, 0, heuristic.estimate(init, codedProblem.getGoal())); if (current.satisfy(codedProblem.getGoal())) { solution = current; fireSolution(new SolutionEvent(this, solution, codedProblem)); Node state = new Node(current); this.setCreatedNodes(this.getCreatedNodes() + 1); op.getCondEffects().stream().filter(ce -> current.satisfy(ce.getCondition())).forEach(ce -> state.apply(ce.getEffects()) ); final double g = current.getCost() + 1; Node result = openSet.get(state); if (result == null) { result = closeSet.get(state); if (result != null) { if (g < result.getCost()) { result.setCost(g); result.setParent(current); result.setOperator(index); result.setDepth(current.getDepth() + 1); open.add(result); openSet.put(result, result); state.setCost(g); state.setParent(current);
/** * 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); }
/** * Get a node from a list of nodes. * * @param states the list of nodes (successors). * @return the node from the list. */ private Node popPriorityNode(Collection<Node> states) { Node state = null; if (!states.isEmpty()) { final Iterator<Node> i = states.iterator(); state = i.next(); while (i.hasNext()) { final Node next = i.next(); if (next.getHeuristic() < state.getHeuristic()) { state = next; } } states.remove(state); } return state; } }
/** * 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 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); }
/** * Return the best node from a list according to the heuristic value. * * @param nodes the list containing nodes. * @return the best node from the nodes' list. */ private Node popBestNode(Collection<Node> nodes) { Node node = null; if (!nodes.isEmpty()) { final Iterator<Node> i = nodes.iterator(); node = i.next(); while (i.hasNext()) { final Node next = i.next(); if (next.getHeuristic() < node.getHeuristic()) { node = next; } } nodes.remove(node); } return node; } }
final Node successor = new Node(nextState); this.setCreatedNodes(this.getCreatedNodes() + 1); successor.setCost(parent.getCost() + op.getCost()); successor.setHeuristic(heuristic.estimate(nextState, problem.getGoal())); successor.setParent(parent); successor.setOperator(index); successor.setDepth(parent.getDepth() + 1); successors.add(successor);
/** * 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); }