11package  aima .core .environment .vacuum ;
22
3- import  java .util .Arrays ;
4- import  java .util .List ;
5- import  java .util .Random ;
6- 
73import  aima .core .agent .Action ;
84import  aima .core .agent .Agent ;
95import  aima .core .agent .EnvironmentState ;
106import  aima .core .agent .Percept ;
117import  aima .core .agent .impl .AbstractEnvironment ;
128import  aima .core .agent .impl .DynamicAction ;
139import  aima .core .search .agent .NondeterministicSearchAgent ;
10+ import  aima .core .util .Util ;
11+ 
12+ import  java .util .Arrays ;
13+ import  java .util .List ;
14+ import  java .util .Random ;
1415
1516/** 
1617 * Artificial Intelligence A Modern Approach (3rd Edition): pg 58.<br> 
2526 * @author Ravi Mohan 
2627 * @author Ciaran O'Reilly 
2728 * @author Mike Stampone 
29+  * @author Ruediger Lunde 
2830 */ 
2931public  class  VacuumEnvironment  extends  AbstractEnvironment  {
3032	// Allowable Actions within the Vacuum Environment 
@@ -38,23 +40,21 @@ public enum LocationState {
3840		Clean , Dirty 
3941	}
4042
41-     // 
43+     private   final   List < String >  locations ; 
4244	protected  VacuumEnvironmentState  envState  = null ;
4345	protected  boolean  isDone  = false ;
4446
4547	/** 
46- 	 * Constructs a vacuum environment with two locations, in which dirt is 
48+ 	 * Constructs a vacuum environment with two locations A and B , in which dirt is 
4749	 * placed at random. 
4850	 */ 
4951	public  VacuumEnvironment () {
50- 		Random  r  = new  Random ();
51- 		envState  = new  VacuumEnvironmentState (
52- 				0  == r .nextInt (2 ) ? LocationState .Clean  : LocationState .Dirty ,
53- 				0  == r .nextInt (2 ) ? LocationState .Clean  : LocationState .Dirty );
52+ 		this (Util .randomBoolean () ? LocationState .Clean  : LocationState .Dirty ,
53+ 				Util .randomBoolean () ? LocationState .Clean  : LocationState .Dirty );
5454	}
5555
5656	/** 
57- 	 * Constructs a vacuum environment with two locations, in which dirt is 
57+ 	 * Constructs a vacuum environment with two locations A and B , in which dirt is 
5858	 * placed as specified. 
5959	 *  
6060	 * @param locAState 
@@ -65,25 +65,69 @@ public VacuumEnvironment() {
6565	 *            <em>Clean</em> or <em>Dirty</em>. 
6666	 */ 
6767	public  VacuumEnvironment (LocationState  locAState , LocationState  locBState ) {
68- 		envState  = new  VacuumEnvironmentState (locAState , locBState );
68+ 		this (Arrays .asList (LOCATION_A , LOCATION_B ), locAState , locBState );
69+ 	}
70+ 
71+ 	protected  VacuumEnvironment (List <String > locations , LocationState ... locStates ) {
72+ 		this .locations  = locations ;
73+ 		envState  = new  VacuumEnvironmentState ();
74+ 		for  (int  i  = 0 ; i  < locations .size () && i  < locStates .length ; i ++)
75+ 			envState .setLocationState (locations .get (i ), locStates [i ]);
76+ 	}
77+ 
78+ 	public  List <String > getLocations () {
79+ 		return  locations ;
6980	}
7081
7182	public  EnvironmentState  getCurrentState () {
7283		return  envState ;
7384	}
74- 	
75- 	public  List <String > getLocations () {
76- 		return  Arrays .asList (LOCATION_A , LOCATION_B );
85+ 
86+ 	public  LocationState  getLocationState (String  location ) {
87+ 		return  envState .getLocationState (location );
88+ 	}
89+ 
90+ 	public  String  getAgentLocation (Agent  a ) {
91+ 		return  envState .getAgentLocation (a );
7792	}
7893
7994	@ Override 
80- 	public  void  executeAction (Agent  a , Action  agentAction ) {
95+ 	public  void  addAgent (Agent  a ) {
96+ 		int  idx  = new  Random ().nextInt (locations .size ());
97+ 		envState .setAgentLocation (a , locations .get (idx ));
98+ 		super .addAgent (a );
99+ 	}
81100
101+ 	public  void  addAgent (Agent  a , String  location ) {
102+ 		// Ensure the agent state information is tracked before 
103+ 		// adding to super, as super will notify the registered 
104+ 		// EnvironmentViews that is was added. 
105+ 		envState .setAgentLocation (a , location );
106+ 		super .addAgent (a );
107+ 	}
108+ 
109+ 	@ Override 
110+ 	public  Percept  getPerceptSeenBy (Agent  anAgent ) {
111+ 		if  (anAgent  instanceof  NondeterministicSearchAgent ) {
112+ 			// This agent expects a fully observable environment. It gets a clone of the environment state. 
113+ 			return  envState .clone ();
114+ 		}
115+ 		// Other agents get a local percept. 
116+ 		String  agentLocation  = envState .getAgentLocation (anAgent );
117+ 		return  new  LocalVacuumEnvironmentPercept (agentLocation , envState .getLocationState (agentLocation ));
118+ 	}
119+ 
120+ 	@ Override 
121+ 	public  void  executeAction (Agent  a , Action  agentAction ) {
82122		if  (ACTION_MOVE_RIGHT  == agentAction ) {
83- 			envState .setAgentLocation (a , LOCATION_B );
123+ 			int  pos  = locations .indexOf (getAgentLocation (a ));
124+ 			if  (pos  < locations .size ()-1 )
125+ 				envState .setAgentLocation (a , locations .get (pos  + 1 ));
84126			updatePerformanceMeasure (a , -1 );
85127		} else  if  (ACTION_MOVE_LEFT  == agentAction ) {
86- 			envState .setAgentLocation (a , LOCATION_A );
128+ 			int  pos  = locations .indexOf (getAgentLocation (a ));
129+ 			if  (pos  > 0 )
130+ 				envState .setAgentLocation (a , locations .get (pos  - 1 ));
87131			updatePerformanceMeasure (a , -1 );
88132		} else  if  (ACTION_SUCK  == agentAction ) {
89133			if  (LocationState .Dirty  == envState .getLocationState (envState 
@@ -99,42 +143,34 @@ public void executeAction(Agent a, Action agentAction) {
99143		}
100144	}
101145
102- 	@ Override 
103- 	public  Percept  getPerceptSeenBy (Agent  anAgent ) {
104- 		if  (anAgent  instanceof  NondeterministicSearchAgent ) {
105-     		// This agent expects a fully observable environment. It gets a clone of the environment state. 
106-     		return  envState .clone ();
107-     	}
108-     	// Other agents get a local percept. 
109- 		String  agentLocation  = envState .getAgentLocation (anAgent );
110- 		return  new  LocalVacuumEnvironmentPercept (agentLocation , envState .getLocationState (agentLocation ));
111- 	}
112- 
113146	@ Override 
114147	public  boolean  isDone () {
115148		return  super .isDone () || isDone ;
116149	}
117150
118- 	 @ Override 
119- 	public   void   addAgent ( Agent   a ) { 
120- 		 int   idx  =  new   Random (). nextInt ( 2 ); 
121- 		 envState . setAgentLocation ( a ,  idx  ==  0  ?  LOCATION_A  :  LOCATION_B ); 
122- 		super . addAgent ( a );
151+ 
152+ 	// Information for grid views... 
153+ 
154+ 	public   int   getXDimension () { 
155+ 		return   locations . size ( );
123156	}
124157
125- 	public  void  addAgent (Agent  a , String  location ) {
126- 		// Ensure the agent state information is tracked before 
127- 		// adding to super, as super will notify the registered 
128- 		// EnvironmentViews that is was added. 
129- 		envState .setAgentLocation (a , location );
130- 		super .addAgent (a );
158+ 	public  int  getYDimension () {
159+ 		return  1 ;
131160	}
132161
133- 	public  LocationState  getLocationState (String  location ) {
134- 		return  envState .getLocationState (location );
162+ 	// 1 means left 
163+ 	public  int  getX (String  location ) {
164+ 		return  getLocations ().indexOf (location ) % getXDimension () + 1 ;
135165	}
136166
137- 	public  String  getAgentLocation (Agent  a ) {
138- 		return  envState .getAgentLocation (a );
167+ 	// 1 means bottom 
168+ 	public  int  getY (String  location ) {
169+ 		return  getYDimension () - getLocations ().indexOf (location ) / getXDimension ();
170+ 	}
171+ 
172+ 	// (1, 1) is bottom left 
173+ 	public  String  getLocation (int  x , int  y ) {
174+ 		return  locations .get ((getYDimension () - y ) * getXDimension () + x  - 1 );
139175	}
140176}
0 commit comments