Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Basic Neural Net AI?  (Read 775 times)
0 Members and 1 Guest are viewing this topic.
Offline sci4me

Junior Devvie





« Posted 2013-02-12 01:25:44 »

Hey guys, I have just started learning about neural nets and im not very good with them so... yeah. I am trying to make a basic ai that will teach a "bot" to stay on the screen. Once i can get this to work, ill do more advanced things. I am using Encog. Here is the code i have so far:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
import java.awt.Color;
import java.awt.Graphics;
import org.encog.engine.network.activation.ActivationSigmoid;
import org.encog.ml.data.MLData;
import org.encog.ml.data.MLDataPair;
import org.encog.ml.train.strategy.RequiredImprovementStrategy;
import org.encog.neural.data.NeuralDataSet;
import org.encog.neural.data.basic.BasicNeuralDataSet;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.networks.layers.BasicLayer;
import org.encog.neural.networks.layers.Layer;
import org.encog.neural.networks.training.Train;
import org.encog.neural.networks.training.propagation.back.Backpropagation;

public class Bot
{
   private BasicNetwork brain;

   private Layer input, output;
   private Layer hidden;

   private int x, y;

   private int epoch = 0;

   private MLDataPair pair;

   public Bot(int x, int y)
   {
      this.x = x;
      this.y = y;

      brain = new BasicNetwork();

      input = new BasicLayer(null, true, 2);
      hidden = new BasicLayer(new ActivationSigmoid(), true, 2);
      output = new BasicLayer(new ActivationSigmoid(), true, 2);

      brain.addLayer(input);
      brain.addLayer(hidden);
      brain.addLayer(output);

      brain.getStructure().finalizeStructure();
      brain.reset();

      for(int i=0; i<1024; i++)
      {
         train();
      }
   }

   public void render(Graphics g)
   {
      g.setColor(Color.red);
      g.fillRect(x, y, 8, 8);
   }

   public void update()
   {
      epoch++;

      for(int i=0; i<64; i++)
      {
         train();
      }

      final MLData output = brain.compute(pair.getInput());

      double xo = output.getData(0);
      double yo = output.getData(1);

      System.out.println(xo + " " + yo);

      if(xo > 0)
      {
         x++;
      }
      else if(xo < 0)
      {
         x--;
      }

      if(yo > 0)
      {
         y++;
      }
      else if(yo < 0)
      {
         y--;
      }
   }

   public void train()
   {
      double xI, yI;

      if(x > 800 || x < 0)
         xI = -1;
      else
         xI = 0;

      if(y > 600 || y < 0)
         yI = -1;
      else
         yI = 0;

      double[][] inputs = new double[][]
      {
      { xI, yI } };
      double[][] desired = new double[][]
      {
      { 0, 0 } };

      NeuralDataSet trainingSet = new BasicNeuralDataSet(inputs, desired);
      final Train train = new Backpropagation(brain, trainingSet);
      train.addStrategy(new RequiredImprovementStrategy(5));

      double error = 0.0;
      do
      {
         train.iteration();
         error = train.getError();
      }
      while(error > 0.00001);

      pair = trainingSet.get(0);
   }
}

If anyone can help me out with this please let me know! THX!
Offline ReBirth
« Reply #1 - Posted 2013-02-12 03:14:11 »

What exactly your problem is?

Neural network only accepts binary and results binary too, so you need to define the problem into that.

And FYI there's also Neuroph lib which is easier than encog.

Offline sci4me

Junior Devvie





« Reply #2 - Posted 2013-02-12 04:33:30 »

a few things, i tried neuroph... couldnt figure it out lols.. but, the problem, it simple doesnt work right. the "bot" goes off the screen... i have nearly no experience with this so im probably doing a lot wrong...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline doos

Senior Devvie


Medals: 2
Projects: 2


Here be random


« Reply #3 - Posted 2013-02-20 23:58:04 »

It seems like you are training on an "outside" concept. If I told you when you were out of a box, how does that help you get back in the box?

It is possible to write the x and y cases seperately as your problem space has two bounds. Ignore Y for now and repeat X to handle that later. Deal with the one dimensional case of a point on a line.

The one dimensional problem is still pretty difficult as there are two directions to be out of bounds.

Lets imagine we had a black box. You pass the black box your X coordinate, and it tells you which direction to aim in:
float input (X) > float direction (-1 left to 1 right). Calling it a float just means we have a continuous numberline.

So now you can generate lots of test cases for your robot (input > output):
10 > 1
699 > -1
4000 > -1

Train your Neural net brain on those situations, and it should figure out whether to go left or right given the input. Or rather, it should have an idea of what output is expected given a particular input. Make sure your network is multilayer (1,2,1) or (1,4,1) maybe.

That is likely to lead to a system which is really boring and homes in on the center of the screen, staying there or wobbling about that central point. To make it more interesting, you could make the neural net output a random chance of pointing towards the center of the screen at any point in time. So maybe we should only check the gps at random, or maybe when we check it, it has a certain chance of being wrong, so 69% of the time it will point in the correct direction. You can do that by treating the sign as the direction and the absolute value as the chance.

In ye olden days, I made a virtual robot which had three input sensors for distance lasers and from that decided the outputs of the motors and explored a maze.. Then I trained its behaviour further in different mazes. Think about what behavior (outputs) you want from your stimulus (inputs), or give a network inputs and see what it can do!

In moths, there is a sensory response to the air from the beat of a bat's wing that goes to the output of flying off course.. All of this is hardwired and runs outside the brain. Suddenly a very confused moth isnt dinner anymore.. Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!