import java.io.*;
import javax.swing.*;

      class fa extends f_xj
      {  
	  double xi[]; // independent variable data set
      double yi[]; //dependent variable data set
      double a[];  //fit function coefficient set
      int nn;
  	  public fa(String filename,double ia[])
      {
        //read the data to curvefit
        //get the data file and initial fit coefficient when class is defined
	    xi=new double[500];
        yi=new double[500];
        int n=ia.length;
        a=new double[n];
        seta(ia);
        int i=-1;
        try{
	    BufferedReader fin=new BufferedReader(new FileReader(filename));
        try {
        while(fin != null)
        {
        i++;
        xi[i]=Text.readDouble(fin);
        yi[i]=Text.readDouble(fin);
        }
        } catch(EOFException e_eof) {System.out.println("end of file"); }
        }catch(IOException e_io) {System.out.println("dosya bulunamadı"); }        
      nn=i;
      a=ia;
      }
      
      public void seta(double ia[])
      {
	      //assign new fit coefficient set 
	      for(int ii=0;ii<nn;ii++)
              a[ii]=ia[ii];
	  }

	  public double[] geta()
      {
	     // return fit coefficient set
	     return a;
	  }
	  
	  double Ps(double x,double ai[])
      {
      //sample specific function 
	  // saturation pressure as a function of saturation temperature
      a=ai;
      double pi=ai[0]*(1.0-Math.exp(-ai[1]*x));
      //pi=Math.pow(10.0,pi);
      return pi; //kPa
      }      
      
      public double func(double ai[])
      {
      double ff=0;
      double w;
      double yy;
       for(int i=0;i<nn;i++)
	    {w=Ps(xi[i],ai);
	    yy=yi[i];
        //System.out.println("x="+xi[i]+"w="+w+"yy="+yy+"ff="+ff);
	    w-=yy; 
        ff+=w*w;
        //System.out.println("x="+xi[i]+"w="+w+"yy="+yy+"ff="+ff); 
	    }
      return ff;
	  }
}

class fb extends fa
{     public fb(String filename,double ia[])
      {super(filename,ia);}
      public double func(double ai[])
      {
      return -super.func(ai);
	  }
}

class OPO19D
{
    public static void main(String args[]) throws IOException
    {
    int N=63;
	int POPSIZE=200;
    int MAXGENS=200;
    int NVARS=2;
    double PXOVER=0.3;
    double PMUTATION=0.02;
    String in_name="a.txt";
    
    Genetic1 xx=new Genetic1(POPSIZE,MAXGENS,NVARS,N,PXOVER,PMUTATION);
    double low[]=new double[NVARS];
    double high[]=new double[NVARS];
    low[0]=30.0;high[0]=50.0;
    low[1]=0.0;high[1]=1.0;
    
    double a[]=new double[NVARS];
    fb fbx=new fb("a.txt",a);
    xx.setPopulation(low,high,fbx);
    a=xx.calculate(fbx,false);
    double x0[]=new double[NVARS];
    for(int i=0;i<NVARS;i++) x0[i]=a[i];
    String s="genetik algoritma sonucu =\n"+Matrix.toStringT(a);
     String s2="Genetik algoritma  çok değişkenli fonksiyon optimizasyonu:";
     JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE);     
    }
}
