//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 PerceptronSample2 {

    public static void main(String args[]) {

        // create training set (logical NOT function)
        //TrainingSet trainingSet = new TrainingSet();
        double a[][]=Text.readDoubleT("not.txt");
        int n=a.length;    // total number of in and outs
        int m=a[0].length; //number of traininng data
        int in=1;
        int out=1;
        // Supervised training  
        TrainingSet trainingSet=new TrainingSet();
        for(int j=0;j<m;j++)
        { double xi[]=new double[in];
       double yi[]=new double[out];
String s1="";	
for(int i=0;i<in;i++)
{ xi[i]=a[i][j];}
for(int i=in;i<(in+out);i++)
{ yi[i-in]=a[i][j];	
System.out.println("i="+(i-in)+"j="+j+"xi="+xi[i-in]+"yi="+yi[i-in]);
}
trainingSet.addElement(new SupervisedTrainingElement(xi, yi));
}
        
        
        
//       trainingSet.addElement(new SupervisedTrainingElement(new double[]{0}, new double[]{0}));
//       trainingSet.addElement(new SupervisedTrainingElement(new double[]{1}, new double[]{1}));

        // create perceptron neural network
        NeuralNetwork myPerceptron = new Perceptron(in, out);

        // 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);

        }

    }

}