1   import interfaces.Controller;
2   import interfaces.AbstractRobot;
3   
4   /**
5   * This controller is based on guesses about expected input values.
6   * I spent about an hour trying to pick "good" expected input values by 
7   * guesswork and ended up just using using GRRs original guess as example.
8   * It is a 3-1 net plus bias.
9   *
10  * @author Graeme Bell
11  */
12  public class GBN1 extends Controller {
13  
14  AbstractRobot r;
15  boolean initcalled;
16  boolean running;
17  boolean paused=false;
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  final float weight00=-1.474f;
34  final float weight10=1.188f;
35  final float weight20=3.93f;
36  final float weightb0=-1.988f;
37  
38      public void run(){
39  
40          running=true;
41              
42          while(running) {    
43                  // get input
44                                                          
45                  input0=r.getSensor1()/100.0f;
46                  input1=r.getSensor2()/100.0f;
47                  input2=r.getSensor3()/100.0f;
48  
49                  //Sigmoidal activation function 1.0/1.0+exp(excitation)
50  
51                  output0 = 1.0f/(1.0f+(float)(Math.exp(  weight00*input0+
52                              weight10*input1+
53                              weight20*input2+
54                              weightb0)));
55  
56                  //decide what to do.
57  
58                  if (output0<0.4) {r.right();}
59                  if (output0<0.6 && output0>=0.4) {r.forward();}
60                  if (output0>=0.6) {r.left();}
61                  
62                  try{Thread.sleep(500);}catch(Exception e){}
63          }
64      }
65  
66      public void initController(AbstractRobot r) {
67  
68          this.r=r;
69          initcalled=true;
70      }
71      
72      public int[] getSensors(){
73      
74          return sensors;
75      }
76      
77      public void halt(){
78      
79          running=false;
80          r.stopMoving();
81      }
82      
83      public AbstractRobot getRobot(){
84      
85          return r;
86      }
87      
88      public GBN1() { }
89  }   
90