Commit f19cafc7 authored by Alexis Lebis's avatar Alexis Lebis

CSDVP courses random gen ok

parent 4dbb9a69
......@@ -8,6 +8,7 @@
#include "model/problem.h"
#include "model/course.h"
#include "model/competency.h"
#include "model/tools.h"
#include "model/exception/csdvpOverlapingBoundaryException.h"
......@@ -140,5 +141,33 @@ int main(int argc, char* argv[])
assert(pb.timeFrames().at(pb.timeFrames().size()-1) == pb.cfg_maximalTimeFrame());
std::cout << "TimeFrames vector correctly init" << std::endl;
assert(pb.coursesCatalogue().size() > 0);
for(int i = 0; i < pb.coursesCatalogue().size(); i++)
assert(pb.coursesCatalogue().at(i).timeFrame().size() > 0);
std::cout << "Course catalogue randomized on TF OK." << std::endl;
std::cout << "Displaying size ("+std::to_string(pb.coursesCatalogue().size())+")/ 2 and -1 and +1 course" << std::endl;
std::cout << pb.coursesCatalogue().at(pb.coursesCatalogue().size()/2 - 1) << std::endl;
std::cout << pb.coursesCatalogue().at(pb.coursesCatalogue().size()/2) << std::endl;
std::cout << pb.coursesCatalogue().at(pb.coursesCatalogue().size()/2 + 1) << std::endl;
int counter;
std::cout << "Cheking Time Frames integrity..." << std::endl;
for(int i = 0; i < pb.timeFrames().size(); i++)
{
counter = 0;
for(int j = 0; j < pb.coursesCatalogue().size(); j++)
{
for(int k = 0; k < pb.coursesCatalogue().at(j).timeFrame().size(); k++)
{
if(pb.coursesCatalogue().at(j).timeFrame().at(k) == pb.timeFrames().at(i))
counter++;
assert(counter <= pb.cfg_courseByTFMax());
}
}
assert(counter >= pb.cfg_courseByTFMin());
}
std::cout << "TF Integrity is OK!" << std::endl;
return EXIT_SUCCESS;
}
\ No newline at end of file
......@@ -64,25 +64,28 @@ Course::Course(int id, int ects, std::string name)
{this->_weightedTeached = wComps;}
// ADDER
void Course::addTeachedComp(std::pair<Competency,double> & wComp)
bool Course::addTeachedComp(std::pair<Competency,double> & wComp)
{
if( _duplicataProtection(&(this->_weightedTeached), wComp.first))
return;
return false;
this->_weightedTeached.push_back(wComp);
return true;
}
void Course::addPrerequisite(Competency & prereq)
bool Course::addPrerequisite(Competency & prereq)
{
if( duplicataFlag((this->_prerequisites), prereq) )
return;
return false;
this->_prerequisites.push_back(prereq);
return true;
}
void Course::addTemporalFrame(int time)
bool Course::addTemporalFrame(int time)
{
if(time < 0)
throw CourseTemporalFrameException(this, time);
if( duplicataFlag((this->_temporalAvailability), time) )
return;
return false;
this->_temporalAvailability.push_back(time);
return true;
}
// DELETER
......@@ -230,6 +233,11 @@ bool Course::_fullEquality(const Course & c) const
std::ostream& operator<<(std::ostream& Stream, const Course & c)
{
std::string s = "Course\n\tid:"+std::to_string(c.id())+"\n\tname:"+c.name()+"\n\tECTS: "+std::to_string(c.ects());
std::string tf;
for(int i = 0; i < c.timeFrame().size()-1; i++)
tf+=std::to_string(c.timeFrame().at(i))+" ; ";
tf+=std::to_string(c.timeFrame().at(c.timeFrame().size()-1));
s+="\n\tTimeFrames: ["+tf+"]";
Stream << s;
return Stream;
}
......
......@@ -82,9 +82,10 @@ class Course
void setTeachedComps(std::vector<std::pair<Competency,double>>&);
// ADDER
void addTeachedComp(std::pair<Competency,double> &);
void addPrerequisite(Competency &);
void addTemporalFrame(int);
/**Returns true if the element has been inserted. Returns false if the insertion has not been performed to prevent a duplicata*/
bool addTeachedComp(std::pair<Competency,double> &);
bool addPrerequisite(Competency &);
bool addTemporalFrame(int);
// DELETER
/// rmPrerequisite return a pointer to the removed competency from the _prerequisite, or NULL if not found.
......
#ifndef SRC_MODEL_EXCEPTION_CSDVP_BADLY_CONFIG_EXCEPTION_H_
#define SRC_MODEL_EXCEPTION_CSDVP_BADLY_CONFIG_EXCEPTION_H_
#include <exception>
#include <string>
class CSDVPBadlyConfiguratedException : public std::exception
{
private:
std::string _msg;
public:
CSDVPBadlyConfiguratedException(std::string failedCondDescr) throw()
{
_msg = "The problem is badly configurated and thus, likely to 1) be overconstrained or 2) can't be generated.\nThe failed condition is: "+failedCondDescr;
}
virtual const char* what() const throw()
{
return _msg.c_str();
}
};
#endif //SRC_MODEL_EXCEPTION_CSDVP_BADLY_CONFIG_EXCEPTION_H_
\ No newline at end of file
......@@ -9,6 +9,7 @@
#include "exception/csdvpOverlapingBoundaryException.h"
#include "exception/notImplementedException.h"
#include "exception/csdvpBadlyConfigException.h"
int CSDVP::CSDVP_COUNTER = 0;
......@@ -69,26 +70,29 @@ int CSDVP::CSDVP_COUNTER = 0;
void CSDVP::setCompetenciesCatalogue(std::vector<Competency> & c)
{this->_availableCompentecies;}
// ADDER
void CSDVP::addTimeFrame(int tF)
bool CSDVP::addTimeFrame(int tF)
{
if(duplicataFlag(this->_timeFrames, tF))
return; // NTD
return false; // NTD
this->_timeFrames.push_back(tF);
return true;
}
void CSDVP::addCourseToCatalogue(Course & c)
bool CSDVP::addCourseToCatalogue(Course & c)
{
if(duplicataFlag(this->_availableCourses, c))
return;
return false;;
this->_availableCourses.push_back(c);
return true;
}
void CSDVP::addCompetencyToCatalogue(Competency & c)
bool CSDVP::addCompetencyToCatalogue(Competency & c)
{
if(duplicataFlag(this->_availableCompentecies, c))
return;
return false;
this->_availableCompentecies.push_back(c);
return true;
}
......@@ -123,6 +127,12 @@ int CSDVP::CSDVP_COUNTER = 0;
throw CSDVPOverlapingBoundariesException(this);
}
// verify if the has enough courses
if( this->_minimalCoursesByTimeFrame >= this->_quantityAvailableCourses)
throw CSDVPBadlyConfiguratedException("this->_minimalCoursesByTimeFrame > this->_quantityAvailableCourses");
if(this->_quantityAvailableCourses < this->_maximalCoursesByTimeFrame)
throw CSDVPBadlyConfiguratedException("this->_quantityAvailableCourses < this->_maximalCoursesByTimeFrame");
this->_isConfig = true;
return this->_isConfig;
}
......@@ -186,6 +196,41 @@ int CSDVP::CSDVP_COUNTER = 0;
{
pb.addTimeFrame(pb.cfg_minimalTimeFrame()+i);
}
/* COURSES ASSIGNATION
* First, we create a tmp sized coursed vector.
* Then, for each timeframe, we randomly pick between [0,vec.size] n courses (n is random in [_minimalCoursesByTimeFrame ; _ maximalCoursesByTimeFrame])
* Then, _availableCourses is all the courses that have at least one assigned TF in vec.
*/
std::vector<Course> tmpCourses;
for(int i = 0; i < pb._quantityAvailableCourses; i++)
{
tmpCourses.push_back(Course::build(CSDVP::_randomizeIn(pb.cfg_minimalTimeFrame(), pb.cfg_maximalTimeFrame())));
}
bool insertRez;
for(int i = 0; i < pb.timeFrames().size(); i++)
{
int nbCoursesInThisTF = CSDVP::_randomizeIn(pb._minimalCoursesByTimeFrame, pb._maximalCoursesByTimeFrame);
int courseIdx;
std::cout << "In the TF "+std::to_string(i)+" I plan " << std::to_string(nbCoursesInThisTF) << " courses." << std::endl;
for(int j = 0; j < nbCoursesInThisTF; j++)
{
insertRez = true;
courseIdx = CSDVP::_randomizeIn(0,tmpCourses.size()-1);
insertRez = tmpCourses.at(courseIdx).addTemporalFrame(pb.timeFrames().at(i));
if(!insertRez) //If a duplicata has been prevented, we do not count the attempt
j--;
}
}
for(int i = 0; i < tmpCourses.size(); i++)
if(tmpCourses.at(i).timeFrame().size() > 0)
pb.addCourseToCatalogue(tmpCourses.at(i));
}
// --------- END GENERATION RELATED FUNCTIONS ---------
......
......@@ -129,9 +129,10 @@ class CSDVP
void setCompetenciesCatalogue(std::vector<Competency> &);
// ADDER
//Any adder has a duplicata protection. Tend to favor addX instead of setX to prevent duplicata
void addTimeFrame(int t);
void addCourseToCatalogue(Course & c);
void addCompetencyToCatalogue(Competency & c);
/**Returns true if the element has been inserted. Returns false if the insertion has not been performed to prevent a duplicata*/
bool addTimeFrame(int t);
bool addCourseToCatalogue(Course & c);
bool addCompetencyToCatalogue(Competency & c);
// === FUNC
/// Checks all configuration attributes. If they have been all set, then isConfig is set to true
......
......@@ -7,7 +7,7 @@
template<typename T>
/** Searches into vec the element findMe. The class T must have T() defined ( T()=default; is OK) */
static std::pair<int, T> findInVector(std::vector<T> & vec, const T & findMe)
static std::pair<int, T> findInVector(std::vector<T> & vec, T & findMe)
{
std::pair<int, T> res;
......@@ -28,7 +28,7 @@ static std::pair<int, T> findInVector(std::vector<T> & vec, const T & findMe)
template<typename T>
/** duplicataFlag returns true if the value (2nd param) searched into (1st param) is found */
static bool duplicataFlag(std::vector<T> & toProtect, const T & toAdd)
static bool duplicataFlag(std::vector<T> & toProtect, T & toAdd)
{
std::pair<int, T> res = findInVector(toProtect, toAdd);
if(res.first < 0)
......
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