import java.util.*;

public class Gene1
{
int N;	//Number of Genes in the cromosome(independent variable, x)
double xN;//Limit value for integer
double xmin,xmax;
BitSet b=new BitSet(N);
double x;

public Gene(double ixmin,double ixmax,int iN)
{//randomly generate the gene
 N=iN;
 xN=Math.pow(2.0,N);
 xmin=ixmin;
 xmax=ixmax;
 setbits();	
}

public Gene1(Gene Father,Gene Mother)
{// create gene frome two parent genes
 // crossover process
//  Genes from Father and Mother should have the same limit values
xmin=Father.xmin;
xmax=Father.xmax; 
N=Father.N;
xN=Father.xN;
int ir1=int_random();
int ir2=int_random();
int ir3=int_random();
int i1,i2,i3;
i1=Math.min(ir1,Math.min(ir2,ir3));
i3=Math.max(ir1,Math.max(ir2,ir3));
if(ir1!=i1 && ir1!=i3) i2=ir1;
else if(ir2!=i1 && ir2!=i3) i2=ir2;
else i2=ir3; 
//System.out.println("i1="+i1+"i2="+i2+"i3="+i3);
   for(int i=0;i<i1;i++)
   {	   
      if(Father.b.get(i1)) {if(Father.b.get(i)) b.set(i);}
      else                {if(Mother.b.get(i)) b.set(i);} 
   }
   for(int i=i1+1;i<i2;i++)
   {	   
      if(Father.b.get(i2)) {if(Father.b.get(i)) b.set(i);}
      else                {if(Mother.b.get(i)) b.set(i);} 
   }
   for(int i=i2+1;i<i3;i++)
   {	   
      if(Father.b.get(i3)) {if(Father.b.get(i)) b.set(i);}
      else                {if(Mother.b.get(i)) b.set(i);} 
   }
   for(int i=i3+1;i<N;i++)
   {	   
      if(Father.b.get(N)) {if(Father.b.get(i)) b.set(i);}
      else                {if(Mother.b.get(i)) b.set(i);} 
   }
}

public double getX()
{
long y=1;
double r=xN;
long z=0;
for(int i=0;i<N;i++)
   {
   if(b.get(i)) {z+=y;}
   y*=2;
   }
 return (xmin+(xmax-xmin)/r*z);  
}  

public void clear()
{//fill the entire population with zero
b.clear(0,N);}

public void fill()
{//fill the entire population with ones
b.clear(0,N);b.flip(0,N);}

public String toString()
{
 return b.toString();
}

public boolean boolean_random()
{
if((int)(Math.round(Math.random()+0.01))==0) return false;
else     return true;
}

public int int_random()
{
  return (int)(Math.random()*N+1.0);
}

public void setbits()
{
for(int i=0;i<N;i++)
   {
   if(boolean_random()) b.set(i);
   }
}

public void mutate()
{ b.flip(int_random());}

}