Why do these nested for loops not work?

I am currently working on training a Neural Network with a genetic algorithm. The DNA of the creatures are the weights of their Neural Networks. Every creature has its own Neural Network with two hidden layers. The whole code is excruciatingly long, but this is the part that is bugging for me. It is a function under the Creature class used to produce a new generation from the fittest creatures once a generation dies out. This is how the function looks:

Creature crossOver(Creature parent2) {  
    Creature child = new Creature();
    for(int i = 0; i<NeuralNetwork.layers.length; i++) {                               //for every layer of the network...//
      for(int j = 0; j<NeuralNetwork.layers[i].neurons.length; i++) {                  //for every neuron of the layer...//
        for(int k = 0; k<NeuralNetwork.layers[i].neurons[j].weights.length; k++) {     //for every weight of the neuron...//
          float determineInheritance = random(1);                                      //50/50 chance for every single weight to be inherited from parent 1 or from parent 2...//
          if(determineInheritance>=0.5) {
            child.NeuralNetwork.layers[i].neurons[j].weights[k] = NeuralNetwork.layers[i].neurons[j].weights[k];
          } else if(determineInheritance<0.5) {
            child.NeuralNetwork.layers[i].neurons[j].weights[k] = parent2.NeuralNetwork.layers[i].neurons[j].weights[k];
          }
        }
      }
    }
    return child;
  }

Unfortunately, I get an array out of bounds exception for the second for loop (the one with integer j). I don't understand why.

Answers

  • edited November 2017

    Note that every creature's network has one input layer with 48 input neurons, two hidden layers with 21 neurons each, and one output layer with 4 neurons. However, layer 0 is considered the first hidden layer, as the input layer simply provides inputs from the environment - it doesn't actually weigh it so technically it's not an actual neuron layer.

    I just added:

    println("Weight has been set for layer " + i + ", neuron " + j + ", weight " + k + ".");

    Under the final for loop. This is what appears in console:

    Weight has been set for layer 0, neuron 0, weight 0.

    Weight has been set for layer 0, neuron 0, weight 1.

    Weight has been set for layer 0, neuron 0, weight 2.

    Weight has been set for layer 0, neuron 0, weight 3.

    Weight has been set for layer 0, neuron 0, weight 4.

    Weight has been set for layer 0, neuron 0, weight 5.

    Weight has been set for layer 0, neuron 0, weight 6.

    Weight has been set for layer 0, neuron 0, weight 7.

    Weight has been set for layer 0, neuron 0, weight 8.

    Weight has been set for layer 0, neuron 0, weight 9.

    Weight has been set for layer 0, neuron 0, weight 10.

    Weight has been set for layer 0, neuron 0, weight 11.

    Weight has been set for layer 0, neuron 0, weight 12.

    Weight has been set for layer 0, neuron 0, weight 13.

    Weight has been set for layer 0, neuron 0, weight 14.

    Weight has been set for layer 0, neuron 0, weight 15.

    Weight has been set for layer 0, neuron 0, weight 16.

    Weight has been set for layer 0, neuron 0, weight 17.

    Weight has been set for layer 0, neuron 0, weight 18.

    Weight has been set for layer 0, neuron 0, weight 19.

    Weight has been set for layer 0, neuron 0, weight 20.

    Weight has been set for layer 0, neuron 0, weight 21.

    Weight has been set for layer 0, neuron 0, weight 22.

    Weight has been set for layer 0, neuron 0, weight 23.

    Weight has been set for layer 0, neuron 0, weight 24.

    Weight has been set for layer 0, neuron 0, weight 25.

    Weight has been set for layer 0, neuron 0, weight 26.

    Weight has been set for layer 0, neuron 0, weight 27.

    Weight has been set for layer 0, neuron 0, weight 28.

    Weight has been set for layer 0, neuron 0, weight 29.

    Weight has been set for layer 0, neuron 0, weight 30.

    Weight has been set for layer 0, neuron 0, weight 31.

    Weight has been set for layer 0, neuron 0, weight 32.

    Weight has been set for layer 0, neuron 0, weight 33.

    Weight has been set for layer 0, neuron 0, weight 34.

    Weight has been set for layer 0, neuron 0, weight 35.

    Weight has been set for layer 0, neuron 0, weight 36.

    Weight has been set for layer 0, neuron 0, weight 37.

    Weight has been set for layer 0, neuron 0, weight 38.

    Weight has been set for layer 0, neuron 0, weight 39.

    Weight has been set for layer 0, neuron 0, weight 40.

    Weight has been set for layer 0, neuron 0, weight 41.

    Weight has been set for layer 0, neuron 0, weight 42.

    Weight has been set for layer 0, neuron 0, weight 43.

    Weight has been set for layer 0, neuron 0, weight 44.

    Weight has been set for layer 0, neuron 0, weight 45.

    Weight has been set for layer 0, neuron 0, weight 46.

    Weight has been set for layer 0, neuron 0, weight 47.

    Weight has been set for layer 0, neuron 0, weight 48.

    Weight has been set for layer 1, neuron 0, weight 0.

    Weight has been set for layer 1, neuron 0, weight 1.

    Weight has been set for layer 1, neuron 0, weight 2.

    Weight has been set for layer 1, neuron 0, weight 3.

    Weight has been set for layer 1, neuron 0, weight 4.

    Weight has been set for layer 1, neuron 0, weight 5.

    Weight has been set for layer 1, neuron 0, weight 6.

    Weight has been set for layer 1, neuron 0, weight 7.

    Weight has been set for layer 1, neuron 0, weight 8.

    Weight has been set for layer 1, neuron 0, weight 9.

    Weight has been set for layer 1, neuron 0, weight 10.

    Weight has been set for layer 1, neuron 0, weight 11.

    Weight has been set for layer 1, neuron 0, weight 12.

    Weight has been set for layer 1, neuron 0, weight 13.

    Weight has been set for layer 1, neuron 0, weight 14.

    Weight has been set for layer 1, neuron 0, weight 15.

    Weight has been set for layer 1, neuron 0, weight 16.

    Weight has been set for layer 1, neuron 0, weight 17.

    Weight has been set for layer 1, neuron 0, weight 18.

    Weight has been set for layer 1, neuron 0, weight 19.

    Weight has been set for layer 1, neuron 0, weight 20.

    Weight has been set for layer 1, neuron 0, weight 21.

    Weight has been set for layer 2, neuron 0, weight 0.

    Weight has been set for layer 2, neuron 0, weight 1.

    Weight has been set for layer 2, neuron 0, weight 2.

    Weight has been set for layer 2, neuron 0, weight 3.

    Weight has been set for layer 2, neuron 0, weight 4.

    Weight has been set for layer 2, neuron 0, weight 5.

    Weight has been set for layer 2, neuron 0, weight 6.

    Weight has been set for layer 2, neuron 0, weight 7.

    Weight has been set for layer 2, neuron 0, weight 8.

    Weight has been set for layer 2, neuron 0, weight 9.

    Weight has been set for layer 2, neuron 0, weight 10.

    Weight has been set for layer 2, neuron 0, weight 11.

    Weight has been set for layer 2, neuron 0, weight 12.

    Weight has been set for layer 2, neuron 0, weight 13.

    Weight has been set for layer 2, neuron 0, weight 14.

    Weight has been set for layer 2, neuron 0, weight 15.

    Weight has been set for layer 2, neuron 0, weight 16.

    Weight has been set for layer 2, neuron 0, weight 17.

    Weight has been set for layer 2, neuron 0, weight 18.

    Weight has been set for layer 2, neuron 0, weight 19.

    Weight has been set for layer 2, neuron 0, weight 20.

    Weight has been set for layer 2, neuron 0, weight 21.

    Then, 'ArrayIndexOutOfBoundsException: 3".

  • @randomdude - your second for loop is incrementing i again instead of j!

Sign In or Register to comment.