/*
 * BinaryString.java
 *
 * Created on November 2, 2005, 5:20 PM
 *
 * 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;

/**
 *
 * @author petr
 */


import java.util.*;

public class BinaryIndividual extends BaseIndividual {
    
        /**
         * Bit string of this individual
         */
	protected boolean[] string;
	
        
        /**
         * Prints bit string
         */
        public String toString(){
            String s="";
            for(int i=0;i<this.strLen;i++){
                if(string[i])s+="1"; else s+="0";
            }
            return s;
        }
        
        
        public BaseIndividual copy() {
             return new BinaryIndividual(this); 
	}
        

        public BinaryIndividual(boolean[] bits){
            super(bits.length);
            this.string = bits;
        }
	
	/* creates random binary coded individual */
	public BinaryIndividual(int len) {
            super(len);
		
            string = new boolean[len];
		
            for(int i=0;i<len;i++)
            {
                string[i] = MyRand.getBoolean();
            }
        }
        
        public void setString(boolean[] str){
            this.string = str;
        }
	
	/* Copy Constructor for reproduction*/
	public BinaryIndividual(BinaryIndividual i) {
		super(i.strLen);
		fitness = i.fitness;
		string = new boolean[i.strLen];
                
		for(int iter=0;iter<i.strLen;iter++) {
			string[iter] = i.string[iter];
		}
	}
	

	
	public void mutation(double mutationProb) {
            for(int i=0;i<strLen;i++){
		if(Math.random() < mutationProb) {
	                string[i] = !string[i];
	        }
            }	
	}
	
	public boolean[] getString() {
		return string;
	}
	
      

	
	
	
	
}

