Skip to content

Commit

Permalink
- Explicit state representation removed from EnvironmentView(was never
Browse files Browse the repository at this point in the history
used)
  • Loading branch information
ruediger.lunde@gmail.com committed Aug 7, 2016
1 parent 5290070 commit 5d3c28b
Show file tree
Hide file tree
Showing 20 changed files with 344 additions and 200 deletions.
15 changes: 7 additions & 8 deletions aima-core/src/main/java/aima/core/agent/EnvironmentView.java
Expand Up @@ -7,6 +7,7 @@
* @author Ravi Mohan * @author Ravi Mohan
* @author Ciaran O'Reilly * @author Ciaran O'Reilly
* @author Mike Stampone * @author Mike Stampone
* @author Ruediger Lunde
*/ */
public interface EnvironmentView { public interface EnvironmentView {
/** /**
Expand All @@ -23,11 +24,10 @@ public interface EnvironmentView {
* *
* @param agent * @param agent
* the Agent just added to the Environment. * the Agent just added to the Environment.
* @param resultingState * @param source
* the EnvironmentState that resulted from the Agent being added * the Environment to which the agent was added.
* to the Environment.
*/ */
void agentAdded(Agent agent, EnvironmentState resultingState); void agentAdded(Agent agent, Environment source);


/** /**
* Indicates the Environment has changed as a result of an Agent's action. * Indicates the Environment has changed as a result of an Agent's action.
Expand All @@ -36,9 +36,8 @@ public interface EnvironmentView {
* the Agent that performed the Action. * the Agent that performed the Action.
* @param action * @param action
* the Action the Agent performed. * the Action the Agent performed.
* @param resultingState * @param source
* the EnvironmentState that resulted from the Agent's Action on * the Environment in which the agent has acted.
* the Environment.
*/ */
void agentActed(Agent agent, Action action, EnvironmentState resultingState); void agentActed(Agent agent, Action action, Environment source);
} }
Expand Up @@ -100,8 +100,8 @@ public void step() {
for (Agent agent : agents) { for (Agent agent : agents) {
if (agent.isAlive()) { if (agent.isAlive()) {
Action anAction = agent.execute(getPerceptSeenBy(agent)); Action anAction = agent.execute(getPerceptSeenBy(agent));
EnvironmentState es = executeAction(agent, anAction); executeAction(agent, anAction);
updateEnvironmentViewsAgentActed(agent, anAction, es); updateEnvironmentViewsAgentActed(agent, anAction);
} }
} }
createExogenousChange(); createExogenousChange();
Expand Down Expand Up @@ -166,14 +166,13 @@ protected void updatePerformanceMeasure(Agent forAgent, double addTo) {


protected void updateEnvironmentViewsAgentAdded(Agent agent) { protected void updateEnvironmentViewsAgentAdded(Agent agent) {
for (EnvironmentView view : views) { for (EnvironmentView view : views) {
view.agentAdded(agent, getCurrentState()); view.agentAdded(agent, this);
} }
} }


protected void updateEnvironmentViewsAgentActed(Agent agent, Action action, protected void updateEnvironmentViewsAgentActed(Agent agent, Action action) {
EnvironmentState state) {
for (EnvironmentView view : views) { for (EnvironmentView view : views) {
view.agentActed(agent, action, state); view.agentActed(agent, action, this);
} }
} }
} }
Expand Up @@ -2,7 +2,7 @@


import aima.core.agent.Action; import aima.core.agent.Action;
import aima.core.agent.Agent; import aima.core.agent.Agent;
import aima.core.agent.EnvironmentState; import aima.core.agent.Environment;
import aima.core.agent.EnvironmentView; import aima.core.agent.EnvironmentView;


/** /**
Expand All @@ -13,13 +13,12 @@
*/ */
public class SimpleEnvironmentView implements EnvironmentView { public class SimpleEnvironmentView implements EnvironmentView {
@Override @Override
public void agentActed(Agent agent, Action action, public void agentActed(Agent agent, Action action, Environment source) {
EnvironmentState resultingState) {
System.out.println("Agent acted: " + action.toString()); System.out.println("Agent acted: " + action.toString());
} }


@Override @Override
public void agentAdded(Agent agent, EnvironmentState resultingState) { public void agentAdded(Agent agent, Environment source) {
System.out.println("Agent added."); System.out.println("Agent added.");
} }


Expand Down
Expand Up @@ -2,7 +2,7 @@


import aima.core.agent.Action; import aima.core.agent.Action;
import aima.core.agent.Agent; import aima.core.agent.Agent;
import aima.core.agent.EnvironmentState; import aima.core.agent.Environment;
import aima.core.agent.EnvironmentView; import aima.core.agent.EnvironmentView;


public class VacuumEnvironmentViewActionTracker implements EnvironmentView { public class VacuumEnvironmentViewActionTracker implements EnvironmentView {
Expand All @@ -18,11 +18,11 @@ public void notify(String msg) {
// Do nothing by default. // Do nothing by default.
} }


public void agentAdded(Agent agent, EnvironmentState state) { public void agentAdded(Agent agent, Environment source) {
// Do nothing by default. // Do nothing by default.
} }


public void agentActed(Agent agent, Action action, EnvironmentState state) { public void agentActed(Agent agent, Action action, Environment source) {
actions.append(action); actions.append(action);
} }


Expand Down
Expand Up @@ -6,7 +6,7 @@


import aima.core.agent.Action; import aima.core.agent.Action;
import aima.core.agent.Agent; import aima.core.agent.Agent;
import aima.core.agent.EnvironmentState; import aima.core.agent.Environment;
import aima.core.agent.EnvironmentView; import aima.core.agent.EnvironmentView;
import aima.core.environment.map.ExtendableMap; import aima.core.environment.map.ExtendableMap;
import aima.core.environment.map.MapAgent; import aima.core.environment.map.MapAgent;
Expand Down Expand Up @@ -96,11 +96,11 @@ public void notify(String msg) {
envChanges.append(msg).append(":"); envChanges.append(msg).append(":");
} }


public void agentAdded(Agent agent, EnvironmentState state) { public void agentAdded(Agent agent, Environment source) {
// Nothing // Nothing
} }


public void agentActed(Agent agent, Action action, EnvironmentState state) { public void agentActed(Agent agent, Action action, Environment source) {
envChanges.append(action).append(":"); envChanges.append(action).append(":");
} }
} }
Expand Down
Expand Up @@ -6,7 +6,7 @@


import aima.core.agent.Action; import aima.core.agent.Action;
import aima.core.agent.Agent; import aima.core.agent.Agent;
import aima.core.agent.EnvironmentState; import aima.core.agent.Environment;
import aima.core.agent.EnvironmentView; import aima.core.agent.EnvironmentView;
import aima.core.environment.map.Map; import aima.core.environment.map.Map;
import aima.core.environment.map.MapAgent; import aima.core.environment.map.MapAgent;
Expand Down Expand Up @@ -39,14 +39,12 @@ public void setUp() {
HeuristicFunction heuristicFunction = new HeuristicFunction() { HeuristicFunction heuristicFunction = new HeuristicFunction() {
public double h(Object state) { public double h(Object state) {
Point2D pt1 = aMap.getPosition((String) state); Point2D pt1 = aMap.getPosition((String) state);
Point2D pt2 = aMap Point2D pt2 = aMap.getPosition(SimplifiedRoadMapOfPartOfRomania.BUCHAREST);
.getPosition(SimplifiedRoadMapOfPartOfRomania.BUCHAREST);
return pt1.distance(pt2); return pt1.distance(pt2);
} }
}; };


recursiveBestFirstSearch = new RecursiveBestFirstSearch( recursiveBestFirstSearch = new RecursiveBestFirstSearch(new AStarEvaluationFunction(heuristicFunction));
new AStarEvaluationFunction(heuristicFunction));
recursiveBestFirstSearchAvoidingLoops = new RecursiveBestFirstSearch( recursiveBestFirstSearchAvoidingLoops = new RecursiveBestFirstSearch(
new AStarEvaluationFunction(heuristicFunction), true); new AStarEvaluationFunction(heuristicFunction), true);
} }
Expand Down Expand Up @@ -80,7 +78,7 @@ public void testAIMA3eFigure3_27() {
"CurrentLocation=In(Arad), Goal=In(Bucharest):Action[name==moveTo, location==Sibiu]:Action[name==moveTo, location==RimnicuVilcea]:Action[name==moveTo, location==Pitesti]:Action[name==moveTo, location==Bucharest]:METRIC[pathCost]=418.0:METRIC[maxRecursiveDepth]=4:METRIC[nodesExpanded]=6:Action[name==NoOp]:", "CurrentLocation=In(Arad), Goal=In(Bucharest):Action[name==moveTo, location==Sibiu]:Action[name==moveTo, location==RimnicuVilcea]:Action[name==moveTo, location==Pitesti]:Action[name==moveTo, location==Bucharest]:METRIC[pathCost]=418.0:METRIC[maxRecursiveDepth]=4:METRIC[nodesExpanded]=6:Action[name==NoOp]:",
envChanges.toString()); envChanges.toString());
} }

@Test @Test
public void testAIMA3eAradNeamtA() { public void testAIMA3eAradNeamtA() {
MapEnvironment me = new MapEnvironment(aMap); MapEnvironment me = new MapEnvironment(aMap);
Expand All @@ -95,8 +93,7 @@ public void testAIMA3eAradNeamtA() {
"CurrentLocation=In(Arad), Goal=In(Neamt):Action[name==moveTo, location==Sibiu]:Action[name==moveTo, location==RimnicuVilcea]:Action[name==moveTo, location==Pitesti]:Action[name==moveTo, location==Bucharest]:Action[name==moveTo, location==Urziceni]:Action[name==moveTo, location==Vaslui]:Action[name==moveTo, location==Iasi]:Action[name==moveTo, location==Neamt]:METRIC[pathCost]=824.0:METRIC[maxRecursiveDepth]=12:METRIC[nodesExpanded]=340208:Action[name==NoOp]:", "CurrentLocation=In(Arad), Goal=In(Neamt):Action[name==moveTo, location==Sibiu]:Action[name==moveTo, location==RimnicuVilcea]:Action[name==moveTo, location==Pitesti]:Action[name==moveTo, location==Bucharest]:Action[name==moveTo, location==Urziceni]:Action[name==moveTo, location==Vaslui]:Action[name==moveTo, location==Iasi]:Action[name==moveTo, location==Neamt]:METRIC[pathCost]=824.0:METRIC[maxRecursiveDepth]=12:METRIC[nodesExpanded]=340208:Action[name==NoOp]:",
envChanges.toString()); envChanges.toString());
} }



@Test @Test
public void testAIMA3eAradNeamtB() { public void testAIMA3eAradNeamtB() {
MapEnvironment me = new MapEnvironment(aMap); MapEnvironment me = new MapEnvironment(aMap);
Expand All @@ -118,12 +115,11 @@ public void notify(String msg) {
envChanges.append(msg).append(":"); envChanges.append(msg).append(":");
} }


public void agentAdded(Agent agent, EnvironmentState state) { public void agentAdded(Agent agent, Environment source) {
// Nothing. // Nothing.
} }


public void agentActed(Agent agent, Action action, public void agentActed(Agent agent, Action action, Environment source) {
EnvironmentState state) {
envChanges.append(action).append(":"); envChanges.append(action).append(":");
} }
} }
Expand Down
Expand Up @@ -6,7 +6,7 @@


import aima.core.agent.Action; import aima.core.agent.Action;
import aima.core.agent.Agent; import aima.core.agent.Agent;
import aima.core.agent.EnvironmentState; import aima.core.agent.Environment;
import aima.core.agent.EnvironmentView; import aima.core.agent.EnvironmentView;
import aima.core.environment.map.ExtendableMap; import aima.core.environment.map.ExtendableMap;
import aima.core.environment.map.MapEnvironment; import aima.core.environment.map.MapEnvironment;
Expand Down Expand Up @@ -44,8 +44,7 @@ public double h(Object state) {
@Test @Test
public void testAlreadyAtGoal() { public void testAlreadyAtGoal() {
MapEnvironment me = new MapEnvironment(aMap); MapEnvironment me = new MapEnvironment(aMap);
LRTAStarAgent agent = new LRTAStarAgent(new OnlineSearchProblem( LRTAStarAgent agent = new LRTAStarAgent(new OnlineSearchProblem(MapFunctionFactory.getActionsFunction(aMap),
MapFunctionFactory.getActionsFunction(aMap),
new DefaultGoalTest("A"), new MapStepCostFunction(aMap)), new DefaultGoalTest("A"), new MapStepCostFunction(aMap)),
MapFunctionFactory.getPerceptToStateFunction(), hf); MapFunctionFactory.getPerceptToStateFunction(), hf);
me.addAgent(agent, "A"); me.addAgent(agent, "A");
Expand All @@ -58,8 +57,7 @@ public void testAlreadyAtGoal() {
@Test @Test
public void testNormalSearch() { public void testNormalSearch() {
MapEnvironment me = new MapEnvironment(aMap); MapEnvironment me = new MapEnvironment(aMap);
LRTAStarAgent agent = new LRTAStarAgent(new OnlineSearchProblem( LRTAStarAgent agent = new LRTAStarAgent(new OnlineSearchProblem(MapFunctionFactory.getActionsFunction(aMap),
MapFunctionFactory.getActionsFunction(aMap),
new DefaultGoalTest("F"), new MapStepCostFunction(aMap)), new DefaultGoalTest("F"), new MapStepCostFunction(aMap)),
MapFunctionFactory.getPerceptToStateFunction(), hf); MapFunctionFactory.getPerceptToStateFunction(), hf);
me.addAgent(agent, "A"); me.addAgent(agent, "A");
Expand All @@ -74,8 +72,7 @@ public void testNormalSearch() {
@Test @Test
public void testNoPath() { public void testNoPath() {
MapEnvironment me = new MapEnvironment(aMap); MapEnvironment me = new MapEnvironment(aMap);
LRTAStarAgent agent = new LRTAStarAgent(new OnlineSearchProblem( LRTAStarAgent agent = new LRTAStarAgent(new OnlineSearchProblem(MapFunctionFactory.getActionsFunction(aMap),
MapFunctionFactory.getActionsFunction(aMap),
new DefaultGoalTest("G"), new MapStepCostFunction(aMap)), new DefaultGoalTest("G"), new MapStepCostFunction(aMap)),
MapFunctionFactory.getPerceptToStateFunction(), hf); MapFunctionFactory.getPerceptToStateFunction(), hf);
me.addAgent(agent, "A"); me.addAgent(agent, "A");
Expand All @@ -95,12 +92,11 @@ public void notify(String msg) {
envChanges.append(msg).append("->"); envChanges.append(msg).append("->");
} }


public void agentAdded(Agent agent, EnvironmentState state) { public void agentAdded(Agent agent, Environment source) {
// Nothing. // Nothing.
} }


public void agentActed(Agent agent, Action action, public void agentActed(Agent agent, Action action, Environment source) {
EnvironmentState state) {
envChanges.append(action).append("->"); envChanges.append(action).append("->");
} }
} }
Expand Down
Expand Up @@ -6,7 +6,7 @@


import aima.core.agent.Action; import aima.core.agent.Action;
import aima.core.agent.Agent; import aima.core.agent.Agent;
import aima.core.agent.EnvironmentState; import aima.core.agent.Environment;
import aima.core.agent.EnvironmentView; import aima.core.agent.EnvironmentView;
import aima.core.environment.map.ExtendableMap; import aima.core.environment.map.ExtendableMap;
import aima.core.environment.map.MapEnvironment; import aima.core.environment.map.MapEnvironment;
Expand Down Expand Up @@ -38,8 +38,7 @@ public void setUp() {
@Test @Test
public void testAlreadyAtGoal() { public void testAlreadyAtGoal() {
MapEnvironment me = new MapEnvironment(aMap); MapEnvironment me = new MapEnvironment(aMap);
OnlineDFSAgent agent = new OnlineDFSAgent(new OnlineSearchProblem( OnlineDFSAgent agent = new OnlineDFSAgent(new OnlineSearchProblem(MapFunctionFactory.getActionsFunction(aMap),
MapFunctionFactory.getActionsFunction(aMap),
new DefaultGoalTest("A"), new MapStepCostFunction(aMap)), new DefaultGoalTest("A"), new MapStepCostFunction(aMap)),
MapFunctionFactory.getPerceptToStateFunction()); MapFunctionFactory.getPerceptToStateFunction());
me.addAgent(agent, "A"); me.addAgent(agent, "A");
Expand All @@ -52,8 +51,7 @@ public void testAlreadyAtGoal() {
@Test @Test
public void testNormalSearch() { public void testNormalSearch() {
MapEnvironment me = new MapEnvironment(aMap); MapEnvironment me = new MapEnvironment(aMap);
OnlineDFSAgent agent = new OnlineDFSAgent(new OnlineSearchProblem( OnlineDFSAgent agent = new OnlineDFSAgent(new OnlineSearchProblem(MapFunctionFactory.getActionsFunction(aMap),
MapFunctionFactory.getActionsFunction(aMap),
new DefaultGoalTest("G"), new MapStepCostFunction(aMap)), new DefaultGoalTest("G"), new MapStepCostFunction(aMap)),
MapFunctionFactory.getPerceptToStateFunction()); MapFunctionFactory.getPerceptToStateFunction());
me.addAgent(agent, "A"); me.addAgent(agent, "A");
Expand All @@ -70,8 +68,7 @@ public void testNoPath() {
aMap = new ExtendableMap(); aMap = new ExtendableMap();
aMap.addBidirectionalLink("A", "B", 1.0); aMap.addBidirectionalLink("A", "B", 1.0);
MapEnvironment me = new MapEnvironment(aMap); MapEnvironment me = new MapEnvironment(aMap);
OnlineDFSAgent agent = new OnlineDFSAgent(new OnlineSearchProblem( OnlineDFSAgent agent = new OnlineDFSAgent(new OnlineSearchProblem(MapFunctionFactory.getActionsFunction(aMap),
MapFunctionFactory.getActionsFunction(aMap),
new DefaultGoalTest("X"), new MapStepCostFunction(aMap)), new DefaultGoalTest("X"), new MapStepCostFunction(aMap)),
MapFunctionFactory.getPerceptToStateFunction()); MapFunctionFactory.getPerceptToStateFunction());
me.addAgent(agent, "A"); me.addAgent(agent, "A");
Expand All @@ -97,8 +94,7 @@ public void testAIMA3eFig4_19() {
aMap.addBidirectionalLink("2,3", "1,3", 1.0); aMap.addBidirectionalLink("2,3", "1,3", 1.0);


MapEnvironment me = new MapEnvironment(aMap); MapEnvironment me = new MapEnvironment(aMap);
OnlineDFSAgent agent = new OnlineDFSAgent(new OnlineSearchProblem( OnlineDFSAgent agent = new OnlineDFSAgent(new OnlineSearchProblem(MapFunctionFactory.getActionsFunction(aMap),
MapFunctionFactory.getActionsFunction(aMap),
new DefaultGoalTest("3,3"), new MapStepCostFunction(aMap)), new DefaultGoalTest("3,3"), new MapStepCostFunction(aMap)),
MapFunctionFactory.getPerceptToStateFunction()); MapFunctionFactory.getPerceptToStateFunction());
me.addAgent(agent, "1,1"); me.addAgent(agent, "1,1");
Expand All @@ -115,12 +111,11 @@ public void notify(String msg) {
envChanges.append(msg).append("->"); envChanges.append(msg).append("->");
} }


public void agentAdded(Agent agent, EnvironmentState state) { public void agentAdded(Agent agent, Environment source) {
// Nothing. // Nothing.
} }


public void agentActed(Agent agent, Action action, public void agentActed(Agent agent, Action action, Environment source) {
EnvironmentState state) {
envChanges.append(action).append("->"); envChanges.append(action).append("->");
} }
} }
Expand Down
Expand Up @@ -6,7 +6,7 @@


import aima.core.agent.Action; import aima.core.agent.Action;
import aima.core.agent.Agent; import aima.core.agent.Agent;
import aima.core.agent.EnvironmentState; import aima.core.agent.Environment;
import aima.core.agent.EnvironmentView; import aima.core.agent.EnvironmentView;
import aima.core.environment.map.ExtendableMap; import aima.core.environment.map.ExtendableMap;
import aima.core.environment.map.MapAgent; import aima.core.environment.map.MapAgent;
Expand Down Expand Up @@ -338,11 +338,11 @@ public void notify(String msg) {
envChanges.append(msg).append(":"); envChanges.append(msg).append(":");
} }


public void agentAdded(Agent agent, EnvironmentState state) { public void agentAdded(Agent agent, Environment source) {
// Nothing. // Nothing.
} }


public void agentActed(Agent agent, Action action, EnvironmentState state) { public void agentActed(Agent agent, Action action, Environment source) {
envChanges.append(action).append(":"); envChanges.append(action).append(":");
} }
} }
Expand Down

0 comments on commit 5d3c28b

Please sign in to comment.