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    /**
010     * First simplification of the TemplateRSP, unfortunately this was not enough to obtain good results.
011     * @author Benoit
012     * 
013     */
014    public class TemplateRSPSimple extends TemplateRSP {
015    
016            
017            public TemplateRSPSimple() {
018                    super();
019                    NB_BITS_CONDITION = 2;
020                    NB_BITS_ACTION = 4;
021                    MIN_SCALE = 0;
022                    STEP_SCALE = 2000;
023    
024                    MIN_NB_ACTIONS_TO_AVOID_COVERING = 4;
025                    
026                    name = "TemplateRSPSimple";
027    
028                    // CONDITIONS
029                    nbPartsCondition = 2;
030                    conditions = new ClassifierComponent[nbPartsCondition];
031    
032                    //              ClassifierComponent c1 = new ClassifierComponent("Moving?",true);
033                    String[] mov = { "0 [Not moving]", "1 [Moving]", "# [Both]" };
034                    ClassifierComponent c1 =
035                            new ClassifierComponent("", false, false, mov, 1, false);
036                    String[] dang = { "0 [Friend]", "1 [Dangerous]", "# [Both]" };
037                    ClassifierComponent c2 =
038                            new ClassifierComponent("", false, false, dang, 1, false);
039                    //              ClassifierComponent c2 = new ClassifierComponent("Dangerous?",true);
040    
041                    conditions[0] = c1;
042                    conditions[1] = c2;
043    
044                    // ACTIONS
045                    nbPartsAction = 3;
046                    actions = new ClassifierComponent[nbPartsAction];
047    
048                    String[] directions = { "0 [Towards]", "1 [Opposite direction]" };
049                    ClassifierComponent a1 =
050                            new ClassifierComponent(
051                                    "Direction ?",
052                                    true,
053                                    false,
054                                    directions,
055                                    1,
056                                    false);
057                    String[] scale = { "00", "01", "10", "11" };
058                    ClassifierComponent a2 =
059                            new ClassifierComponent("Scale ?", true, false, scale, 2, false);
060                    ClassifierComponent a3 =
061                            new ClassifierComponent("Preventing Collisions ?", true);
062    
063                    actions[0] = a1;
064                    actions[1] = a2;
065                    actions[2] = a3;
066            }
067    
068            /**
069             * Only the Action Part was modified (4 bits)
070             * @see Template#getVectorFromAction(Classifier)
071             */
072            public Vector2D getVectorFromAction(
073                    Classifier c,
074                    Entity me,
075                    Entity otherEntity) {
076    
077                    int minScale = MIN_SCALE;
078                    //              int maxScale = 8000 ;
079                    int step = STEP_SCALE;
080                    int preventCollValue = otherEntity.getSize() + 5;
081                    //              System.out.println(c);          
082                    String action = c.getAction();
083    
084                    /* 1 bit for the angle of the vector -> 2 positions N,S */
085                    String angle = action.substring(0, 1);
086                    /* 2 bits for the scale of the response -> 4 values */
087                    String scale = action.substring(1, 3);
088                    /* 1 bit to allow (or not) to prevent collisions */
089                    String preventColl = action.substring(3, 4);
090    
091                    double myX = me.getCoord().getX();
092                    double myY = me.getCoord().getY();
093    
094                    /* Vector that will be returned */
095                    Vector2D result = new Vector2D();
096    
097                    // compute unit vector me-OtherEntity
098                    result.setUnitVector(me.getCoord(), otherEntity.getCoordNearTo(me));
099                    //              //System.out.println("DA-"+DA);
100                    double distToEntity = me.distanceTo(otherEntity.getCoordNearTo(me));
101    
102                    /* inverse-square distance response */
103                    double den = distToEntity * distToEntity;
104    
105                    /* if preventing collisions, we add a parameter to the denominator */
106                                    if (preventColl.equals("1"))
107                                            den = Math.pow(distToEntity + ((double) preventCollValue), 2);
108    
109                    if (den < 5) {
110                            den = 5;
111                    }
112    
113                    /* Angle */
114                    int intAngle = Utils.getValueFromBits(angle);
115                    switch (intAngle) {
116                            case 1 : /* opposite direction */
117                                    result.opposite();
118                                    break;
119                            default : /* NORTH : 0 same direction, nothing to do*/
120                                    break;
121                    }
122    
123                    /* Scale of the response */
124                    double valScale = minScale + step * Utils.getValueFromBits(scale);
125    
126                    result.multiplyByConstant(valScale / den);
127    
128    //              if (preventColl.equals("1")) {
129    //                      if (me.distanceTo(otherEntity.getCoordNearTo(me))
130    //                              < (me.getRadiusBody() + otherEntity.getRadiusBody())) {
131    //                              // we are too close ! a force should repell us as well
132    //                              result.add(
133    //                                      getVectorFromAction(
134    //                                              new Classifier("##", "1010"),
135    //                                              me,
136    //                                              otherEntity));
137    //                      }
138    //              }
139    
140                    return result;
141    
142            }
143    
144    }