Skip to content

Commit

Permalink
styling fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jonpsy committed Mar 14, 2021
1 parent cea3a21 commit fd5ecd0
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions include/ensmallen_bits/nsga2/nsga2_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ typename MatType::elem_type NSGA2::Optimize(

ElemType performance = std::numeric_limits<ElemType>::max();

for(const arma::Col<ElemType>& objective: populationFval)
for (const arma::Col<ElemType>& objective: populationFval)
if (arma::accu(objective) < performance)
performance = arma::accu(objective);

Expand Down Expand Up @@ -396,37 +396,39 @@ inline void NSGA2::CrowdingDistanceAssignment(
std::vector<double>& crowdingDistance)
{
size_t fSize = front.size();
std::vector<size_t> sortedIdx(fSize); // Stores the sorted indices of the fronts.
// Stores the sorted indices of the fronts.
std::vector<size_t> sortedIdx(fSize);
std::iota(sortedIdx.begin(), sortedIdx.end(), 0u);

for (size_t m = 0; m < numObjectives; m++)
{
std::vector<double> fValues{};
std::transform( // Cache the Fvalues of individuals for current objective.
front.begin(), front.end(), std::back_inserter(fValues),
[&](const size_t& individual) {
return populationFval[individual](m);
});

// Sort the front indices based on ascending FValue of the current objective.
// Cache Fvalues of individuals for current objective.
arma::vec fValues(fSize);
std::transform(front.begin(), front.end(), fValues.begin(),
[&](const size_t& individual)
{
return populationFval[individual](m);
});

// Sort front indices by ascending FValue for current objective.
std::sort(sortedIdx.begin(), sortedIdx.end(),
[&](const size_t& frontIdxA, const size_t& frontIdxB) {
return (fValues[frontIdxA] <
fValues[frontIdxB]);
});
[&](const size_t& frontIdxA, const size_t& frontIdxB)
{
return (fValues[frontIdxA] < fValues[frontIdxB]);
});

crowdingDistance[front[sortedIdx[0]]] = std::numeric_limits<double>::infinity();
crowdingDistance[front[sortedIdx[0]]] =
std::numeric_limits<double>::infinity();
crowdingDistance[front[sortedIdx[fSize - 1]]] =
std::numeric_limits<double>::infinity();
std::numeric_limits<double>::infinity();
double minFval = *(std::min_element(fValues.begin(), fValues.end()));
double maxFval = *(std::max_element(fValues.begin(), fValues.end()));
double scale = (maxFval == minFval) ? 1 : maxFval - minFval;

for (size_t i = 1; i < fSize - 1; i++)
{
crowdingDistance[front[sortedIdx[i]]] +=
(fValues[sortedIdx[i + 1]] - fValues[sortedIdx[i - 1]]) /
scale;
crowdingDistance[front[sortedIdx[i]]] +=
(fValues[sortedIdx[i + 1]] - fValues[sortedIdx[i - 1]]) / scale;
}
}
}
Expand Down

0 comments on commit fd5ecd0

Please sign in to comment.