Guest User

Untitled

a guest
Jul 15th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. private List<Operator> alg(List<Fact> state, List<List<Fact>> history,
  2. List<Operator> path) {
  3. List<Operator> result = null;
  4.  
  5. // final state included in current state => WIN
  6. if (state.containsAll(finalState)) {
  7. result = new ArrayList<Operator>();
  8. result.addAll(path);
  9. } else {
  10. List<Operator> viableActions = viableActions(state, history);
  11. if (viableActions.isEmpty()) { // no possible actions => FAIL
  12. result = null;
  13. } else {
  14. // choose operator
  15. Operator chosen = viableActions.get(0);
  16. List<Fact> newState = applyOperator(state, chosen);
  17.  
  18. history.add(state);
  19.  
  20. // recursive call
  21. path.add(chosen);
  22. List<Operator> call = alg(newState, history, path);
  23. path.remove(path.size() - 1);
  24.  
  25. if (call == null) {
  26. viableActions.remove(chosen);
  27. if (viableActions.isEmpty()) {
  28. result = null;
  29. } else {
  30. // choose next operator from the list
  31. chosen = viableActions.get(0);
  32. newState = applyOperator(state, chosen);
  33.  
  34. // recursive call
  35. path.add(chosen);
  36. result = alg(newState, history, path);
  37. path.remove(path.size() - 1);
  38. }
  39. } else {
  40. result = call;
  41. }
  42.  
  43. history.remove(state);
  44. }
  45. }
  46. return result;
  47. }
Add Comment
Please, Sign In to add comment