Skip to content
Snippets Groups Projects
Commit 6d071624 authored by Caron Olivier's avatar Caron Olivier
Browse files

nsga2 in progress

parent e4d42c99
No related branches found
No related tags found
No related merge requests found
...@@ -30,6 +30,7 @@ Authors: Olivier Caron and additional contributors (see Authors) ...@@ -30,6 +30,7 @@ Authors: Olivier Caron and additional contributors (see Authors)
#include "opt/manySolutions/geneticAlgorithm/mutation/Mutation.h" #include "opt/manySolutions/geneticAlgorithm/mutation/Mutation.h"
#include "opt/manySolutions/geneticAlgorithm/replacement/Replacement.h" #include "opt/manySolutions/geneticAlgorithm/replacement/Replacement.h"
#include "opt/manySolutions/geneticAlgorithm/NS_GA2_Population.h"
#include <vector> #include <vector>
...@@ -66,24 +67,47 @@ class NS_GA2 : public GeneticAlgorithm<POPULATION> { ...@@ -66,24 +67,47 @@ class NS_GA2 : public GeneticAlgorithm<POPULATION> {
private: private:
void fastNonDominatedSort(POPULATION pop) { vector<vector<unsigned long long int>> fastNonDominatedSort(NS_GA2_POPULATION pop) {
auto popSize = pop.size(); auto popSize = pop.size();
vector<vector<INDIVIDUAL>> S(popSize) ; pop.initRanks();
vector<vector<unsigned long long int>> S(popSize) ;
vector<unsigned long long int> n(popSize) ; vector<unsigned long long int> n(popSize) ;
vector<vector<INDIVIDUAL>> F ; vector<vector<unsigned long long int>> F ; F.clear();
for (auto ind_p = 0 ; ind_p < popSize ; ind_p++) { for (auto ind_p = 0 ; ind_p < popSize ; ind_p++) {
S[ind_p].clear() ; S[ind_p].clear() ;
n[ind_p] = 0 ; n[ind_p] = 0 ;
for (auto ind_q = 0 ; ind_q < pop.size(); ind_q++) for (auto ind_q = 0 ; ind_q < pop.size(); ind_q++)
if (pop[ind_p].fitness() > pop[ind_q].fitness()) // if (p dominates q) if (pop[ind_p].fitness() > pop[ind_q].fitness()) // if (p dominates q)
S[ind_p].push_back(pop[ind_q]) ; // add q to the set of solutions dominated by p S[ind_p].push_back(ind_q) ; // add q to the set of solutions dominated by p
else if (pop[ind_p].fitness() < pop[ind_q].fitness()) else if (pop[ind_p].fitness() < pop[ind_q].fitness())
n[ind_p]++ ; // increment the domination counter of p n[ind_p]++ ; // increment the domination counter of p
if (n[ind_p] == 0) // p belongs to the first front if (n[ind_p] == 0) { // p belongs to the first front
pop.setRank(1); pop.setRank(ind_p,0);
F[0].push_back(ind_p);
}
}
unsigned long long int i = 0 ; // initialize the front counter
while (F[i].size() != 0) {
vector<unsigned long long int> Q ; // Used to store the members of the next front
Q.clear() ;
for(unsigned long long int ind_p : F[i])
for (unsigned long long int ind_q : S[ind_p]) {
n[ind_q]-- ;
if (n[ind_q]==0) { // q belongs to the next front
pop.setRank(ind_q,i+1);
Q.push_back(ind_q);
}
}
i++ ;
F[i]=Q ;
} }
return F ;
} }
void crowding-distance-assignment(vector<unsigned long long int> Front, NS_GA2_POPULATION pop ) {
auto F_size=Front.size() ;
for(unsigned long long int ind : Front)
pop.setCrowdingDistance(ind,0) ;
for ...
} }
}; };
......
...@@ -26,7 +26,7 @@ Authors: Olivier Caron and additional contributors (see Authors) ...@@ -26,7 +26,7 @@ Authors: Olivier Caron and additional contributors (see Authors)
#include "core/Archive.h" #include "core/Archive.h"
#include "opt/manySolutions/geneticAlgorithm/Population.h" #include "opt/manySolutions/geneticAlgorithm/Population.h"
#include <vector> #include <vector>
#include <cassert>
namespace opt::manySolutions::geneticAlgorithm { namespace opt::manySolutions::geneticAlgorithm {
/** /**
...@@ -35,7 +35,7 @@ namespace opt::manySolutions::geneticAlgorithm { ...@@ -35,7 +35,7 @@ namespace opt::manySolutions::geneticAlgorithm {
* @tparam SOLUTION * @tparam SOLUTION
*/ */
template < typename SOLUTION > template < typename SOLUTION >
class NS_GA_2_Population : public Population<SOLUTION> { class NS_GA2_Population : public Population<SOLUTION> {
protected: protected:
std::vector<unsigned long long int > ranks ; // the calculated rank for each solution of the archive std::vector<unsigned long long int > ranks ; // the calculated rank for each solution of the archive
std::vector<unsigned long long int> crowding_distance ; std::vector<unsigned long long int> crowding_distance ;
...@@ -44,24 +44,34 @@ namespace opt::manySolutions::geneticAlgorithm { ...@@ -44,24 +44,34 @@ namespace opt::manySolutions::geneticAlgorithm {
/** /**
* initialize an empty population * initialize an empty population
*/ */
NS_GA_2_Population() : Population<SOLUTION>() {} NS_GA2_Population() : Population<SOLUTION>() {}
/**
* initialize ranks to -1
*/
void initRanks() { void initRanks() {
if (this->archive.size()==0) return ; if (this->archive.size()==0) return ;
this->ranks.resize(this->archive.size()); this->ranks.resize(this->archive.size());
std::fill(this->ranks.begin(), this->ranks.end(), 0); std::fill(this->ranks.begin(), this->ranks.end(), -1);
} }
void setRank(const unsigned long long int index, const unsigned long long int value) /**
[[assert: index > 0 and index < this->archive.size()]] * update the rank for a solution specified by its position (index)
{ * @param index the index of the solution in the archive
* @param value the new rank value
*/
void setRank(const unsigned long long int index, const unsigned long long int value) {
assert(index > 0 and index < this->archive.size());
this->ranks[index] = value ; this->ranks[index] = value ;
} }
/**
* provides the rank of a solution specified by its position (index)
* @param index
* @return the rank value
*/
unsigned long long int getRankAt(const unsigned long long int index) const { unsigned long long int getRankAt(const unsigned long long int index) const {
check assert(index > 0 and index < this->archive.size());
return this->ranks[index] ; return this->ranks[index] ;
} }
private:
void checkRank()
}; };
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment