/
SimpleProblemSolvingAgent.cs
136 lines (119 loc) · 4.37 KB
/
SimpleProblemSolvingAgent.cs
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System.Collections.Generic;
using aima.core.agent;
using aima.core.agent.impl;
using aima.core.util;
using aima.core.search.framework.problem;
namespace aima.core.search.framework
{
/**
* 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>
*
* @author Ciaran O'Reilly
* @author Mike Stampone
*/
public abstract class SimpleProblemSolvingAgent : AbstractAgent
{
// seq, an action sequence, initially empty
private List<Action> seq = new List<Action>();
//
private bool formulateGoalsIndefinitely = true;
private int maxGoalsToFormulate = 1;
private int goalsFormulated = 0;
/**
* Constructs a simple problem solving agent which will formulate goals
* indefinitely.
*/
public SimpleProblemSolvingAgent()
{
formulateGoalsIndefinitely = true;
}
/**
* Constructs a simple problem solving agent which will formulate, at
* maximum, the specified number of goals.
*
* @param maxGoalsToFormulate
* the maximum number of goals this agent is to formulate.
*/
public SimpleProblemSolvingAgent(int maxGoalsToFormulate)
{
formulateGoalsIndefinitely = false;
this.maxGoalsToFormulate = maxGoalsToFormulate;
}
// function SIMPLE-PROBLEM-SOLVING-AGENT(percept) returns an action
public override Action execute(Percept p)
{
Action action = NoOpAction.NO_OP;
// state <- UPDATE-STATE(state, percept)
updateState(p);
// if seq is empty then do
if (0 == seq.Count)
{
if (formulateGoalsIndefinitely
|| goalsFormulated < maxGoalsToFormulate)
{
if (goalsFormulated > 0)
{
notifyViewOfMetrics();
}
// goal <- FORMULATE-GOAL(state)
System.Object goal = formulateGoal();
goalsFormulated++;
// problem <- FORMULATE-PROBLEM(state, goal)
Problem problem = formulateProblem(goal);
// seq <- SEARCH(problem)
seq.AddRange(search(problem));
if (0 == seq.Count)
{
// Unable to identify a path
seq.Add(NoOpAction.NO_OP);
}
}
else
{
// Agent no longer wishes to
// achieve any more goals
setAlive(false);
notifyViewOfMetrics();
}
}
if (seq.Count > 0)
{
// action <- FIRST(seq)
action = Util.first(seq);
// seq <- REST(seq)
seq = Util.rest(seq);
}
return action;
}
// PROTECTED METHODS
protected abstract State updateState(Percept p);
protected abstract System.Object formulateGoal();
protected abstract Problem formulateProblem(System.Object goal);
protected abstract List<Action> search(Problem problem);
protected abstract void notifyViewOfMetrics();
}
}