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

in progress

parent 6d071624
No related branches found
No related tags found
No related merge requests found
......@@ -16,7 +16,8 @@ add_executable(NWFSP nwfsp.cpp)
add_executable(TSP tspMonoObjective.cpp)
add_executable(bTSP tspBiObjective.cpp)
add_executable(TSPTIMEWINDOW tspTimeWindow.cpp)
add_executable(archiveBiObjectiveTSP archiveBiObjectiveTSP.cpp)
add_executable(archiveBiObjectiveTSP archiveBiObjectiveTSP.cpp
../src/opt/manySolutions/geneticAlgorithm/NS_GA2_Solution.h)
add_executable(tspGreedy tspGreedy.cpp)
#add_executable(TSPTIMEWINDOW2 tspTimeWindow2.cpp)
......
......@@ -36,6 +36,7 @@
#include "opt/checkpoint/ArchiveCheckpoint.h"
#include "opt/manySolutions/geneticAlgorithm/GeneticAlgorithm.h"
#include "opt/manySolutions/geneticAlgorithm/NS_GA2.h"
#include "opt/manySolutions/geneticAlgorithm/NS_GA2_Population.h"
#include "opt/factory/singleSolution/NeighborFactory.h"
#include "opt/singleSolution/neighborhood/neighbor/IndexNeighbor.h"
......
......@@ -65,9 +65,39 @@ class NS_GA2 : public GeneticAlgorithm<POPULATION> {
*/
virtual ~NS_GA2() = default;
/**
* The operator that runs the ns-ga2 algorithm
* @param NS_GA2_Population<INDIVIDUAL> sol the population
*/
void operator()(NS_GA2_Population<INDIVIDUAL> &sol) override {
this->init(sol);
unsigned long long int N =sol.size() ;
//code for first generation
POPULATION R = sol ; // do a copy of initial population (parent)
this->selection->operator()(sol);
this->crossover->operator()(this->selection->getSelectedPopulation());
this->mutation->operator()(this->crossover->getOffspring());
R(this->mutation->getOffspring()) ; // merge parent and offspring
vector<vector<unsigned long long int>> F = this->fastNonDominatedSort(R);
sol.clear() ; unsigned long long int i = 0;
while ((sol.size() + F[i].size() ) < N )
this->crowdingDistanceAssignment(F[i],R) ;
sol()
while (newSol)
while (this->criterion->operator()()) { // <1>
this->numberOfGenerations++;
this->selection->operator()(sol); // <2>
this->crossover->operator()(this->selection->getSelectedPopulation()); // <3>
this->mutation->operator()(this->crossover->getOffspring()); // <4>
this->replacement->operator()(sol,this->mutation->getOffspring()) ; // <5>
this->criterion->update(); // <6>
this->checkpoint->operator()(sol); // <7>
}
}
private:
vector<vector<unsigned long long int>> fastNonDominatedSort(NS_GA2_POPULATION pop) {
vector<vector<unsigned long long int>> fastNonDominatedSort(NS_GA2_Population<INDIVIDUAL> &pop) {
auto popSize = pop.size();
pop.initRanks();
vector<vector<unsigned long long int>> S(popSize) ;
......@@ -103,11 +133,28 @@ class NS_GA2 : public GeneticAlgorithm<POPULATION> {
}
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 ...
void crowdingDistanceAssignment(vector<unsigned long long int> & Front, NS_GA2_Population<INDIVIDUAL> & pop ) {
auto F_size=Front.size() ; // number of solutions in front
for(unsigned long long int ind : Front) pop.setCrowdingDistance(ind,0) ; // initialize distance
for (unsigned long long int ind_m = 0 ; ind_m < pop[0].fitness().objectives().size(); ind_m ++) {
// sort using each objective value
std::sort(Front.begin(), Front.end(),
[&ind_m, &pop](const unsigned long long int &i1, const auto unsigned long long int &i2) {
return pop[i1].fitness()[ind_m] > pop[i2].fitness()[ind_m];
});
pop.setCrowdingDistance(Front[0], std::numeric_limits<double>::infinity());
pop.setCrowdingDistance(Front[F_size - 1], std::numeric_limits<double>::infinity());
for (unsigned long long int i = 1; i < (F_size - 1); i++)
pop.setCrowdingDistance(Front[i], pop.setCrowdingDistance(Front[i]) +
(pop[Front[i + 1]].fitness()[ind_m] -
pop[Front[i - 1]].fitness()[ind_m]) /
(pop[Front[0]].fitness()[ind_m] -
pop[Front[F_size - 1]].fitness()[ind_m])
);
}
}
};
......
......@@ -38,7 +38,7 @@ namespace opt::manySolutions::geneticAlgorithm {
class NS_GA2_Population : public Population<SOLUTION> {
protected:
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> crowdingDistance ;
public:
/**
......@@ -72,6 +72,25 @@ namespace opt::manySolutions::geneticAlgorithm {
assert(index > 0 and index < this->archive.size());
return this->ranks[index] ;
}
/**
* set the crowding distance of the ith individual
* @param index the nth individual
* @param value the new value of the crowding distance
*/
void setCrowdingDistance(const unsigned long long int index, const unsigned long long int value) {
assert(index > 0 and index < this->archive.size());
this->crowdingDistance[index]=value;
}
/**
* provides the crowding distance of the ith individual
* @param index the nth individual
* @return
*/
unsigned long long int getCrowdingDistance(unsigned long long int index) const {
assert(index > 0 and index < this->archive.size());
return this->crowdingDistance[index];
}
};
}
......
/***************************************************************************************
* MH-Builder, a framework for designing adaptive metaheuristics *
* for single and multi-objective optimization. *
* (c) 2019 University of Lille, CNRS *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
* for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************
Authors: Olivier Caron and additional contributors (see Authors)
****************************************************************************************/
#ifndef MH_BUILDER_NS_GA2_SOLUTION_H
#define MH_BUILDER_NS_GA2_SOLUTION_H
template <typename T>
concept NS_GA2_Solution =
requires(T sol) {
sol.setRank(const unsigned long long int );
unsigned long long int sol.getRank() const ;
sol.setCrowdingDistance();
};
#endif //MH_BUILDER_NS_GA2_SOLUTION_H
......@@ -49,6 +49,7 @@ namespace test {
ASSERT_FALSE(f1 < f2);
ASSERT_TRUE(f2 < f1);
ASSERT_FALSE(f2 > f1);
ASSERT_TRUE(f1 == f1);
}
TEST_F(FitnessTest, TestWhenFitnessMaxCompareMonoObj) {
......@@ -62,6 +63,7 @@ namespace test {
ASSERT_TRUE(f1 < f2);
ASSERT_FALSE(f2 < f1);
ASSERT_TRUE(f2 > f1);
ASSERT_TRUE(f1 == f1);
}
TEST_F(FitnessTest, TestWhenFitnessMinCompareBiObj) {
......@@ -116,8 +118,8 @@ namespace test {
ASSERT_FALSE(f2 > f1); //F1 ne domine pas F2
ASSERT_FALSE(f1 > f3); //F1 domine F3
ASSERT_FALSE(f3 < f1); //F3 est dominé par F1
ASSERT_TRUE(f1 < f3); //F1 n'est pas dominé par F3
ASSERT_TRUE(f3 > f1); //F3 ne domine pas F1
ASSERT_TRUE(f1 < f3); //F1 est dominé par F3
ASSERT_TRUE(f3 > f1); //F3 domine F1
ASSERT_TRUE(f4 > f1); // F4 domine F1
ASSERT_TRUE(f1 < f4); // F1 est dominé par F4
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment