EnvironmentMaze.java

Package: environment/
File: EnvironmentMaze.java

  1. package environment;
  2.  
  3. import coupling.interaction.Interaction;
  4. import coupling.interaction.Interaction040;
  5. import existence.Existence0501;
  6. import existence.Existence050;
  7.  
  8. /**
  9.  * This class implements the Small Loop Environment
  10.  *
  11.  * The Small Loop Problem: A challenge for artificial emergent cognition.
  12.  * Olivier L. Georgeon, James B. Marshall.
  13.  * BICA2012, Annual Conference on Biologically Inspired Cognitive Architectures.
  14.  * Palermo, Italy. (October 31, 2012).
  15.  * http://e-ernest.blogspot.fr/2012/05/challenge-emergent-cognition.html
  16.  */
  17. public class EnvironmentMaze extends Environment050
  18. {
  19. private static final int ORIENTATION_UP = 0;
  20. private static final int ORIENTATION_RIGHT = 1;
  21. private static final int ORIENTATION_DOWN = 2;
  22. private static final int ORIENTATION_LEFT = 3;
  23.  
  24. // The Small Loop Environment
  25.  
  26. private static final int WIDTH = 6;
  27. private static final int HEIGHT = 6;
  28. private int m_x = 4;
  29. private int m_y = 1;
  30. private int m_o = 2;
  31.  
  32. private char[][] m_board =
  33. {
  34. {'x', 'x', 'x', 'x', 'x', 'x'},
  35. {'x', ' ', ' ', ' ', ' ', 'x'},
  36. {'x', ' ', 'x', 'x', ' ', 'x'},
  37. {'x', ' ', ' ', 'x', ' ', 'x'},
  38. {'x', 'x', ' ', ' ', ' ', 'x'},
  39. {'x', 'x', 'x', 'x', 'x', 'x'},
  40. };
  41.  
  42. private char[] m_agent =
  43. { '^', '>', 'v', '<' };
  44.  
  45. public EnvironmentMaze(Existence050 existence){
  46. super(existence);
  47. }
  48.  
  49. @Override
  50. protected void init(){
  51.  
  52. //Settings for a nice demo in the Simple Maze
  53. Interaction040 turnLeft = this.getExistence().addOrGetPrimitiveInteraction("^t", -3); // Left toward empty
  54. Interaction040 turnRight = this.getExistence().addOrGetPrimitiveInteraction("vt", -3); // Right toward empty
  55. Interaction040 touchRight = this.getExistence().addOrGetPrimitiveInteraction("\\t", -1); // Touch right wall
  56. this.getExistence().addOrGetPrimitiveInteraction("\\f", -1); // Touch right empty
  57. Interaction040 touchLeft = this.getExistence().addOrGetPrimitiveInteraction("/t", -1); // Touch left wall
  58. this.getExistence().addOrGetPrimitiveInteraction("/f", -1); // Touch left empty
  59. Interaction040 froward = this.getExistence().addOrGetPrimitiveInteraction(">t", 5); // Move
  60. this.getExistence().addOrGetPrimitiveInteraction(">f", -10); // Bump
  61. Interaction040 touchForward = this.getExistence().addOrGetPrimitiveInteraction("-t", -1); // Touch wall
  62. this.getExistence().addOrGetPrimitiveInteraction("-f", -1); // Touch empty
  63. this.getExistence().addOrGetAbstractExperience(turnLeft);
  64. this.getExistence().addOrGetAbstractExperience(turnRight);
  65. this.getExistence().addOrGetAbstractExperience(touchRight);
  66. this.getExistence().addOrGetAbstractExperience(touchLeft);
  67. this.getExistence().addOrGetAbstractExperience(froward);
  68. this.getExistence().addOrGetAbstractExperience(touchForward);
  69. }
  70.  
  71. public Interaction enact(Interaction intendedInteraction)
  72. {
  73. Interaction040 enactedInteraction = null;
  74.  
  75. if (intendedInteraction.getLabel().substring(0,1).equals(">"))
  76. enactedInteraction = move();
  77. else if (intendedInteraction.getLabel().substring(0,1).equals("^"))
  78. enactedInteraction = left();
  79. else if (intendedInteraction.getLabel().substring(0,1).equals("v"))
  80. enactedInteraction = right();
  81. else if (intendedInteraction.getLabel().substring(0,1).equals("-"))
  82. enactedInteraction = Touch();
  83. else if (intendedInteraction.getLabel().substring(0,1).equals("\\"))
  84. enactedInteraction = TouchRight();
  85. else if (intendedInteraction.getLabel().substring(0,1).equals("/"))
  86. enactedInteraction = TouchLeft();
  87.  
  88. // print the maze
  89. for (int i = 0; i < HEIGHT; i++)
  90. {
  91. for (int j = 0; j < WIDTH; j++)
  92. {
  93. if (i == m_y && j== m_x)
  94. System.out.print(m_agent[m_o]);
  95. else
  96. System.out.print(m_board[i][j]);
  97. }
  98. System.out.println();
  99. }
  100.  
  101. return enactedInteraction;
  102. }
  103.  
  104. /**
  105. * Turn to the right.
  106. */
  107. private Interaction040 right()
  108. {
  109. m_o++;
  110. if (m_o > ORIENTATION_LEFT)
  111. m_o = ORIENTATION_UP;
  112.  
  113. return this.getExistence().addOrGetPrimitiveInteraction("vt",0);
  114. }
  115.  
  116. /**
  117. * Turn to the left.
  118. */
  119. private Interaction040 left()
  120. {
  121. m_o--;
  122. if (m_o < 0)
  123. m_o = ORIENTATION_LEFT;
  124.  
  125. return this.getExistence().addOrGetPrimitiveInteraction("^t",0);
  126. }
  127.  
  128. /**
  129. * Move forward to the direction of the current orientation.
  130. */
  131. private Interaction040 move()
  132. {
  133. Interaction040 enactedInteraction = this.getExistence().addOrGetPrimitiveInteraction(">f",0);
  134.  
  135. if ((m_o == ORIENTATION_UP) && (m_y > 0) && (m_board[m_y - 1][m_x] == ' ' )){
  136. m_y--;
  137. enactedInteraction = this.getExistence().addOrGetPrimitiveInteraction(">t",0);
  138. }
  139.  
  140. if ((m_o == ORIENTATION_DOWN) && (m_y < HEIGHT) && (m_board[m_y + 1][m_x] == ' ' )){
  141. m_y++;
  142. enactedInteraction = this.getExistence().addOrGetPrimitiveInteraction(">t",0);
  143. }
  144.  
  145. if ((m_o == ORIENTATION_RIGHT) && ( m_x < WIDTH ) && (m_board[m_y][m_x + 1] == ' ' )){
  146. m_x++;
  147. enactedInteraction = this.getExistence().addOrGetPrimitiveInteraction(">t",0);
  148. }
  149. if ((m_o == ORIENTATION_LEFT) && ( m_x > 0 ) && (m_board[m_y][m_x - 1] == ' ' )){
  150. m_x--;
  151. enactedInteraction = this.getExistence().addOrGetPrimitiveInteraction(">t",0);
  152. }
  153.  
  154. return enactedInteraction;
  155. }
  156.  
  157. /**
  158. * Touch the square forward.
  159. * Succeeds if there is a wall, fails otherwise
  160. */
  161. private Interaction040 Touch()
  162. {
  163. Interaction040 enactedInteraction = this.getExistence().addOrGetPrimitiveInteraction("-t",0);
  164.  
  165. if (((m_o == ORIENTATION_UP) && (m_y > 0) && (m_board[m_y - 1][m_x] == ' ')) ||
  166. ((m_o == ORIENTATION_DOWN) && (m_y < HEIGHT) && (m_board[m_y + 1][m_x] == ' ')) ||
  167. ((m_o == ORIENTATION_RIGHT) && (m_x < WIDTH) && (m_board[m_y][m_x + 1] == ' ')) ||
  168. ((m_o == ORIENTATION_LEFT) && (m_x > 0) && (m_board[m_y][m_x - 1] == ' ')))
  169. {
  170. enactedInteraction = this.getExistence().addOrGetPrimitiveInteraction("-f",0); }
  171.  
  172. return enactedInteraction;
  173. }
  174.  
  175. /**
  176. * Touch the square to the right.
  177. * Succeeds if there is a wall, fails otherwise.
  178. */
  179. private Interaction040 TouchRight()
  180. {
  181. Interaction040 enactedInteraction = this.getExistence().addOrGetPrimitiveInteraction("\\t",0);
  182.  
  183. if (((m_o == ORIENTATION_UP) && (m_x > 0) && (m_board[m_y][m_x + 1] == ' ')) ||
  184. ((m_o == ORIENTATION_DOWN) && (m_x < WIDTH) && (m_board[m_y][m_x - 1] == ' ')) ||
  185. ((m_o == ORIENTATION_RIGHT) && (m_y < HEIGHT) && (m_board[m_y + 1][m_x] == ' ')) ||
  186. ((m_o == ORIENTATION_LEFT) && (m_y > 0) && (m_board[m_y - 1][m_x] == ' ')))
  187. {enactedInteraction = this.getExistence().addOrGetPrimitiveInteraction("\\f",0);
  188. }
  189.  
  190. return enactedInteraction;
  191. }
  192.  
  193. /**
  194. * Touch the square forward.
  195. * Succeeds if there is a wall, fails otherwise
  196. */
  197. private Interaction040 TouchLeft()
  198. {
  199. Interaction040 enactedInteraction = this.getExistence().addOrGetPrimitiveInteraction("/t",0);
  200.  
  201. if (((m_o == ORIENTATION_UP) && (m_x > 0) && (m_board[m_y][m_x - 1] == ' ')) ||
  202. ((m_o == ORIENTATION_DOWN) && (m_x < WIDTH) && (m_board[m_y][m_x + 1] == ' ')) ||
  203. ((m_o == ORIENTATION_RIGHT) && (m_y > 0) && (m_board[m_y - 1][m_x] == ' ')) ||
  204. ((m_o == ORIENTATION_LEFT) && (m_y < HEIGHT) && (m_board[m_y + 1][m_x] == ' ')))
  205. {enactedInteraction = this.getExistence().addOrGetPrimitiveInteraction("/f",0);}
  206.  
  207. return enactedInteraction;
  208. }
  209. }
  210.  

See public discussions about this page or start a new discussion by clicking on the Google+ Share button. Please type the #IDEALMOOCEnvironmentMaze hashtag in your post: