001 /*
002 * SimuCS - Simulator to use with Classifier Systems
003 * MSc project - Oxford University
004 * by Benoit Isaac - Summer 2005
005 */
006
007 package simuLCS;
008 import java.awt.Color;
009 import java.awt.Graphics2D;
010 import java.awt.geom.Point2D;
011 import java.util.Random;
012
013 /**
014 * Agent having two behaviours and switching between them every X time steps.
015 * @author Benoit
016 *
017 */
018 public class AgentClassifierAutomatic extends AgentClassifier {
019
020 /**
021 * Constant for the first behaviour
022 */
023 public static final int REAL_BEHAVIOUR_1 = 0;
024
025 /**
026 * Constant for the second behaviour
027 */
028 public static final int REAL_BEHAVIOUR_2 = 1;
029
030 /**
031 * The first behaviour
032 */
033 protected ClassifierSet c1;
034
035 /**
036 * The second behaviour
037 */
038 protected ClassifierSet c2;
039
040 /**
041 * Number of steps it has already moved (in order to switch)
042 */
043 protected int nbOfStepsMoving = 0;
044
045 /**
046 * The Agent switches behaviour every <code>intervalSwitchingBehaviours</code> time steps.
047 */
048 protected int intervalSwitchingBehaviours = 3000;
049
050 /**
051 * Maximum number of pixels the agent can move for one time step
052 */
053 protected double maxSpeed = 5;
054
055 /**
056 * Constructs Agent Ra, Robot Automatic.
057 * @param gen
058 * @param a
059 * @param name
060 */
061 public AgentClassifierAutomatic(Random gen, Arena a, String name) {
062 super(gen, a, name);
063
064 this.setColor_int(Color.ORANGE);
065 this.setColor_ext(Color.BLACK);
066
067 TemplateRSP t = new TemplateRSP();
068 t.setMIN_SCALE(0);
069 t.setSTEP_SCALE(2000);
070 this.setTemplate(t);
071
072 /* behaviour 1: going towards the ducks */
073 c1 = new ClassifierSet(t);
074 // c1a : avoiding wall
075 Classifier c1a = new Classifier("01", "1000011", "Avoiding Wall");
076 // c1b : attracted by ducks
077 Classifier c1b = new Classifier("10", "0011111", "Going towards ducks");
078
079 c1.addClassifier(c1a);
080 c1.addClassifier(c1b);
081
082 /* behaviour 2: going away from the ducks */
083 c2 = new ClassifierSet(t);
084 // c1a : avoiding wall
085 Classifier c2a = new Classifier("01", "1000010", "Avoiding Wall");
086 // c1b : repelled by ducks
087 Classifier c2b =
088 new Classifier("10", "1011111", "Going away from ducks");
089
090 c2.addClassifier(c2a);
091 c2.addClassifier(c2b);
092
093 // starting with the following behaviour
094 behaviour = c1;
095
096 }
097
098 /**
099 * Moves the agent.
100 * Moves the ghost and applies the learning system if <code>isLearning == true</code>.
101 * @see #isLearning
102 */
103 public void move(
104 Arena arena,
105 Entity[] others,
106 int nbEntities,
107 Graphics2D g) {
108 Point2D startAgent = (Point2D) getCoord().clone();
109
110 nbOfStepsMoving++;
111
112 if ((nbOfStepsMoving % intervalSwitchingBehaviours) == 0) {
113 // switching the behaviours
114 if (behaviour == c2) {
115 behaviour = c1;
116 } else {
117 behaviour = c2;
118 }
119 }
120 // Moving the real agent
121 // super.move(arena, others, nbEntities, g);
122
123 double newX = coord.getX();
124 double newY = coord.getY();
125
126 Vector2D newMovVector =
127 getNewMovVectorAccordingTo(
128 behaviour,
129 arena,
130 others,
131 nbEntities,
132 g,
133 null);
134
135 // newMovVector = Vector2D.getCorrectedVector(newMovVector,arena,this,g);
136
137 // in order to avoid pushing the other agent out of the arena
138 if (arena.distanceTo(this.getCoord()) < 70
139 && arena.distanceTo(this.getCoord()) > 60) {
140 Vector2D left = newMovVector.getNormalLeft();
141 left.opposite();
142 newMovVector.add(left);
143 //newMovVector = Vector2D.getCorrectedVector(newMovVector,arena,this,g);
144 }
145 double norm = newMovVector.getNorm();
146 if (norm > maxSpeed) {
147
148 newMovVector.multiplyByConstant(1 / (newMovVector.getNorm()));
149 newMovVector.multiplyByConstant(maxSpeed);
150 }
151
152 lastMovVector = newMovVector;
153
154 newX = newX + newMovVector.getX();
155 newY = newY + newMovVector.getY();
156
157 coord.setLocation(newX, newY);
158
159 }
160
161 /**
162 * Agent Ra IS dangerous
163 */
164 public boolean isDangerous() {
165 return true;
166 }
167
168 }