Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private List<Operator> alg(List<Fact> state, List<List<Fact>> history,
- List<Operator> path) {
- List<Operator> result = null;
- // final state included in current state => WIN
- if (state.containsAll(finalState)) {
- result = new ArrayList<Operator>();
- result.addAll(path);
- } else {
- List<Operator> viableActions = viableActions(state, history);
- if (viableActions.isEmpty()) { // no possible actions => FAIL
- result = null;
- } else {
- // choose operator
- Operator chosen = viableActions.get(0);
- List<Fact> newState = applyOperator(state, chosen);
- history.add(state);
- // recursive call
- path.add(chosen);
- List<Operator> call = alg(newState, history, path);
- path.remove(path.size() - 1);
- if (call == null) {
- viableActions.remove(chosen);
- if (viableActions.isEmpty()) {
- result = null;
- } else {
- // choose next operator from the list
- chosen = viableActions.get(0);
- newState = applyOperator(state, chosen);
- // recursive call
- path.add(chosen);
- result = alg(newState, history, path);
- path.remove(path.size() - 1);
- }
- } else {
- result = call;
- }
- history.remove(state);
- }
- }
- return result;
- }
Add Comment
Please, Sign In to add comment