/*
 * Creation.cpp
 *
 *  Created on: Jul 29, 2015
 *      Author: jjpalacios
 */

#include "Creation.h"

namespace clbDEB {

//=============================================================================
//
//	Abstract class Creation
//
//=============================================================================
//=============================================================================
//		CONSTRUCTORS / INITIALIZERS
//=============================================================================
//-----  Main constructor  ----------------------------------------------------
Creation::Creation(ParameterDB * parameters) {
	if(parameters != NULL)
		this->setup(parameters);
}


//=============================================================================
//		METHODS
//=============================================================================
//-----  createPopulation  ----------------------------------------------------
Population * Creation::createPopulation(typeOfIndividual individualType,
		const int genomeLength, const int size,	const SharedVars *svars)
		const {

	Population * population = new Population();

	for(int i=0; i < size; i++)
		population->addIndividual(
				this->createIndividual(individualType, genomeLength, svars));
	return population;
}





//=============================================================================
//
//	Class RandomCreation
//
//=============================================================================
//=============================================================================
//		METHODS
//=============================================================================
Individual * RandomCreation::createIndividual(typeOfIndividual individualType,
		const int genomeLength, const SharedVars *svars) const {

	if(individualType == INTEGER_INDIVIDUAL) {
		IntegerIndividual *individual;
		individual = new IntegerIndividual(genomeLength);

		for(int i=0; i < genomeLength; i++) {
			individual->setGene(
					i,
					svars->rng->getInteger(
							svars->minGene[i], svars->maxGene[i])
			);
		}
		return individual;
	}

	if(individualType == DOUBLE_INDIVIDUAL) {
		DoubleIndividual *individual;
		individual = new DoubleIndividual(genomeLength);

		for(int i=0; i < genomeLength; i++) {
			individual->setGene(
					i,
					svars->rng->getDouble(
							svars->minGene[i], svars->maxGene[i])
			);
		}
		return individual;
	}

	std::string err = "The random creation operator cannot create individuals";
	err += " of the specified type.";
	throw ClbException(err);

	return NULL;
}


}

