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!