package fr.univ_lehavre.iwocs.m2.vortex.tourbillon;
//import ReadFileTxt2Csv;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.encog.*;
import org.encog.ml.data.specific.CSVNeuralDataSet;
import org.encog.util.csv.CSVFormat;


import org.encog.engine.network.activation.ActivationSigmoid;
import org.encog.ml.data.MLDataSet;
import org.encog.ml.data.MLData;
import org.encog.ml.data.MLDataPair;
import org.encog.ml.data.basic.BasicMLDataSet;
import org.encog.ml.train.MLTrain;
import org.encog.neural.data.basic.BasicNeuralData;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.networks.layers.BasicLayer;
import org.encog.neural.networks.training.propagation.resilient.ResilientPropagation;

public class DataSetFeed {
	public static double data[][] = null;

	/**
	 * The ideal data necessary for XOR.
	 */
	public static double res[][] = null;
	
public static void main(String ...args) throws IOException
{
	BasicNetwork network=new BasicNetwork();
	network.addLayer(new BasicLayer(new ActivationSigmoid(),false,15));
	network.addLayer(new BasicLayer(new ActivationSigmoid(),false,4));
	network.addLayer(new BasicLayer(new ActivationSigmoid(),false,4));
	network.addLayer(new BasicLayer(new ActivationSigmoid(),false,1));
	network.getStructure().finalizeStructure();
	network.reset();
	
	 
	ReadFileTxt2Csv dat = new ReadFileTxt2Csv(args[0],args[1],args[2]);
	//ReadFileTxt2Csv rs =new ReadFileTxt2Csv(args[0],args[1],args[2]);
	int i=0;
	//Iterator it =new dat.Txt2Csv(args[1]).Iterator();
	//while (dat.Txt2DoubleArray(args[1])
			
	double[][] vectIn =(double[][]) dat.Txt2DoubleArray(args[1]).get(1);
	double[][] vectOut=(double[][]) dat.Txt2DoubleArray(args[2]).get(1);
	//data=vectIn;
	//dat.Test(args[0],args[1],args[2]);
	res=vectOut;
	//double[] res= {};
	//BasicNeuralData dataIn= new BasicNeuralData(data);
	//BasicNeuralData dataIdeal= new BasicNeuralData(res);
	try {	
	MLDataSet trainingSet = new BasicMLDataSet (data,res) ;
	MLTrain train=new ResilientPropagation(network,trainingSet);
	
	//trainingSet.add(dataIn, dataIdeal);
	
	
	int epoch=1;
	do{
	train.iteration();
	System.out.println("Test#"+epoch+", Error "+train.getError());
	epoch++;
	}while(train.getError()>0.001);
	
	
	System.out.println("NeuralNetworkResults:");
	for(MLDataPair pair:trainingSet){
	 MLData output= network.compute(pair.getInput());
	System.out.println(pair.getInput().getData(0)
	+","+pair.getInput().getData(1)
	+",actual="+output.getData(0)+
	",ideal="+ pair.getIdeal().getData(0));
	}
	}catch(Exception e) {
		e.printStackTrace();
	}
	}
}
