/*
 * twoPointCrossover.java
 *
 * Created on 7. leden 2006, 21:38
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package cz.pcland.ga.crossover;

import cz.pcland.ga.BaseIndividual;
import cz.pcland.ga.BinaryIndividual;
import cz.pcland.ga.MyRand;
import java.util.*;
/**
 *
 * @author petr
 */
public class twoPointCrossover implements CrossoverOpearator {
    
    /** Creates a new instance of twoPointCrossover */
    public twoPointCrossover() {
         
    }
    
    public String toString() {
        return "2 point crossover";
    }
    
    public BaseIndividual[] crossOver(BaseIndividual i1, BaseIndividual i2, int strLen) {
        BinaryIndividual binary1=(BinaryIndividual)i1;
        BinaryIndividual binary2=(BinaryIndividual)i2;
         int rand1 = Math.abs(MyRand.getInt()%(strLen));
         int rand2 = Math.abs(MyRand.getInt()%(strLen));
         if(rand1<rand2) {
         //    rand2++;
         }else {
             int tmp = rand1;
             rand1 = rand2;
             rand2 = tmp;
         }
         
         
         boolean[] str1 = new boolean[strLen];
         boolean[] str2 = new boolean[strLen];
         
         for(int i=0;i<strLen;i++) {
             if(i>=rand1 && i<=rand2) {
                 str1[i] = binary2.getString()[i];
                 str2[i] = binary1.getString()[i];
             }else {
                 str1[i] = binary1.getString()[i];
                 str2[i] = binary2.getString()[i];
             }
         }
         
        BinaryIndividual[] ret = {new BinaryIndividual(strLen), new BinaryIndividual(strLen)};
        ret[0].setString(str1);
        ret[1].setString(str2);
        
        return ret;
    }
    
}
