import java.io.Serializable;
import java.util.Vector;
import org.neuroph.core.*;
import org.neuroph.core.input.*;
import org.neuroph.core.learning.*;
import org.neuroph.core.transfer.*;
import org.neuroph.util.*;
import org.neuroph.util.plugins.*;
import org.neuroph.nnet.*;
import java.util.*;
import javax.swing.JOptionPane;

public class neural
{
NeuralNetwork neuralNetwork;
int in;
int ara;
int out;
public neural(String nwt,String tft,String train_name,int ini,int arai,int outi)
{
//in  : number of input parameters
//ara : number of middle parameters
//out : number of output parameters
// tft 

// nwt Network type
// options:
// ==================
// "Adeline"
// "Perceptron"
// "MLP"   or "MultiLayerPerceptron"
// "Hopfield"
// "BAM"
// "Kohonen";
// "SupervisedHebbian"
// "UnsupervisedHebbian"
// "MaxNet"
// "Instar"
// "Outstar"
// "Competitive"
// "Rbf"
// ==================
//train_name : training folder (tmp file contains in+out columns)
// Transfer function type 
// "LINEAR"
// "RAMP"
// "STEP"
// "SIGMOID"
// "TANH"
// "GAUSSIAN"
// "TRAPEZOID"
// "SGN"
in=ini;
ara=arai;
out=outi;
TransferFunctionType transferfunctiontype;
double a[][]=Text.readDoubleT(train_name);
int n=a.length;    // total number of in and outs
int m=a[0].length; //number of traininng data
System.out.println("n="+n+"m="+m);
if(tft.equals("Step") || tft.equals("STEP")) transferfunctiontype=TransferFunctionType.STEP;
else if(tft.equals("Linear") || tft.equals("LINEAR")) transferfunctiontype=TransferFunctionType.LINEAR;
else if(tft.equals("Ramp") || tft.equals("RAMP")) transferfunctiontype=TransferFunctionType.RAMP;
else if(tft.equals("Sigmoid") || tft.equals("SIGMOID")) transferfunctiontype=TransferFunctionType.SIGMOID;
else if(tft.equals("Tanh") || tft.equals("TANH")) transferfunctiontype=TransferFunctionType.TANH;
else if(tft.equals("Gaussian") || tft.equals("GAUSSIAN")) transferfunctiontype=TransferFunctionType.GAUSSIAN;
else if(tft.equals("Trapezoid") || tft.equals("TRAPEZOID")) transferfunctiontype=TransferFunctionType.TRAPEZOID;
else if(tft.equals("Sgn") || tft.equals("SGN")) transferfunctiontype=TransferFunctionType.SGN;
else transferfunctiontype=TransferFunctionType.STEP;

if(nwt.equals("Adeline")) neuralNetwork=new Adaline(in);
else if(nwt.equals("Perceptron")) neuralNetwork=new Perceptron(in,out,transferfunctiontype); 
//else if(nwt.equals("MLP") || nwt.equals("MultiLayerPerceptron")) 
//{String s=""+in+" "+ara+" "+out;	
//neuralNetwork=new MultiLayerPerceptron(s,transferfunctiontype); 
//}
else if(nwt.equals("Hopfield")) neuralNetwork=new Hopfield(in);
else if(nwt.equals("BAM")) neuralNetwork=new BAM(in,out);
else if(nwt.equals("Kohonen")) neuralNetwork=new Kohonen(in,out);
else if(nwt.equals("SupervisedHebbian")) neuralNetwork=new SupervisedHebbianNetwork(in,out,transferfunctiontype); 	
else if(nwt.equals("UnsupervisedHebbian")) neuralNetwork=new UnsupervisedHebbianNetwork(in,out,transferfunctiontype); 	
else if(nwt.equals("MaxNet")) neuralNetwork=new MaxNet(in);
else if(nwt.equals("Instar")) neuralNetwork=new Instar(in);
else if(nwt.equals("Outstar")) neuralNetwork=new Instar(in); 	
else if(nwt.equals("Compatitive")) neuralNetwork=new CompetitiveNetwork(in,out); 	
else if(nwt.equals("Rbf")) neuralNetwork=new RbfNetwork(in,ara,out); 	
else neuralNetwork=new Perceptron(in,out,transferfunctiontype);
//Supervised training  
TrainingSet trainingSet=new TrainingSet();
double xi[]=new double[in];
double yi[]=new double[out];
for(int j=0;j<m;j++)
{
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));
}
        // learn the training set
        neuralNetwork.learnInSameThread(trainingSet);

        // test perceptron
        System.out.println("Testing trained perceptron");
        testNeuralNetwork(neuralNetwork, trainingSet);
        
//neuralNetwork.learn(trainingSet);
neuralNetwork.save("perceptron.nnet");
}

public void cikti(double xi[])
{
     Vector <Double> networkInput = new Vector<Double> ();
     //Double x0,x1;
     //x0=new Double(JOptionPane.showInputDialog(" ilk girdi : "));
     //x1=new Double(JOptionPane.showInputDialog(" ikinci girdi : "));
     for(int i=0;i<xi.length;i++) networkInput.add(new Double(xi[i])); 
     //networkInput.add(x0);
     //networkInput.add(x1);
     // set network input
     neuralNetwork.setInput(networkInput);
     // calculate network
     neuralNetwork.calculate();
     // get network output
     Vector <Double> networkOutput = neuralNetwork.getOutput();
     Iterator i=networkOutput.iterator();
     String ss="";
     Double p;     
     while(i.hasNext()) {p = (Double)(i.next());System.out.println(p.doubleValue());ss+=p.toString();}
     JOptionPane.showMessageDialog(null,ss);
}

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

        }

    }
        
public void cikti()
{
     Vector <Double> networkInput = new Vector<Double> ();
     double xi[]=new double[2];
     xi[0]=new Double(JOptionPane.showInputDialog(" ilk girdi : "));
     xi[1]=new Double(JOptionPane.showInputDialog(" ikinci girdi : "));
     for(int i=0;i<2;i++) networkInput.add(new Double(xi[i])); 
     //networkInput.add(x0);
     //networkInput.add(x1);
     // set network input
     neuralNetwork.setInput(networkInput);
     // calculate network
     neuralNetwork.calculate();
     // get network output
     Vector <Double> networkOutput = neuralNetwork.getOutput();
     Iterator i=networkOutput.iterator();
     String ss="";
     Double p;
     while(i.hasNext()) {p = (Double)(i.next());ss+=p.toString();}
     JOptionPane.showMessageDialog(null,ss);
}

/*        
public double[][] cikti(double xii[][])
{    
     double xi[][]=Text.T(xii);
     int n=xi.length;  //girdi sayısı
     int m=xi[0].length; //veri sayısı
     int np1=n+1;
     double yi[][]=new double[np1][m];
     for(int j=0;j<m;j++)
     {
     Vector <Double> networkInput = new Vector<Double> ();
     for(int i=0;i<n;i++)
     {Double xx=new Double(xi[i][j]); 
     //System.out.print("i="+i+"xx="+xx);   
     networkInput.add(xx);
     }
     //System.out.println(); 
     // set network input
     neuralNetwork.setInput(networkInput);
     // calculate network
     neuralNetwork.calculate();
     // get network output
     Vector <Double> networkOutput = neuralNetwork.getOutput();
     Iterator iter=networkOutput.iterator();
     for(int i=0;i<n;i++)
     {yi[i][j]=xi[i][j];
     System.out.print(yi[i][j]+" ");
     }
     Double p;
     for(int i=n;i<(in+out);i++)
     {p = (Double)(iter.next());
      yi[i][j]=p.doubleValue();
      System.out.println(yi[i][j]+" ");
     }
     }
     return yi;
}
*/

public static void main(String arg[])
{
neural nn=new neural("Perceptron","LINEAR","or.txt",1,1,1);
double xi[]={223.15};
nn.cikti(xi);

}

}
