?? individual.cpp
字號(hào):
}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;}NsgaIndividual::~NsgaIndividual(void) { // Here Individual's destructor is called automatically. // No need to call Individual::~Individual explicitly. delete [] fitness; delete [] objFunction;}NsgaIndividual & NsgaIndividual::operator= (const NsgaIndividual &sourceInd) { int ii; *((Individual *)this) = sourceInd; for (ii=0; ii<globalSetup->finalNoOfObjectives; ii++) { fitness[ii] = sourceInd.fitness[ii]; objFunction[ii] = sourceInd.objFunction[ii]; } rank = sourceInd.rank; crowdingDistance = sourceInd.crowdingDistance; return *this;}NsgaIndividual & NsgaIndividual::operator= (const Individual &sourceInd){ int ii; Individual::operator=(sourceInd); NsgaIndividual *newSource = (NsgaIndividual *)&sourceInd; for(ii = 0; ii < globalSetup->finalNoOfObjectives; ii++) { fitness[ii] = newSource->fitness[ii]; objFunction[ii] = newSource->objFunction[ii]; } rank = newSource->rank; crowdingDistance = newSource->crowdingDistance; return *this;}std::ostream &operator << (std::ostream &out, Individual &x) { out << x.chrom; out << "Objective Function value = " << *(x.objFunction) << std::endl; for (int i=0; i<globalSetup->finalNoOfConstraints; i++) out << "Constraint Violation #" << i << " " << x.violation[i] << std::endl; return out;}/***Crowding comparison operator: Determines if NsgaIndvidual 1 is better than NsgaIndividual 2.If the rank of individual 1 is less than that of individual 2, then return 1. If the rank of individual 1 and 2 are same and the crowding distance of individual 1 is greater than that of individual 2 then return 1. For all other cases return 0.*/bool crowdingComp (const NsgaIndividual *guy1, const NsgaIndividual *guy2) { if((guy1->rank < guy2->rank)|| ((guy1->rank == guy2->rank)&& (guy1->crowdingDistance > guy2->crowdingDistance))) return 1; else return 0;}/***Constraint tournament comparison operator: Determines if the individual 1 is better than individual 2.The flag compareWhat selects whether the objective value or the fitness is to be compared. If objective value is to be compared and we are required to maximise the objective function then the fitness is set to be equal to the objective value. On the other hand if the objective function is to be minimized, then the fitness is set to negative of objective function.If both the individuals are feasible or if they have the same amount of infeasibility (quantified by total constraint violation), then return 1 if the fitnessof individual 1 is greater than that of individual 2. On the other hand if individual 1 is feasible and individual 2 is infeasible then return 1.If both individuals are infeasible but individual 1 has lower infeasibility (lower constraint violation value) then return 1.For all other cases return 0 */bool constrTournComp (const Individual *guy1, const Individual *guy2, int compareWhat) { double guy1Fitness, guy2Fitness; switch(compareWhat) { case FITNESS: guy1Fitness = *(guy1->fitness); guy2Fitness = *(guy2->fitness); break; case OBJECTIVE: if(*(globalSetup->typeOfOptimizations) == Maximization) { guy1Fitness = *(guy1->objFunction); guy2Fitness = *(guy2->objFunction); } else { guy1Fitness = -(*(guy1->objFunction)); guy2Fitness = -(*(guy2->objFunction)); } break; default: exit(0); } if((guy1->penalty <= ZERO) && (guy2->penalty > ZERO)) return 1; else if(((guy1->penalty <= ZERO)&&(guy2->penalty <= ZERO))|| (fabs(guy1->penalty-guy2->penalty) <= ZERO)) { if(guy1Fitness > guy2Fitness) return 1; } else if(guy1->penalty < guy2->penalty) return 1; return 0;}/**Domination determinator. Determines if NsgaIndvidual 1 dominates NsgaIndvidual 2.If the constraint handling method is either penalty method or if there are no constraints, then -- If every fitness value of individual 1 is greater than or equal to the fitness value of individual 2 then return 1. -- If every fitness value of individual 2 is greater than or equal to the fitness value of individual 2 then return -1. -- For all other cases return 0.If the constraint handling method is through tournament selection, then -- If both individuals are feasible or have the same amount of infeasibility, then if every fitness value of individual 1 is greater than or equal to the fitness value of individual 2 then return 1. -- If indiviudal 1 is feasible and individual 2 is infeasible return 1. -- If both individuals are infeasible, but the infeasiblity of individual 1 is lesser than individual 2 then return 1 -- If both individuals are feasible or have the same amount of infeasibility, then if every fitness value of individual 2 is greater than or equal to the fitness value of individual 1 then return -1. -- If indiviudal 2 is feasible and individual 1 is infeasible then return -1 -- If both individuals are infeasible, but the infeasiblity of individual 2 is lesser than individual 1 then return -1. -- For all other cases return 0.*/int dominates (const NsgaIndividual *guy1, const NsgaIndividual *guy2){ int ii, idomj = 0, jdomi = 0; switch(globalSetup->constraintMethod) { case NoConstraints: for(ii = 0; ii < globalSetup->finalNoOfObjectives; ii++) { if(guy1->fitness[ii] > guy2->fitness[ii]) idomj = 1; else if(guy1->fitness[ii] < guy2->fitness[ii]) jdomi = 1; } break; case Penalty: for(ii = 0; ii < globalSetup->finalNoOfObjectives; ii++) { if(guy1->fitness[ii] > guy2->fitness[ii]) idomj = 1; else if(guy1->fitness[ii] < guy2->fitness[ii]) jdomi = 1; } break; case Tournament: if((guy1->penalty <= ZERO)&&(guy2->penalty <= ZERO)) { for(ii = 0; ii < globalSetup->finalNoOfObjectives; ii++) { if(guy1->fitness[ii] > guy2->fitness[ii]) idomj = 1; else if(guy1->fitness[ii] < guy2->fitness[ii]) jdomi = 1; } } else if(((guy1->penalty <= ZERO)&&(guy2->penalty <= ZERO))|| (fabs(guy1->penalty-guy2->penalty) <= ZERO)) { for(ii = 0; ii < globalSetup->finalNoOfObjectives; ii++) { if(guy1->fitness[ii] > guy2->fitness[ii]) idomj = 1; else if(guy1->fitness[ii] < guy2->fitness[ii]) jdomi = 1; } } else { if(guy1->penalty < guy2->penalty) idomj = 1; else if(guy2->penalty < guy1->penalty) jdomi = 1; } break; default: exit(0); } if((idomj == 1)&&(jdomi == 1)) return INONDOMJ; else if((idomj == 1)&&(jdomi == 0)) return IDOMJ; else if((idomj == 0)&&(jdomi == 1)) return JDOMI; else return INONDOMJ;}/// Penaly constraint comparison operator: Checks if indiviudal 1 is better than individual 2 if the constraint handling is through penalty method.bool constrPenaltyComp(const Individual *guy1, const Individual *guy2) { if(*(globalSetup->typeOfOptimizations) == Maximization) { if(*(guy1->objFunction) - guy1->penalty > *(guy2->objFunction) - guy2->penalty) return 1; } else { if(*(guy1->objFunction) + guy1->penalty < *(guy2->objFunction) + guy2->penalty) return 1; } return 0;}///Individual comparison operator: Determines if individual 1 is better than individual 2 when no constraints are present.bool individualComp(const Individual *guy1, const Individual *guy2) { if(*(globalSetup->typeOfOptimizations) == Maximization) { if(*(guy1->objFunction) > *(guy2->objFunction)) return 1; } else { if(*(guy1->objFunction) < *(guy2->objFunction)) return 1; } return 0;}bool isBetter(const Individual *guy1, const Individual *guy2){ bool status; switch(globalSetup->constraintMethod) { case NoConstraints: status = individualComp(guy1, guy2); break; case Penalty: status = constrPenaltyComp(guy1, guy2); break; case Tournament: status = constrTournComp(guy1, guy2, OBJECTIVE); break; default: exit(0); } return status;}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -