Neural net learning algorithm

I have been working on making a neural network that has a goal, of hitting a moving target, it has inputs based on the distance from the shooter to the target from both axis, the rotation of the shooter, and the wind speed.

Every 5 seconds the shooter resets to a different position on the screen.

I have attempted to use a back prop algorithm, by giving calculating the error based on the distance the bullet hits from the target, and if the shooter doesn't fire a shot in the 5 second window there is also an error propagated.

My network however does not seem to be learning properly, I was wondering if anyone could point me in the right direction.

Here is my code:

import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Random; import java.util.stream.DoubleStream; import javax.swing.JFrame; public class ClassMain extends JFrame{ int ShooterLocationX = 100; int ShooterLocationY = 150; int shooterDiameter = 35; int targetX = 525; int targetY = 100; int targetWidth = 15; int targetLength = 50; double error; double targetVectorX=ShooterLocationX + 100; double targetVectorY = ShooterLocationY+shooterDiameter/2; boolean direction = false; boolean ShotFired = false; boolean rotateDirection; boolean start =true; boolean shotbeenFires; double ShooterRotation = 0; double[] outputs1 = new double[4]; double[] outputs2 = new double[8]; double[] outputs3 = new double[8]; double[] outputs4 = new double[3]; Percepton[] input = new Percepton[4]; Percepton[] hidden = new Percepton[8]; Percepton[] hidden2 = new Percepton[8]; Percepton[] output = new Percepton[3]; Shooter shot = new Shooter(ShooterRotation); Random random = new Random(); long StartTime = 0; long CurrTime = 0; public static void main(String []args){ ClassMain CM= new ClassMain(); CM.setup(); } public void setup(){ setSize(900,300); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); createBufferStrategy(2); repaint(); for(int i = 0; i < 4; i ++){ input[i] = new Percepton(); input[i].setInputNumber(1); } for(int i =0;i < 8;i++){ hidden[i] = new Percepton(); hidden[i].setInputNumber(input.length); } for(int i =0;i < 8;i++){ hidden2[i] = new Percepton(); hidden2[i].setInputNumber(input.length); } for(int i = 0;i < 3; i++){ output[i] = new Percepton(); output[i].setInputNumber(hidden.length); } } public void run(){ double[] temp = new double[10]; if(start){ StartTime = System.currentTimeMillis(); start = false; } CurrTime = System.currentTimeMillis(); if(CurrTime-StartTime > 5000){ ShooterLocationX=random.nextInt(400); ShooterLocationY=random.nextInt(275); shot.newX = ShooterLocationX+(shooterDiameter/2)-5; shot.newY = ShooterLocationY+(shooterDiameter/2)-5; shot.windSpeed = (float)(random.nextInt(200)-100)/100; start = true; if(!shotbeenFires){ error+=15; } for(int n = 0; n < output.length;n++){ output[n].learn(n, error, output[n].weights[n]); } for(int n = 0; n < hidden2.length;n++){ hidden2[n].learn(n, error, hidden2[n].weights[n]); } for(int n = 0; n < hidden.length;n++){ hidden[n].learn(n, error, hidden[n].weights[n]); } for(int n = 0; n < input.length;n++){ input[n].learn(n, error, input[n].weights[n]); } error = 0; shotbeenFires = false; } outputs1[0] = input[0].sigmoid((((Math.max(ShooterLocationX,targetX)-Math.min(ShooterLocationX,targetX))/300)*2)*input[0].weights[0],1*input[0].biasWeight); outputs1[1] = input[1].sigmoid((((Math.max(ShooterLocationY,targetY)-Math.min(ShooterLocationY,targetY))/150)*2)*input[1].weights[0],1*input[1].biasWeight); outputs1[2] = input[2].sigmoid((ShooterRotation)*input[2].weights[0],1*input[2].biasWeight); outputs1[3] = input[3].sigmoid((shot.windSpeed)*input[3].weights[0],1*input[3].biasWeight); System.out.println("\n"); for(int n = 0;n < hidden.length;n++){ for(int i = 0;i<temp.length;i++){ temp[i] = 0; } for(int i = 0;i < outputs1.length;i++){ temp[i] = outputs1[i]*hidden[n].weights[i]; } outputs2[n] = (float)DoubleStream.of(temp).sum(); outputs2[n] = hidden[1].sigmoid(outputs2[n],1*hidden[n].biasWeight); //System.out.print(outputs2[n]); } System.out.println("\n"); for(int n = 0;n < hidden2.length;n++){ for(int i = 0;i<temp.length;i++){ temp[i] = 0; } for(int i = 0;i < outputs2.length;i++){ temp[i] = outputs2[i]*hidden2[n].weights[i]; } outputs3[n] = (float)DoubleStream.of(temp).sum(); outputs3[n] = hidden2[1].sigmoid(outputs3[n],1*hidden2[n].biasWeight); //System.out.print(outputs3[n]); } System.out.println("\n"); for(int n = 0;n < output.length;n++){ for(int i = 0;i<temp.length;i++){ temp[i] = 0; } for(int i = 0;i < outputs1.length;i++){ temp[i] = outputs3[n]*hidden[n].weights[i]; } outputs4[n] = (float)DoubleStream.of(temp).sum(); outputs4[n] = output[n].sigmoid(outputs4[n],1*output[n].biasWeight); //System.out.print(outputs4[n]); } if(!ShotFired){ System.out.println("\n"); if(outputs4[0] > 0.5){ ShotFired = true; shot.rotation = ShooterRotation; } if (outputs4[1] > 0.5){ ShooterRotation = ShooterRotation + outputs3[1]; } if(outputs4[2] > 0.5){ ShooterRotation =ShooterRotation - outputs3[2]; } if(ShooterRotation*360 > 360){ ShooterRotation = ((ShooterRotation * 360) + 360) /360; }else if(ShooterRotation * 360 < 0){ ShooterRotation = ((ShooterRotation*360)-360) /360; } } for(int i = 0; i < 1; i++){ if(ShotFired){ shotbeenFires = true; shot.newLocationX(shot.newX); shot.newLocationY(shot.newY); } if(targetY < 20 ||targetY+targetLength >300){ direction = !direction; } if(direction){ targetY += 8; }else{ targetY -= 8; } if(shot.newX + (15)>= targetX && shot.newX + 15 < targetX +targetWidth && shot.newY + 15> targetY && shot.newY <targetY + targetLength){ System.out.println("Target Hit"); error -= 10; shot.newX = ShooterLocationX + shooterDiameter/2-5; shot.newY = ShooterLocationY + shooterDiameter/2-5; ShotFired = false; }else if(shot.newX > 600|| shot.newX<10||shot.newY<10||shot.newY> 290){ System.out.println("MISS"); error += (shot.newY - targetY); shot.newX = ShooterLocationX + shooterDiameter/2-5; shot.newY = ShooterLocationY + shooterDiameter/2-5; ShotFired = false; } } repaint(); } public void paint(Graphics g){ targetVectorX = ShooterLocationX + shooterDiameter/2 + Math.cos(Math.toRadians(ShooterRotation)) * 100; targetVectorY = ShooterLocationY + shooterDiameter/2 + Math.sin(Math.toRadians(ShooterRotation)) * 100; super.paint(g); Graphics2D g2 = (Graphics2D)g; g2.fillOval(ShooterLocationX, ShooterLocationY, shooterDiameter, shooterDiameter); g2.setPaint(Color.RED); g2.drawLine(ShooterLocationX + shooterDiameter/2, ShooterLocationY+shooterDiameter/2, (int)targetVectorX, (int)targetVectorY); g2.setPaint(Color.BLUE); g2.fillRect(targetX, targetY, targetWidth, targetLength); g2.setPaint(Color.RED); g2.fillOval((int)shot.newX, (int)shot.newY, 10, 10); g2.setPaint(Color.gray); g2.fillRect(600, 0, 300,300); for(int i = 0; i < input.length;i++){ if(outputs1[i] > 0.5){ g2.setPaint(Color.red); }else{ g2.setPaint(Color.white); } g2.drawOval(625+i*35, 250, 25, 25); for(int j = 0;j<hidden.length;j++){ g2.drawLine(640+i*35, 250, 615+j*30, 195); } } for(int i = 0; i < hidden.length;i++){ if(outputs2[i] > 0.5){ g2.setPaint(Color.red); }else{ g2.setPaint(Color.white); } g2.drawOval(605+i*30, 175, 20, 20); for(int j = 0;j<hidden2.length;j++){ g2.drawLine(615+i*30, 175, 615+j*30, 145); } } for(int i = 0; i < hidden2.length;i++){ if(outputs3[i] > 0.5){ g2.setPaint(Color.red); }else{ g2.setPaint(Color.white); } g2.drawOval(605+i*30, 125, 20, 20); for(int j = 0;j<output.length;j++){ g2.drawLine(615+i*30, 125, 697+j*50, 75); } } for(int i = 0; i < output.length;i++){ if(outputs4[i] > 0.5){ g2.setPaint(Color.red); }else{ g2.setPaint(Color.white); } g2.drawOval(685+i*50, 50, 25, 25); } try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } run(); } } public class Shooter { double windSpeed = 0.5; int distance = 0; double newX; double newY; double rotation; double temp = 0; public Shooter(double rotate){ rotation = rotate*360; windSpeed = windSpeed*5; } public void newLocationX(double XPrev){ newX = XPrev + Math.cos(Math.toRadians(rotation)) * 20; } public void newLocationY(double YPrev){ newY = YPrev + Math.sin(Math.toRadians(rotation)) * 20; newY =newY - windSpeed; } } import java.util.Random; public class Percepton { int numOfInputs = 100; float[] weights = new float[numOfInputs]; int sum; double learningRate = 0.1; float biasWeight; float a; float out; Random random = new Random(); public void setWeight(int index,float weight){ weights[index] = weight; } public void setInputNumber(int inputs){ numOfInputs = inputs; for(int i = 0; i < inputs; i++){ weights[i] = (float)(random.nextInt(400)-200)/200; biasWeight = (float)(random.nextInt(400)-200)/200; //System.out.println(weights[i]); } } public void learn(int index, double error, float value){ weights[index] += (float)learningRate * (float)error * value; } public double sigmoid(double input,double bias){ return (float) (1/(1+Math.exp(-input*bias))); } }

Category:java Views:2 Time:2016-11-29

Related post

  • what practical proofs are there about the Turing completeness of neural nets? what nns can execute code/algorithms? 2010-06-07

    I'm interested in the computational power of neural nets. It is generally accepted that recurrent neural nets are Turing complete. Now I was searching for some papers which proofs this. What I found so far: Turing computability with neural nets, Hava

  • Neural net input/output 2009-07-30

    Can anyone explain to me how to do more complex data sets like team stats, weather, dice, complex number types i understand all the math and how everything works i just dont know how to input more complex data, and then how to read the data it spits

  • Machine Learning Algorithm for Predicting Order of Events? 2010-03-26

    Simple machine learning question. Probably numerous ways to solve this: There is an infinite stream of 4 possible events: 'event_1', 'event_2', 'event_4', 'event_4' The events do not come in in completely random order. We will assume that there are s

  • how useful is Turing completeness? are neural nets turing complete? 2010-06-07

    While reading some papers about the Turing completeness of recurrent neural nets (for example: Turing computability with neural nets, Hava T. Siegelmann and Eduardo D. Sontag, 1991), I got the feeling that the proof which was given there was not real

  • Neural Network, Genetic algorithm as an Intrusion detection system 2011-05-30

    Hi I need some help on getting started with creating my first algorithm; I want to create a NN/Genetic Algorithm for use as an Intrusion detection system. But I’m struggling with some points (never written an algorithm before.) I want to develop in C

  • Neural net training continuos values 2012-03-07

    As so many papers describes about that NN can predict stock return I also started looking into this method. I am not able to train NN precisly, my prediction does not match even on the training dataset. I am using technical indicators as input and ma

  • General Machine Learning Algorithm, Training Set -> "Predictor" 2012-03-10

    Consider the following code: struct TrainingExample { array<double, N> input; array<double, M> output; }; struct Predictor { Predictor(const vector<TrainingExample>& trainingSet); array<double, M> predict(const array<do

  • Learning Algorithm and Saving Data in Software 2008-12-31

    I'm coming from a web-development background and I am wondering how I would make a learning algorithm in Java/C++. Not so much the algorithm part, but making the program "remember" what it learned from the previous day. I would think something like s

  • Learning Algorithms and Data Structures Fundamentals 2010-02-20

    Can you recommend me a book or (better!) a site with many hard problems and exercises about data structures? I'm already answering project Euler questions, but these questions are about interesting, but uncommon algorithms. I hardly used even a simpl

  • Machine Learning Algorithm for Peer-to-Peer Nodes 2010-05-30

    I want to apply machine learning to a classification problem in a parallel environment. Several independent nodes, each with multiple on/off sensors, can communicate their sensor data with the goal of classifying an event as defined by a heuristic, t

  • Machine learning algorithm 2011-04-09

    I am looking to research an appropriate algorithm for my purpose, can someone suggest a good learning algorithm for the following scenario: A user can search for some word in a set of sentences. I will then return the top 10 sentences based on that k

  • Is there a supervised learning algorithm that takes tags as input, and produces a probability as output? 2011-04-28

    Let's say I want to determine the probability that I will upvote a question on SO, based only on which tags are present or absent. Let's also imagine that I have plenty of data about past questions that I did or did not upvote. Is there a machine lea

  • iterative machine learning algorithm 2011-06-24

    I need a machine learning algorithm which takes some training samples of form (x,y), and compute approximate function f:X->Y such that the error is minimum. error is defined as the difference b/n y and f(x). But this learning algorithm must be a i

  • What machine learning algorithm should I use for Connect 4? 2011-09-21

    I have an AI that is good at playing Connect 4 (using minimax). Now I want to use some machine learning algorithm to learn from this AI that I have, and I would like to do that by just letting them play against each other. What algorithm would be goo

  • Neural nets as universal approximators 2011-11-16

    The formal statement of universal approximation theorem states that neural nets with single hidden layer can approximate any function which is continuous on m-dimensional unit hypercube. But how about functions which are not continuous, is anything k

  • Choosing Machine Learning Algorithm and tool 2011-12-27

    I am working on a binary classification problem i.e need to classify my data into two categories. For every entity have extracted 5 features. Now I want to decide on which algorithm I should use. Also I wanted to know what will be the most convenient

  • Machine learning algorithm for this task? 2012-01-07

    Trying to write some code that deals with this task: • As an starting point, I have around 20 "profiles" (imagine a landscape profile), i.e. one-dimensional arrays of around 1000 real values. • Each profile has a real-valued desired outcome, the "eff

  • Is the HTM cortical learning algorithm defined by Numenta's paper restricted by Euclidean geometry? 2012-01-13

    Specifically, their most recent implementation. http://www.numenta.com/htm-overview/htm-algorithms.php Essentially, I'm asking whether non-euclidean relationships, or relationships in patterns that exceed the dimensionality of the inputs, can be effe

  • What machine learning algorithm is appropriate for shooting basketballs? 2012-02-13

    We are making a robot that shoots basketballs into hoops. From an image and our knowledge of the camera's angle and the target's dimensions (the targets are coated with retroreflective tape), we know how far away we are, X and Y (distance being Z, mo

  • Is there a machine learning algorithm which successfully learns the parity function? 2012-02-28

    The parity function is a function from a vector of n bits and outputs 1 if the sum is odd and 0 otherwise. This can be viewed as a classification task, where the n input are the features. Is there any machine learning algorithm which would be able to

  • Neural net package in R 2012-03-01

    Which package in R contain recursive neural network and auto associative neural network functions? I am using nnet and neuralnet function but need to use recursive neural network or auto associative neural net. --------------Solutions------------- Go

  • Learning C# help or hinder VB.NET learning 2008-11-21

    I have just moved job and gone from VB 6 to VB.Net and found the learning jump fairly steep, have more a problem with the object / conceptual side of things .. but getting there now ... but as I was a assembler / C++ 10/15 years ago and was consideri

  • Perceptron learning algorithm not converging to 0 2009-11-08

    Here is my perceptron implementation in ANSI C: #include <stdio.h> #include <stdlib.h> #include <math.h> float randomFloat() { srand(time(NULL)); float r = (float)rand() / (float)RAND_MAX; return r; } int calculateOutput(float weigh

  • What kind of learning algorithm would you use to build a model of how long it takes a human to solve a given Sudoku situation? 2009-11-27

    I don't have much experience in machine learning, pattern recognition, data mining, etc. and in their underlying theory and systems. I would like to develop an artificial model of the time it takes a human to make a move in a given Sudoku puzzle. So

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.136 (s). 11 q(s)