I'm having problems with converging 2 separate Neural Networks into one.
I am literally stumped at the moment.
The program creates two separate Neural Networks with random Weights.
A class called Genome holds the Neural Network and can create a completely new Genome using another Genome's Neural Network.
The aim is to have bots that have neural networks that can fish out food. The most successfull neural networks(bots) reproduce to create an even better one. (including Mutations)
Anyone Who Posts an answer I will give +1 Rep for at least trying to help me out
I have literally No Idea why I'm getting errors.
Error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AI.Node.input(Node.java:43)
at AI.NeuralNetwork.input(NeuralNetwork.java:103)
at AI.Running.keyPress(Running.java:57)
The Code:
package AI;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import AI.NeuralNetwork;
import Resource.*;
public class Running extends State {
static WindowCreator win;
NeuralNetwork n;
NeuralNetwork n2;
float[] in;
public Running(){
win = new WindowCreator(this);}
public static void main(String[] args) {
Running running = new Running();}
public void init() {
in = new float[1];
in[0] = 10;
fullscreen = false;
printFPS = false;
n = new NeuralNetwork(1,1,10,2);
n2 = new NeuralNetwork(1,1,10,2);
n.createNetwork();
n2.createNetwork();
n.input(in);
n2.input(in);
System.out.println("Neural 1: " + n.getOutput(0));
System.out.println("Neural 2: " + n2.getOutput(0));
}@Override
public void keyPress(KeyEvent e) {
int key = e.getKeyCode();
if(key == e.VK_SPACE){
Genome a = new Genome(n);
Genome b = new Genome(n2);
n = a.returnConvergedNet(b);
n2 = b.returnConvergedNet(a);
n.input(in);
n2.input(in);
System.out.println("Neural 1: " + n.getOutput(0));
System.out.println("Neural 2: " + n2.getOutput(0));
}
if(key == e.VK_ESCAPE){
win.shutdown();
}
}
@Override
public void keyRell(KeyEvent arg0) {
// TODO Auto-generated method stub
}@Override
public void mouse(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void mousePress(MouseEvent arg0) {
// TODO Auto-generated method stub
}@Override
public void mouseRell(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void render(Graphics arg0) {
// TODO Auto-generated method stub
}@Override
public void shutdown() {
// TODO Auto-generated method stub
}
@Override
public void update() {
// TODO Auto-generated method stub
}}
public class Genome {
public final int mutationRate = 1;
public int splitn;
public int splitno;
public int spliti;
NeuralNetwork net;
public Genome(NeuralNetwork n){
net = n;
splitn = net.node[0].length;
splitno = net.out.length;
spliti = net.in.length;
}
public NeuralNetwork returnConvergedNet(Genome g){
Random a = new Random();
int split = a.nextInt(splitn);
Node[][] nod = new Node[net.node.length][net.node[0].length];
Node[] ot = new Node[net.out.length];
Node[] ni = new Node[net.in.length];
for(int i = 0; i < net.node.length; i ++){
for(int b = 0; b < split; b++){
nod = net.node;
}
for(int b = split; i < net.node.length; i ++){
nod = g.net.node;
}
}
split = a.nextInt(splitno);
for(int i = 0; i < split; i ++){
ot = net.out;
}
for(int i = split; i < net.out.length; i ++){
ot = g.net.out;
}
split = a.nextInt(spliti);
for(int i = 0; i < split; i ++){
ni = net.in;
}
for(int i = split; i < net.in.length; i ++){
ni = g.net.in;
}
NeuralNetwork network = new NeuralNetwork(net.in.length,net.out.length,net.node.length,net.node[0].length);
network.CreateFromArrays(nod, ot, ni);
return network;
}
public Genome converge(Genome g){
Random a = new Random();
int split = a.nextInt(splitn);
Node[][] nod = new Node[net.node.length][net.node[0].length];
Node[] ot = new Node[net.out.length];
Node[] ni = new Node[net.in.length];
for(int i = 0; i < net.node.length; i ++){
for(int b = 0; b < split; b++){
nod = net.node;
}
for(int b = split; i < net.node.length; i ++){
nod = g.net.node;
}
}
split = a.nextInt(splitno);
for(int i = 0; i < split; i ++){
ot = net.out;
}
for(int i = split; i < net.out.length; i ++){
ot = g.net.out;
}
split = a.nextInt(spliti);
for(int i = 0; i < split; i ++){
ni = net.in;
}
for(int i = split; i < net.in.length; i ++){
ni = g.net.in;
}
NeuralNetwork network = new NeuralNetwork(net.in.length,net.out.length,net.node.length,net.node[0].length);
network.CreateFromArrays(nod, ot, ni);
return new Genome(network);
}
}
package AI;
import java.util.Random;public class NeuralNetwork {
public Node[] in;
public Node[] out;
public Node[][] node; public NeuralNetwork(int ins, int outs, int layers, int num){
in = new Node[ins];
out = new Node[outs];
node = new Node[layers][num];
}
public float[][] returnInWeights(){
float[][] ini = new float[in.length][node[0].length];
for(int i = 0; i < in.length; i ++){
for(int b = 0; b < node[0].length; b++){
ini = in.weight;
}
}
return ini;
}
public float[][][] returnNodeNormWeights(){
float[][][] weight = new float[node.length][node[0].length][node[0][0].weight.length];
for(int i = 0; i < node.length - 1; i ++){
for(int b = 0; b < node.length; b ++){
for(int a = 0; a < node.weight.length; a++){
weight[a] =node.weight[a];
}
}
}
return weight;
}
public float[][] returnOutNodeWeights(){
int length = node.length - 1;
float[][] nodes = new float[node[length].length][node[length][node[length].length - 1].weight.length];
for(int i = 0; i < node[length].length; i ++){
for(int b = 0; b < node[length][node[length].length - 1].weight.length; b++){
nodes = node[length].weight;
}
}
return nodes;
}
public float[] returnRanWeights(int amount){
Random a = new Random();
float[] weight = new float[amount];
for(int i = 0; i < amount; i ++){
weight = a.nextFloat() + a.nextFloat() - 1;
}
return weight;
}
public void CreateFromArrays(Node[][] Hidden, Node[] in, Node[] out){
this.node = Hidden;
this.in = in;
this.out = out;
}
public void createNetwork(){
Random a = new Random();
float w = a.nextFloat() + a.nextFloat() - 1;
for(int i = 0; i < in.length; i ++){
in = new Node(returnRanWeights(node[0].length));
}
for(int i = 0; i < node.length; i ++){
for(int b = 0; b < node.length; b ++){
if(i < node.length - 1){
node = new Node(returnRanWeights(node[i + 1].length));
}else{
node = new Node(returnRanWeights(out.length));
}
node.setBiasWeight(w);
}
}
for(int i = 0; i < out.length; i ++){
out = new Node(null);
}
}
public void input(float[] inp){
for(int i = 0; i < in.length; i++){
in.value = inp;
}
for(int i = 0; i < node.length; i ++){
for(int b = 0; b < node.length; b ++){
if(i == 0){
node.input(in, b);
}else{
node.input(node[i - 1], b);
}
}
}
for(int i = 0; i < out.length; i++){
out.input(node[node.length - 1], i);
}
}
public float getOutput(int num){
return out[num].getOutput();
}
public float[] getOutput(){
float[] a = new float[out.length];
for(int i = 0; i < a.length; i++){
a = getOutput(i);
}
return a;
}
}
package AI;
public class Node {public float[] weight;
public float value;
public float activation;
public final float e = 2.7183f;
public final float p = 0.5f;
public final float bias = 1.0f;
public float biasWeight = 0f;
public Node(float[] weight){
this.weight = weight;
}
public void setBiasWeight(float w){
biasWeight = w;
}
public float activationSigmoidMethod(float activation){
double a = -activation/p;
double b = e;
double c = Math.pow(e, a);
double e = 1 + c;
double f = 1/e;
return (float) f;
}
public void input(Node[] node, int num){
activation += bias*biasWeight;
for(int i = 0; i < node.length; i++){
activation += (node.value * node.weight[num]);
}
value = activationSigmoidMethod(activation);
activation = 0;
}
public float getOutput(){
return value;
}
}
Thanks In Advance.
Gen.