Benutzer-Werkzeuge

Webseiten-Werkzeuge


ein_neuronales_netz_fuer_den_nxt_roboter

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen RevisionVorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
ein_neuronales_netz_fuer_den_nxt_roboter [2024/01/26 11:11] – [Quellcode] torsten.roehlein_neuronales_netz_fuer_den_nxt_roboter [2024/01/26 11:20] (aktuell) – [Quellcode] torsten.roehl
Zeile 123: Zeile 123:
    
 public class Main { public class Main {
- public static Methods bpn = new Methods();+   public static Methods bpn = new Methods();
    
- public static void main(String args[]) throws InterruptedException {+   public static void main(String args[]) throws InterruptedException {
    
- //Initialisieren der Sensoren+   //Initialisieren der Sensoren
      
- TouchSensor s1 = new TouchSensor(SensorPort.S1); +   TouchSensor s1 = new TouchSensor(SensorPort.S1); 
- LightSensor s2 = new LightSensor(SensorPort.S2); +   LightSensor s2 = new LightSensor(SensorPort.S2); 
- TouchSensor s3 = new TouchSensor(SensorPort.S3);+   TouchSensor s3 = new TouchSensor(SensorPort.S3);
    
- //Initialisieren der Zählvariable i, des Weißstandarts +   //Initialisieren der Zählvariable i, des Weißstandarts 
- // und des Input- und Output-Vektors+   // und des Input- und Output-Vektors
      
- int i, white; +   int i, white; 
- int inp[] = { 0, 0, 0 }; +   int inp[] = { 0, 0, 0 }; 
- int out[] = { 0, 0 };+   int out[] = { 0, 0 };
    
- Sound.beep(); +   Sound.beep(); 
- System.out.println("Train");+   System.out.println("Train");
    
- // Trainieren des bpn (Backpropagation-Netzwerks) in 500 Epochen +   // Trainieren des bpn (Backpropagation-Netzwerks) in 500 Epochen 
- for (i = 0; i < 500; i++) { +   for (i = 0; i < 500; i++) { 
- bpn.train(1); +      bpn.train(1); 
- System.out.println(bpn.trainedEpochs);+      System.out.println(bpn.trainedEpochs); 
 +   }
    
- } +   Sound.twoBeeps();
-  +
- Sound.twoBeeps();+
      
- //Definieren des Weißstandards und der Motorgeschwindigkeiten+   //Definieren des Weißstandards und der Motorgeschwindigkeiten
      
- white = s2.readValue();+   white = s2.readValue();
    
- Motor.A.setSpeed(300); +   Motor.A.setSpeed(300); 
- Motor.C.setSpeed(300);+   Motor.C.setSpeed(300);  
 +   Sound.twoBeeps();
    
- Sound.twoBeeps();+   while (!Button.ENTER.isPressed()) {
    
- while (!Button.ENTER.isPressed()) {+       System.out.println(s2.readValue());
    
-   +       if (s1.isPressed()==true) 
- System.out.println(s2.readValue());+          inp[0] = 1; // Sensor 1 an 
 +       else 
 +          inp[0] = 0// Sensor 1 aus
    
- if (s1.isPressed()==true+       if (s2.readValue() > white + 15
- inp[0] = 1; // Sensor 1 an +           inp[1] = 1; // Sensor 2 schwarz 
- else +       else 
- inp[0] = 0; // Sensor 1 aus+           inp[1] = 0; // Sensor 2 weiß
    
- if (s2.readValue() > white + 15+       if (s3.isPressed()==true
- inp[1] = 1; // Sensor 2 schwarz +           inp[2] = 1; // Sensor 3 an 
- else +       else 
- inp[1] = 0; // Sensor 2 weiß+           inp[2] = 0; // Sensor 3 aus
    
- if (s3.isPressed()==true) +       bpn.test(inp, out);
- inp[2] = 1; // Sensor 3 an +
- else +
- inp[2] = 0// Sensor 3 aus+
    
- bpn.test(inp, out);+       if (out[0] == 1) 
 +           Motor.A.forward(); 
 +       else 
 +          Motor.A.backward();
    
- if (out[0] == 1) +      if (out[1] == 1) 
- Motor.A.forward(); +          Motor.C.forward(); 
- else +      else 
- Motor.A.backward();+          Motor.C.backward();
    
- if (out[1] == 1) +      Thread.sleep(500);
- Motor.C.forward(); +
- else +
- Motor.C.backward();+
    
- Thread.sleep(500); +   
-  +
- +
      
    
    
- Motor.A.stop(); +   Motor.A.stop(); 
- Motor.C.stop(); +   Motor.C.stop(); 
- Sound.beep();+   Sound.beep();
      
- Button.ESCAPE.addButtonListener(new ButtonListener() { +   Button.ESCAPE.addButtonListener(new ButtonListener() { 
- public void buttonPressed(Button b) { +      public void buttonPressed(Button b) { 
- LCD.drawString("Program stop", 0, 3); +           LCD.drawString("Program stop", 0, 3); 
- } +      
- public void buttonReleased(Button b) { +       
- System.exit(0); +      public void buttonReleased(Button b) { 
- } +         System.exit(0); 
- });+      
 +   });
    
    
    
 } }
 +
 +
 </Code> </Code>
  
Zeile 223: Zeile 223:
  public static int data3[][] = {{0,0,1}, {0,1}};  public static int data3[][] = {{0,0,1}, {0,1}};
  public static int data4[][] = {{0,1,0}, {0,0}};  public static int data4[][] = {{0,1,0}, {0,0}};
-  +  
- +
  public static double input[] = {0,0,0,1};  public static double input[] = {0,0,0,1};
  public static double w1[][] = {{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}};  public static double w1[][] = {{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}};
Zeile 232: Zeile 231:
  public static double delta2[] = {0,0};  public static double delta2[] = {0,0};
  public static double delta1[] = {0,0,0};  public static double delta1[] = {0,0,0};
-  
    
  public static int trainedEpochs = 0;  public static int trainedEpochs = 0;
-  +   
-  +
-   +
-  +
- +
  public Methods() {  public Methods() {
  byte i, j;  byte i, j;
Zeile 250: Zeile 244:
    
  public static void train(int e) {  public static void train(int e) {
- for(int i=0; i +    for(int i=0; i 
- learn( data1[0], data1[1] ); +      learn( data1[0], data1[1] ); 
- learn( data2[0], data2[1] ); +      learn( data2[0], data2[1] ); 
- learn( data3[0], data3[1] ); +      learn( data3[0], data3[1] ); 
- learn( data4[0], data4[1] ); +      learn( data4[0], data4[1] ); 
- trainedEpochs++; +      trainedEpochs++; 
- }+      }
  }  }
    
Zeile 262: Zeile 256:
  public static void learn( int inp[], int out[] ) {  public static void learn( int inp[], int out[] ) {
  int i, j;  int i, j;
- double sum, out_j; + double sum, out_j; 
- +
    
  // Initialisieren der Input-Vekoren  // Initialisieren der Input-Vekoren
Zeile 273: Zeile 266:
    
  hidden[j] = 1 / ( 1 + Math.exp(-sum));  hidden[j] = 1 / ( 1 + Math.exp(-sum));
- } + 
- +
    
  // Berechnen der Output-Werte  // Berechnen der Output-Werte
Zeile 282: Zeile 274:
  output[j] = 1 / (1 + Math.exp(-sum));  output[j] = 1 / (1 + Math.exp(-sum));
  }  }
-  
-  
-  
    
    
Zeile 312: Zeile 301:
  // Anpassen der Gewichte w1  // Anpassen der Gewichte w1
  for(i=0; i  for(i=0; i
- } + 
- +
    
  public static void test(int inp[], int out[]) {  public static void test(int inp[], int out[]) {
Zeile 333: Zeile 321:
  // Berechnen der Output-Werte  // Berechnen der Output-Werte
  for(j=0; j  for(j=0; j
-  
  sum = 0;  sum = 0;
  for(i=0; i  for(i=0; i
-  
    
  output[j] = 1 / (1 + Math.exp(-sum));  output[j] = 1 / (1 + Math.exp(-sum));
Zeile 343: Zeile 329:
    
  // Übertragen des Outputs auf das Array out[]  // Übertragen des Outputs auf das Array out[]
- for(i=0; i= 0.5 ) +     for(i=0; i= 0.5 ) 
- out[i] = 1; +         out[i] = 1; 
- else +     else 
- out[i] = 0;+         out[i] = 0;
  }  }
          
Zeile 356: Zeile 342:
 </Code> </Code>
  
 +<WRAP center round info 60%>
 +FIXME Der Quellcode enthält noch Fehler 
 +</WRAP>
  
  
 +==== Quellen ==== 
 +  * Download des orginales Artikels erschienen in JavaWorld 2005 
 +          * [[https://www.informatics4kids.de/addons-i4k/doc/article/neural_network_lego.pdf|A neural network for Java Lego robots]]
  
ein_neuronales_netz_fuer_den_nxt_roboter.1706267469.txt.gz · Zuletzt geändert: 2024/01/26 11:11 von torsten.roehl