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
009 import simuLCS.graphics.*;
010
011 /**
012 * This Template is used to describe R. Vaughan's model of a duck as a set of rules (Experiment 1).
013 * @author Benoit
014 *
015 */
016 public class TemplateRSP extends Template {
017
018 // protected int MIN_SCALE = 50;
019 // protected int STEP_SCALE = 500 ;
020
021 protected int MIN_SCALE = 500;
022 protected int STEP_SCALE = 500;
023
024 protected int MIN_NB_ACTIONS_TO_AVOID_COVERING = 4;
025
026 public TemplateRSP() {
027 super();
028 NB_BITS_CONDITION = 2;
029 NB_BITS_ACTION = 7;
030 name = "TemplateRSP";
031
032 // CONDITIONS
033 nbPartsCondition = 2;
034 conditions = new ClassifierComponent[nbPartsCondition];
035
036 // ClassifierComponent c1 = new ClassifierComponent("Moving?",true);
037 String[] mov = { "0 [Not moving]", "1 [Moving]", "# [Both]" };
038 ClassifierComponent c1 =
039 new ClassifierComponent("", false, false, mov, 1, false);
040 String[] dang = { "0 [Not dangerous]", "1 [Dangerous]", "# [Both]" };
041 ClassifierComponent c2 =
042 new ClassifierComponent("", false, false, dang, 1, false);
043 // ClassifierComponent c2 = new ClassifierComponent("Dangerous?",true);
044
045 conditions[0] = c1;
046 conditions[1] = c2;
047
048 // ACTIONS
049 nbPartsAction = 3;
050 actions = new ClassifierComponent[nbPartsAction];
051
052 String[] directions =
053 {
054 "00 [Towards]",
055 "01 [On the right]",
056 "10 [Opposite direction]",
057 "11 [On the left]" };
058 ClassifierComponent a1 =
059 new ClassifierComponent(
060 "Direction ?",
061 true,
062 false,
063 directions,
064 2,
065 false);
066 String[] scale =
067 {
068 "0000",
069 "0001",
070 "0010",
071 "0011",
072 "0100",
073 "0101",
074 "0110",
075 "0111",
076 "1000",
077 "1001",
078 "1010",
079 "1011",
080 "1100",
081 "1101",
082 "1110",
083 "1111" };
084 ClassifierComponent a2 =
085 new ClassifierComponent("Scale ?", true, false, scale, 4, false);
086 ClassifierComponent a3 =
087 new ClassifierComponent("Preventing Collisions ?", true);
088
089 actions[0] = a1;
090 actions[1] = a2;
091 actions[2] = a3;
092 }
093
094 /**
095 * Gives the definition of an Entity according to the criteria given
096 * by this template (is moving ? is Dangerous ?)
097 * This function gives the meaning of the Conditions bits
098 * @see Template#testCondition(int, Agent)
099 */
100 public String testCondition(int i, Entity otherEntity) {
101
102 switch (i) {
103 case 0 : /* Is it a Moving Entity ? */
104 if (otherEntity.isMoving()) {
105 return "1";
106 } else {
107 return "0";
108 }
109 case 1 : /* Is it a Dangerous Entity ? */
110 if (otherEntity.isDangerous()) {
111 return "1";
112 } else {
113 return "0";
114 }
115 default :
116 return Config.ERROR; /* outside the range */
117
118 }
119
120 }
121
122 /**
123 * Calculate the Vector generated by the Action part of the Classifier
124 * This function gives the meaning of the Action part.
125 * @see Template#getVectorFromAction(Classifier)
126 */
127 public Vector2D getVectorFromAction(
128 Classifier c,
129 Entity me,
130 Entity otherEntity) {
131
132 int minScale = MIN_SCALE;
133 // int maxScale = 8000 ;
134 int step = STEP_SCALE;
135 int preventCollValue = otherEntity.getSize() + 10;
136 // System.out.println(c);
137 String action = c.getAction();
138
139 /* 2 bits for the angle of the vector -> 4 positions N,E,S,W */
140 String angle = action.substring(0, 2);
141 /* 4 bits for the scale of the response -> 16 values [500,1000,...,8000]*/
142 String scale = action.substring(2, 6);
143 /* 1 bit to allow (or not) to prevent collisions */
144 String preventColl = action.substring(6, 7);
145
146 double myX = me.getCoord().getX();
147 double myY = me.getCoord().getY();
148
149 /* Vector that will be returned */
150 Vector2D result = new Vector2D();
151
152 // compute unit vector me-OtherEntity
153 result.setUnitVector(me.getCoord(), otherEntity.getCoordNearTo(me));
154 // //System.out.println("DA-"+DA);
155 double distToEntity = me.distanceTo(otherEntity.getCoordNearTo(me));
156
157 /* inverse-square distance response */
158 double den = distToEntity * distToEntity;
159
160 /* if preventing collisions, we add a parameter to the denominator */
161 if (preventColl.equals("1"))
162 den = Math.pow(distToEntity + ((double) preventCollValue), 2);
163
164 if (den < 5) {
165 den = 5;
166 }
167
168 /* Angle */
169 int intAngle = Utils.getValueFromBits(angle);
170 switch (intAngle) {
171 case 2 : /* opposite direction */
172 result.opposite();
173 break;
174 case 1: /* EAST */
175 result = result.getNormalLeft();
176 result.opposite();
177 break;
178 case 3: /* WEST */
179 result = result.getNormalLeft();
180 break;
181 default : /* NORTH : 0 same direction, nothing to do*/
182 break;
183 }
184
185 /* Scale of the response */
186 double valScale = minScale + step * Utils.getValueFromBits(scale);
187
188 result.multiplyByConstant(valScale / den);
189
190 return result;
191
192 }
193
194 /**
195 * Since the Action is given as a string of N bits, there are
196 * 2^N possible actions
197 * @see Template#getNbPossibleActions()
198 */
199 public int getNbPossibleActions() {
200 return (int) Math.pow(2, NB_BITS_ACTION);
201 }
202
203
204 public G_Panel getPanConditions() {
205 return G_TemplatePanConditions.getInstance(this);
206 }
207
208 /* (non-Javadoc)
209 * @see Template#getPanActions()
210 */
211 public G_Panel getPanActions() {
212 return G_TemplatePanActions.getInstance(this);
213 }
214
215 public void updateValuesUI() {
216 G_TemplatePanConditions.getInstance(this).updateValues();
217 G_TemplatePanActions.getInstance(this).updateValues();
218 }
219
220 /* (non-Javadoc)
221 * @see simuLCS.Template#getMinNbActions()
222 */
223 public int getMinNbActions() {
224 return MIN_NB_ACTIONS_TO_AVOID_COVERING;
225 }
226
227 /**
228 * @return
229 */
230 public int getMIN_NB_ACTIONS_TO_AVOID_COVERING() {
231 return MIN_NB_ACTIONS_TO_AVOID_COVERING;
232 }
233
234 /**
235 * @return
236 */
237 public int getMIN_SCALE() {
238 return MIN_SCALE;
239 }
240
241 /**
242 * @return
243 */
244 public int getSTEP_SCALE() {
245 return STEP_SCALE;
246 }
247
248 /**
249 * @param i
250 */
251 public void setMIN_NB_ACTIONS_TO_AVOID_COVERING(int i) {
252 MIN_NB_ACTIONS_TO_AVOID_COVERING = i;
253 }
254
255 /**
256 * @param i
257 */
258 public void setMIN_SCALE(int i) {
259 MIN_SCALE = i;
260 }
261
262 /**
263 * @param i
264 */
265 public void setSTEP_SCALE(int i) {
266 STEP_SCALE = i;
267 }
268
269 }