Existence031.java

Package: existence/
File: Existence031.java

  1. package existence;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6.  
  7. import agent.Anticipation;
  8. import agent.Anticipation031;
  9. import coupling.Experiment;
  10. import coupling.Result;
  11. import coupling.interaction.Interaction;
  12. import coupling.interaction.Interaction030;
  13. import coupling.interaction.Interaction031;
  14. import existence.Existence010.Mood;
  15.  
  16. /**
  17.  * Existence031 can adapt to Environment010 020 030 031.
  18.  * Like Existence030, Existence031 seeks to enact interactions that have positive valence.
  19.  * Existence031 illustrates the benefit of reinforcing the weight of composite interactions
  20.  * and of using the weight of activated interactions to balance the decision.
  21.  */
  22. public class Existence031 extends Existence030 {
  23.  
  24. @Override
  25. public String step() {
  26.  
  27. List<Anticipation> anticipations = anticipate();
  28. Experiment experience = selectExperience(anticipations);
  29.  
  30. /** Change the call to the function returnResult to change the environment */
  31. //Result result = returnResult010(experience);
  32. Result result = returnResult030(experience);
  33. //Result result = returnResult031(experience);
  34.  
  35. Interaction031 enactedInteraction = getInteraction(experience.getLabel() + result.getLabel());
  36. System.out.println("Enacted "+ enactedInteraction.toString());
  37.  
  38. if (enactedInteraction.getValence() >= 0)
  39. this.setMood(Mood.PLEASED);
  40. else
  41. this.setMood(Mood.PAINED);
  42.  
  43. this.learnCompositeInteraction(enactedInteraction);
  44.  
  45. this.setEnactedInteraction(enactedInteraction);
  46.  
  47. return "" + this.getMood();
  48. }
  49.  
  50. /**
  51. * Record the composite interaction from the context interaction and the enacted interaction.
  52. * Increment its weight.
  53. */
  54. @Override
  55. public void learnCompositeInteraction(Interaction030 enactedInteraction){
  56. Interaction030 preInteraction = this.getEnactedInteraction();
  57. Interaction030 postInteraction = enactedInteraction;
  58. if (preInteraction != null){
  59. Interaction031 interaction = (Interaction031)addOrGetCompositeInteraction(preInteraction, postInteraction);
  60. interaction.incrementWeight();
  61. }
  62. }
  63.  
  64. @Override
  65. protected Interaction031 createInteraction(String label){
  66. return new Interaction031(label);
  67. }
  68.  
  69. /**
  70. * Computes the list of anticipations
  71. * @return the list of anticipations
  72. */
  73. @Override
  74. public List<Anticipation> anticipate(){
  75. List<Anticipation> anticipations = this.getDefaultAnticipations();
  76.  
  77. if (this.getEnactedInteraction() != null){
  78. for (Interaction activatedInteraction : getActivatedInteractions()){
  79. Anticipation031 proposition = new Anticipation031(((Interaction031)activatedInteraction).getPostInteraction().getExperience(), ((Interaction031)activatedInteraction).getWeight() * ((Interaction031)activatedInteraction).getPostInteraction().getValence());
  80. int index = anticipations.indexOf(proposition);
  81. if (index < 0)
  82. anticipations.add(proposition);
  83. else
  84. ((Anticipation031)anticipations.get(index)).addProclivity(((Interaction031)activatedInteraction).getWeight() * ((Interaction031)activatedInteraction).getPostInteraction().getValence());
  85. }
  86. }
  87. return anticipations;
  88. }
  89.  
  90. /**
  91. * all experiences as proposed by default with a proclivity of 0
  92. * @return the list of anticipations
  93. */
  94. protected List<Anticipation> getDefaultAnticipations(){
  95. List<Anticipation> anticipations = new ArrayList<Anticipation>();
  96. for (Experiment experience : this.EXPERIENCES.values()){
  97. Anticipation031 anticipation = new Anticipation031(experience, 0);
  98. anticipations.add(anticipation);
  99. }
  100. return anticipations;
  101. }
  102.  
  103. public Experiment selectExperience(List<Anticipation> anticipations){
  104. // The list of anticipations is never empty because all the experiences are proposed by default with a proclivity of 0
  105. Collections.sort(anticipations);
  106. for (Anticipation anticipation : anticipations)
  107. System.out.println("propose " + anticipation.toString());
  108.  
  109. Anticipation031 selectedAnticipation = (Anticipation031)anticipations.get(0);
  110. return selectedAnticipation.getExperience();
  111. }
  112.  
  113. @Override
  114. protected Interaction031 getInteraction(String label){
  115. return (Interaction031)INTERACTIONS.get(label);
  116. }
  117.  
  118. @Override
  119. public Interaction031 getEnactedInteraction(){
  120. return (Interaction031)super.getEnactedInteraction();
  121. }
  122.  
  123. /**
  124. * Environment031
  125. * Before time T1 and after time T2: E1 results in R1; E2 results in R2
  126. * between time T1 and time T2: E1 results R2; E2results in R1.
  127. */
  128. protected final int T1 = 8;
  129. protected final int T2 = 15;
  130. private int clock = 0;
  131. protected int getClock(){
  132. return this.clock;
  133. }
  134. protected void incClock(){
  135. this.clock++;
  136. }
  137.  
  138. public Result returnResult031(Experiment experience){
  139.  
  140. Result result = null;
  141.  
  142. this.incClock();
  143.  
  144. if (this.getClock() <= this.T1 || this.getClock() > this.T2){
  145. if (experience.equals(this.addOrGetExperience(this.LABEL_E1)))
  146. result = this.createOrGetResult(this.LABEL_R1);
  147. else
  148. result = this.createOrGetResult(this.LABEL_R2);
  149. }
  150. else {
  151. if (experience.equals(this.addOrGetExperience(this.LABEL_E1)))
  152. result = this.createOrGetResult(this.LABEL_R2);
  153. else
  154. result = this.createOrGetResult(this.LABEL_R1);
  155. }
  156. return result;
  157. }
  158. }
  159.  

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