-
Notifications
You must be signed in to change notification settings - Fork 793
/
SimpleProblemSolvingAgent.java
101 lines (91 loc) · 3.4 KB
/
SimpleProblemSolvingAgent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package aima.core.search.agent;
import aima.core.agent.impl.SimpleAgent;
import aima.core.search.framework.problem.Problem;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Queue;
/**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 3.1, page 67.<br>
* <br>
* <pre>
* function SIMPLE-PROBLEM-SOLVING-AGENT(percept) returns an action
* persistent: seq, an action sequence, initially empty
* state, some description of the current world state
* goal, a goal, initially null
* problem, a problem formulation
*
* state <- UPDATE-STATE(state, percept)
* if seq is empty then
* goal <- FORMULATE-GOAL(state)
* problem <- FORMULATE-PROBLEM(state, goal)
* seq <- SEARCH(problem)
* if seq = failure then return a null action
* action <- FIRST(seq)
* seq <- REST(seq)
* return action
* </pre>
*
* Figure 3.1 A simple problem-solving agent. It first formulates a goal and a
* problem, searches for a sequence of actions that would solve the problem, and
* then executes the actions one at a time. When this is complete, it formulates
* another goal and starts over.<br>
*
* @param <P> The type used to represent percepts
* @param <S> The type used to represent states
* @param <A> The type of the actions to be used to navigate through the state space
*
* @author Ciaran O'Reilly
* @author Mike Stampone
* @author Ruediger Lunde
*/
public abstract class SimpleProblemSolvingAgent<P, S, A> extends SimpleAgent<P, A> {
/// seq, an action sequence, initially empty
private Queue<A> seq = new LinkedList<>();
/// function SIMPLE-PROBLEM-SOLVING-AGENT(percept) returns an action
/**
* Template method which decides about the action to perform next taking into account the current percept.
* @param percept The current percept
* @return An action or empty if started at the goal, current goal unreachable, or no further goal exists
*/
@Override
public Optional<A> act(P percept) {
/// state <- UPDATE-STATE(state, percept)
updateState(percept);
/// if seq is empty then do
if (seq.isEmpty()) {
/// goal <- FORMULATE-GOAL(state)
Optional<Object> goal = formulateGoal();
if (goal.isPresent()) {
/// problem <- FORMULATE-PROBLEM(state, goal)
Problem<S, A> problem = formulateProblem(goal.get());
/// seq <- SEARCH(problem)
Optional<List<A>> actions = search(problem);
// actions is empty if goal is unreachable
// actions contains empty list of actions if agent is at the goal
actions.ifPresent(as -> seq.addAll(as));
} else {
handleNoGoal();
}
}
/// if seq = failure then return a null action
/// action <- FIRST(seq)
/// seq <- REST(seq)
return Optional.ofNullable(!seq.isEmpty() ? seq.remove() : null);
}
protected abstract void updateState(P percept);
/**
* Primitive operation, responsible for goal generation. Implementations are
* allowed to return empty to indicate that the agent has finished the job.
*/
protected abstract Optional<Object> formulateGoal();
protected abstract Problem<S, A> formulateProblem(Object goal);
protected abstract Optional<List<A>> search(Problem<S, A> problem);
/**
* Primitive operation, which decides what to do if goal formulation failed.
* In this default implementation the agent dies.
*/
protected void handleNoGoal() {
setAlive(false);
}
}