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

adaptive refactoring

parent 824908b9
No related branches found
No related tags found
No related merge requests found
Pipeline #4715 failed
[10/01/2025] Version 0.1.6 improvement of the adaptive controller framework
[17/07/2024] Version 0.1.5 hypervolume computation, archive checkpoint refactoring
[24/06/2024] Version 0.1.4 Genetic Algorithms (including NSGA-II)
[06/02/2024] Version 0.1.3 Bounded Archive improvements
......
......@@ -38,7 +38,7 @@ PROJECT_NAME = "MH-Builder"
# could be handy for archiving the generated documentation or if some version
# control system is used.
PROJECT_NUMBER = 0.1.5
PROJECT_NUMBER = 0.1.6
# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
......
......@@ -42,8 +42,8 @@ using namespace representation::permutation::problems::fsp;
#include "opt/singleSolution/localsearch/LocalSearch.h"
#include "opt/singleSolution/localsearch/SimulatedAnnealing.h"
#include "opt/adaptive/TimeWindow.h"
#include "opt/adaptive/ChangeIfSlowImprovementWindow.h"
#include "core/adaptive/TimeWindow.h"
#include "core/adaptive/ChangeIfSlowImprovement.h"
#include "util/TimerHelper.h"
......@@ -146,7 +146,7 @@ int main(void) {
for (unsigned long long int i = 1 ; i<=vec_functions.size();i++) durations.push_back(criterion_length) ;
// Step 4, encapsulation of the algorithm to the adaptive time windows
opt::adaptive::TimeWindow<SOL,opt::singleSolution::localsearch::SimulatedAnnealing<SOL> > timeWindow(*sa, vec_functions, durations);
core::adaptive::TimeWindow<SOL,opt::singleSolution::localsearch::SimulatedAnnealing<SOL> > timeWindow(*sa, vec_functions, durations);
std::cout << "INITIAL SOLUTION AFTER NEH" << std::endl;
std::cout << sol2 << std::endl;
......@@ -197,7 +197,7 @@ int main(void) {
std::vector<std::function<void(opt::singleSolution::localsearch::SimulatedAnnealing<SOL>&)>> vec_functions2 = {setToSwap,setToShift};
// Step 4, encapsulation of the algorithm to the adaptive time windows
opt::adaptive::ChangeIfSlowImprovementWindow<SOL,opt::singleSolution::localsearch::SimulatedAnnealing<SOL> > adaptiveWindow(*sa, vec_functions2, 4, 0.02, 10,eval);
core::adaptive::ChangeIfSlowImprovement<SOL,opt::singleSolution::localsearch::SimulatedAnnealing<SOL> > adaptiveWindow(*sa, vec_functions2, 4, 0.02, 10,eval);
std::cout << "using ChangeIfSlowImprovementWindow strategy during " << vec_functions.size()*criterion_length << std::endl;
std::cout << "every 4 seconds, alternate the neighbor operators: swap and shift if the fitness improves less than 0.02 %" << std::endl;
std::cout << "repeat 10 times, so 40 seconds for the complete run "<< std::endl;
......
......@@ -23,7 +23,7 @@
#include "util/ParserHelper.h"
#include "util/RNGHelper.h"
#include "opt/adaptive/TimeWindow.h"
#include "core/adaptive/TimeWindow.h"
#include "representation/permutation/problems/tsp/TSP.h"
......@@ -111,7 +111,7 @@ int main(void) {
for (unsigned long long int i = 1 ; i<=functions.size();i++) durations.push_back(criterion_length) ;
// encapsulation of the algorithm with the adaptive timewindow object
opt::adaptive::TimeWindow<SOL, opt::singleSolution::localsearch::HillClimbing<SOL>> timeWindow(*algo, functions, durations);
core::adaptive::TimeWindow<SOL, opt::singleSolution::localsearch::HillClimbing<SOL>> timeWindow(*algo, functions, durations);
std::cout << "ADAPTIVE VERSION" << std::endl ;
std::cout << "INITIAL SOLUTION" << std::endl;
......
......@@ -23,7 +23,7 @@
#include "util/ParserHelper.h"
#include "util/RNGHelper.h"
#include "opt/adaptive/TimeWindow.h"
#include "core/adaptive/TimeWindow.h"
#include "representation/permutation/problems/tsp/TSP.h"
......@@ -122,7 +122,7 @@ int main(void) {
for (unsigned long long int i = 1 ; i<=functions.size();i++) durations.push_back(criterion_length) ;
// encapsulation of the algorithm with the adaptive timewindow object
opt::adaptive::TimeWindow<SOL, opt::singleSolution::localsearch::HillClimbing<SOL>> timeWindow(*algo, functions, durations);
core::adaptive::TimeWindow<SOL, opt::singleSolution::localsearch::HillClimbing<SOL>> timeWindow(*algo, functions, durations);
std::cout << "ADAPTIVE VERSION" << std::endl ;
std::cout << "INITIAL SOLUTION" << std::endl;
......
......@@ -19,8 +19,8 @@
Authors: Olivier Caron and additional contributors (see Authors)
****************************************************************************************/
#ifndef MH_BUILDER_ADAPTIVE_WINDOW_H
#define MH_BUILDER_ADAPTIVE_WINDOW_H
#ifndef MH_BUILDER_ADAPTIVE_CONTROLLER_H
#define MH_BUILDER_ADAPTIVE_CONTROLLER_H
#include "core/Algorithm.h"
#include <functional>
......@@ -28,28 +28,28 @@
#include "opt/criterion/TimeCriterion.h"
namespace opt::adaptive {
namespace core::adaptive {
/**
* abstract class for all adaptive strategies of a given algorithm
* @tparam IN the type of a solution
* @tparam ALGO the type of the algorithm
*/
template<typename IN, typename ALGO>
class AdaptiveWindow : public core::Algorithm<IN> {
class AdaptiveController : public core::Algorithm<IN> {
public:
/**
* Initialisation
* @param _algorithm the algorithm
* @param _functions the vector of functions that modify the structure of the algorithm
*/
AdaptiveWindow(ALGO &_algorithm, std::vector<std::function<void(ALGO &)>> &_functions) :
AdaptiveController(ALGO &_algorithm, std::vector<std::function<void(ALGO &)>> &_functions) :
algorithm(_algorithm), functions(_functions) {}
/**
* run the algorithm, change dynamically the structure of the algorithm thanks to
* the vector of functions
* @param _in the initial solution
*/
virtual void operator()(IN &_in) = 0 ;
virtual void operator()(IN &_in) override = 0 ;
protected:
ALGO &algorithm;
......@@ -57,4 +57,4 @@ namespace opt::adaptive {
};
}
#endif //MH_BUILDER_ADAPTIVE_WINDOW_H
#endif //MH_BUILDER_ADAPTIVE_CONTROLLER_H
......@@ -19,19 +19,19 @@
Authors: Olivier Caron and additional contributors (see Authors)
****************************************************************************************/
#ifndef MH_BUILDER_CHANGE_IF_SLOW_IMPROVEMENT_WINDOW_H
#define MH_BUILDER_CHANGE_IF_SLOW_IMPROVEMENT_WINDOW_H
#ifndef MH_BUILDER_CHANGE_IF_SLOW_IMPROVEMENT_H
#define MH_BUILDER_CHANGE_IF_SLOW_IMPROVEMENT_H
#include "core/Algorithm.h"
#include "core/Eval.h"
#include <functional>
#include <iostream>
#include "opt/criterion/TimeCriterion.h"
#include "opt/adaptive/AdaptiveWindow.h"
#include "AdaptiveController.h"
namespace opt::adaptive {
namespace core::adaptive {
/**
* adaptive strategy that regularly modifies the structure of the algorithm
* if the solution does not improve by a given percentage
......@@ -39,7 +39,7 @@ namespace opt::adaptive {
* @tparam ALGO the type of the algorithm
*/
template<typename IN, typename ALGO>
class ChangeIfSlowImprovementWindow : public opt::adaptive::AdaptiveWindow<IN,ALGO> {
class ChangeIfSlowImprovement : public core::adaptive::AdaptiveController<IN,ALGO> {
public:
/**
* Initialisation of the Eval Switch Adaptive Strategy
......@@ -50,9 +50,9 @@ namespace opt::adaptive {
* @param _max_iterations the maximum number of iterations
* @param _eval The eval function
*/
ChangeIfSlowImprovementWindow(ALGO &_algorithm, std::vector<std::function<void(ALGO &)>> &_functions,
ChangeIfSlowImprovement(ALGO &_algorithm, std::vector<std::function<void(ALGO &)>> &_functions,
int _duration, double _improvement_percentage, int _max_iterations, core::Eval<IN> &_eval)
: opt::adaptive::AdaptiveWindow<IN,ALGO>(_algorithm, _functions),
: core::adaptive::AdaptiveController<IN,ALGO>(_algorithm, _functions),
duration(_duration),improvement_percentage(_improvement_percentage),
max_iterations(_max_iterations), eval(_eval) {}
......@@ -61,7 +61,7 @@ namespace opt::adaptive {
* run the algorithm with the adaptive strategy
* @param _in the initial solution
*/
void operator()(IN &_in) {
void operator()(IN &_in) override {
// init phase
auto fitness_scalar_value = _in.fitness().scalar() ;
int counter=1 ; int select_function=0 ;
......@@ -70,14 +70,11 @@ namespace opt::adaptive {
// perform algorithm with the specific adaptive strategy
while (counter <= this->max_iterations ) {
auto current_fitness_value = _in.fitness().scalar() ;
std::cout << "Iteration no " << counter << ": " << fitness_scalar_value << " - " << current_fitness_value << std::endl;
std::cout << "current percentage improvement:" << (abs(fitness_scalar_value-current_fitness_value)*100)/fitness_scalar_value << std::endl;
if ((abs(fitness_scalar_value-current_fitness_value)*100)/fitness_scalar_value <
this->improvement_percentage) {
std::cout << "modify algorithm !" << (select_function % this->functions.size()) << std::endl ;
this->functions[select_function % this->functions.size()](this->algorithm);
select_function++ ;
} else std::cout << "NO modify algorithm !" << std::endl;
}
fitness_scalar_value = current_fitness_value ;
timeCriterion.init();
this->algorithm(_in, timeCriterion);
......@@ -93,4 +90,4 @@ namespace opt::adaptive {
};
}
#endif //MH_BUILDER_CHANGE_IF_SLOW_IMPROVEMENT_WINDOW_H
#endif //MH_BUILDER_CHANGE_IF_SLOW_IMPROVEMENT_H
......@@ -26,11 +26,11 @@
#include <functional>
#include <iostream>
#include "opt/criterion/TimeCriterion.h"
#include "opt/adaptive/AdaptiveWindow.h"
#include "AdaptiveController.h"
namespace opt::adaptive {
namespace core::adaptive {
/**
* adaptive strategy which modifies the structure of the algorithm
* at different times indicated by the ‘durations’ vector
......@@ -38,7 +38,7 @@ namespace opt::adaptive {
* @tparam ALGO the type of the algorithm
*/
template<typename IN, typename ALGO>
class TimeWindow : public opt::adaptive::AdaptiveWindow<IN,ALGO> {
class TimeWindow : public core::adaptive::AdaptiveController<IN,ALGO> {
public:
/**
* Initialisation of the Time Adaptive Strategy
......@@ -48,7 +48,7 @@ namespace opt::adaptive {
*/
TimeWindow(ALGO &_algorithm, std::vector<std::function<void(ALGO &)>> &_functions,
std::vector<long long int> &_durations)
: opt::adaptive::AdaptiveWindow<IN,ALGO>(_algorithm, _functions),durations(_durations){
: core::adaptive::AdaptiveController<IN,ALGO>(_algorithm, _functions),durations(_durations){
if (this->durations.size() != _functions.size()) {
std::cerr << "durations vector and functions vector must be the same size !!";
exit(0);
......@@ -59,7 +59,7 @@ namespace opt::adaptive {
* run the algorithm with the adaptive strategy
* @param _in the initial solution
*/
void operator()(IN &_in) {
void operator()(IN &_in) override {
for (unsigned long long int i = 0; i < this->durations.size(); i++) {
criterion::TimeCriterion<IN> timeCriterion(this->durations[i]);
this->functions[i](this->algorithm);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment