/*
 * Encog Artificial Intelligence Framework v2.x
 * Java Examples
 * http://www.heatonresearch.com/encog/
 * http://code.google.com/p/encog-java/
 * 
 * Copyright 2008-2009, Heaton Research Inc., and individual contributors.
 * See the copyright.txt in the distribution for a full listing of 
 * individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

//package org.encog.examples.neural.xorresilient;

//import org.encog.neural.activation.ActivationSigmoid;
package fr.univ_lehavre.iwocs.m2.vortex.tourbillon;
import org.encog.engine.network.activation.ActivationSigmoid;
import org.encog.neural.data.NeuralData;
import org.encog.neural.data.NeuralDataPair;
import org.encog.neural.data.NeuralDataSet;
import org.encog.neural.data.basic.BasicNeuralDataSet;
import org.encog.neural.networks.BasicNetwork;
import org.encog.neural.networks.layers.BasicLayer;
import org.encog.neural.networks.training.Train;
import org.encog.neural.networks.training.propagation.resilient.ResilientPropagation;
import org.encog.util.logging.EncogLogging;

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

/**
 * XOR: This example is essentially the "Hello World" of neural network
 * programming.  This example shows how to construct an Encog neural
 * network to predict the output from the XOR operator.  This example
 * uses resilient propagation (RPROP) to train the neural network.
 * RPROP is the best general purpose supervised training method provided by
 * Encog.
 * 
 * For the XOR example with RPROP I use 4 hidden neurons.  XOR can get by on just
 * 2, but often the random numbers generated for the weights are not enough for
 * RPROP to actually find a solution.  RPROP can have issues on really small
 * neural networks, but 4 neurons seems to work just fine.
 */

// Lust
// Vanity
// Gluttony
// Wrath
// Greed
// -> Envy
// -> Sloth
public class bullesAnalyseur {

	public static double XOR_INPUT[][] = { { 0.0, 0.0 }, { 1.0, 0.0 },
			{ 0.0, 1.0 }, { 1.0, 1.0 } };

	public static double XOR_IDEAL[][] = { { 0.0 }, { 1.0 }, { 1.0 }, { 0.0 } };

	public static double formalize(String s){
		String sBase = s.substring(0, 9);
		Double sDouble = new Double(sBase);
		sDouble*=Math.pow(10,(new Double(s.substring(10))));
		//System.out.println(sDouble);
		return sDouble;
	}
	
	public static void main(final String args[]) {
		//System.out.println(formalize("2.2808500e-01"));
		
		
		Scanner sc=null;
		Scanner scWin=null;
		Scanner scFail=null;
		try{
			sc 		= new Scanner(new File("NORMA_50HZ_DT1-8_000090.txt"));
			scWin 	= new Scanner(new File("NORMA_50HZ_DT1-8_000090_tra.txt"));
			scFail 	= new Scanner(new File("fail.txt"));
		}catch(Exception e){
			e.printStackTrace();
		}
		
		ArrayList<Double[]> vectors = new ArrayList<Double[]>();
		ArrayList<Integer[]> vectorsWin = new ArrayList<Integer[]>();
		ArrayList<Integer[]> vectorsFail = new ArrayList<Integer[]>();
		
		//Lecture des trajectoires
		while(sc.hasNext()){
			Double[] values = new Double[3];
			//values[0] = formalize(sc.next());
			//values[1] = formalize(sc.next());
			//values[2] = formalize(sc.next());

			values[0] = new Double(sc.next());
			values[1] = new Double(sc.next());
			values[2] = new Double(sc.next());
			sc.next();
			sc.next();
			vectors.add(values.clone());
		}
		
		//Lecture des trajectoires validées
		while(scWin.hasNext()){
			Integer[] values = new Integer[6];
			values[0] = new Integer(scWin.next());
			values[1] = new Integer(scWin.next());
			values[2] = new Integer(scWin.next());
			values[3] = new Integer(scWin.next());
			values[4] = new Integer(scWin.next());
			values[5] = ((int)Math.random())*10+90;
			vectorsWin.add(values.clone());
		}
		
		//Lecture des trajectoires incorrectes
		while(scFail.hasNext()){
			Integer[] values = new Integer[6];
			values[0] = new Integer(scFail.next());
			values[1] = new Integer(scFail.next());
			values[2] = new Integer(scFail.next());
			values[3] = new Integer(scFail.next());
			values[4] = new Integer(scFail.next());
			values[5] = ((int)Math.random())*10;
			vectorsFail.add(values.clone());
		}
		
		ArrayList<Integer[]> trajectoires = new ArrayList<Integer[]>();
		for (Integer[] traj : vectorsWin){
			trajectoires.add(traj);
		}
		for (Integer[] traj : vectorsFail){
			trajectoires.add(traj);
		}
		Integer[][] trajectoiresTab = new Integer[trajectoires.size()][6];
		Iterator<Integer[]> trajIterator = trajectoires.iterator();
		int cpt=0;
		while (trajIterator.hasNext()) {
			trajectoiresTab[cpt++]=trajIterator.next();
		}
		// Ici, on a un tableau de trajectoires avec les 5 points et leur notes ; plus qu'à convertir
		// les 5 points en 15 coordonnées.
		
		System.out.println("Largeur : "+trajectoiresTab.length);
		//System.out.println("Longueur : "+trajectoiresTab[0].length);
		//XOR_INPUT =
		int lengthArray = vectors.size();
		XOR_INPUT = new double[lengthArray][3];
		int cpt=0;
		for(Iterator<Double[]> i = vectors.iterator(); i.hasNext();){
			if(cpt<XOR_INPUT.length){
				Double[] tmp = i.next();
				for(int j=0; j<3; j++){
					
						XOR_INPUT[cpt][j]=tmp[j];
						
				}
			}
			cpt++;
			
		}

		//Test 
		for(int i=0; i<XOR_INPUT.length; i++){
			for(int j=0; j<3; j++){
				System.out.println(XOR_INPUT[i][j]);
				
			}
			System.out.println(('\n'));
		}
		
		// todo fonction pour récupérer des sample de test. XOR[numLigne][value].
		// une fonction de test = 3 coordonnées * 5 points = 15 valeurs
		
		
		
		//Logging.stopConsoleLogging();
		
		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();

		/*NeuralDataSet trainingSet = new BasicNeuralDataSet(XOR_INPUT, XOR_IDEAL);
		
		// train the neural network
		final Train train = new ResilientPropagation(network, trainingSet);

		
		int epoch = 1;

		do {
			train.iteration();
			System.out
					.println("Epoch #" + epoch + " Error:" + train.getError());
			epoch++;
		} while(train.getError() > 0.01);

		// test the neural network
		System.out.println("Neural Network Results:");
		/*for(NeuralDataPair pair: trainingSet ) {
			final NeuralData 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));
		}*/
		
		sc.close();
		scWin.close();
		scFail.close();
		
	}
}

