?? individual.cpp
字號:
/***************************************************************/* Single & Multi-Objective Real-Coded Genetic Algorithms Code *//* Author: Kumara Sastry *//* Illinois Genetic Algorithms Laboratory (IlliGAL) *//* Deparment of General Engineering *//* University of Illinois at Urbana-Champaign *//* 104 S. Mathews Ave, Urbana, IL 61801 *//***************************************************************/#include "individual.hpp"// Started Modification 02/14/2001:21:19:59 - PVPextern GlobalSetup *globalSetup;extern void globalEvaluate(double *values, double *objFunction, double *violation, double *penalty, int *noOfViolations);Individual::Individual(void) { // This is the default constructor. Here, the properties // of the Individual will be set appropriately. It should be // noted that the default constructor of a parent class is // automatically called when a child class object is constructed. // The reason why I am saying this is that we have double *fitness // etc and we would have to initialize it in the NsgaIndividual // class constructor to an array of fitness, etc. Here, we should // take care that we do not allocate it again. Therefore, we have // to check what type of GA this is, before we allocate memory. // The things to be initialized: // 1. Chromosome chrom; - just call default constructor - will be initialized to a random chromosome based on bounds // 2. double *fitness; - initialize to new doubl if GA is SGA // 3. double *objFunction; - -do- // 4. double penalty; - initialize to ZERO. // 5. double *violation, totalViolation; - Set violation to NULL if unconstrained. // 6. int *freezeMask; - initialize to int[globalSetup->noOfVariables] and set everything to 0. // Do the evaluation after everything is done. // Chromosomes default constructor is called automatically. if (!(globalSetup->finalNoOfConstraints)) violation = NULL; else violation = new double[globalSetup->finalNoOfConstraints]; noOfViolations = 0; penalty = 0.0; if (globalSetup->gaType==SGA) { fitness = new double(0.00); objFunction = new double(0.00); if(!(globalSetup->loadPopulation)) evaluateFitness(); // If Nsga, it's constructor will call it's evaluate. }}Individual::Individual(const Individual &sourceInd ) { // Call operator= of chromosomes and just copy all other values after appropriate allocations int ii; chrom = sourceInd.chrom; if (globalSetup->gaType==SGA) { fitness = new double(*(sourceInd.fitness)); objFunction = new double(*(sourceInd.objFunction)); } if (globalSetup->finalNoOfConstraints) { noOfViolations = sourceInd.noOfViolations; violation = new double[globalSetup->finalNoOfConstraints]; for(ii = 0; ii < globalSetup->finalNoOfConstraints; ii++) violation[ii] = sourceInd.violation[ii]; penalty = sourceInd.penalty; } else violation = NULL;}Individual::Individual(const Individual *sourceInd ){ int ii; chrom = sourceInd->chrom; if (globalSetup->gaType==SGA) { fitness = new double(*(sourceInd->fitness)); objFunction = new double(*(sourceInd->objFunction)); } if (globalSetup->finalNoOfConstraints) { noOfViolations = sourceInd->noOfViolations; violation = new double[globalSetup->finalNoOfConstraints]; for(ii = 0; ii < globalSetup->finalNoOfConstraints; ii++) violation[ii] = sourceInd->violation[ii]; penalty = sourceInd->penalty; } else violation = NULL;}Individual & Individual::operator= (const Individual &sourceInd) { // Difference between copy constructor and this is // that this guy has already been allocated some memory // for the dynamically allocated stuff and so it has to be freed // and copied or copied without reallocation. int ii; chrom = sourceInd.chrom; if (globalSetup->gaType==SGA) { *fitness = (*(sourceInd.fitness)); *objFunction = (*(sourceInd.objFunction)); } if(globalSetup->finalNoOfConstraints) { noOfViolations = sourceInd.noOfViolations; for(ii = 0; ii < globalSetup->finalNoOfConstraints; ii++) violation[ii] = sourceInd.violation[ii]; penalty = sourceInd.penalty; } return *this;}void Individual::loadIndividual(double *varValues, double *objValues, double *constViolValues, double penaltyValue) { int ii; for(ii = 0; ii < globalSetup->noOfDecisionVariables; ii++) chrom[ii] = varValues[ii]; for(ii = 0; ii < globalSetup->finalNoOfObjectives; ii++) objFunction[ii] = objValues[ii]; if(globalSetup->finalNoOfConstraints) { noOfViolations = 0; for(ii = 0; ii < globalSetup->finalNoOfConstraints; ii++) violation[ii] = constViolValues[ii]; if(violation[ii] >= 1.0e-5) { noOfViolations++; } penalty = penaltyValue; }}void Individual::evaluateFitness(void) { double *values; int ii; values = new double[globalSetup->noOfDecisionVariables]; for (ii = 0; ii < globalSetup->noOfDecisionVariables; ii++) values[ii] = chrom[ii]; globalEvaluate(values, objFunction, violation, &penalty, &noOfViolations); delete [] values;}void Individual::mutate(int *freezeMask) { switch(globalSetup->mutationType) { case Selective: chrom.mutateMinMax(freezeMask); break; case Genewise: chrom.mutateNormal(freezeMask); break; case Polynomial: chrom.mutatePolynomial(freezeMask); break; default: exit(0); }}Individual::~Individual(void) { if (globalSetup->gaType==SGA) { delete fitness; delete objFunction; } if (globalSetup->finalNoOfConstraints) delete [] violation;}// Some stuff for NsgaIndividualNsgaIndividual::NsgaIndividual(void) { // At this point, chrom is ready, violation is ready and freezeMask is there even though it is // not necessary. We have to initialize fitness and objFunction and do the evaluation fitness = new double[globalSetup->finalNoOfObjectives]; objFunction = new double[globalSetup->finalNoOfObjectives]; rank = 0; crowdingDistance=0.0; if(!(globalSetup->loadPopulation)) evaluateFitness();}NsgaIndividual::NsgaIndividual(const Individual &sourceInd):Individual(sourceInd) { int ii; NsgaIndividual *newSource = (NsgaIndividual *) &sourceInd; fitness = new double[globalSetup->finalNoOfObjectives]; objFunction = new double[globalSetup->finalNoOfObjectives]; for(ii = 0; ii < globalSetup->finalNoOfObjectives; ii++) { fitness[ii] = newSource->fitness[ii]; objFunction[ii] = newSource->objFunction[ii]; } rank = newSource->rank; crowdingDistance = newSource->crowdingDistance;}NsgaIndividual::NsgaIndividual(const Individual *sourceInd):Individual(*sourceInd){ int ii; NsgaIndividual *newSource = (NsgaIndividual *) sourceInd; fitness = new double[globalSetup->finalNoOfObjectives]; objFunction = new double[globalSetup->finalNoOfObjectives]; for (ii = 0; ii < globalSetup->finalNoOfObjectives; ii++) { fitness[ii] = newSource->fitness[ii]; objFunction[ii] = newSource->objFunction[ii]; } rank = newSource->rank; crowdingDistance = newSource->crowdingDistance;}NsgaIndividual::NsgaIndividual(const NsgaIndividual &sourceInd):Individual(sourceInd) { int ii; fitness = new double[globalSetup->finalNoOfObjectives]; objFunction = new double[globalSetup->finalNoOfObjectives]; for (ii = 0; ii < globalSetup->finalNoOfObjectives; ii++) { fitness[ii] = sourceInd.fitness[ii]; objFunction[ii] = sourceInd.objFunction[ii]; } rank = sourceInd.rank; crowdingDistance = sourceInd.crowdingDistance;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -