/*
 * SimpleCrossover.java
 *
 * Created on 7. leden 2006, 21:33
 *
 * 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 SimpleCrossover implements CrossoverOpearator{
    
    /** Creates a new instance of SimpleCrossover */
    public SimpleCrossover() {
        
    }
    
    public  String toString() {
        return "Simple crossover";
    }
    
    public BaseIndividual[] crossOver(BaseIndividual i1, BaseIndividual i2, int strLen) {
        BinaryIndividual binary1=(BinaryIndividual)i1;
        BinaryIndividual binary2=(BinaryIndividual)i2;
        int position = Math.abs(MyRand.getInt())%strLen; //position = 0..strLen-1
        
        boolean[] str1 = new boolean[strLen];
        boolean[] str2 = new boolean[strLen];
        
        for(int i=0 ; i<position ; i++){
            str1[i]=binary1.getString()[i];
            str2[i]=binary2.getString()[i];
        }
        
        for(int i=position ; i<strLen ; i++){
            str2[i]=binary1.getString()[i];
            str1[i]=binary2.getString()[i];
        }
        
        BinaryIndividual[] ret = {new BinaryIndividual(strLen), new BinaryIndividual(strLen)};
        ret[0].setString(str1);
        ret[1].setString(str2);
        
        return ret;
    }
    
}
