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     * A more simplified version of the TemplateRSP, with 2 bits for the Condition and 3 bits for the Action
011     * @author Benoit
012     * 
013     */
014    public class TemplateRSPVerySimple extends TemplateRSP {
015    
016            
017            public TemplateRSPVerySimple() {
018                    super();
019                    NB_BITS_CONDITION = 2;
020                    NB_BITS_ACTION = 3;
021                    MIN_SCALE = 0;
022                    STEP_SCALE = 2000;
023    
024                    MIN_NB_ACTIONS_TO_AVOID_COVERING = 0;
025                    
026                    name = "TemplateRSPVerySimple";
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 = 2;
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            /* (non-Javadoc)
069             * @see Template#getVectorFromAction(Classifier)
070             */
071            public Vector2D getVectorFromAction(
072                    Classifier c,
073                    Entity me,
074                    Entity otherEntity) {
075                    
076                    if(Config.PRINT_MODE > 8)
077                    {       
078                            System.out.println("TemplateRSPVerySimple - GetVectorFromAction:"+
079                                    c.getAction());
080                    }
081    
082                    int minScale = MIN_SCALE;
083                    //              int maxScale = 8000 ;
084                    int step = STEP_SCALE;
085                    int preventCollValue = otherEntity.getSize() + 5;
086                    //              System.out.println(c);          
087                    String action = c.getAction();
088    
089                    /* 1 bit for the angle of the vector -> 2 positions N,S */
090                    String angle = action.substring(0, 1);
091                    /* 2 bits for the scale of the response -> 4 values */
092                    String scale = action.substring(1, 3);
093                    /* 1 bit to allow (or not) to prevent collisions */
094    //              String preventColl = action.substring(3, 4);
095    
096                    double myX = me.getCoord().getX();
097                    double myY = me.getCoord().getY();
098    
099                    /* Vector that will be returned */
100                    Vector2D result = new Vector2D();
101    
102                    // compute unit vector me-OtherEntity
103                    result.setUnitVector(me.getCoord(), otherEntity.getCoordNearTo(me));
104                    //              //System.out.println("DA-"+DA);
105                    double distToEntity = me.distanceTo(otherEntity.getCoordNearTo(me));
106    
107                    /* inverse-square distance response */
108                    double den = distToEntity * distToEntity;
109    
110                    /* if preventing collisions, we add a parameter to the denominator */
111    //                              if (preventColl.equals("1"))
112    //                                      den = Math.pow(distToEntity + ((double) preventCollValue), 2);
113    
114                    if (den < 5) {
115                            den = 5;
116                    }
117    
118                    /* Angle */
119                    int intAngle = Utils.getValueFromBits(angle);
120                    switch (intAngle) {
121                            case 1 : /* opposite direction */
122                                    result.opposite();
123                                    break;
124                            default : /* NORTH : 0 same direction, nothing to do*/
125                                    break;
126                    }
127    
128                    /* Scale of the response */
129                    double valScale = minScale + step * Utils.getValueFromBits(scale);
130    
131                    result.multiplyByConstant(valScale / den);
132    
133    //              if (preventColl.equals("1")) {
134    //                      if (me.distanceTo(otherEntity.getCoordNearTo(me))
135    //                              < (me.getRadiusBody() + otherEntity.getRadiusBody())) {
136    //                              // we are too close ! a force should repell us as well
137    //                              result.add(
138    //                                      getVectorFromAction(
139    //                                              new Classifier("##", "1010"),
140    //                                              me,
141    //                                              otherEntity));
142    //                      }
143    //              }
144    
145                    return result;
146            }
147    
148    }