|
| 1 | +package aima.search.online; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Hashtable; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import aima.basic.Agent; |
| 8 | +import aima.basic.Percept; |
| 9 | +import aima.search.framework.Problem; |
| 10 | +import aima.search.framework.Successor; |
| 11 | + |
| 12 | +/** |
| 13 | + * Artificial Intelligence A Modern Approach (2nd Edition): Figure 4.23, page |
| 14 | + * 128.<br> |
| 15 | + * <code> |
| 16 | + * function LRTA*AGENT(s') returns an action |
| 17 | + * inputs: s', a percept that identifies the current state |
| 18 | + * static: result, a table, indexed by action and state, initially empty |
| 19 | + * H, a table of cost estimates indexed by state, initially empty |
| 20 | + * s, a, the previous state and action, initially null |
| 21 | + * |
| 22 | + * if GOAL-TEST(s') then return stop |
| 23 | + * if s' is new state (not in H) then H[s'] <- h(s') |
| 24 | + * unless s is null |
| 25 | + * result[a, s] <- s' |
| 26 | + * H[s] <- min LRTA*-COST(s, b, result[b, s], H) |
| 27 | + * b (element of) ACTIONS(s) |
| 28 | + * a <- an action b in ACTIONS(s') that minimizes LRTA*-COST(s', b, result[b, s'], H) |
| 29 | + * s <- s' |
| 30 | + * return a |
| 31 | + * |
| 32 | + * |
| 33 | + * function LRTA*-COST(s, a, s', H) returns a cost estimate |
| 34 | + * if s' is undefined then return h(s) |
| 35 | + * else return c(s, a, s') + H[s'] |
| 36 | + * </code> |
| 37 | + * |
| 38 | + * Figure 4.23 LRTA*-AGENT selects an action according to the value of |
| 39 | + * neighboring states, which are updated as the agent moves about the state |
| 40 | + * space.<br> |
| 41 | + * Note: This algorithm fails to exit if the goal does not exist (e.g. A<->B Goal=X), |
| 42 | + * this could be an issue with the implementation. Comments welcome. |
| 43 | + */ |
| 44 | + |
| 45 | +/** |
| 46 | + * @author Ciaran O'Reilly |
| 47 | + * |
| 48 | + */ |
| 49 | +public class LRTAStarAgent extends Agent { |
| 50 | + |
| 51 | + private Problem problem; |
| 52 | + // static: result, a table, indexed by action and state, initially empty |
| 53 | + private final Hashtable<ActionState, Percept> result = new Hashtable<ActionState, Percept>(); |
| 54 | + // H, a table of cost estimates indexed by state, initially empty |
| 55 | + private final Hashtable<Percept, Double> H = new Hashtable<Percept, Double>(); |
| 56 | + // s, a, the previous state and action, initially null |
| 57 | + private Percept s = null; |
| 58 | + private Object a = null; |
| 59 | + |
| 60 | + public LRTAStarAgent(Problem problem) { |
| 61 | + setProblem(problem); |
| 62 | + } |
| 63 | + |
| 64 | + public Problem getProblem() { |
| 65 | + return problem; |
| 66 | + } |
| 67 | + |
| 68 | + public void setProblem(Problem problem) { |
| 69 | + this.problem = problem; |
| 70 | + init(); |
| 71 | + } |
| 72 | + |
| 73 | + // function LRTA*AGENT(s') returns an action |
| 74 | + // inputs: s', a percept that identifies the current state |
| 75 | + @Override |
| 76 | + public String execute(Percept sComma) { |
| 77 | + |
| 78 | + // if GOAL-TEST(s') then return stop |
| 79 | + if (!goalTest(sComma)) { |
| 80 | + // if s' is new state (not in H) then H[s'] <- h(s') |
| 81 | + if (!H.containsKey(sComma)) { |
| 82 | + H.put(sComma, getProblem().getHeuristicFunction() |
| 83 | + .getHeuristicValue(sComma)); |
| 84 | + } |
| 85 | + // unless s is null |
| 86 | + if (null != s) { |
| 87 | + // result[a, s] <- s' |
| 88 | + result.put(new ActionState(a, s), sComma); |
| 89 | + |
| 90 | + // H[s] <- min LRTA*-COST(s, b, result[b, s], H) |
| 91 | + // b (element of) ACTIONS(s) |
| 92 | + double min = Double.MAX_VALUE; |
| 93 | + for (Object b : actions(s)) { |
| 94 | + double cost = lrtaCost(s, b, result.get(new ActionState(b, |
| 95 | + s))); |
| 96 | + if (cost < min) { |
| 97 | + min = cost; |
| 98 | + } |
| 99 | + } |
| 100 | + H.put(s, min); |
| 101 | + } |
| 102 | + // a <- an action b in ACTIONS(s') that minimizes LRTA*-COST(s', b, |
| 103 | + // result[b, s'], H) |
| 104 | + double min = Double.MAX_VALUE; |
| 105 | + // Just in case no actions |
| 106 | + a = Agent.NO_OP; |
| 107 | + for (Object b : actions(sComma)) { |
| 108 | + double cost = lrtaCost(sComma, b, result.get(new ActionState(b, |
| 109 | + sComma))); |
| 110 | + if (cost < min) { |
| 111 | + min = cost; |
| 112 | + a = b; |
| 113 | + } |
| 114 | + } |
| 115 | + } else { |
| 116 | + a = Agent.NO_OP; |
| 117 | + } |
| 118 | + |
| 119 | + // s <- s' |
| 120 | + s = sComma; |
| 121 | + |
| 122 | + if (Agent.NO_OP.equals(a)) { |
| 123 | + // I'm either at the Goal or can't get to it, |
| 124 | + // which in either case I'm finished so just die. |
| 125 | + die(); |
| 126 | + } |
| 127 | + // return a |
| 128 | + return a.toString(); |
| 129 | + } |
| 130 | + |
| 131 | + // |
| 132 | + // PRIVATE METHODS |
| 133 | + // |
| 134 | + private void init() { |
| 135 | + live(); |
| 136 | + result.clear(); |
| 137 | + H.clear(); |
| 138 | + s = null; |
| 139 | + a = null; |
| 140 | + } |
| 141 | + |
| 142 | + private boolean goalTest(Percept state) { |
| 143 | + return getProblem().isGoalState(state); |
| 144 | + } |
| 145 | + |
| 146 | + // function LRTA*-COST(s, a, s', H) returns a cost estimate |
| 147 | + private double lrtaCost(Percept s, Object action, Percept sComma) { |
| 148 | + // if s' is undefined then return h(s) |
| 149 | + if (null == sComma) { |
| 150 | + return getProblem().getHeuristicFunction().getHeuristicValue(s); |
| 151 | + } |
| 152 | + // else return c(s, a, s') + H[s'] |
| 153 | + return getProblem().getStepCostFunction().calculateStepCost(s, sComma, |
| 154 | + action.toString()) |
| 155 | + + H.get(sComma); |
| 156 | + } |
| 157 | + |
| 158 | + private List<Object> actions(Percept state) { |
| 159 | + List<Object> actions = new ArrayList<Object>(); |
| 160 | + |
| 161 | + List<Successor> successors = getProblem().getSuccessorFunction() |
| 162 | + .getSuccessors(state); |
| 163 | + |
| 164 | + for (Successor s : successors) { |
| 165 | + actions.add(s.getAction()); |
| 166 | + } |
| 167 | + |
| 168 | + return actions; |
| 169 | + } |
| 170 | +} |
0 commit comments