//package org.neuroph.samples;

import org.neuroph.core.NeuralNetwork;
import org.neuroph.nnet.Perceptron;
import org.neuroph.core.learning.TrainingSet;
import org.neuroph.core.learning.TrainingElement;
import org.neuroph.core.learning.SupervisedTrainingElement;
import java.util.Vector;

/**
* This sample shows how to create, train, save and load simple Perceptron neural network
*/
public class PerceptronSample {

    public static void main(String args[]) {

        // create training set (logical AND function)
        TrainingSet trainingSet = new TrainingSet();
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{0,0 }, new double[]{0}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{0,1}, new double[]{0}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{1,0 }, new double[]{0}));
        trainingSet.addElement(new SupervisedTrainingElement(new double[]{1,1}, new double[]{1}));
        

        // create perceptron neural network
        NeuralNetwork myPerceptron = new Perceptron(2, 1);

        // learn the training set
        myPerceptron.learnInSameThread(trainingSet);

        // test perceptron
        System.out.println("Testing trained perceptron");
        testNeuralNetwork(myPerceptron, trainingSet);

        // save trained perceptron
        myPerceptron.save("mySamplePerceptron.nnet");

        // load saved neural network
        NeuralNetwork loadedPerceptron = NeuralNetwork.load("mySamplePerceptron.nnet");

        // test loaded neural network
        System.out.println("Testing loaded perceptron");
        testNeuralNetwork(loadedPerceptron, trainingSet);

    }

    public static void testNeuralNetwork(NeuralNetwork nnet, TrainingSet tset) {

        for(TrainingElement trainingElement : tset.trainingElements()) {

            nnet.setInput(trainingElement.getInput());
            nnet.calculate();
            Vector<Double> networkOutput = nnet.getOutput();
            System.out.print("Input: " + trainingElement.getInput());
            System.out.println(" Output: " + networkOutput);

        }

    }

}