Commit f19cafc7 authored by Alexis Lebis's avatar Alexis Lebis

CSDVP courses random gen ok

parent 4dbb9a69
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "model/problem.h" #include "model/problem.h"
#include "model/course.h" #include "model/course.h"
#include "model/competency.h" #include "model/competency.h"
#include "model/tools.h"
#include "model/exception/csdvpOverlapingBoundaryException.h" #include "model/exception/csdvpOverlapingBoundaryException.h"
...@@ -140,5 +141,33 @@ int main(int argc, char* argv[]) ...@@ -140,5 +141,33 @@ int main(int argc, char* argv[])
assert(pb.timeFrames().at(pb.timeFrames().size()-1) == pb.cfg_maximalTimeFrame()); assert(pb.timeFrames().at(pb.timeFrames().size()-1) == pb.cfg_maximalTimeFrame());
std::cout << "TimeFrames vector correctly init" << std::endl; 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; return EXIT_SUCCESS;
} }
\ No newline at end of file
...@@ -64,25 +64,28 @@ Course::Course(int id, int ects, std::string name) ...@@ -64,25 +64,28 @@ Course::Course(int id, int ects, std::string name)
{this->_weightedTeached = wComps;} {this->_weightedTeached = wComps;}
// ADDER // ADDER
void Course::addTeachedComp(std::pair<Competency,double> & wComp) bool Course::addTeachedComp(std::pair<Competency,double> & wComp)
{ {
if( _duplicataProtection(&(this->_weightedTeached), wComp.first)) if( _duplicataProtection(&(this->_weightedTeached), wComp.first))
return; return false;
this->_weightedTeached.push_back(wComp); this->_weightedTeached.push_back(wComp);
return true;
} }
void Course::addPrerequisite(Competency & prereq) bool Course::addPrerequisite(Competency & prereq)
{ {
if( duplicataFlag((this->_prerequisites), prereq) ) if( duplicataFlag((this->_prerequisites), prereq) )
return; return false;
this->_prerequisites.push_back(prereq); this->_prerequisites.push_back(prereq);
return true;
} }
void Course::addTemporalFrame(int time) bool Course::addTemporalFrame(int time)
{ {
if(time < 0) if(time < 0)
throw CourseTemporalFrameException(this, time); throw CourseTemporalFrameException(this, time);
if( duplicataFlag((this->_temporalAvailability), time) ) if( duplicataFlag((this->_temporalAvailability), time) )
return; return false;
this->_temporalAvailability.push_back(time); this->_temporalAvailability.push_back(time);
return true;
} }
// DELETER // DELETER
...@@ -230,6 +233,11 @@ bool Course::_fullEquality(const Course & c) const ...@@ -230,6 +233,11 @@ bool Course::_fullEquality(const Course & c) const
std::ostream& operator<<(std::ostream& Stream, const Course & c) 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 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; Stream << s;
return Stream; return Stream;
} }
......
...@@ -82,9 +82,10 @@ class Course ...@@ -82,9 +82,10 @@ class Course
void setTeachedComps(std::vector<std::pair<Competency,double>>&); void setTeachedComps(std::vector<std::pair<Competency,double>>&);
// ADDER // ADDER
void addTeachedComp(std::pair<Competency,double> &); /**Returns true if the element has been inserted. Returns false if the insertion has not been performed to prevent a duplicata*/
void addPrerequisite(Competency &); bool addTeachedComp(std::pair<Competency,double> &);
void addTemporalFrame(int); bool addPrerequisite(Competency &);
bool addTemporalFrame(int);
// DELETER // DELETER
/// rmPrerequisite return a pointer to the removed competency from the _prerequisite, or NULL if not found. /// 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 @@ ...@@ -9,6 +9,7 @@
#include "exception/csdvpOverlapingBoundaryException.h" #include "exception/csdvpOverlapingBoundaryException.h"
#include "exception/notImplementedException.h" #include "exception/notImplementedException.h"
#include "exception/csdvpBadlyConfigException.h"
int CSDVP::CSDVP_COUNTER = 0; int CSDVP::CSDVP_COUNTER = 0;
...@@ -69,26 +70,29 @@ int CSDVP::CSDVP_COUNTER = 0; ...@@ -69,26 +70,29 @@ int CSDVP::CSDVP_COUNTER = 0;
void CSDVP::setCompetenciesCatalogue(std::vector<Competency> & c) void CSDVP::setCompetenciesCatalogue(std::vector<Competency> & c)
{this->_availableCompentecies;} {this->_availableCompentecies;}
// ADDER // ADDER
void CSDVP::addTimeFrame(int tF) bool CSDVP::addTimeFrame(int tF)
{ {
if(duplicataFlag(this->_timeFrames, tF)) if(duplicataFlag(this->_timeFrames, tF))
return; // NTD return false; // NTD
this->_timeFrames.push_back(tF); this->_timeFrames.push_back(tF);
return true;
} }
void CSDVP::addCourseToCatalogue(Course & c) bool CSDVP::addCourseToCatalogue(Course & c)
{ {
if(duplicataFlag(this->_availableCourses, c)) if(duplicataFlag(this->_availableCourses, c))
return; return false;;
this->_availableCourses.push_back(c); this->_availableCourses.push_back(c);
return true;
} }
void CSDVP::addCompetencyToCatalogue(Competency & c) bool CSDVP::addCompetencyToCatalogue(Competency & c)
{ {
if(duplicataFlag(this->_availableCompentecies, c)) if(duplicataFlag(this->_availableCompentecies, c))
return; return false;
this->_availableCompentecies.push_back(c); this->_availableCompentecies.push_back(c);
return true;
} }
...@@ -123,6 +127,12 @@ int CSDVP::CSDVP_COUNTER = 0; ...@@ -123,6 +127,12 @@ int CSDVP::CSDVP_COUNTER = 0;
throw CSDVPOverlapingBoundariesException(this); 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; this->_isConfig = true;
return this->_isConfig; return this->_isConfig;
} }
...@@ -186,6 +196,41 @@ int CSDVP::CSDVP_COUNTER = 0; ...@@ -186,6 +196,41 @@ int CSDVP::CSDVP_COUNTER = 0;
{ {
pb.addTimeFrame(pb.cfg_minimalTimeFrame()+i); 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 --------- // --------- END GENERATION RELATED FUNCTIONS ---------
......
...@@ -129,9 +129,10 @@ class CSDVP ...@@ -129,9 +129,10 @@ class CSDVP
void setCompetenciesCatalogue(std::vector<Competency> &); void setCompetenciesCatalogue(std::vector<Competency> &);
// ADDER // ADDER
//Any adder has a duplicata protection. Tend to favor addX instead of setX to prevent duplicata //Any adder has a duplicata protection. Tend to favor addX instead of setX to prevent duplicata
void addTimeFrame(int t); /**Returns true if the element has been inserted. Returns false if the insertion has not been performed to prevent a duplicata*/
void addCourseToCatalogue(Course & c); bool addTimeFrame(int t);
void addCompetencyToCatalogue(Competency & c); bool addCourseToCatalogue(Course & c);
bool addCompetencyToCatalogue(Competency & c);
// === FUNC // === FUNC
/// Checks all configuration attributes. If they have been all set, then isConfig is set to true /// Checks all configuration attributes. If they have been all set, then isConfig is set to true
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
template<typename T> template<typename T>
/** Searches into vec the element findMe. The class T must have T() defined ( T()=default; is OK) */ /** 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; std::pair<int, T> res;
...@@ -28,7 +28,7 @@ static std::pair<int, T> findInVector(std::vector<T> & vec, const T & findMe) ...@@ -28,7 +28,7 @@ static std::pair<int, T> findInVector(std::vector<T> & vec, const T & findMe)
template<typename T> template<typename T>
/** duplicataFlag returns true if the value (2nd param) searched into (1st param) is found */ /** 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); std::pair<int, T> res = findInVector(toProtect, toAdd);
if(res.first < 0) 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