Advertisement
froleyks

FroleyksHeuristic.java

Nov 23rd, 2018
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. package edu.kit.aquaplanning.planners.heuristic;
  2.  
  3. import java.util.List;
  4. import java.util.ArrayList;
  5. import edu.kit.aquaplanning.model.ground.Action;
  6. import edu.kit.aquaplanning.model.ground.AtomSet;
  7. import edu.kit.aquaplanning.model.ground.Atom;
  8. import edu.kit.aquaplanning.model.ground.Plan;
  9. import edu.kit.aquaplanning.model.ground.Goal;
  10. import edu.kit.aquaplanning.model.ground.State;
  11. import edu.kit.aquaplanning.model.ground.GroundPlanningProblem;
  12. import edu.kit.aquaplanning.planners.GroundRelaxedPlanningGraph;
  13. import edu.kit.aquaplanning.model.lifted.PlanningProblem;
  14. import edu.kit.aquaplanning.planners.SearchNode;
  15.  
  16. /**
  17.  * Rename this class: [Lastname]sHeuristic .
  18.  */
  19. public class FroleyksHeuristic extends Heuristic {
  20.  
  21.     // All information your heuristic should need
  22.     // (together with the search node)
  23.     private GroundPlanningProblem groundProblem;
  24.     private PlanningProblem liftedProblem;
  25.  
  26.     // Define more members, if needed ...
  27.  
  28.     /**
  29.      * Constructor (please do not change the signature except for the class name)
  30.      */
  31.     public FroleyksHeuristic(GroundPlanningProblem groundProblem, PlanningProblem liftedProblem) {
  32.         this.groundProblem = groundProblem;
  33.         this.liftedProblem = liftedProblem;
  34.         List<Action> ac = groundProblem.getActions();
  35.         System.out.println(ac.get(0));
  36.     }
  37.  
  38.     /**
  39.      * Implement this method.
  40.      */
  41.     @Override
  42.     public int value(SearchNode node) {
  43.         State state = node.state;
  44.  
  45.         // Is the goal already satisfied (in a relaxed definition)?
  46.         if (groundProblem.getGoal().isSatisfiedRelaxed(state)) {
  47.             return 0;
  48.         }
  49.  
  50.         // Traverse deletion-relaxed planning graph
  51.         List<State> states = new ArrayList<>();
  52.         states.add(state);
  53.         GroundRelaxedPlanningGraph graph = new GroundRelaxedPlanningGraph(state, groundProblem.getActions());
  54.         while (graph.hasNextLayer()) {
  55.             State nextState = graph.computeNextLayer();
  56.             states.add(nextState);
  57.  
  58.             State g_hat = new State(groundProblem.getGoal().getAtoms());
  59.             List<Action> allActions = groundProblem.getActions();
  60.             Plan p = new Plan();
  61.             // Goal reached?
  62.             // if (g_hat.isSatisfiedRelaxed(nextState)) {
  63.             if (nextState.holdsAllTrueAtomsFrom(g_hat)) {
  64.                 for (int stateIndex = states.size() - 2; stateIndex > -1; --stateIndex) {
  65.                     State s_hat = states.get(stateIndex);
  66.                     List<Action> A = new ArrayList<>();
  67.  
  68.                     // Choose set of actions − with a sledgehammer
  69.                     for (Action action : allActions) {
  70.                         if (s_hat.holdsAllTrueAtomsFrom(g_hat)) {
  71.                             break;
  72.                         }
  73.                         if (action.isApplicableRelaxed(s_hat)) {
  74.                             A.add(action);
  75.                             action.applyRelaxed(s_hat);
  76.                         }
  77.                     }
  78.  
  79.                     for (Action action : A) {
  80.                         p.appendAtFront(action);
  81.                         g_hat.removeAll(action.getEffectsPos());
  82.                     }
  83.  
  84.                     for (Action action : A) {
  85.                         g_hat.removeAll(action.getPreconditionsPos());
  86.                     }
  87.                 }
  88.                 return p.getLength();
  89.             }
  90.         }
  91.  
  92.         // Goals could not be reached: unsolvable from this state
  93.       return Integer.MAX_VALUE;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement