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

nsga-2 in progress

parent d65d30eb
Branches
No related tags found
No related merge requests found
......@@ -28,7 +28,8 @@ find_package(PythonLibs)
#
if (PYTHONLIBS_FOUND)
add_executable(tspGA tspGeneticAlgorithm.cpp)
add_executable(tspGA tspGeneticAlgorithm.cpp
../src/opt/manySolutions/geneticAlgorithm/NS_GA2.h)
add_executable(ExhaustiveTSP tspExhaustive.cpp)
add_executable(oneMaxGA oneMaxGeneticAlgorithm.cpp)
include_directories(${PYTHON_INCLUDE_DIRS})
......
......@@ -35,6 +35,7 @@
#include "opt/criterion/TimeCriterion.h"
#include "opt/checkpoint/ArchiveCheckpoint.h"
#include "opt/manySolutions/geneticAlgorithm/GeneticAlgorithm.h"
#include "opt/manySolutions/geneticAlgorithm/NS_GA2.h"
#include "opt/factory/singleSolution/NeighborFactory.h"
#include "opt/singleSolution/neighborhood/neighbor/IndexNeighbor.h"
......@@ -89,7 +90,7 @@ int main(void) {
std::cout << "the init population (sorted):" << std::endl << population << std::endl ;
STOP stopGen(5000) ; // using TimeCriterion with 5 seconds
STOP stopGen(60000) ; // using TimeCriterion with 5 seconds
SELECTION sel(5, population.size()*.8);
CROSSOVER crossover(0.8, eval) ;
......
/***************************************************************************************
* 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_H
#define MH_BUILDER_NS_GA2_H
#include "opt/manySolutions/geneticAlgorithm/GeneticAlgorithm.h"
#include "core/Criterion.h"
#include "opt/manySolutions/geneticAlgorithm/selection/Selection.h"
#include "opt/manySolutions/geneticAlgorithm/crossover/Crossover.h"
#include "opt/manySolutions/geneticAlgorithm/mutation/Mutation.h"
#include "opt/manySolutions/geneticAlgorithm/replacement/Replacement.h"
#include <vector>
namespace opt::manySolutions::geneticAlgorithm {
/**
* Class representing an NS_GA2 Genetic Algorithm
* see https://ieeexplore.ieee.org/document/996017
* @tparam POPULATION
*/
template<typename POPULATION>
class NS_GA2 : public GeneticAlgorithm<POPULATION> {
using typename GeneticAlgorithm<POPULATION>::INDIVIDUAL ;
public:
/**
* Constructor of a Genetic Algorithm
* @param _selection the selection operator
* @param _crossover the crossover operator
* @param _mutation the mutation operator
* @param _criterion stop criterion
*/
explicit NS_GA2(selection::Selection<POPULATION> &_selection,
crossover::Crossover<POPULATION> &_crossover, mutation::Mutation<POPULATION> &_mutation ,
replacement::Replacement<POPULATION> &_replacement,
core::Criterion<POPULATION> &_criterion)
: GeneticAlgorithm<POPULATION>(_selection,_crossover,_mutation,_replacement,_criterion)
{}
/**
* Default destructor
*/
virtual ~NS_GA2() = default;
private:
void fastNonDominatedSort(POPULATION pop) {
auto popSize = pop.size();
vector<vector<INDIVIDUAL>> S(popSize) ;
vector<unsigned long long int> n(popSize) ;
vector<vector<INDIVIDUAL>> F ;
for (auto ind_p = 0 ; ind_p < popSize ; ind_p++) {
S[ind_p].clear() ;
n[ind_p] = 0 ;
for (auto ind_q = 0 ; ind_q < pop.size(); ind_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
else if (pop[ind_p].fitness() < pop[ind_q].fitness())
n[ind_p]++ ; // increment the domination counter of p
if (n[ind_p] == 0) // p belongs to the first front
pop.setRank(1);
}
}
}
};
}
#endif //MH_BUILDER_NS_GA2_H
/***************************************************************************************
* 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_GA_NS_GA_II_POPULATION_H
#define MH_BUILDER_GA_NS_GA_II_POPULATION_H
#include "core/Eval.h"
#include "core/Archive.h"
#include "opt/manySolutions/geneticAlgorithm/Population.h"
#include <vector>
namespace opt::manySolutions::geneticAlgorithm {
/**
* Class representing a population for Genetic Algorithms
* with additional properties required for NS_GA_2 algorithm
* @tparam SOLUTION
*/
template < typename SOLUTION >
class NS_GA_2_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 ;
public:
/**
* initialize an empty population
*/
NS_GA_2_Population() : Population<SOLUTION>() {}
void initRanks() {
if (this->archive.size()==0) return ;
this->ranks.resize(this->archive.size());
std::fill(this->ranks.begin(), this->ranks.end(), 0);
}
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 ;
}
unsigned long long int getRankAt(const unsigned long long int index) const {
check
return this->ranks[index] ;
}
private:
void checkRank()
};
}
#endif //MH_BUILDER_GA_NS_GA_II_POPULATION_H
......@@ -19,8 +19,8 @@
Authors: Lucien Mousin and additional contributors (see Authors)
****************************************************************************************/
#ifndef EASYMETA_FITNESSTEST_H
#define EASYMETA_FITNESSTEST_H
#ifndef EASYMETA_FITNESS_TEST_H
#define EASYMETA_FITNESS_TEST_H
#include "core/fitness/FitnessMin.h"
#include "core/fitness/FitnessMax.h"
......@@ -145,4 +145,4 @@ namespace test {
}
}
#endif //EASYMETA_FITNESSTEST_H
#endif //EASYMETA_FITNESS_TEST_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment