GA fails with FeedfowardLogic
When creating Feedforward network with Feedforwardpattern, the default logic is set to SimpleRecurrentLogic. The default logic works with Genetic Algorithm. However, when the logic is set to Feedforward Logic using network.setLogic() method after the network has been generated from pattern, GA will fail with the following error:
Exception in thread "main" java.lang.NullPointerException
at org.encog.neural.networks.logic.FeedforwardLogic.compute(Unknown Source)
at org.encog.neural.networks.BasicNetwork.compute(Unknown Source)
at org.encog.neural.networks.BasicNetwork.calculateError(Unknown Source)
at org.encog.neural.networks.training.TrainingSetScore.calculateScore(Unknown Source)
at org.encog.neural.networks.training.genetic.GeneticScoreAdapter.calculateScore(Unknown Source)
at org.encog.solve.genetic.GeneticAlgorithm.calculateScore(Unknown Source)
at org.encog.neural.networks.training.genetic.NeuralGeneticAlgorithm.(Unknown Source)
This happens with Simulated Annealing too.




I was not able to. There are also no line numbers in your trace, so I can tell exactly what is happening. I created the following network.
BasicNetwork network = new BasicNetwork();
network.setLogic(new FeedforwardLogic());
network.addLayer(new BasicLayer(2));
network.addLayer(new BasicLayer(3));
network.addLayer(new BasicLayer(1));
network.getStructure().finalizeStructure();
network.reset();
I could train with a GA just fine. What lines of code are you using to create your network? I also tried modifying the feedforward pattern to use feedforward logic, and things seemed to work just fine.
Jeff
I am using feedforwardPattern to generate network. Below is what the code looks like. Note that if I comment the network.setLogic(new FeedforwardLogic()), the default logic will be simplerecurrentLogic which will work with GA. However, when the network logic is set to feeforward, I get the following exception.
Network Type: feedforward
Training Method: ga
Network input size: 4
Network output size: 1
Exception in thread "main" java.lang.NullPointerException
at org.encog.neural.networks.logic.FeedforwardLogic.compute(Unknown Source)
at org.encog.neural.networks.BasicNetwork.compute(Unknown Source)
at org.encog.neural.networks.BasicNetwork.calculateError(Unknown Source)
at org.encog.neural.networks.training.TrainingSetScore.calculateScore(Unknown Source)
at org.encog.neural.networks.training.genetic.GeneticScoreAdapter.calculateScore(Unknown Source)
at org.encog.solve.genetic.GeneticAlgorithm.calculateScore(Unknown Source)
at org.encog.neural.networks.training.genetic.NeuralGeneticAlgorithm.(Unknown Source)
FeedForwardPattern pattern = new FeedForwardPattern();
pattern.setInputNeurons(inputNeurons);
pattern.setOutputNeurons(outputNeurons);
pattern.setActivationFunction(activation);
BasicNetwork network = pattern.generate();
network.reset();
network.setLogic(new FeedforwardLogic());
return network;
You are changing the structure of the network and not calling finalizeStructure . This causes things to not get setup properly. Generally you create a neural network with Encog and the last thing you do before reseting it is call finalizeStructure. If I just change the logic after using the pattern to create it, I get the same NPE you got above.
If you use the following code it will work just fine.
network.setLogic(new FeedforwardLogic());
network.getStructure().finalizeStructure();
You just need to reinit the network with a call to finalizeStructure.