diff --git a/moeo/src/fitness/moeoExpBinaryIndicatorBasedDualFitnessAssignment.h b/moeo/src/fitness/moeoExpBinaryIndicatorBasedDualFitnessAssignment.h new file mode 100644 index 000000000..d0a0e1317 --- /dev/null +++ b/moeo/src/fitness/moeoExpBinaryIndicatorBasedDualFitnessAssignment.h @@ -0,0 +1,67 @@ + +#include + +template +class moeoExpBinaryIndicatorBasedDualFitnessAssignment : public moeoExpBinaryIndicatorBasedFitnessAssignment +{ +protected: + eoPop _feasible_pop; + eoPop _unfeasible_pop; + +public: + typedef typename MOEOT::ObjectiveVector ObjectiveVector; + + moeoExpBinaryIndicatorBasedDualFitnessAssignment( + moeoNormalizedSolutionVsSolutionBinaryMetric & metric, + const double kappa = 0.05 + ) : moeoExpBinaryIndicatorBasedFitnessAssignment( metric, kappa ) {} + + //! Split up the population in two: in one pop the feasible individual, in the other the feasible ones + virtual void split( eoPop & pop ) + { + _feasible_pop.reserve(pop.size()); + _unfeasible_pop.reserve(pop.size()); + + for( typename eoPop::iterator it=pop.begin(), end=pop.end(); it != end; ++it ) { + // The ObjectiveVector should implement "is_feasible" + if( it->objectiveVector().is_feasible() ) { + _feasible_pop.push_back( *it ); + } else { + _unfeasible_pop.push_back( *it ); + } + } + } + + /*! If the population is homogeneous (only composed of feasible individuals or unfeasible ones), + * then apply the operators on the whole population. + * But, if there is at least one feasible individual, then apply them only on the feasible individuals. + */ + virtual void operator()(eoPop < MOEOT > & pop) + { + // separate the pop in the members + split( pop ); + + eoPop* ppop; + // if there is at least one feasible individual, it will supersede all the unfeasible ones + if( _feasible_pop.size() == 0 ) { + ppop = & _unfeasible_pop; + } else { + ppop = & _feasible_pop; + } + + this->setup(*ppop); + this->computeValues(*ppop); + this->setFitnesses(*ppop); + } + + virtual void setFitnesses(eoPop < MOEOT > & pop) + { + for (unsigned int i=0; icomputeFitness(i), pop[i].is_feasible() ); + } + } + + +}; + diff --git a/moeo/src/fitness/moeoExpBinaryIndicatorBasedFitnessAssignment.h b/moeo/src/fitness/moeoExpBinaryIndicatorBasedFitnessAssignment.h index 3159c2840..7d8bb63b8 100644 --- a/moeo/src/fitness/moeoExpBinaryIndicatorBasedFitnessAssignment.h +++ b/moeo/src/fitness/moeoExpBinaryIndicatorBasedFitnessAssignment.h @@ -58,6 +58,7 @@ class moeoExpBinaryIndicatorBasedFitnessAssignment : public moeoBinaryIndicatorB /** The type of objective vector */ typedef typename MOEOT::ObjectiveVector ObjectiveVector; + typedef typename ObjectiveVector::Type Type; /** * Ctor. @@ -72,7 +73,7 @@ class moeoExpBinaryIndicatorBasedFitnessAssignment : public moeoBinaryIndicatorB * Sets the fitness values for every solution contained in the population _pop * @param _pop the population */ - void operator()(eoPop < MOEOT > & _pop) + virtual void operator()(eoPop < MOEOT > & _pop) { // 1 - setting of the bounds setup(_pop); @@ -145,7 +146,7 @@ class moeoExpBinaryIndicatorBasedFitnessAssignment : public moeoBinaryIndicatorB /** the scaling factor */ double kappa; /** the computed indicator values */ - std::vector < std::vector > values; + std::vector < std::vector > values; /** @@ -181,6 +182,7 @@ class moeoExpBinaryIndicatorBasedFitnessAssignment : public moeoBinaryIndicatorB for (unsigned int i=0; i<_pop.size(); i++) { values[i].resize(_pop.size()); + // the metric may not be symetric, thus neither is the matrix for (unsigned int j=0; j<_pop.size(); j++) { if (i != j) @@ -193,10 +195,10 @@ class moeoExpBinaryIndicatorBasedFitnessAssignment : public moeoBinaryIndicatorB /** - * Sets the fitness value of the whple population + * Sets the fitness value of the whole population * @param _pop the population */ - void setFitnesses(eoPop < MOEOT > & _pop) + virtual void setFitnesses(eoPop < MOEOT > & _pop) { for (unsigned int i=0; i<_pop.size(); i++) { @@ -209,9 +211,9 @@ class moeoExpBinaryIndicatorBasedFitnessAssignment : public moeoBinaryIndicatorB * Returns the fitness value of the _idx th individual of the population * @param _idx the index */ - double computeFitness(const unsigned int _idx) + Type computeFitness(const unsigned int _idx) { - double result = 0; + Type result(0.0); for (unsigned int i=0; i #include #include +#include #include #include #include