Skip to content

Commit f4ac4a1

Browse files
committed
Wumpus agent application improved
- now experiments with different SAT solvers are supported - metrics are provided including reasoning time - KB was optimized
2 parents 23308d3 + 5b82658 commit f4ac4a1

File tree

13 files changed

+262
-227
lines changed

13 files changed

+262
-227
lines changed

aima-core/src/main/java/aima/core/environment/wumpusworld/EfficientHybridWumpusAgent.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import aima.core.agent.Action;
44
import aima.core.agent.EnvironmentViewNotifier;
55
import aima.core.agent.Percept;
6+
import aima.core.logic.propositional.inference.DPLL;
7+
import aima.core.logic.propositional.inference.DPLLSatisfiable;
68
import aima.core.search.framework.SearchForActions;
79
import aima.core.search.framework.problem.GeneralProblem;
810
import aima.core.search.framework.problem.Problem;
@@ -75,18 +77,18 @@ public class EfficientHybridWumpusAgent extends HybridWumpusAgent {
7577
private Set<Room> visitedRooms = new HashSet<>();
7678

7779
public EfficientHybridWumpusAgent() {
78-
// i.e. default is a 4x4 world as depicted in figure 7.2
79-
this(4, new AgentPosition(1, 1, AgentPosition.Orientation.FACING_NORTH));
80+
this(4, 4, new AgentPosition(1, 1, AgentPosition.Orientation.FACING_NORTH));
8081
}
8182

82-
public EfficientHybridWumpusAgent(int caveDimensions, AgentPosition start) {
83-
this(caveDimensions, start, null);
83+
public EfficientHybridWumpusAgent(int caveXDim, int caveYDim, AgentPosition start) {
84+
this(caveXDim, caveYDim, start, new DPLLSatisfiable(), null);
8485
}
8586

86-
public EfficientHybridWumpusAgent(int caveDimensions, AgentPosition start, EnvironmentViewNotifier notifier) {
87-
super(caveDimensions, start, notifier);
88-
getKB().disableNavSentences();
89-
modelCave = new WumpusCave(caveDimensions, caveDimensions);
87+
public EfficientHybridWumpusAgent(int caveXDim, int caveYDim, AgentPosition start, DPLL satSolver,
88+
EnvironmentViewNotifier notifier) {
89+
super(caveXDim, caveYDim, start, satSolver, notifier);
90+
getKB().disableNavSentences(); // Optimization: Verbosity of produced sentences is reduced.
91+
modelCave = new WumpusCave(caveXDim, caveYDim);
9092
visitedRooms.add(currentPosition.getRoom());
9193
}
9294

@@ -105,16 +107,15 @@ public Action execute(Percept percept) {
105107
// TELL(KB, MAKE-PERCEPT-SENTENCE(percept, t))
106108
getKB().makePerceptSentence((WumpusPercept) percept, t);
107109
// TELL the KB the temporal "physics" sentences for time t
108-
getKB().tellTemporalPhysicsSentences(t);
110+
// Optimization: The agent is aware of it's position - the KB can profit from that!
111+
getKB().tellTemporalPhysicsSentences(t, currentPosition);
109112

110113
Set<Room> safe = null;
111114
Set<Room> unvisited = null;
112115

113116
// Speed optimization: Do not ask anything during plan execution (different from pseudo-code)
114117
if (plan.isEmpty()) {
115-
notifyViews("Reasoning (t=" + t + ", Percept=" + percept + ", Pos=" + currentPosition +
116-
", KB.size=" + getKB().size() + ", KB.sym.size=" + getKB().getSymbols().size() +
117-
", KB.cnf.size=" + getKB().asCNF().size() + ") ...");
118+
notifyViews("Reasoning (t=" + t + ", Percept=" + percept + ", Pos=" + currentPosition + ") ...");
118119

119120
// safe <- {[x, y] : ASK(KB, OK<sup>t</sup><sub>x,y</sub>) = true}
120121
safe = getKB().askSafeRooms(t);
@@ -172,10 +173,8 @@ public Action execute(Percept percept) {
172173
getKB().makeActionSentence(action, t);
173174
// t <- t+1
174175
t = t + 1;
175-
176176
updateAgentPosition(action);
177177
visitedRooms.add(currentPosition.getRoom());
178-
getKB().makePositionSentence(currentPosition, t);
179178
// return action
180179
return action;
181180
}
@@ -192,7 +191,6 @@ public Action execute(Percept percept) {
192191
* goal from the current position.
193192
*/
194193
public List<WumpusAction> planRoute(Set<AgentPosition> goals, Set<Room> allowed) {
195-
196194
modelCave.setAllowed(allowed);
197195
Problem<AgentPosition, WumpusAction> problem = new GeneralProblem<>(currentPosition,
198196
WumpusFunctions.createActionsFunction(modelCave),
@@ -201,7 +199,7 @@ public List<WumpusAction> planRoute(Set<AgentPosition> goals, Set<Room> allowed)
201199
new AStarSearch<>(new GraphSearch<>(), new ManhattanHeuristicFunction(goals));
202200
Optional<List<WumpusAction>> actions = search.findActions(problem);
203201

204-
return actions.isPresent() ? actions.get() : Collections.EMPTY_LIST;
202+
return actions.isPresent() ? actions.get() : Collections.emptyList();
205203
}
206204

207205
/**

aima-core/src/main/java/aima/core/environment/wumpusworld/HybridWumpusAgent.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import aima.core.agent.impl.AbstractAgent;
77
import aima.core.logic.propositional.inference.DPLL;
88
import aima.core.logic.propositional.inference.DPLLSatisfiable;
9-
import aima.core.logic.propositional.inference.OptimizedDPLL;
9+
import aima.core.search.framework.Metrics;
1010
import aima.core.search.framework.SearchForActions;
1111
import aima.core.search.framework.problem.GeneralProblem;
1212
import aima.core.search.framework.problem.Problem;
@@ -87,16 +87,16 @@ public class HybridWumpusAgent extends AbstractAgent {
8787

8888
public HybridWumpusAgent() {
8989
// i.e. default is a 4x4 world as depicted in figure 7.2
90-
this(4, new AgentPosition(1, 1, AgentPosition.Orientation.FACING_NORTH));
90+
this(4, 4, new AgentPosition(1, 1, AgentPosition.Orientation.FACING_NORTH));
9191
}
9292

93-
public HybridWumpusAgent(int caveDimensions, AgentPosition start) {
94-
this(caveDimensions, start, new DPLLSatisfiable(), null);
93+
public HybridWumpusAgent(int caveXDim, int caveYDim, AgentPosition start) {
94+
this(caveXDim, caveYDim, start, new DPLLSatisfiable(), null);
9595
}
9696

97-
public HybridWumpusAgent(int caveDimensions, AgentPosition start, DPLL dpll, EnvironmentViewNotifier notifier) {
98-
// kb = new WumpusKnowledgeBase(new OptimizedDPLL(), caveDimensions, start); // buggy?
99-
kb = new WumpusKnowledgeBase(dpll, caveDimensions, start);
97+
public HybridWumpusAgent(int caveXDim, int caveYDim, AgentPosition start, DPLL satSolver,
98+
EnvironmentViewNotifier notifier) {
99+
kb = new WumpusKnowledgeBase(satSolver, caveXDim, caveYDim, start);
100100
this.start = start;
101101
this.currentPosition = start;
102102
this.notifier = notifier;
@@ -127,8 +127,7 @@ public Action execute(Percept percept) {
127127

128128
// Speed optimization: Do not ask anything during plan execution (different from pseudo-code)
129129
if (plan.isEmpty()) {
130-
notifyViews("Reasoning (t=" + t + ", Percept=" + percept + ", KB.size=" + kb.size() +
131-
", KB.sym.size=" + kb.getSymbols().size() + ", KB.cnf.size=" + kb.asCNF().size() + ") ...");
130+
notifyViews("Reasoning (t=" + t + ", Percept=" + percept + ") ...");
132131
currentPosition = kb.askCurrentPosition(t);
133132
notifyViews("Ask position -> " + currentPosition);
134133
// safe <- {[x, y] : ASK(KB, OK<sup>t</sup><sub>x,y</sub>) = true}
@@ -235,7 +234,7 @@ public List<WumpusAction> planRoute(Set<AgentPosition> goals, Set<Room> allowed)
235234
new AStarSearch<>(new GraphSearch<>(), new ManhattanHeuristicFunction(goals));
236235
Optional<List<WumpusAction>> actions = search.findActions(problem);
237236

238-
return actions.isPresent() ? actions.get() : Collections.EMPTY_LIST;
237+
return actions.isPresent() ? actions.get() : Collections.emptyList();
239238
}
240239

241240
/**
@@ -282,6 +281,10 @@ public List<WumpusAction> planShot(Set<Room> possibleWumpus, Set<Room> allowed)
282281
return actions;
283282
}
284283

284+
public Metrics getMetrics() {
285+
return kb.getMetrics();
286+
}
287+
285288
protected void notifyViews(String msg) {
286289
if (notifier != null)
287290
notifier.notifyViews(msg);

aima-core/src/main/java/aima/core/environment/wumpusworld/WumpusCave.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public WumpusCave(int caveXDimension, int caveYDimension) {
6464
*/
6565
public WumpusCave(int caveXDimension, int caveYDimension, String config) {
6666
this(caveXDimension, caveYDimension);
67+
if (config.length() != 2 * caveXDimension * caveYDimension)
68+
throw new IllegalStateException("Wrong configuration length.");
6769
for (int i = 0; i < config.length(); i++) {
6870
char c = config.charAt(i);
6971
Room r = new Room(i / 2 % caveXDimension + 1, caveYDimension - i / 2 / caveXDimension);

0 commit comments

Comments
 (0)