Existence050.java

Package: existence/
File: Existence050.java

  1. package existence;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import agent.Anticipation;
  7. import agent.Anticipation031;
  8. import coupling.Experiment;
  9. import coupling.Experiment040;
  10. import coupling.Experiment050;
  11. import coupling.interaction.Interaction;
  12. import coupling.interaction.Interaction030;
  13. import coupling.interaction.Interaction031;
  14. import coupling.interaction.Interaction032;
  15. import coupling.interaction.Interaction040;
  16. import environment.Environment;
  17. import environment.Environment050;
  18. import environment.EnvironmentMaze;
  19.  
  20. public class Existence050 extends Existence040{
  21.  
  22. private Environment environment;
  23. protected Environment getEnvironment(){
  24. return this.environment;
  25. }
  26.  
  27. @Override
  28. protected void initExistence(){
  29. /** You can instantiate another environment here. */
  30. this.environment = new Environment050(this);
  31. //this.environment = new EnvironmentMaze(this);
  32. }
  33.  
  34. @Override
  35. public String step() {
  36.  
  37. List<Anticipation> anticipations = anticipate();
  38. Experiment050 experience = (Experiment050)selectExperience(anticipations);
  39.  
  40. Interaction040 intendedInteraction = experience.getIntendedInteraction();
  41. System.out.println("Intended "+ intendedInteraction.toString());
  42.  
  43. Interaction040 enactedInteraction = enact(intendedInteraction);
  44.  
  45. if (enactedInteraction != intendedInteraction)
  46. experience.addEnactedInteraction(enactedInteraction);
  47.  
  48. System.out.println("Enacted "+ enactedInteraction.toString());
  49.  
  50. if (enactedInteraction.getValence() >= 0)
  51. this.setMood(Mood.PLEASED);
  52. else
  53. this.setMood(Mood.PAINED);
  54.  
  55. this.learnCompositeInteraction(enactedInteraction);
  56.  
  57. this.setPreviousSuperInteraction(this.getLastSuperInteraction());
  58. this.setEnactedInteraction(enactedInteraction);
  59.  
  60. return "" + this.getMood();
  61. }
  62.  
  63. /**
  64. * Computes the list of anticipations
  65. * @return the list of anticipations
  66. */
  67. @Override
  68. public List<Anticipation> anticipate(){
  69.  
  70. List<Anticipation> anticipations = getDefaultAnticipations();
  71. List<Interaction> activatedInteractions = this.getActivatedInteractions();
  72.  
  73. if (this.getEnactedInteraction() != null){
  74. for (Interaction activatedInteraction : activatedInteractions){
  75. if (((Interaction031)activatedInteraction).getPostInteraction().getExperience() != null){
  76. Anticipation031 anticipation = new Anticipation031(((Interaction031)activatedInteraction).getPostInteraction().getExperience(), ((Interaction031)activatedInteraction).getWeight() * ((Interaction031)activatedInteraction).getPostInteraction().getValence());
  77. int index = anticipations.indexOf(anticipation);
  78. if (index < 0)
  79. anticipations.add(anticipation);
  80. else
  81. ((Anticipation031)anticipations.get(index)).addProclivity(((Interaction031)activatedInteraction).getWeight() * ((Interaction031)activatedInteraction).getPostInteraction().getValence());
  82. }
  83. }
  84. }
  85.  
  86. for (Anticipation anticipation : anticipations){
  87. for (Interaction interaction : ((Experiment050)((Anticipation031)anticipation).getExperience()).getEnactedInteractions()){
  88. for (Interaction activatedInteraction : activatedInteractions){
  89. if (interaction == ((Interaction032)activatedInteraction).getPostInteraction()){
  90. int proclivity = ((Interaction032)activatedInteraction).getWeight() * ((Interaction032)interaction).getValence();
  91. ((Anticipation031)anticipation).addProclivity(proclivity);
  92. }
  93. }
  94. }
  95. }
  96.  
  97. return anticipations;
  98. }
  99.  
  100. @Override
  101. public Experiment050 addOrGetAbstractExperience(Interaction040 interaction) {
  102. String label = interaction.getLabel().replace('e', 'E').replace('r', 'R').replace('>', '|');
  103. if (!EXPERIENCES.containsKey(label)){
  104. Experiment050 abstractExperience = new Experiment050(label);
  105. abstractExperience.setIntendedInteraction(interaction);
  106. interaction.setExperience(abstractExperience);
  107. EXPERIENCES.put(label, abstractExperience);
  108. }
  109. return (Experiment050)EXPERIENCES.get(label);
  110. }
  111.  
  112. /**
  113. * Create an interaction from its label.
  114. * @param label: This interaction's label.
  115. * @param valence: the interaction's valence.
  116. * @return The created interaction
  117. */
  118. public Interaction040 addOrGetPrimitiveInteraction(String label, int valence) {
  119. if (!INTERACTIONS.containsKey(label)){
  120. Interaction040 interaction = createInteraction(label);
  121. interaction.setValence(valence);
  122. INTERACTIONS.put(label, interaction);
  123. }
  124. Interaction040 interaction = (Interaction040)INTERACTIONS.get(label);
  125. return interaction;
  126. }
  127.  
  128. @Override
  129. protected List<Anticipation> getDefaultAnticipations(){
  130. List<Anticipation> anticipations = new ArrayList<Anticipation>();
  131. for (Experiment experience : this.EXPERIENCES.values()){
  132. Experiment040 defaultExperience = (Experiment040)experience;
  133. if (defaultExperience.getIntendedInteraction().isPrimitive()){
  134. Anticipation031 anticipation = new Anticipation031(experience, 0);
  135. anticipations.add(anticipation);
  136. }
  137. }
  138. return anticipations;
  139. }
  140.  
  141. @Override
  142. public Interaction040 enact(Interaction030 intendedInteraction){
  143.  
  144. if (intendedInteraction.isPrimitive())
  145. return (Interaction040)this.getEnvironment().enact(intendedInteraction);
  146. else {
  147. // Enact the pre-interaction
  148. Interaction040 enactedPreInteraction = enact(intendedInteraction.getPreInteraction());
  149. if (!enactedPreInteraction.equals(intendedInteraction.getPreInteraction()))
  150. // if the preInteraction failed then the enaction of the intendedInteraction is interrupted here.
  151. return enactedPreInteraction;
  152. else{
  153. // Enact the post-interaction
  154. Interaction040 enactedPostInteraction = enact(intendedInteraction.getPostInteraction());
  155. return (Interaction040)addOrGetCompositeInteraction(enactedPreInteraction, enactedPostInteraction);
  156. }
  157. }
  158. }
  159.  
  160. }
  161.  

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