/src/main/java/org/encog/examples/neural/xor/XORPSO.java

http://github.com/encog/encog-java-examples · Java · 70 lines · 29 code · 10 blank · 31 comment · 0 complexity · 3de7a0182fb082a4a026923d67c8ed97 MD5 · raw file

  1. /*
  2. * Encog(tm) Java Examples v3.4
  3. * http://www.heatonresearch.com/encog/
  4. * https://github.com/encog/encog-java-examples
  5. *
  6. * Copyright 2008-2017 Heaton Research, Inc.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * For more information on Heaton Research copyrights, licenses
  21. * and trademarks visit:
  22. * http://www.heatonresearch.com/copyright
  23. */
  24. package org.encog.examples.neural.xor;
  25. import org.encog.Encog;
  26. import org.encog.mathutil.randomize.NguyenWidrowRandomizer;
  27. import org.encog.mathutil.randomize.Randomizer;
  28. import org.encog.ml.CalculateScore;
  29. import org.encog.ml.data.MLDataSet;
  30. import org.encog.ml.data.basic.BasicMLDataSet;
  31. import org.encog.ml.train.MLTrain;
  32. import org.encog.neural.networks.BasicNetwork;
  33. import org.encog.neural.networks.training.TrainingSetScore;
  34. import org.encog.neural.networks.training.pso.NeuralPSO;
  35. import org.encog.util.simple.EncogUtility;
  36. /**
  37. * XOR-PSO: This example solves the classic XOR operator neural
  38. * network problem. However, it uses PSO training.
  39. *
  40. * @author $Author$
  41. * @version $Revision$
  42. */
  43. public class XORPSO {
  44. public static double XOR_INPUT[][] = { { 0.0, 0.0 }, { 1.0, 0.0 },
  45. { 0.0, 1.0 }, { 1.0, 1.0 } };
  46. public static double XOR_IDEAL[][] = { { 0.0 }, { 1.0 }, { 1.0 }, { 0.0 } };
  47. public static void main(final String args[]) {
  48. MLDataSet trainingSet = new BasicMLDataSet(XOR_INPUT, XOR_IDEAL);
  49. BasicNetwork network = EncogUtility.simpleFeedForward(2, 2, 0, 1, false);
  50. CalculateScore score = new TrainingSetScore(trainingSet);
  51. Randomizer randomizer = new NguyenWidrowRandomizer();
  52. final MLTrain train = new NeuralPSO(network,randomizer,score,20);
  53. EncogUtility.trainToError(train, 0.01);
  54. network = (BasicNetwork)train.getMethod();
  55. // test the neural network
  56. System.out.println("Neural Network Results:");
  57. EncogUtility.evaluate(network, trainingSet);
  58. Encog.getInstance().shutdown();
  59. }
  60. }