1   import interfaces.Controller;
2   import interfaces.AbstractRobot;
3   
4   
5   /**
6   * This controller is based on the simulator's measured input values
7   * It is a 3-1 net plus bias.
8   *
9   * @author Graeme Bell
10  */
11  
12  public class GBN2a extends Controller {
13  
14  AbstractRobot r;
15  boolean initcalled;
16  boolean running;
17  boolean paused;
18  
19  private int[] sensors={Controller.SENSOR_TYPE_LIGHT,Controller.SENSOR_TYPE_LIGHT,Controller.SENSOR_TYPE_LIGHT};
20  
21  // this looks rubbish - but it will compile to run extremely fast 
22  // this is because the weights will be inlined and the inputs/outputs
23  // won't need dereferencing or array overhead.
24  
25  float input0;
26  float input1;
27  float input2;
28  
29  // bias input is assumed to be +1
30  
31  float output0;
32  
33  
34  final float weight00=8.191f;
35  final float weight10=-4.495f;
36  final float weight20=34.566f;
37  final float weightb0=-25.26f;
38  
39      public void run(){
40  
41          running=true;
42              
43          while (running) {
44              
45                  // get input
46                                                          
47                  input0=r.getSensor1()/100.0f;
48                  input1=r.getSensor2()/100.0f;
49                  input2=r.getSensor3()/100.0f;
50  
51                  //Sigmoidal activation function 1.0/1.0+exp(excitation)
52  
53                  output0 = 1.0f/(1.0f+(float)(Math.exp(  weight00*input0+
54                              weight10*input1+
55                              weight20*input2+
56                              weightb0)));
57  
58                  //decide what to do.
59  
60                  if (output0<0.4) {r.right();}
61                  if (output0<0.6 && output0>=0.4) {r.forward();}
62                  if (output0>=0.6) {r.left();}
63                  
64                  try{Thread.sleep(500);}catch(Exception e){}
65          }
66      }
67  
68      
69      public void initController(AbstractRobot r) {
70  
71          this.r=r;
72          initcalled=true;
73      }
74      
75      public int[] getSensors(){
76      
77          return sensors;
78      }
79      
80      public void halt(){
81      
82          running=false;
83          r.stopMoving();
84      }
85      
86      public AbstractRobot getRobot(){
87      
88          return r;
89      }
90      
91      public GBN2a() {    }
92  }   
93