Neural Network Genome Help Please :'(

Started by
12 comments, last by CryoGenesis 12 years ago
Hello, Sorry for another post (the other post's title didn't fit right).
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 smile.png


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.
Advertisement
Look at "neuralNullPointer2.jpg". There you try to access a weight array thats not initialized.

More null pointer issues are shown in "neuralNullPointer1.jpg".

Please: try to use a runtime debugger!

good luck!

Look at "neuralNullPointer2.jpg". There you try to access a weight array thats not initialized.

More null pointer issues are shown in "neuralNullPointer1.jpg".

Please: try to use a runtime debugger!

good luck!


Oh thanks, never knew I could do that.
Cheers!

Oh thanks, never knew I could do that.
Cheers!


Sorry for the lack of explanation. I was in hurry.

I dont know about your skills in neural networks.
Maybe I am wrong but you maybe need some more experience in programming techniques to program such complex systems.

I suggest you get "Eclipse IDE for Java Developers", thats my favourite IDE when I program in Java: You can get it from "http://www.eclipse.org/downloads/".
i ve heared also that "NetBeans IDE" should be good. I never tried it.

Learn how to use it an learn how to use the debug function.

Good luck!

Joe

[quote name='CryoGenesis' timestamp='1332345867' post='4923970']
Oh thanks, never knew I could do that.
Cheers!


Sorry for the lack of explanation. I was in hurry.

I dont know about your skills in neural networks.
Maybe I am wrong but you maybe need some more experience in programming techniques to program such complex systems.

I suggest you get "Eclipse IDE for Java Developers", thats my favourite IDE when I program in Java: You can get it from "http://www.eclipse.org/downloads/".
i ve heared also that "NetBeans IDE" should be good. I never tried it.

Learn how to use it an learn how to use the debug function.

Good luck!

Joe
[/quote]

I have Eclipse and I believe my knowledge of neural networks is good.
The neural network that I have programmed works fine but unfortunately I can't seem to merge two seperate neural networks.
I'm doing this in the first place so the neural networks wont need any backpropagation algorithm. The neural networks are trained using a genetic algorithm.
In doing this I can get creatures to search for food.
The most successful creature gets to reproduce.
The creatures that reproduce with each other merge each others networks + any random mutation that happens.

Would you know how to merge to seperate networks because that is the problem I am having :/.
First I tried copying the weight floats themselves but now I tried to split the nodes instead and I am getting problems with copying pre initialised weights.

I'm doing this in the first place so the neural networks wont need any backpropagation algorithm.


I dont get the point of this. What has this to do with GA?


The most successful creature gets to reproduce.


Whats about the almost "most successful creatures"? Are they dont worth the try?



Would you know how to merge to seperate networks because that is the problem I am having :/.
First I tried copying the weight floats themselves but now I tried to split the nodes instead and I am getting problems with copying pre initialised weights.



Oh man! Thats hard to read. You dont use any translator, are you?

Anyway:

I am not very familiar with genetic algorithms. But as far as i know it goes like this:
1. make a population of neural networks, generate the weights randomly (there are also several algorithms to produce optimized weights for quicker results, buts not necessary)
2. calculate the performance( cost factor or productivity or waht ever you want to call it) - factor for each network.
3. define a gene (binary or float,(stochastic?)) for each network.
4. choose some networks with the highest performance.
5. combine them by their genes and make new population of networks out of it.
6. GOTO: 1{ UNTIL CPU REACHES MAX_TEMPERATURE OR WIFE GETS HOME;}

So I think your problem may be is 2,3 and or 5.

If you have a constant network size, then I would prefer combining the nets by their weights.

J
Can you describe where your problem is, in terms of my latter post?

[quote name='CryoGenesis' timestamp='1332540291' post='4924764']
I'm doing this in the first place so the neural networks wont need any backpropagation algorithm.


I dont get the point of this. What has this to do with GA?


The most successful creature gets to reproduce.


Whats about the almost "most successful creatures"? Are they dont worth the try?


Would you know how to merge to seperate networks because that is the problem I am having :/.
First I tried copying the weight floats themselves but now I tried to split the nodes instead and I am getting problems with copying pre initialised weights.



Oh man! Thats hard to read. You dont use any translator, are you?

Anyway:

I am not very familiar with genetic algorithms. But as far as i know it goes like this:
1. make a population of neural networks, generate the weights randomly (there are also several algorithms to produce optimized weights for quicker results, buts not necessary)
2. calculate the performance( cost factor or productivity or waht ever you want to call it) - factor for each network.
3. define a gene (binary or float,(stochastic?)) for each network.
4. choose some networks with the highest performance.
5. combine them by their genes and make new population of networks out of it.
6. GOTO: 1{ UNTIL CPU REACHES MAX_TEMPERATURE OR WIFE GETS HOME;}

So I think your problem may be is 2,3 and or 5.

If you have a constant network size, then I would prefer combining the nets by their weights.

J
[/quote]

The whole point is to make a program that can has creatures that can learn.
The way the creatures would reproduce is based on their fitness level. The higher the fitness level, the more times they reproduce (a creature can reproduce more than once each generation).
Each creature has a Genome.
Each genome holds the information to the creatures Neural Network (Brain).
Every time the creature reproduces the two creature's Genomes split into two pieces (the size of the two pieces is a random integer).
This then creates a new Genome which is passed on to the next generation.

The problem is that I cannot find a way to decode the Neural Network into the Genome then merge two genomes to create a new Neural Network.
So my problem is Number 5.
Hello Gen

Is it possible to consider the list of weights as the genome itself ?
So you can alter and combine these lists to get new altered or combined genomes.
Honestly I don't know how to combine two genomes here, but I would first try to do some kind of average of genomes ?

Good luck smile.png
Nico

This topic is closed to new replies.

Advertisement