import java.io.*;
import java.text.*;
import java.util.Locale;
import java.io.*;
import javax.swing.JOptionPane;


class fa extends f_xj
{
//çözümü istenen fonksiyon	
 public double func(double x[])
 {
    double ff= (x[0]-2.0)*(x[0]-2.0)+(x[1]-3.0)*(x[1]-3.0);
    return ff; 
}
}

public class Montecarlo
{
    public static double[] montekarlo(f_xj f,double xmin[],double xmax[],int maxit)
    {
	int n=xmin.length;    
	double x[]=new double[n];
	double x1[]=new double[n];
	double dx[]=new double[n];
	double best[]=new double[n];
	double fbest=0;
	//randomly select points
	int mi=0;
	double pdf=0;
	double ff,f1,df;
	f1=0;
	for(int i=0;i<n;i++)
	{ x[i]=xmin[i]+(xmax[i]-xmin[i])*Math.random();if(i==0) {best[i]=x[i];fbest=f.func(best);}}
	ff=f.func(x);
	while(mi<maxit)
	{
    for(int i=0;i<n;i++)
	{ x1[i]=xmin[i]+(xmax[i]-xmin[i])*Math.random();}
	if(f1<fbest) {fbest=f1;best=x1;}
	f1=f.func(x);
	df=f1-ff;
	if(df<0) {x=x1;ff=f1;}
	mi++;
    }
    return best;
    }
    
	
    public static double  gaussrandom()
    { int iset=0;
    double gset=0;
    double fac,rsq,v1,v2;
    double aret=0;
    if(iset==0)
    {
     do{
	 v1=2.0*Math.random()-1.0;
	 v2=2.0*Math.random()-1.0;
	 rsq=v1*v1+v2*v2;  
     }while(rsq>=1.0 || rsq==0.0);	   
    fac=Math.sqrt(-2.0*Math.log(rsq)/rsq);
    gset=v1*fac;
    iset=1;
    aret=v2*fac;
    }
    else
    {
    iset=0;
    aret=gset;
    }
    return aret;	
    }

    public static void main(String args[]) throws IOException
    {
	double low[]=new double[2];
	double high[]=new double[2];
    low[0]=-10.0;high[0]=10.0;
    low[1]=-10.0;high[1]=10.0;
    fa fax=new fa();
   
    double a[]=montekarlo(fax,low,high,10000000);
    String s="simulated annealing (tavlama simulasyonu) metodu sonucu =\n"+Matrix.toStringT(a);
    String s2="simulated annealing :";
    JOptionPane.showMessageDialog(null,s,s2,JOptionPane.PLAIN_MESSAGE);     
    }
}