Existence030.java

Package: existence/
File: Existence030.java

  1. package existence;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import agent.Anticipation;
  7. import agent.Anticipation030;
  8. import coupling.Experiment;
  9. import coupling.Result;
  10. import coupling.interaction.Interaction;
  11. import coupling.interaction.Interaction030;
  12.  
  13. /**
  14.  * Existence030 is a sort of Existence020.
  15.  * It learns composite interactions (Interaction030).
  16.  * It bases its next choice on anticipations that can be made from reactivated composite interactions.
  17.  * Existence030 illustrates the benefit of basing the next decision upon the previous enacted Interaction.
  18.  */
  19. public class Existence030 extends Existence020 {
  20.  
  21. private Interaction030 enactedInteraction;
  22.  
  23. @Override
  24. protected void initExistence(){
  25. Experiment e1 = addOrGetExperience(LABEL_E1);
  26. Experiment e2 = addOrGetExperience(LABEL_E2);
  27. Result r1 = createOrGetResult(LABEL_R1);
  28. Result r2 = createOrGetResult(LABEL_R2);
  29. addOrGetPrimitiveInteraction(e1, r1, -1);
  30. addOrGetPrimitiveInteraction(e1, r2, 1);
  31. addOrGetPrimitiveInteraction(e2, r1, -1);
  32. addOrGetPrimitiveInteraction(e2, r2, 1);
  33. }
  34.  
  35. @Override
  36. public String step() {
  37.  
  38. List<Anticipation> anticipations = anticipate();
  39. Experiment experience = selectInteraction(anticipations).getExperience();
  40.  
  41. /** Change the call to the function returnResult to change the environment */
  42. //Result result = returnResult010(experience);
  43. Result result = returnResult030(experience);
  44.  
  45. Interaction030 enactedInteraction = getInteraction(experience.getLabel() + result.getLabel());
  46. System.out.println("Enacted "+ enactedInteraction.toString());
  47.  
  48. if (enactedInteraction.getValence() >= 0)
  49. this.setMood(Mood.PLEASED);
  50. else
  51. this.setMood(Mood.PAINED);
  52.  
  53. this.learnCompositeInteraction(enactedInteraction);
  54.  
  55. this.setEnactedInteraction(enactedInteraction);
  56.  
  57. return "" + this.getMood();
  58. }
  59.  
  60. /**
  61. * Learn the composite interaction from the previous enacted interaction and the current enacted interaction
  62. */
  63. public void learnCompositeInteraction(Interaction030 interaction){
  64. Interaction030 preInteraction = this.getEnactedInteraction();
  65. Interaction030 postInteraction = interaction;
  66. if (preInteraction != null)
  67. addOrGetCompositeInteraction(preInteraction, postInteraction);
  68. }
  69.  
  70. /**
  71. * Records a composite interaction in memory
  72. * @param preInteraction: The composite interaction's pre-interaction
  73. * @param postInteraction: The composite interaction's post-interaction
  74. * @return the learned composite interaction
  75. */
  76. public Interaction030 addOrGetCompositeInteraction(
  77. Interaction030 preInteraction, Interaction030 postInteraction) {
  78. int valence = preInteraction.getValence() + postInteraction.getValence();
  79. Interaction030 interaction = (Interaction030)addOrGetInteraction(preInteraction.getLabel() + postInteraction.getLabel());
  80. interaction.setPreInteraction(preInteraction);
  81. interaction.setPostInteraction(postInteraction);
  82. interaction.setValence(valence);
  83. System.out.println("learn " + interaction.getLabel());
  84. return interaction;
  85. }
  86.  
  87. @Override
  88. protected Interaction030 createInteraction(String label){
  89. return new Interaction030(label);
  90. }
  91.  
  92. /**
  93. * Computes the list of anticipations
  94. * @return the list of anticipations
  95. */
  96. public List<Anticipation> anticipate(){
  97. List<Anticipation> anticipations = new ArrayList<Anticipation>();
  98. if (this.getEnactedInteraction() != null){
  99. for (Interaction activatedInteraction : this.getActivatedInteractions()){
  100. Interaction030 proposedInteraction = ((Interaction030)activatedInteraction).getPostInteraction();
  101. anticipations.add(new Anticipation030(proposedInteraction));
  102. System.out.println("afforded " + proposedInteraction.toString());
  103. }
  104. }
  105. return anticipations;
  106. }
  107.  
  108. protected Interaction030 selectInteraction(List<Anticipation> anticipations){
  109. Collections.sort(anticipations);
  110. Interaction intendedInteraction;
  111. if (anticipations.size() > 0){
  112. Interaction030 affordedInteraction = ((Anticipation030)anticipations.get(0)).getInteraction();
  113. if (affordedInteraction.getValence() >= 0)
  114. intendedInteraction = affordedInteraction;
  115. else
  116. intendedInteraction = (Interaction030)this.getOtherInteraction(affordedInteraction);
  117. }
  118. else
  119. intendedInteraction = this.getOtherInteraction(null);
  120. return (Interaction030)intendedInteraction;
  121. }
  122.  
  123. /**
  124. * Get the list of activated interactions
  125. * An activated interaction is a composite interaction whose preInteraction matches the enactedInteraction
  126. * @return the list of anticipations
  127. */
  128. public List<Interaction> getActivatedInteractions() {
  129. List<Interaction> activatedInteractions = new ArrayList<Interaction>();
  130. if (this.getEnactedInteraction() != null)
  131. for (Interaction activatedInteraction : this.INTERACTIONS.values())
  132. if (((Interaction030)activatedInteraction).getPreInteraction() == this.getEnactedInteraction())
  133. activatedInteractions.add((Interaction030)activatedInteraction);
  134. return activatedInteractions;
  135. }
  136.  
  137. @Override
  138. protected Interaction030 getInteraction(String label){
  139. return (Interaction030)INTERACTIONS.get(label);
  140. }
  141.  
  142. public Interaction getOtherInteraction(Interaction interaction) {
  143. Interaction otherInteraction = (Interaction)INTERACTIONS.values().toArray()[0];
  144. if (interaction != null)
  145. for (Interaction e : INTERACTIONS.values()){
  146. if (e.getExperience() != null && e.getExperience()!=interaction.getExperience()){
  147. otherInteraction = e;
  148. break;
  149. }
  150. }
  151. return otherInteraction;
  152. }
  153.  
  154. protected void setEnactedInteraction(Interaction030 enactedInteraction){
  155. this.enactedInteraction = enactedInteraction;
  156. }
  157. protected Interaction030 getEnactedInteraction(){
  158. return this.enactedInteraction;
  159. }
  160.  
  161. /**
  162. * Environment030
  163. * Results in R1 when the current experience equals the previous experience
  164. * and in R2 when the current experience differs from the previous experience.
  165. */
  166. protected Result returnResult030(Experiment experience){
  167. Result result = null;
  168. if (this.getPreviousExperience() == experience)
  169. result = this.createOrGetResult(this.LABEL_R1);
  170. else
  171. result = this.createOrGetResult(this.LABEL_R2);
  172. this.setPreviousExperience(experience);
  173.  
  174. return result;
  175. }
  176. }
  177.  

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