Newer
Older
abgabensammlungSS15 / ea / Ub7 / PBIL / src / PBilIndividual.java
@Jan-Peter Hohloch Jan-Peter Hohloch on 15 Jun 2015 5 KB EA: fix code
//import tools.RandomNumberGenerator;

import java.util.BitSet;
import java.util.Random;

/**
 * @author Maximus
 *
 */
public class PBilIndividual implements Comparable<PBilIndividual> {
	protected BitSet m_Genotype;
	protected int m_GenotypeLength;
	protected Random r = new Random();

	/** Constructor **/
	public PBilIndividual(double[] propVector) {
		// This method should call initGenotype() to initialize the genotype
		this.m_GenotypeLength = propVector.length;
		initGenotype(propVector);

	}

	/**
	 * cloning constructor
	 * 
	 * @param toClone
	 */
	private PBilIndividual(PBilIndividual toClone) {
		this.m_Genotype = (BitSet) toClone.m_Genotype.clone();// allowed, has
																// only native
																// typed
																// fields
		this.m_GenotypeLength = toClone.m_GenotypeLength;
	}

	/**
	 * This method creates a deep copy of an individual. It should clone all
	 * objects contained by this object.
	 * 
	 * @return An deep copy of this {@link PBilIndividual}
	 */
	@Override
	public Object clone() {

		PBilIndividual c = new PBilIndividual(new double[m_GenotypeLength]);
		c.setGenotype((BitSet) this.m_Genotype.clone());
		return c;

		// return new GAIndividual(this);
	}

	/**
	 * This method evaluates the GAIndividual as a simple
	 * "maximize number of bits" problem. The fitness is the number of true bits
	 * in m_Genotype. Best fitness is reached if there are no false bits.
	 * 
	 * @return The number of false bits (less is better!)
	 */
	public double evaluateAsMaxiBits() {
		int zeros = 0;
		for (int i = 0; i < m_GenotypeLength; i++) {
			if (!m_Genotype.get(i)) {
				zeros++;
			}
		}
		return zeros;
	}

	/**
	 * 
	 * @return best fitness ==0
	 */
	public double evaluateAsTwin() {
		int f1 = 0;
		int f2 = 0;
		for (int i = 0; i < (m_GenotypeLength / 2); i++) {
			if (m_Genotype.get(i)) {
				f1++;
			}
		}
		for (int i = (m_GenotypeLength / 2); i < m_GenotypeLength; i++) {
			if (m_Genotype.get(i)) {
				f2++;
			}
		}
		return (m_GenotypeLength / 2) - Math.abs(f1 - f2);
	}

	/**
	 * This method will return a string description of the GAIndividal notably
	 * the genotype: '0011000101'
	 * 
	 * @return A descriptive string
	 */
	public String getStringRepresentation() {
		// this should return exactly the representation shown in the comment
		// above:
		// only 0 (for false bits) and 1 (for true bits) with no extra white
		// space!
		String representation = "";
		for (int i = 0; i < m_GenotypeLength; i++) {
			representation = m_Genotype.get(i) ? representation + "1"
					: representation + "0";

		}
		return representation;
	}

	/**
	 * This method will allow the user to read the GA genotype
	 * 
	 * @return BitSet
	 */
	public BitSet getGenotype() {
		return m_Genotype;
	}

	/**
	 * This method will allow the user to set the current GA genotype. Should
	 * check if the length of the BitSet and the genotype length match.
	 * 
	 * @param b
	 *            The new genotype of the Individual
	 */
	public void setGenotype(BitSet b) {

		this.m_Genotype = b;

	}

	/**
	 * This method allows the user to read the length of the genotype. This may
	 * be necessary since BitSet.length only returns the index of the last
	 * significant bit.
	 * 
	 * @return The length of the genotype.
	 */
	public int getGenotypeLength() {
		return m_GenotypeLength;
	}

	// /**
	// * This method performs a simple one point mutation in the genotype
	// (script
	// * 5.2.2). Please use the tools.RandomNumberGenerator
	// */
	// public void mutate() {
	// m_Genotype.flip(RandomNumberGenerator
	// .randomInt(0, m_GenotypeLength - 1));
	// }

	// /**
	// * This method performs a simple one point crossover of two GAIndividuals
	// * (script 5.2.1). Please use the tools.RandomNumberGenerator
	// *
	// * @return An array of length 2 with the two resulting GAIndivuals
	// */
	// public static PBilIndividual[] crossover(PBilIndividual ind1,
	// PBilIndividual ind2) {
	// assert ind1.m_GenotypeLength == ind2.m_GenotypeLength;
	//
	// int splitPoint = RandomNumberGenerator.randomInt(0,
	// ind1.m_GenotypeLength - 1);
	// PBilIndividual ind1Clone = (PBilIndividual) ind1.clone();
	// PBilIndividual ind2Clone = (PBilIndividual) ind2.clone();
	// for (int i = splitPoint; i < ind1Clone.m_GenotypeLength; i++) {
	// ind1Clone.m_Genotype.set(i, ind2.m_Genotype.get(i));
	// ind2Clone.m_Genotype.set(i, ind1.m_Genotype.get(i));
	//
	// }
	// System.out.println("______");
	// System.out.println(ind1.getStringRepresentation());
	// System.out.println(ind2.getStringRepresentation());
	// System.out.println(splitPoint);
	// System.out.println(ind1Clone.getStringRepresentation());
	// System.out.println(ind2Clone.getStringRepresentation());

	// return new PBilIndividual[] { ind2Clone, ind1Clone };
	// }

	/**
	 * This method initializes the GA genotype randomly. Please use the
	 * tools.RandomNumberGenerator
	 * 
	 * @param propVector
	 */
	public void initGenotype(double[] propVector) {
		m_Genotype = new BitSet(m_GenotypeLength);
		for (int i = 0; i < m_GenotypeLength; i++) {
			if (r.nextDouble() < propVector[i]) {
				m_Genotype.set(i);
			}
		}
	}

	@Override
	public int compareTo(PBilIndividual o) {
		return ((Double) this.evaluateAsMaxiBits()).compareTo(o
				.evaluateAsMaxiBits());
	}
}