Commit 62f4c9fb authored by jerem's avatar jerem

mut + cross

parent fa4eea0f
......@@ -91,26 +91,32 @@ int main(int argc, char* argv[]){
std::cout << "getQuantityCoursesToPick : " << std::to_string(pb.getQuantityCoursesToPick()) << std::endl;
std::cout << "cfg_quantityCourses() : " << std::to_string(pb.cfg_quantityCourses()) << std::endl;
CursusInit init(pb.getQuantityCoursesToPick()-5,0);//pb.cfg_quantityCourses());//pb.getQuantityCoursesToPick(),pb.cfg_quantityCourses(), pb.seed());
/**@todo make size of the pb accessible as well as size of an individu*/
int size_of_the_pb = 30;
ConstraintsECTS ctrECTS(pb, job);
ConstraintsRepetition ctrRep(pb, job);
std::pair<bool,double> res;
//CursusInit init(pb.getQuantityCoursesToPick()-5,0);
CursusInit init(pb.getQuantityCoursesToPick(),pb.coursesCatalogue().size(),pb.seed());
//pb.cfg_quantityCourses());//pb.getQuantityCoursesToPick(),pb.cfg_quantityCourses(), pb.seed());
CursusEval eval;
CursusMutation mut;
CursusCrossover xOver;
eoGenContinue<Cursus> cont(100); // runs for 100 gen
CursusCrossover cross(pb, ctrRep, init);
CursusMutation mut(pb, ctrRep);
eoGenContinue<Cursus> cont(1000); // runs for 100 gen
//xOver, xOver rate, mutation, mutation rate
eoSGATransform<Cursus> transform(xOver, 0.1, mut, 0.1);
eoSGATransform<Cursus> transform(cross, 1, mut, 1);
eoDetTournamentSelect<Cursus> selectOne(2); //selection method by tournament, here against 2
eoSelectPerc<Cursus> select(selectOne);
eoGenerationalReplacement<Cursus> replace;
eoPop<Cursus> pop;
/**@todo make size of the pb accessible as well as size of an individu*/
int size_of_the_pb = 30;
ConstraintsECTS ctrECTS(pb, job);
ConstraintsRepetition ctrRep(pb, job);
std::pair<bool,double> res;
for(int i = 0; i < size_of_the_pb; i++)
{
......@@ -121,12 +127,38 @@ int main(int argc, char* argv[]){
std::cout << "IND#" << std::to_string(i) << "\nFirst: " << res.first << "\nSecond: " << std::to_string(res.second) << std::endl;
pop.push_back(c1);
}
//MUTATION TEST
/*
pop[0].printOn(std::cout);
std::cout << std::endl;
for(int i=0; i<50; i++){
mut(pop[0]);
pop[0].printOn(std::cout);
std::cout << std::endl;
}*/
//CROSS TEST
/*
pop[0].printOn(std::cout);
std::cout << std::endl;
pop[1].printOn(std::cout);
std::cout << std::endl;
for(int i=0; i<50; i++){
cross(pop[0],pop[1]);
pop[0].printOn(std::cout);
std::cout << std::endl;
pop[1].printOn(std::cout);
std::cout << std::endl;
std::cout << std::endl;
}
*/
for(int i = 0; i < pb.coursesCatalogue().size(); i++)
{
std::cout << pb.coursesCatalogue().at(i) << std::endl;
}
std::cout << "===== CURRENT POP =====" << std::endl;
pop.printOn(std::cout);
std::cout << "===== =====" << std::endl;
......@@ -140,6 +172,8 @@ int main(int argc, char* argv[]){
std::cout << " fitness:" << pop.best_element().fitness() << std::endl;
std::cout << "===============" << std::endl;
std::cout << "cpt: " << cross.cpt << std::endl;
// ================================= END CEAO ZONE ===============================
......
......@@ -48,6 +48,7 @@ set(BUILD_DIR build)
# Path
set(PARADISEO_SRC_PATHS
/home/jerem/Softs/ParadisEO-2.0/
${PARADISEO_ROOT}
$ENV{PARADISEO_ROOT}
/usr/local/
......@@ -101,6 +102,7 @@ endif()
set(PARADISEO_FOUND true) # will be set to false if one of the required modules is not found
set(FIND_PARADISEO_LIB_PATHS
/home/jerem/Softs/ParadisEO-2.0/build/
${PARADISEO_ROOT}/${BUILD_DIR}
$ENV{PARADISEO_ROOT}
/usr/local/
......
#ifndef SRC_MODEL_EA_CROSSOVER_H_
#define SRC_MODEL_EA_CROSSOVER_H_
#include <eoOrderXover.h>
#include "cursus.h"
template <class EOT>
class crossCSDVP: public eoQuadOp<EOT>
{
typedef eoOrderXover<Cursus> CursusCrossover;
public:
crossCSDVP(CSDVP& _pb, ConstraintsRepetition& _ctr, CursusInit& _init):pb(_pb),ctr(_ctr), init(_init), cpt(0){}
virtual bool operator()(EOT& _chrom1, EOT& _chrom2){
cpt++;
//Integrity Check BEFORE CROSS
std::pair<bool, double> ctrRes1= ctr.integrityCheck(_chrom1);
std::pair<bool, double> ctrRes2= ctr.integrityCheck(_chrom2);
assert(ctrRes1.first && ctrRes2.first);
//Cross by using tmp solution
EOT tmp;
tmp.resize(0);
for(int i=0; i<_chrom1.size(); i++)
tmp.push_back(_chrom1[i]);
cross(_chrom1,_chrom2);
cross(_chrom2, tmp);
//Integrity Check AFTER CROSS
ctrRes1= ctr.integrityCheck(_chrom1);
ctrRes2= ctr.integrityCheck(_chrom2);
assert(ctrRes1.first && ctrRes2.first);
return true;
}
//Cross modify _chrom1
void cross(EOT& _chrom1,const EOT& _chrom2){
bool needRepair=false;
bool errorFound=false;
unsigned int i, tmp;
const std::vector<int> TF = pb.timeFrames();
unsigned int nbTF=TF.size();
unsigned int sizeTF=_chrom1.size()/nbTF;
std::vector<Course> catalogue = pb.coursesCatalogue();
std::vector<int> courseID;
//Create a vector with all position
for(i=0; i<catalogue.size(); i++)
courseID.push_back(i);
//Define a cut point between time frames
unsigned int cut = eo::rng.random(nbTF-1)+1;
//copie right part from cut with chrom2, -1 if impossible
for(i=cut*sizeTF; i<_chrom1.size(); i++){
if(notin(_chrom1, _chrom2[i],i)){
_chrom1[i]=_chrom2[i];
}
else{
_chrom1[i]=-1;
needRepair=true;
}
}
//Repair
if(needRepair){
//search a course available in the TF for each -1 (impossible position of previous step)
coursenotin(courseID, _chrom1);
for(i=0; i<_chrom1.size(); i++){
if(_chrom1[i]==-1){
tmp=0;
while(!isInTF(catalogue[courseID[tmp]], (i/sizeTF)+pb.cfg_minimalTimeFrame()) && tmp < courseID.size()){
tmp++;
}
if(tmp<courseID.size()){
_chrom1[i]=courseID[tmp];
remove(courseID, _chrom1[i]);
}
else{
//no possibility
errorFound=true;
}
}
}
}
//If no possibility, reinit the sol
if(errorFound){
std::cout << "Impossibility in Cross > Reinit sol" << std::endl;
init(_chrom1);
}
}
//Return if a course can be in a TF
bool isInTF(Course& _c, int _TF){
bool res=false;
std::vector<int> tf=_c.timeFrame();
for(int i=0; i<tf.size(); i++)
if(_TF=tf[i])
res=true;
return res;
}
//remove an _id from a vector
void remove(std::vector<int>& _chrom, int _id){
int i=0;
while(_chrom[i]!= _id)
i++;
_chrom[i]=_chrom[_chrom.size()-1];
_chrom.pop_back();
}
//return if a id is not in the left side of a sol (side depend of _size)
bool notin(EOT& _chrom, int _id, int _size){
bool res=true;
int i=0;
while(res && i<_size){
if(_chrom[i]==_id)
res=false;
i++;
}
return res;
}
//_CourseID subastraction from _chrom
void coursenotin(std::vector<int>& _courseID, std::vector<int>& _chrom){
int tmp;
for (int i=0; i<_chrom.size(); i++){
tmp=0;
while(tmp<_courseID.size() && _chrom[i]!=_courseID[tmp])
tmp++;
if(tmp<=_courseID.size()){
_courseID[tmp]=_courseID[_courseID.size()-1];
_courseID.pop_back();
}
}
}
int cpt;
private:
CSDVP pb;
ConstraintsRepetition ctr;
CursusInit init;
};
typedef crossCSDVP<Cursus> CursusCrossover;
#endif // SRC_MODEL_EA_CROSSOVER_H_
......@@ -20,39 +20,25 @@ class eoInitCSDVP: public eoInit<EOT>
typedef typename EOT::AtomType AtomType;
eoInitCSDVP(unsigned chromSize, unsigned maxVal, int _seed)
: _chromSize(chromSize), _maxVal(maxVal)
eoInitCSDVP(unsigned _chromSize, unsigned _maxVal, int _seed)
: chromSize(_chromSize), maxVal(_maxVal), seed(_seed)
{}
virtual void operator()(EOT& chrom)
{
chrom.resize(_chromSize);
std::vector<int> rdVec;
for(int i = 0; i < this->_maxVal; i++)
rdVec.push_back(i);
std::random_shuffle(rdVec.begin(), rdVec.end());
std::vector<int> r2; //trying tmp struct
for(int i = 0; i < rdVec.size(); i++)
r2.push_back(rdVec.at(i));
for(unsigned idx=0;idx <chrom.size();idx++)
{
unsigned int tmp = r2.at(idx);
// chrom[idx]=tmp; // DOES NOT WORK
chrom[idx] = idx; //DOES WORK;
}
//std::random_shuffle(chrom.begin(), chrom.end(),gen);
chrom.resize(0);
for(int i = 0; i < maxVal; i++)
chrom.push_back(i);
std::random_shuffle(chrom.begin(), chrom.end());
chrom.resize(chromSize);
chrom.invalidate();
}
private :
unsigned _chromSize;
unsigned _maxVal;
unsigned chromSize;
unsigned maxVal;
UF_random_generator<unsigned int> gen;
int _seed;
int seed;
};
typedef eoInitCSDVP<Cursus> CursusInit;
......
......@@ -5,7 +5,91 @@
#include <eoShiftMutation.h>
#include "cursus.h"
#include "model/constraints/repetitionConstraints.h"
template <class EOT>
class mutCSDVP: public eoMonOp<EOT>
{
public:
mutCSDVP(CSDVP& _pb, ConstraintsRepetition& _ctr):pb(_pb),ctr(_ctr){}
//_CourseID subastraction from _chrom
void notin(std::vector<int>& _courseID, std::vector<int>& _chrom){
int tmp;
for (int i=0; i<_chrom.size(); i++){
tmp=0;
while(tmp<_courseID.size() && _chrom[i]!=_courseID[tmp])
tmp++;
if(tmp<=_courseID.size()){
_courseID[tmp]=_courseID[_courseID.size()-1];
_courseID.pop_back();
}
}
}
virtual bool operator()(EOT& _chrom){
bool res=false;
std::pair<bool, double> ctrRes= ctr.integrityCheck(_chrom);
//Integrity Check AFTER CROSS
assert(ctrRes.first);
//_chrom.printOn(std::cout);
//std::cout << std::endl;
//On cherche une mutation
unsigned int i;
const std::vector<int> TF = pb.timeFrames();
unsigned int nbTF=TF.size();
unsigned int sizeTF=_chrom.size()/nbTF;
const std::vector<Course> catalogue = pb.coursesCatalogue();
//std::cout << "Catalogue Size: " << catalogue.size() << ", nbTF: " << nbTF << ", sizeTF: " << sizeTF << std::endl;
//std::cout << "TF min: " << pb.cfg_courseByTFMin() << std::endl;
std::vector<int> courseID;
std::vector<int> TFavailabilities;
for(i=0; i<catalogue.size(); i++)
courseID.push_back(i);
//on garde que les cours pas encore choisi
notin(courseID,_chrom);
/*
//std::cout << "id des cours dispos: ";
for(int i=0; i<courseID.size(); i++)
std::cout << courseID[i] << " ";
std::cout << std::endl;
*/
//on en prend un au hasard dedans
unsigned int newCourse=eo::rng.random(courseID.size());
//std::cout << "newCourse: " << courseID[newCourse] << std::endl;
//on regarde les time frame dispo pour ce cours
TFavailabilities=catalogue[courseID[newCourse]].timeFrame();
//on tape au hazard dans une de ces time frame
unsigned int replaceTF=eo::rng.random(TFavailabilities.size());
unsigned int oldCourse=eo::rng.random(sizeTF);
//std::cout << "Try to Replace " << _chrom[((TFavailabilities[replaceTF]-pb.cfg_minimalTimeFrame())*sizeTF) + oldCourse] << " by " << courseID[newCourse] << " in TF: " << TFavailabilities[replaceTF] << std::endl;
_chrom[((TFavailabilities[replaceTF]-pb.cfg_minimalTimeFrame())*sizeTF) + oldCourse]=courseID[newCourse];
//std::cout << std::endl << std::endl;
ctrRes= ctr.integrityCheck(_chrom);
res=true;
_chrom.invalidate();
return res;
}
private:
CSDVP pb;
ConstraintsRepetition ctr;
};
typedef mutCSDVP<Cursus> CursusMutation;
typedef eoSwapMutation<Cursus> CursusMutation;
#endif // SRC_MODEL_EA_MUTATION_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment