// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   SimpleCrossover.java

package drm.genetics.simple;

import drm.genetics.AbstractCrossover;
import drm.genetics.Individual;
import java.io.PrintStream;
import java.util.Random;

public class SimpleCrossover extends AbstractCrossover
{

	public boolean DEBUG_CROSS;

	public SimpleCrossover()
	{
		DEBUG_CROSS = false;
	}

	public SimpleCrossover(int type)
	{
		super(type);
		DEBUG_CROSS = false;
	}

	public void crossSame(Individual individ1, Individual individ2)
	{
		int size = Math.min(individ1.accessGenome().length, individ2.accessGenome().length);
		drm.genetics.Gene newGenom[] = individ1.getGenome();
		drm.genetics.Gene gen1[] = individ1.accessGenome();
		drm.genetics.Gene gen2[] = individ2.accessGenome();
		int debug[] = new int[size];
		switch(type)
		{
		case 1: // '\001'
			int p = random.nextInt(size);
			System.arraycopy(gen2, p, newGenom, p, size - p);
			System.arraycopy(gen1, p, gen2, p, size - p);
			if(DEBUG_CROSS)
			{
				for(int i = p; i < size; i++)
					debug[i] = 1;

			}
			break;

		case 2: // '\002'
			int p1 = random.nextInt(size);
			int p2 = random.nextInt(size);
			int pt = Math.min(p1, p2);
			p2 = Math.max(p1, p2);
			p1 = pt;
			System.arraycopy(gen2, p1, newGenom, p1, (p2 - p1) + 1);
			System.arraycopy(gen1, p1, gen2, p1, (p2 - p1) + 1);
			if(DEBUG_CROSS)
			{
				for(int i = p1; i < p2; i++)
					debug[i] = 1;

			}
			break;

		case -1: 
			for(int i = 0; i < size; i++)
				if(random.nextBoolean())
				{
					newGenom[i] = gen2[i];
					gen2[i] = gen1[i];
					if(DEBUG_CROSS)
						debug[i] = 1;
				}

			break;
		}
		if(DEBUG_CROSS)
		{
			System.out.print("Crossover, crossing: ");
			for(int i = 0; i < debug.length; i++)
				System.out.print(debug[i]);

			System.out.println();
		}
		individ1.accessGenome(newGenom);
		individ2.updateWeight();
	}
}
