Commit 706034b4 authored by Alexis Lebis's avatar Alexis Lebis

Prerequistes constraint implem, seems ok, to test

parent e1f68a3a
......@@ -18,6 +18,7 @@
#include <model/constraints/ectsConstraints.h>
#include <model/constraints/repetitionConstraints.h>
#include <model/constraints/professionConstraints.h>
#include <model/constraints/prerequisitesConstraints.h>
#include <model/exception/magnitudeException.h>
#include <model/exception/competencyEvolvingException.h>
......@@ -100,7 +101,7 @@ 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());
CursusInit init(pb.getQuantityCoursesToPick(),0);//pb.cfg_quantityCourses());//pb.getQuantityCoursesToPick(),pb.cfg_quantityCourses(), pb.seed());
CursusEval eval;
CursusMutation mut;
CursusCrossover xOver;
......@@ -120,6 +121,7 @@ int main(int argc, char* argv[]){
ConstraintsECTS ctrECTS(pb, job);
ConstraintsRepetition ctrRep(pb, job);
ConstraintsProfession ctrJob(pb, job);
ConstraintsPrerequisites ctrPrq(pb, job);
std::pair<bool,double> res;
for(int i = 0; i < size_of_the_pb; i++)
......@@ -128,8 +130,9 @@ int main(int argc, char* argv[]){
eval(c1);
//res = ctrECTS.integrityCheck(c1);
//res = ctrRep.integrityCheck(c1);
res = ctrJob.integrityCheck(c1);
std::cout << "IND#" << std::to_string(i) << "\nFirst: " << res.first << "\nSecond: " << std::to_string(res.second) << std::endl;
//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;
pop.push_back(c1);
}
......
......@@ -7,6 +7,7 @@ SET (EXERCICE_SOURCES
ectsConstraints.cpp
repetitionConstraints.cpp
professionConstraints.cpp
prerequisitesConstraints.cpp
)
ADD_LIBRARY(lCstr STATIC ${EXERCICE_SOURCES})
\ No newline at end of file
#include "prerequisitesConstraints.h"
#include <string>
#include <utility>
#include "model/course.h"
#include "model/competency.h"
#include "model/tools.h"
#include "model/exception/competencyEvolvingException.h"
std::pair<bool, double> ConstraintsPrerequisites::integrityCheck(Cursus indiv)
{
int currentTF = 0;
int notFound = 0;
int notRespected = 0;
int score = 0;
int nbPrereq = 0;
//Each comp availble at a specific TF. Decay can be applied between i and i+1
std::vector<std::vector<Competency>> compByTF(this->_pb.timeFrames().size());
Course currentCourse;
Competency currentCompetency;
std::pair<int, int> prereqFound; prereqFound.first = 0; prereqFound.second = 0;
std::pair<int, Competency> alreadyExists;
bool changedTF = false;
for(int i = 0; i < indiv.size() ; i++)
{
if(currentTF != i / this->_pb.cfg_pickedCoursesByTimeFrame())
{
std::cout << "I've changed of TF" << std::endl;
changedTF = true;
}
else
changedTF = false;
currentTF = i / this->_pb.cfg_pickedCoursesByTimeFrame();
std::cout << "Current TF: " << std::to_string(currentTF) << std::endl;
//If changedTF is set to true, then we have changed of TF, we need to make available all the Comp in TF-1 here in TF, in addition to the new that'll be discovered in TF
for(int j = 0 ; changedTF && j < compByTF.at(currentTF-1).size(); j++)
{
// HERE, VARIABLE DECAY CAN BE APPLIED!!!!!!!!
compByTF.at(currentTF).push_back(compByTF.at(currentTF-1).at(j));
}
// Then, we explore the current TF for new Comp
currentCourse = this->_pb.coursesCatalogue().at(indiv.at(i));
std::cout << "\tPrereq: " << std::to_string(currentCourse.prerequisites().size()) << std::endl;
nbPrereq += currentCourse.prerequisites().size();
for(int j = 0; j < currentCourse.teachedCompetenciesWeighted().size() ; j++)
{
currentCompetency = currentCourse.teachedCompetenciesWeighted().at(j).first;
//Comp already exists in the array ?
alreadyExists = findInVector(compByTF.at(currentTF), currentCompetency);
if(alreadyExists.first >= 0) //Already Exists in the array
{
try
{
compByTF.at(currentTF).at(alreadyExists.first).evolveTowards(currentCompetency.magnitude());
}
catch(CompetencyEvolvingException & e) //if CEE is thrown, then magnitude has been auto rebased
{
//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;
}
}
else
{
// Check if prerequisites exists in TF-1
if(currentTF > 0)
prereqFound = this->_prereqsInPreviousTF(compByTF.at(currentTF-1), currentCourse.prerequisites());
notFound += prereqFound.first;
notRespected += prereqFound.second;
compByTF.at(currentTF).push_back(Competency::build(currentCompetency.c_magnitude().value(), currentCompetency.c_name()));
}
}
}
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;
double metric = 1.0 - ( (((double)2 * (double)notFound) + (double)notRespected ) / (2 * (double) nbPrereq) );
std::cout << "Metric: " << std::to_string(metric) << std::endl;
return std::pair<bool, double>(isOK, metric);
}
std::pair<int, int> ConstraintsPrerequisites::_prereqsInPreviousTF(std::vector<Competency> cInTF, std::vector<Competency> prereqs)
{
int notFound = 0;
int notRespected = 0;
bool found = false;
for(int i = 0; i < prereqs.size(); i++)
{
found = false;
for(int j = 0 ; j < cInTF.size() && !found; j++)
{
if(prereqs.at(i)==cInTF.at(j))
{
found = true;
if(prereqs.at(i).c_magnitude().value() > cInTF.at(j).c_magnitude().value())
notRespected++;
}
}
if(!found)
notFound++;
}
return std::pair<int, int>(notFound, notRespected);
}
\ No newline at end of file
#ifndef SRC_MODEL_CONSTRAINTS_PREREQUISITES_CONSTRAINTS_H_
#define SRC_MODEL_CONSTRAINTS_PREREQUISITES_CONSTRAINTS_H_
#include <vector>
#include <utility>
#include <eo>
#include "model/problem.h"
#include "model/profession.h"
#include "model/ea/cursus.h"
/**
* This class is used to verify constraints regarding the prerequisites of each courses of an individu (indiv).
* Each prerequisites of each courses must be respected. Note that the position of the courses in the indiv is important,
* since it represents how the courses are dispatched in the timeframes.
* Logically, a course see in TF t DO NOT CONTRIBUTE in the prerequisite for the courses in x < t
*/
class ConstraintsPrerequisites
{
private:
CSDVP _pb;
Profession _job;
/**
* Checks the prerequisites prereqs in the competencies cInTF (generally, the competencies from the previous TF).
* It returns a std::pair, where the first element indicates how many prerequisites HAS NOT BEEN FOUND.
* The second elements indicates how many prerequisites has not enought mastery (BUT EXISTS in cInTF!)
*/
std::pair<int,int> _prereqsInPreviousTF(std::vector<Competency> cInTF, std::vector<Competency> prereqs);
public:
ConstraintsPrerequisites(const CSDVP & csdvp, const Profession & job)
: _pb(csdvp), _job(job) {}
/** Integrity check is used to investigate wheteher or not one indiv respects the constraints represented by THIS.
* Returns a std::pair. First is a boolean set to true when the indiv passes the test and therefore is compilant with the constraint, false otherwise. Second is the associated metric, mostly usable during fitness calcul.
* @todo Decay competency magnitude
*/
std::pair<bool, double> integrityCheck(Cursus indiv);
};
#endif // SRC_MODEL_CONSTRAINTS_PREREQUISITES_CONSTRAINTS_H_
\ No newline at end of file
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