Commit f42d9a7a authored by jeremie.humeau's avatar jeremie.humeau

init + eval

parent ee30dd60
......@@ -11,6 +11,7 @@
#include <model/ea/cursus.h>
#include <model/ea/initializer.h>
#include <model/ea/initConstraint.h>
#include <model/ea/mutation.h>
#include <model/ea/crossover.h>
#include <model/ea/evaluator.h>
......@@ -91,8 +92,8 @@ int main(int argc, char* argv[]){
job.addPrerequisite(tmpC);
tmpC = pb.competencyCatalogue().at(1);
job.addPrerequisite(tmpC);
tmpC = Competency::build(0.5,"Wesh");
job.addPrerequisite(tmpC);
//tmpC = Competency::build(0.5,"Wesh");
//job.addPrerequisite(tmpC);
tmpC = pb.competencyCatalogue().at(8);
job.addPrerequisite(tmpC);
// ===== END PB CONFIG =====
......@@ -103,7 +104,7 @@ int main(int argc, char* argv[]){
std::cout << "cfg_quantityCourses() : " << std::to_string(pb.cfg_quantityCourses()) << std::endl;
/**@todo make size of the pb accessible as well as size of an individu*/
int size_of_the_pb = 30;
int size_of_the_pb = 100;
ConstraintsECTS ctrECTS(pb, job);
ConstraintsRepetition ctrRep(pb, job);
......@@ -112,18 +113,19 @@ int main(int argc, char* argv[]){
std::pair<bool,double> res;
//CursusInit init(pb.getQuantityCoursesToPick()-5,0);
CursusInit init(pb.getQuantityCoursesToPick(),pb.coursesCatalogue().size(),pb.seed());
//CursusInit init(pb.getQuantityCoursesToPick(),pb.coursesCatalogue().size(),pb.seed());
CursusInitConstraint init(pb);
//pb.cfg_quantityCourses());//pb.getQuantityCoursesToPick(),pb.cfg_quantityCourses(), pb.seed());
CursusEval eval;
CursusEval eval(ctrRep, ctrJob, ctrECTS);
CursusCrossover cross(pb, ctrRep, init);
CursusMutation mut(pb, ctrRep);
eoGenContinue<Cursus> cont(1000); // runs for 100 gen
eoGenContinue<Cursus> cont(10000); // runs for 100 gen
//xOver, xOver rate, mutation, mutation rate
eoSGATransform<Cursus> transform(cross, 1, mut, 1);
eoDetTournamentSelect<Cursus> selectOne(2); //selection method by tournament, here against 2
eoSGATransform<Cursus> transform(cross, 0.1, mut, 0.7);
eoDetTournamentSelect<Cursus> selectOne(5); //selection method by tournament, here against 2
eoSelectPerc<Cursus> select(selectOne);
eoGenerationalReplacement<Cursus> replace;
eoPop<Cursus> pop;
......@@ -138,7 +140,7 @@ int main(int argc, char* argv[]){
//res = ctrRep.integrityCheck(c1);
//res = ctrJob.integrityCheck(c1);
res = ctrPrq.integrityCheck(c1);
std::cout << "IND#" << std::to_string(i) << "\nFirst: " << res.first << "\nSecond: " << std::to_string((double)res.second) << std::endl;
//std::cout << "IND#" << std::to_string(i) << "\nFirst: " << res.first << "\nSecond: " << std::to_string((double)res.second) << std::endl;
pop.push_back(c1);
}
//MUTATION TEST
......@@ -169,7 +171,7 @@ int main(int argc, char* argv[]){
for(int i = 0; i < pb.coursesCatalogue().size(); i++)
{
std::cout << pb.coursesCatalogue().at(i) << std::endl;
//std::cout << pb.coursesCatalogue().at(i) << std::endl;
}
......
......@@ -27,7 +27,7 @@ std::pair<bool, double> ConstraintsPrerequisites::integrityCheck(Cursus indiv)
bool changedTF = false;
std::cout << "=========START========" << std::endl;
//std::cout << "=========START========" << std::endl;
for(int i = 0; i < indiv.size() ; i++)
{
if(currentTF != i / this->_pb.cfg_pickedCoursesByTimeFrame())
......@@ -50,7 +50,7 @@ std::pair<bool, double> ConstraintsPrerequisites::integrityCheck(Cursus indiv)
// Then, we explore the current TF for new Comp
currentCourse = this->_pb.coursesCatalogue().at(indiv.at(i));
std::cout << "\t" << currentCourse << std::endl;
//std::cout << "\t" << currentCourse << std::endl;
//std::cout << "\tPrereq: " << std::to_string(currentCourse.prerequisites().size()) << std::endl;
nbPrereq += currentCourse.prerequisites().size();
......@@ -64,7 +64,7 @@ std::pair<bool, double> ConstraintsPrerequisites::integrityCheck(Cursus indiv)
if(alreadyExists.first >= 0) //Already Exists in the array
{
std::cout << currentCompetency.c_name() << " already exists" <<std::endl;
//std::cout << currentCompetency.c_name() << " already exists" <<std::endl;
try
{
compByTF.at(currentTF).at(alreadyExists.first).evolveTowards(currentCompetency.magnitude());
......@@ -75,7 +75,7 @@ std::pair<bool, double> ConstraintsPrerequisites::integrityCheck(Cursus indiv)
//compToAnswer.at(posFound.first) = e.getCompetency();
//std::cout << "INFO:\n(during ConstraintsProfession)\n\n Compentecy evolution throw an exception. Auto rebase. New value is " << e.getCompetency() << std::endl;
//std::cout << "Comp rebased val is" << compByTF.at(currentTF).at(alreadyExists.first) << std::endl;
std::cout << "exception.Rebased";
//std::cout << "exception.Rebased";
}
}
else
......@@ -98,22 +98,22 @@ std::pair<bool, double> ConstraintsPrerequisites::integrityCheck(Cursus indiv)
}
}
std::cout << "==EXPLORING COMP BY TF" << std::endl;
//std::cout << "==EXPLORING COMP BY TF" << std::endl;
for(int i = 0; i < compByTF.size(); i++)
{
std::cout << "TF#" << std::to_string(i) << std::endl;
//std::cout << "TF#" << std::to_string(i) << std::endl;
for(int j = 0; j < compByTF.at(i).size() ; j++)
{
std::cout << compByTF.at(i).at(j) << std::endl;
//std::cout << compByTF.at(i).at(j) << std::endl;
}
}
bool isOK = ((notFound == 0) && (notRespected == 0));
std::cout << "Not Found: " << std::to_string(notFound) << std::endl;
std::cout << "Not Respected: " << std::to_string(notRespected) << std::endl;
std::cout << "Nb Prereq: " << std::to_string(nbPrereq) << std::endl;
//std::cout << "Not Found: " << std::to_string(notFound) << std::endl;
//std::cout << "Not Respected: " << std::to_string(notRespected) << std::endl;
//std::cout << "Nb Prereq: " << std::to_string(nbPrereq) << std::endl;
double metric = 1.0 - ( (((double)2 * (double)notFound) + (double)notRespected ) / (2 * (double) nbPrereq) );
std::cout << "Metric: " << std::to_string(metric) << std::endl;
//std::cout << "Metric: " << std::to_string(metric) << std::endl;
return std::pair<bool, double>(isOK, metric);
}
......@@ -129,11 +129,11 @@ std::pair<int, int> ConstraintsPrerequisites::_prereqsInPreviousTF(std::vector<C
for(int i = 0; i < prereqs.size(); i++)
{
found = false;
std::cout << "Looking for " << prereqs.at(i) << std::endl;
//std::cout << "Looking for " << prereqs.at(i) << std::endl;
for(int j = 0 ; j < cInTF.size() && !found; j++)
{
std::cout << "\n\t" << cInTF.at(j) << std::endl;
//std::cout << "\n\t" << cInTF.at(j) << std::endl;
if(prereqs.at(i)==cInTF.at(j))
{
......@@ -146,6 +146,6 @@ std::pair<int, int> ConstraintsPrerequisites::_prereqsInPreviousTF(std::vector<C
if(!found)
notFound++;
}
std::cout << "NF: " << std::to_string(notFound) << " | NR: " << std::to_string(notRespected) << std::endl;
//std::cout << "NF: " << std::to_string(notFound) << " | NR: " << std::to_string(notRespected) << std::endl;
return std::pair<int, int>(notFound, notRespected);
}
\ No newline at end of file
}
......@@ -42,7 +42,7 @@ std::pair<bool, double> ConstraintsProfession::integrityCheck(Cursus indiv)
{
//Should has NTD here
//compToAnswer.at(posFound.first) = e.getCompetency();
std::cout << "INFO:\n(during ConstraintsProfession)\n\n Compentecy evolution throw an exception. Auto rebase. New value is " << e.getCompetency() << std::endl;
//std::cout << "INFO:\n(during ConstraintsProfession)\n\n Compentecy evolution throw an exception. Auto rebase. New value is " << e.getCompetency() << std::endl;
}
}
}
......@@ -57,10 +57,10 @@ std::pair<bool, double> ConstraintsProfession::integrityCheck(Cursus indiv)
score++;
}
std::cout << "Score: " << std::to_string(score) << std::endl;
std::cout << "Size: " << std::to_string(compToAnswer.size()) << std::endl;
//std::cout << "Score: " << std::to_string(score) << std::endl;
//std::cout << "Size: " << std::to_string(compToAnswer.size()) << std::endl;
bool res = score == 0;
return std::pair<bool, double>(res, 1 - ( (double)score / (double)compToAnswer.size()));
}
\ No newline at end of file
}
#ifndef SRC_MODEL_EA_CROSSOVER_H_
#define SRC_MODEL_EA_CROSSOVER_H_
#include <model/ea/initConstraint.h>
template <class EOT>
class crossCSDVP: public eoQuadOp<EOT>
......@@ -8,7 +9,7 @@ class crossCSDVP: public eoQuadOp<EOT>
public:
crossCSDVP(CSDVP& _pb, ConstraintsRepetition& _ctr, CursusInit& _init):pb(_pb),ctr(_ctr), init(_init), cpt(0){}
crossCSDVP(CSDVP& _pb, ConstraintsRepetition& _ctr, CursusInitConstraint& _init):pb(_pb),ctr(_ctr), init(_init), cpt(0){}
virtual bool operator()(EOT& _chrom1, EOT& _chrom2){
......@@ -141,7 +142,7 @@ class crossCSDVP: public eoQuadOp<EOT>
CSDVP pb;
ConstraintsRepetition ctr;
CursusInit init;
CursusInitConstraint init;
};
typedef crossCSDVP<Cursus> CursusCrossover;
......
......@@ -13,4 +13,4 @@
*/
typedef eoInt<eoMinimizingFitness> Cursus;
#endif // SRC_MODEL_EA_CURSUS_H_
\ No newline at end of file
#endif // SRC_MODEL_EA_CURSUS_H_
......@@ -2,7 +2,24 @@
void CursusEval::operator()(Cursus & _cursus){
double fit=0.0;
int pCE, pCP, pCR;
pCE=1;
pCR=1;
pCP=1;
std::pair<bool, double> resCE;
std::pair<bool, double> resCP;
std::pair<bool, double> resCR;
fit = 1.0;
resCE=ce.integrityCheck(_cursus);
resCP=cp.integrityCheck(_cursus);
resCR=cr.integrityCheck(_cursus);
//std::cout << resCP.first << " " << resCP.second << std::endl;
double sum=pCE*resCE.second + pCR*resCR.second + pCP*resCP.second;
fit=1.0/(1+sum)*100;
_cursus.fitness(fit);
}
......@@ -4,11 +4,24 @@
#include <eoEvalFunc.h>
#include "cursus.h"
#include <model/constraints/ectsConstraints.h>
#include <model/constraints/repetitionConstraints.h>
#include <model/constraints/professionConstraints.h>
#include <model/constraints/prerequisitesConstraints.h>
class CursusEval : public eoEvalFunc<Cursus>
{
public:
CursusEval(ConstraintsRepetition& _cr, ConstraintsProfession& _cp, ConstraintsECTS& _ce): cr(_cr), cp(_cp), ce(_ce){}
void operator()(Cursus & _cursus);
private:
ConstraintsRepetition cr;
ConstraintsProfession cp;
ConstraintsECTS ce;
};
#endif // SRC_MODEL_EA_EVALUATOR_H_
\ No newline at end of file
#endif // SRC_MODEL_EA_EVALUATOR_H_
#ifndef SRC_MODEL_EA_INITCONSTRAINT_H_
#define SRC_MODEL_EA_INITCONSTRAINT_H_
#include<queue>
#include<vector>
#include<cassert>
#include<algorithm>
#include<random>
#include<functional>
#include <eoInit.h>
#include "model/problem.h"
#include "cursus.h"
template <class EOT>
class eoInitConstraintCSDVP: public eoInit<EOT>
{
public:
typedef typename EOT::AtomType AtomType;
eoInitConstraintCSDVP(CSDVP& _pb): pb(_pb){
std::vector<int> tmp;
chromSize=pb.getQuantityCoursesToPick();
maxVal=pb.coursesCatalogue().size();
seed=pb.seed();
TF = pb.timeFrames();
nbTF=TF.size();
sizeTF=chromSize/nbTF;
catalogue = pb.coursesCatalogue();
possibleIDbyTF.resize(nbTF);
for(int i=0; i<maxVal; i++){
tmp=catalogue[i].timeFrame();
for(int j=0; j<tmp.size(); j++){
possibleIDbyTF[tmp[j]-pb.cfg_minimalTimeFrame()].push_back(i);
}
}
for(int i=0; i<possibleIDbyTF.size(); i++){
std::cout << "Possible course in TF " << i+pb.cfg_minimalTimeFrame() << ": ";
for(int j=0; j<possibleIDbyTF[i].size(); j++){
std::cout << possibleIDbyTF[i][j] << " ";
}
std::cout << std::endl;
}
}
virtual void operator()(EOT& chrom){
int cpt=0;
// std::cout << "Enter init" << std::endl;
unsigned int r=eo::rng.random(possibleIDbyTF[0].size());
chrom.resize(0);
chrom.push_back(possibleIDbyTF[0][r]);
//std::cout << "push " << possibleIDbyTF[0][r] << std::endl;
for(int i = 1; i < chromSize; i++){
cpt=0;
r=eo::rng.random(possibleIDbyTF[i/sizeTF].size());
while(!notin(chrom, possibleIDbyTF[i/sizeTF][r], i) && cpt<maxVal){
r=eo::rng.random(possibleIDbyTF[i/sizeTF].size());
cpt++;
}
if(cpt<maxVal){
std::cout << i << " " << r << " ";
chrom.push_back(possibleIDbyTF[i/sizeTF][r]);
//std::cout << "push " << possibleIDbyTF[i/sizeTF][r] << std::endl;
}
else{
r=eo::rng.random(maxVal);
while(!notin(chrom, r, i))
r=eo::rng.random(maxVal);
chrom.push_back(r);
//std::cout << "push with error " << r << std::endl;
}
}
chrom.invalidate();
// chrom.printOn(std::cout);
//std::cout << std::endl;
//std::cout << "End init" << std::endl;
}
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;
}
private :
std::vector< std::vector<int> > possibleIDbyTF;
std::vector<Course> catalogue;
std::vector<int> TF;
unsigned int nbTF;
unsigned int sizeTF;
CSDVP pb;
unsigned chromSize;
unsigned maxVal;
UF_random_generator<unsigned int> gen;
int seed;
};
typedef eoInitConstraintCSDVP<Cursus> CursusInitConstraint;
#endif // SRC_MODEL_EA_INITIALIZER_H_
#ifndef SRC_MODEL_EA_INITIALIZER_H_
#define SRC_MODEL_EA_INITIALIZER_H_
#include<queue>
#include<vector>
#include<cassert>
#include<algorithm>
#include<random>
#include<functional>
#include <eoInit.h>
#include "model/problem.h"
#include "cursus.h"
template <class EOT>
class eoInitCSDVP: public eoInit<EOT>
{
public:
typedef typename EOT::AtomType AtomType;
eoInitCSDVP(unsigned _chromSize, unsigned _maxVal, int _seed)
: chromSize(_chromSize), maxVal(_maxVal), seed(_seed)
{}
virtual void operator()(EOT& chrom)
{
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;
UF_random_generator<unsigned int> gen;
int seed;
};
typedef eoInitCSDVP<Cursus> CursusInit;
#endif // SRC_MODEL_EA_INITIALIZER_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