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

temporaire

parent f8e5bebc
No related branches found
No related tags found
No related merge requests found
Pipeline #2906 failed
......@@ -9,6 +9,7 @@
add_executable(MH-Builder main.cpp)
add_executable(FSP fsp.cpp)
add_executable(NWFSP nwfsp.cpp)
add_executable(TSP tspMonoObjective.cpp)
add_executable(bTSP tspBiObjective.cpp)
add_executable(TSPTIMEWINDOW tspTimeWindow.cpp)
......
//
// Created by Olivier on 15/09/2022
//
#include "../src/MH_Builder.h"
#include "representation/permutation/problems/nwfsp/NWFSP.h"
#include "representation/permutation/problems/nwfsp/NWFSPEval.h"
#include "representation/permutation/problems/nwfsp/algo/NWNEH.h"
#include <iostream>
using namespace std ;
int main(void){
typedef core::FitnessMin<double, double> FIT;
typedef nwfsp::NWFSPSol<FIT> SOL;
typedef nwfsp::NWFSPEval<SOL> EVAL;
nwfsp::NWFSP *fsp_instance ;
//fsp_instance = nwfsp::NWFSP::create("../../instances/fsp/fspOlivier.txt");
fsp_instance = nwfsp::NWFSP::create("../../instances/fsp/020_05_02.txt");
//----------- INITIAL SOLUTION -------------------//
EVAL eval1(*fsp_instance);
SOL sol(fsp_instance->getN());
eval1(sol) ;
std::cout << "FSP: INIT SOL" << std::endl;
std::cout << sol << std::endl;
nwfsp::algo::NWNEH<SOL> neh(*fsp_instance, eval1);
neh.init(sol) ;
std::cout << "NEH: INIT SOL" << std::endl;
std::cout << sol << std::endl;
neh(sol);
std::cout << "NEH: SOL" << std::endl;
std::cout << sol << std::endl;
cout << "Nb Eval: " << eval1.getEvalCounter() << endl ;
//---------- STOP CRITERION ---------------------//
cout << "version with nb eval stop criterion" << endl ;
EVAL eval2(*fsp_instance);
nwfsp::algo::NWNEH<SOL> neh2(*fsp_instance, eval2);
SOL sol2(fsp_instance->getN());
eval2(sol2);
opt::CriterionFactory<SOL, EVAL> criterionFactory;
unsigned long long int criterium_length = 50 ;
core::Criterion<SOL> *criterion = criterionFactory.create("eval", criterium_length, eval2);
std::cout << "NEH: INIT SOL" << std::endl;
neh2.init(sol2);
std::cout << sol2 << std::endl;
neh2(sol2, *criterion);
std::cout << "NEH: SOL" << std::endl;
std::cout << sol2 << std::endl;
cout << "Nb Eval: " << eval2.getEvalCounter() << endl ;
return 0;
}
\ No newline at end of file
......@@ -24,7 +24,7 @@ namespace core {
* Initialization of the criterion
* Do nothing by default
*/
virtual void init() {}
virtual void init() { } ;
/**
* Initialization of the criterion with an Input
......
......@@ -16,6 +16,8 @@ namespace core {
public:
using core::Criterion<IN>::operator();
void init() override {}
bool operator()() override {
return true;
}
......
......@@ -19,13 +19,16 @@ namespace opt {
* Constructor of a criterion based on the number of evaluation
* @param _max_eval the number of maximum evaluation
*/
explicit EvalCriterion(unsigned long long int _max_eval, core::Eval<IN> &_eval) : max_eval(_max_eval), eval(_eval) {}
explicit EvalCriterion(unsigned long long int _max_eval, core::Eval<IN> &_eval) : max_eval(_max_eval), eval(_eval) {
init();
}
void init() override {
last_check = eval.getEvalCounter();
}
bool operator()() override {
std::cout << "max_eval " << max_eval << std::endl;
if (max_eval == 0)
return true;
return (eval.getEvalCounter() - last_check) < max_eval;
......
//
// Created by Olivier Caron on 03/10/2022
//
#ifndef MH_BUILDER_NWFSP_H
#define MH_BUILDER_NWFSP_H
using namespace std ;
namespace nwfsp{
/*! Define type for a No-Wait FlowShop Problem solution */
template <typename FIT>
class NWFSPSol: public representation::PermutationSolution<FIT> {
public:
/**
* Construct a fsp solution of size n
* @param n size of permutation (number of jobs)
*/
explicit NWFSPSol(unsigned long long int n = 0) : representation::PermutationSolution<FIT>(n) {}
};
/**
* Class representing a No-Wait FlowShop Problem) instance
*/
class NWFSP {
private:
/**
* private default constructor
* used by static create method
*/
NWFSP() = default ;
/**
* Read an instance file
* @param instanceFile a data files
*/
void readInstance(const std::string &instanceFile) {
std::string line, tmp;
std::ifstream input(instanceFile);
if (!input)
throw std::runtime_error("Error: unable to open benchmark file");
input >> N >> M >> seed;
due_date.resize(N);
processing_time.resize(M);
for(unsigned long long int i = 0; i < M; i++){
processing_time[i].resize(N);
}
total_job_processing_time.resize(N);
std::fill(total_job_processing_time.begin(), total_job_processing_time.end(), 0);
int num_job;
for (unsigned long long int i = 0; i < N; i++) {
input >> num_job >> due_date[i];
for(unsigned long long int j = 0; j < M; j++){
input >> processing_time[j][i];
total_job_processing_time[i] += processing_time[j][i];
}
}
input.close();
}
/**
* initialize the delay matrix d (Bertolissi 2000)
* d[i][j] corresponds to the delay between the start of job i and the start of following job j
* on the same first machine
*/
void initDelay() {
d.resize(N) ;
for (unsigned long long int i = 0; i < this->N; i++) {
d[i].resize(N) ;
for (unsigned long long int j = 0; j < this->N; j++) {
if (i != j) {
this->d[i][j] = this->processing_time[0][i];
int maxi=0 ;
for (unsigned long long int r = 0; r < this->M; r++) {
int nb = 0 ;
for (unsigned long long int h = 1;h<=r;h++) nb=nb+this->processing_time[h][i];
for (unsigned long long int h=0;h<r;h++) nb = nb-this->processing_time[h][j];
maxi=max(maxi,nb);
}
this->d[i][j]+=maxi;
}
}
}
}
public:
/**
* create a NWFSP instance
* @param instanceFile a data files
*/
static NWFSP * create(const string &instancefile) {
NWFSP *inst ;
inst = new NWFSP();
inst->readInstance(instancefile) ;
inst->initDelay() ;
return inst ;
}
/**
* Get the number of jobs
* @return the number of jobs
*/
unsigned long long int getN() const{
return N;
}
/**
* Get the number of machines
* @return the number of machines
*/
unsigned long long int getM() const{
return M;
}
/**
* Get the processing time matrix
* @return the matrix of the the processing time
*/
std::vector<std::vector<unsigned long long int>> getProcessingTimeMatrix() const {
return processing_time;
}
/**
* Get the total processing time for job i
* @return the total processing time for job i
*/
unsigned long long int getTotalProcessingTime(unsigned long long int i) const {
return total_job_processing_time[i];
}
/**
* Get the task time of the job j at the machine i
* @return the task time of the job j at the machine i
*/
unsigned long long int getProcessingTime(unsigned long long int i, unsigned long long int j) const{
return processing_time[i][j];
}
/**
*
* @param i job i
* @param j job j following i
* @return the delay between the start of job i and the start of following job j on the first machine
*/
unsigned long long int delay(const unsigned long long int i, const unsigned long long int j) const{
return this->d[i][j] ;
}
protected:
/*! Number of jobs */
unsigned long long int N;
/*! Number of machines */
unsigned long long int M;
/*! Processing time matrix */
std::vector<std::vector<unsigned long long int> > processing_time;
/*! Total job processing time */
std::vector<unsigned long long int> total_job_processing_time;
/*! delay matrix d (Bertolissi 2000)
* d[i][j] corresponds to the delay between the start of job i and the start of following job j
* on the same first machine*/
std::vector<std::vector<unsigned long long int> > d;
/*! Seed */
unsigned long long int seed;
/*! Due date vector*/
std::vector<unsigned long long int> due_date;
};
}
#endif //MH_BUILDER_NWFSP_H
//
// Created by Asuncion on 13/02/2020.
//
#ifndef MH_BUILDER_NWFSPEVAL_H
#define MH_BUILDER_NWFSPEVAL_H
using namespace std ;
namespace nwfsp {
/**
* Class representing an evaluator for Flowshop problem
*/
template <typename NWFSPSol>
class NWFSPEval : public core::Eval<NWFSPSol> {
public:
/**
* Constructor of the evaluation
* @param _fsp data for evaluator
*/
explicit NWFSPEval(NWFSP &_fsp) : fsp(_fsp){
}
/**
* Evaluate a FSP solution
* @param sol a solution
*/
void operator()(NWFSPSol &sol) override {
/*! fitness solution */
auto &fit = sol.fitness();
auto objv = fit.objectives();
/** single objective : makespan **/
objv.resize(1);
objv[0]= makespan(sol);
fit.objectives(objv);
this->eval_counter++;
}
virtual unsigned long long int makespan(const NWFSPSol &_fs) const {
return completionTime(_fs.size()-1,_fs);
}
protected:
/*! FSP data */
NWFSP fsp;
virtual unsigned long long int completionTime(const unsigned long long int pos,const NWFSPSol &_fs) const {
if (pos==0) return fsp.getTotalProcessingTime(_fs[0]);
else {
int time=0;
for (unsigned long long int k=1;k<=pos;k++) time+= fsp.delay(_fs[k-1],_fs[k]) ;
return time+fsp.getTotalProcessingTime(_fs[pos]);
}
}
};
}
#endif //MH_BUILDER_NWFSPEVAL_H
\ No newline at end of file
//
// Created by Olivier on 03/10/2022.
//
#ifndef MH_BUILDER_NWNEH_H
#define MH_BUILDER_NWNEH_H
using namespace nwfsp ;
namespace nwfsp::algo {
template<typename SOL>
class NWNEH : core::Algorithm<SOL> {
public:
//typedef typename SOL::FITNESS FIT;
explicit NWNEH(NWFSP &_instance, NWFSPEval<SOL> &_eval) : instance(_instance), eval(_eval), criterion(nullptr){
}
void init(SOL &sol) {
auto comparator=[this](int job1,int job2) {
return this->instance.getTotalProcessingTime(job1) > this->instance.getTotalProcessingTime(job2) ;
};
sort (sol.begin(), sol.end(), comparator) ;
}
void operator()(SOL &sol) {
SOL best, tmp;
// first member
best.push_back(sol[0]);
// for each other members
for (unsigned long long int i=1; i < instance.getN(); i++) {
// insert sol[i] at the best position
if (canContinue()) opt::BestInsertion<SOL>::bestInsert(best, sol[i], eval) ;
}
if (canContinue()) eval(best);
sol = best;
};
void operator()(SOL &sol, NWFSP &_instance) {
instance = _instance;
operator()(sol);
}
void operator()(SOL &sol, core::Criterion<SOL> &c) {
criterion = &c ;
if (canContinue()) operator()(sol);
}
private:
bool canContinue() {
cout << "CanContinue ? " ;
if (criterion == nullptr) { cout << "nullptr" << endl ; return true ; }
else { bool b = criterion->operator()(); cout << b << endl; return b ; }
}
protected:
NWFSP &instance;
NWFSPEval<SOL> &eval;
core::Criterion<SOL> *criterion ;
};
}
#endif //MH_BUILDER_NEH_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment