Basic Neural Net AI?

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:


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!

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.

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…

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… :slight_smile: