import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//çıkış ve varış noktaları belirlenmemiş tur
public class TSP_gene1
{
public int n;
public double x[],y[];
public double Fitness;    // uyum değeri 
public double RFitness;   // relatif uyum
public double CFitness;   // kümülatif uyum
public boolean Breeder;

ArrayList<String> v;
String dizi;

public TSP_gene1(double xi[],double yi[])
{n=xi.length;
v=new ArrayList<String>(n);
x=new double[n];
y=new double[n];
Breeder=false;
for(int i=0;i<n;i++) {x[i]=xi[i];y[i]=yi[i];v.add(""+i);}
Collections.shuffle(v); 
mesafe();
}

public void mutate()
{
int n1=(int)(n*Math.random());
int n2=(n1+1)%n; 
//int n2=(int)(n*Math.random());
Collections.swap(v, n1, n2);	
}


public void swap()
{int pos[]=new int[n];
 int key[]=new int[n];
 int p,k;
 int n1=0;
for(int i=0;i<n;i++) 
{ p=Collections.binarySearch(v,""+i);
  if(p==i) {pos[i]=p;n1++;} 
}	
for(int i=0;i<n1;i+=2)
{Collections.swap(v,pos[i],pos[i+1]);}
}

public TSP_gene1(TSP_gene1 mother)
{n=mother.n;
 v=kopye(mother.v);
 x=new double[n];
 y=new double[n];
 for(int i=0;i<n;i++) {x[i]=mother.x[i];y[i]=mother.y[i];}
 mesafe();
}

public TSP_gene1(int a[],double xi[],double yi[])
{
ArrayList<String> mother=add(a);
v=kopye(mother);
Collection torba=v;
n=xi.length;
x=new double[n];
y=new double[n];
for(int i=0;i<n;i++) {x[i]=xi[i];y[i]=yi[i];}
mesafe();
}

public TSP_gene1(String dosya)
{double a[][]=Text.readDouble(dosya);
n=a[0].length;
v=new ArrayList<String>(n);
x=new double[n];
y=new double[n]; 
for(int i=0;i<n;i++) {x[i]=a[0][i];y[i]=a[1][i];v.add(""+i);}
Collections.shuffle(v);
mesafe();
}
	public TSP_gene1(TSP_gene1 mother, TSP_gene1 father)
    {
    n=mother.n;
    x=new double[n];
    y=new double[n];
    for(int i=0;i<n;i++)
    {x[i]=mother.x[i];y[i]=mother.y[i];}
    ArrayList<String> cocuk=s_cocuk(mother.v,father.v);
	v=kopye(cocuk);
    mesafe();
	}
	
public TSP_gene1(ArrayList<String> mother,double xi[],double yi[])
{
v=kopye(mother);
Collection torba=v;
n=xi.length;
x=new double[n];
y=new double[n];
for(int i=0;i<n;i++) {x[i]=xi[i];y[i]=yi[i];}
mesafe();
}

public void plot()
{
double xi[]=new double[n];
double yi[]=new double[n];
    int a=0;
    int b=0;
	String s1="";
	String s2="";
    Iterator i=v.iterator();
    int j=0;
    double mesafe=0;
    dizi="0";
    s1=(String)i.next();
    a=Integer.parseInt(s1); 
    xi[j]=x[a];
    yi[j]=y[a];
    j++;
    while(i.hasNext()) 
    {
    s2=(String)i.next();
    b=Integer.parseInt(s2);    
    mesafe+=Math.sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
    xi[j]=x[b];
    yi[j]=y[b];
    j++;
    a=b;
    }
    Plot pp=new Plot(xi,yi);
    double wx[]=new double[2];
    double wy[]=new double[2];
    wx[0]=xi[0];
    wy[0]=yi[0];
    wx[1]=xi[n-1];
    wy[1]=yi[n-1];
    pp.addData(wx,wy);
    pp.addData(xi,yi);
    pp.setPlotType(1,32);
    pp.setPlotType(2,22);
    pp.setPlabel("TSP problem distance = "+mesafe);
    pp.setXlabel("x, km");
    pp.setYlabel("y, km");
    pp.plot();
}

public double mesafe()
{
    int a=0;
    int b=0;
	String s1="";
	String s2="";
    Iterator i=v.iterator();
    int j=0;
    double mesafe;
    dizi="0";
    s1=(String)i.next();
    a=Integer.parseInt(s1);
    mesafe=0.0;
    while(i.hasNext()) 
    {
    s2=(String)i.next();
    b=Integer.parseInt(s2);
    mesafe+=Math.sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
    a=b;
    }
    Fitness=1.0/mesafe;
    return mesafe;	    
}

public void setFitness()
{Fitness=1.0/mesafe();}

public double getFitness()
{return Fitness;}

public void setRFitness(double RFitnessi)
{RFitness=RFitnessi;}

public double getRFitness()
{return RFitness;}

public void setCFitness(double CFitnessi)
{CFitness=CFitnessi;}

public double getCFitness()
{return CFitness;}
  public static String print(Collection c)
  {
    String s="";
    Iterator i=c.iterator();
    while(i.hasNext()) {s+=i.next()+"\n";}
    return s;
  }
  
  public String dizi()
  {return print(v);} 
    
  public ArrayList<String>kopye(ArrayList<String>list1)
    {
        ArrayList<String> bilesimVectoru = new ArrayList<String>();
        String s1,s2;
        Iterator n1=list1.iterator();
        while(n1.hasNext())
        {
            s1=(String)n1.next();
            bilesimVectoru.add(s1);
        }
        return bilesimVectoru;
    }
    
    public static ArrayList<String> add(int a[])
    {
        ArrayList<String> bilesimVectoru = new ArrayList<String>();
        String s1;
        for(int i=0;i<a.length;i++)
        {
            s1=""+a[i];
            if(!bilesimVectoru.contains(s1))
                bilesimVectoru.add(s1);
        }
        return bilesimVectoru;
    }
        
    public ArrayList<String> bilesim(ArrayList<String>list1, int a)
    {
        ArrayList<String> bilesimVectoru = new ArrayList<String>();
        String s1,s2;
        Iterator n1=list1.iterator();
        String b=new String(""+a);
        while(n1.hasNext())
        {
            s1=(String)n1.next();
            bilesimVectoru.add(s1);
        }
        s2=(String)b;
            if(!bilesimVectoru.contains(s2))
                bilesimVectoru.add(s2);
        return bilesimVectoru;
    }
    
    public ArrayList<String> bilesim(ArrayList<String> list1, ArrayList<String> list2)
    {
        ArrayList<String> bilesimVectoru = new ArrayList<String>();
        String s1,s2;
        Iterator n1=list1.iterator();
        Iterator n2=list2.iterator();
        while(n1.hasNext())
        {
            s1=(String)n1.next();
            bilesimVectoru.add(s1);
        }
        while(n2.hasNext())
        {
            s2=(String)n2.next();
            if(!bilesimVectoru.contains(s2))
                bilesimVectoru.add(s2);
        }
        return bilesimVectoru;
    }
    
    public  ArrayList<String> altküme(ArrayList<String> list1, int nn)
    {
        ArrayList<String> bilesimVectoru = new ArrayList<String>();
        String s1,s2;
        Iterator n1=list1.iterator();
        for(int i=0;i<nn;i++)
        {
            s1=(String)n1.next();
            bilesimVectoru.add(s1);
        }
        return bilesimVectoru;
    }
    
    public  ArrayList<String> kalanküme(ArrayList<String> list1, int nn)
    {
        ArrayList<String> bilesimVectoru = new ArrayList<String>();
        String s1,s2;
        Iterator n1=list1.iterator();
        
        for(int i=0;i<nn;i++)
        { s1=(String)n1.next();}
        
        while(n1.hasNext())
        {
            s1=(String)n1.next();
            bilesimVectoru.add(s1);
        }
        return bilesimVectoru;
    }
            
    public  ArrayList<String> s_cocuk(ArrayList<String> mother, ArrayList<String> father)
    {int nn=(int)(n*Math.random())+1;
    //System.out.println("nn="+nn);
	 ArrayList<String> mother1=altküme(mother,nn);
	 ArrayList<String> mother2=kalanküme(mother,nn);   
	 ArrayList<String> father1=altküme(father,nn);
	 ArrayList<String> father2=kalanküme(father,nn);
	 ArrayList<String> cocuk=bilesim(mother1,father1);
	 ArrayList<String> cocuk1=bilesim(cocuk,mother2);
	 ArrayList<String> cocuk2=bilesim(cocuk1,father2);
	 return cocuk2;    
	}
		
	public String ciktiS()
    {
        Iterator enm = v.iterator();
        String s="";
        int a=0;
        String s1="";
        while(enm.hasNext())
        {  s1=(String)enm.next();
           a=Integer.parseInt(s1);
	        s+=s1+" ("+x[a]+","+y[a]+") \n";
	    }
    return s;
    }
    
	public String[] ciktiSA()
    {
        Iterator enm = v.iterator();
        String s[]=new String[n];
        int i=0;
        while(enm.hasNext())
        {  s[i]= (String)enm.next()+" ";i++;}
    return s;
    }
    
    public String toString()
    {
	String s=ciktiS()+" mesafe = "+mesafe();
    return s;
    }
}
