Existence010.java

Package: existence/
File: Existence010.java

  1. package existence;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import coupling.Experiment;
  6. import coupling.Result;
  7. import coupling.interaction.Interaction;
  8. import coupling.interaction.Interaction010;
  9.  
  10. /**
  11.  * An Existence010 simulates a "stream of intelligence" made of a succession of Experiences and Results.
  12.  * The Existence010 is SELF-SATISFIED when the Result corresponds to the Result it expected, and FRUSTRATED otherwise.
  13.  * Additionally, the Existence0 is BORED when it has been SELF-SATISFIED for too long, which causes it to try another Experience.
  14.  * An Existence1 is still a single entity rather than being split into an explicit Agent and Environment.
  15.  */
  16. public class Existence010 implements Existence {
  17.  
  18. public final String LABEL_E1 = "e1";
  19. public final String LABEL_E2 = "e2";
  20. public final String LABEL_R1 = "r1";
  21. public final String LABEL_R2 = "r2";
  22. public enum Mood {SELF_SATISFIED, FRUSTRATED, BORED, PAINED, PLEASED};
  23.  
  24. protected Map<String ,Experiment> EXPERIENCES = new HashMap<String ,Experiment>();
  25. protected Map<String ,Result> RESULTS = new HashMap<String ,Result>();
  26. protected Map<String , Interaction> INTERACTIONS = new HashMap<String , Interaction>() ;
  27.  
  28. protected final int BOREDOME_LEVEL = 4;
  29.  
  30. private Mood mood;
  31. private int selfSatisfactionCounter = 0;
  32. private Experiment previousExperience;
  33.  
  34. public Existence010(){
  35. initExistence();
  36. }
  37.  
  38. protected void initExistence(){
  39. Experiment e1 = addOrGetExperience(LABEL_E1);
  40. addOrGetExperience(LABEL_E2);
  41. this.setPreviousExperience(e1);
  42. }
  43.  
  44. @Override
  45. public String step() {
  46.  
  47. Experiment experience = this.getPreviousExperience();
  48. if (this.getMood() == Mood.BORED){
  49. experience = getOtherExperience(experience);
  50. this.setSelfSatisfactionCounter(0);
  51. }
  52.  
  53. Result anticipatedResult = predict(experience);
  54.  
  55. Result result = returnResult010(experience);
  56.  
  57. this.addOrGetPrimitiveInteraction(experience, result);
  58.  
  59. if (result == anticipatedResult){
  60. this.setMood(Mood.SELF_SATISFIED);
  61. this.incSelfSatisfactionCounter();
  62. }
  63. else{
  64. this.setMood(Mood.FRUSTRATED);
  65. this.setSelfSatisfactionCounter(0);
  66. }
  67. if (this.getSelfSatisfactionCounter() >= BOREDOME_LEVEL)
  68. this.setMood(Mood.BORED);
  69.  
  70. this.setPreviousExperience(experience);
  71.  
  72. return experience.getLabel() + result.getLabel() + " " + this.getMood();
  73. }
  74.  
  75. /**
  76. * Create an interaction as a tuple <experience, result>.
  77. * @param experience: The experience.
  78. * @param result: The result.
  79. * @return The created interaction
  80. */
  81. protected Interaction addOrGetPrimitiveInteraction(Experiment experience, Result result) {
  82. Interaction interaction = addOrGetInteraction(experience.getLabel() + result.getLabel());
  83. interaction.setExperience(experience);
  84. interaction.setResult(result);
  85. return interaction;
  86. }
  87.  
  88. /**
  89. * Records an interaction in memory.
  90. * @param label: The label of this interaction.
  91. * @return The interaction.
  92. */
  93. protected Interaction addOrGetInteraction(String label) {
  94. if (!INTERACTIONS.containsKey(label))
  95. INTERACTIONS.put(label, createInteraction(label));
  96. return INTERACTIONS.get(label);
  97. }
  98.  
  99. protected Interaction010 createInteraction(String label){
  100. return new Interaction010(label);
  101. }
  102.  
  103. /**
  104. * Finds an interaction from its label
  105. * @param label: The label of this interaction.
  106. * @return The interaction.
  107. */
  108. protected Interaction getInteraction(String label){
  109. return INTERACTIONS.get(label);
  110. }
  111.  
  112. /**
  113. * Finds an interaction from its experience
  114. * @return The interaction.
  115. */
  116. protected Result predict(Experiment experience){
  117. Interaction interaction = null;
  118. Result anticipatedResult = null;
  119.  
  120. for (Interaction i : INTERACTIONS.values())
  121. if (i.getExperience().equals(experience))
  122. interaction = i;
  123.  
  124. if (interaction != null)
  125. anticipatedResult = interaction.getResult();
  126.  
  127. return anticipatedResult;
  128. }
  129.  
  130. /**
  131. * Creates a new experience from its label and stores it in memory.
  132. * @param label: The experience's label
  133. * @return The experience.
  134. */
  135. protected Experiment addOrGetExperience(String label) {
  136. if (!EXPERIENCES.containsKey(label))
  137. EXPERIENCES.put(label, createExperience(label));
  138. return EXPERIENCES.get(label);
  139. }
  140.  
  141. protected Experiment createExperience(String label){
  142. return new Experiment(label);
  143. }
  144.  
  145. /**
  146. * Finds an experience different from that passed in parameter.
  147. * @param experience: The experience that we don't want
  148. * @return The other experience.
  149. */
  150. protected Experiment getOtherExperience(Experiment experience) {
  151. Experiment otherExperience = null;
  152. for (Experiment e : EXPERIENCES.values()){
  153. if (e!=experience){
  154. otherExperience = e;
  155. break;
  156. }
  157. }
  158. return otherExperience;
  159. }
  160.  
  161. /**
  162. * Creates a new result from its label and stores it in memory.
  163. * @param label: The result's label
  164. * @return The result.
  165. */
  166. protected Result createOrGetResult(String label) {
  167. if (!RESULTS.containsKey(label))
  168. RESULTS.put(label, new Result(label));
  169. return RESULTS.get(label);
  170. }
  171.  
  172. public Mood getMood() {
  173. return mood;
  174. }
  175. public void setMood(Mood mood) {
  176. this.mood = mood;
  177. }
  178.  
  179. public Experiment getPreviousExperience() {
  180. return previousExperience;
  181. }
  182. public void setPreviousExperience(Experiment previousExperience) {
  183. this.previousExperience = previousExperience;
  184. }
  185.  
  186. public int getSelfSatisfactionCounter() {
  187. return this.selfSatisfactionCounter;
  188. }
  189. public void setSelfSatisfactionCounter(int selfSatisfactionCounter) {
  190. this.selfSatisfactionCounter = selfSatisfactionCounter;
  191. }
  192. public void incSelfSatisfactionCounter(){
  193. this.selfSatisfactionCounter++;
  194. }
  195.  
  196. /**
  197. * The Environment010
  198. * E1 results in R1. E2 results in R2.
  199. * @param experience: The current experience.
  200. * @return The result of this experience.
  201. */
  202. public Result returnResult010(Experiment experience){
  203. if (experience.equals(addOrGetExperience(LABEL_E1)))
  204. return createOrGetResult(LABEL_R1);
  205. else
  206. return createOrGetResult(LABEL_R2);
  207. }
  208.  
  209. }
  210.  

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