📄 spaceconstraint.cpp
字号:
return s;}bool ConstraintRoomNotAvailable::computeInternalStructure(Rules& r){ this->room_ID=r.searchRoom(this->roomName); assert(this->room_ID>=0); return true;}//critical function here - must be optimized for speedint ConstraintRoomNotAvailable::fitness( SpaceChromosome& c, Rules& r, const int days[/*MAX_ACTIVITIES*/], const int hours[/*MAX_ACTIVITIES*/], QString* conflictsString){ //if the matrices roomsMatrix is already calculated, do not calculate it again! if(crt_chrom!=&c || crt_rules!=&r){ rooms_conflicts = c.getRoomsMatrix(r, days, hours, roomsMatrix); crt_chrom = &c; crt_rules = &r; } //Calculates the number of hours when the roomr is supposed to be occupied, //but it is not available //This function consideres all the hours, I mean if there are for example 5 weekly courses //scheduled on that hour (which is already a broken compulsory constraint - we only //are allowed 1 weekly activity for a certain room at a certain hour) we calculate //5 broken constraints for that function. //TODO: decide if it is better to consider only 2 or 10 as a return value in this particular case //(currently it is 10) int i=this->room_ID; int j=this->d; int nbroken; //without logging if(conflictsString==NULL){ nbroken=0; for(int k=h1; k<h2; k++) if(roomsMatrix[i][j][k]>0) nbroken+=roomsMatrix[i][j][k]; } //with logging else{ nbroken=0; for(int k=h1; k<h2; k++) if(roomsMatrix[i][j][k]>0){ if(conflictsString!=NULL){ *conflictsString+= (QObject::tr("Space constraint room not available broken for room %1 on day %2, hour %3") .arg(r.internalRoomsList[i]->name) .arg(r.daysOfTheWeek[j]) .arg(k)); *conflictsString += ". "; *conflictsString += (QObject::tr("This increases the conflicts total by %1").arg(roomsMatrix[i][j][k]*weight)); *conflictsString += "\n"; } nbroken+=roomsMatrix[i][j][k]; } } return int (ceil ( weight * nbroken ) );}bool ConstraintRoomNotAvailable::isRelatedToActivity(Activity* a){ if(a) ; return false;}bool ConstraintRoomNotAvailable::isRelatedToTeacher(Teacher* t){ if(t) ; return false;}bool ConstraintRoomNotAvailable::isRelatedToSubject(Subject* s){ if(s) ; return false;}bool ConstraintRoomNotAvailable::isRelatedToSubjectTag(SubjectTag* s){ if(s) ; return false;}bool ConstraintRoomNotAvailable::isRelatedToStudentsSet(Rules& r, StudentsSet* s){ if(s) ; if(&r) ; return false;}bool ConstraintRoomNotAvailable::isRelatedToEquipment(Equipment* e){ if(e) ; return false;}bool ConstraintRoomNotAvailable::isRelatedToBuilding(Building* b){ if(b) ; return false;}bool ConstraintRoomNotAvailable::isRelatedToRoom(Room* r){ return this->roomName==r->name;}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////ConstraintRoomTypeNotAllowedSubjects::ConstraintRoomTypeNotAllowedSubjects() : SpaceConstraint(){ this->type=CONSTRAINT_ROOM_TYPE_NOT_ALLOWED_SUBJECTS;}ConstraintRoomTypeNotAllowedSubjects::ConstraintRoomTypeNotAllowedSubjects(double w, bool c, const QString& room_type) : SpaceConstraint(w, c){ this->type=CONSTRAINT_ROOM_TYPE_NOT_ALLOWED_SUBJECTS; this->roomType=room_type;}bool ConstraintRoomTypeNotAllowedSubjects::computeInternalStructure(Rules& r){ this->_nRooms=0; for(int i=0; i<r.nInternalRooms; i++) if(r.internalRoomsList[i]->type == this->roomType) this->_rooms[this->_nRooms++]=i; this->_nActivities=0; for(int i=0; i<r.nInternalActivities; i++) if(this->searchNotAllowedSubject(r.internalActivitiesList[i].subjectName) == true) this->_activities[this->_nActivities++]=i; return true;}void ConstraintRoomTypeNotAllowedSubjects::addNotAllowedSubject(const QString& subjectName){ this->subjects.append(subjectName);}int ConstraintRoomTypeNotAllowedSubjects::removeNotAllowedSubject(const QString& subjectName){ int tmp=this->subjects.remove(subjectName); return tmp;}bool ConstraintRoomTypeNotAllowedSubjects::searchNotAllowedSubject(const QString& subjectName){ int tmp=this->subjects.findIndex(subjectName); if(tmp>=0) return true; else return false;}QString ConstraintRoomTypeNotAllowedSubjects::getXmlDescription(Rules& r){ if(&r!=NULL) ; QString s="<ConstraintRoomTypeNotAllowedSubjects>\n"; s+=" <Weight>"+QString::number(weight)+"</Weight>\n"; s+=" <Compulsory>"; s+=yesNo(this->compulsory); s+="</Compulsory>\n"; s+=" <Room_Type>"+protect(this->roomType)+"</Room_Type>\n"; for(QStringList::Iterator it=this->subjects.begin(); it!=this->subjects.end(); it++) s+=" <Subject>"+protect(*it)+"</Subject>\n"; s+="</ConstraintRoomTypeNotAllowedSubjects>\n"; return s;}QString ConstraintRoomTypeNotAllowedSubjects::getDescription(Rules& r){ if(&r!=NULL) ; QString s="Room type not allowed subjects"; s+=", "; s+=QObject::tr("W:%1").arg(this->weight);s+=", "; s+=QObject::tr("C:%1").arg(yesNoTranslated(this->compulsory));s+=", "; s+=QObject::tr("RT:%1").arg(this->roomType);s+=","; for(QStringList::Iterator it=this->subjects.begin(); it!=this->subjects.end(); it++) s+=QObject::tr("S:%1").arg(*it); s+=","; return s;}QString ConstraintRoomTypeNotAllowedSubjects::getDetailedDescription(Rules& r){ if(&r!=NULL) ; QString s=QObject::tr("Space constraint"); s+="\n"; s+=QObject::tr("Room type not allowed subjects"); s+="\n"; s+=QObject::tr("Weight=%1").arg(this->weight);s+="\n"; s+=QObject::tr("Compulsory=%1").arg(yesNoTranslated(this->compulsory));s+="\n"; s+=QObject::tr("Room Type=%1").arg(this->roomType);s+="\n"; for(QStringList::Iterator it=this->subjects.begin(); it!=this->subjects.end(); it++){ s+=QObject::tr("Subject=%1").arg(*it); s+="\n"; } return s;}//critical function here - must be optimized for speedint ConstraintRoomTypeNotAllowedSubjects::fitness( SpaceChromosome& c, Rules& r, const int days[/*MAX_ACTIVITIES*/], const int hours[/*MAX_ACTIVITIES*/], QString* conflictsString){ //if the matrix roomsMatrix is already calculated, do not calculate it again! if(crt_chrom!=&c || crt_rules!=&r){ rooms_conflicts = c.getRoomsMatrix(r, days, hours, roomsMatrix); crt_chrom = &c; crt_rules = &r; } //Calculates the number of conflicts - the type of the room //does not allow a certain kind of subject //The fastest way seems to iterate over all activities //involved in this constraint (every not allowed subject), //find the scheduled room and check to see if the type //of this room is accepted or not. int nbroken; //without logging if(conflictsString==NULL){ nbroken=0; for(int i=0; i<this->_nActivities; i++){ int ac=this->_activities[i]; int parity=r.internalActivitiesList[ac].parity==PARITY_WEEKLY?2:1; int rm=c.rooms[ac]; if(rm==UNALLOCATED_SPACE) continue; QString typ=r.internalRoomsList[rm]->type; //maybe this can be a little speeded up by keeping a list of the types //and comparing integers instead of strings. if(typ==this->roomType) nbroken+=parity; } } //with logging else{ nbroken=0; for(int i=0; i<this->_nActivities; i++){ int ac=this->_activities[i]; int parity=r.internalActivitiesList[ac].parity==PARITY_WEEKLY?2:1; int rm=c.rooms[ac]; if(rm==UNALLOCATED_SPACE) continue; QString typ=r.internalRoomsList[rm]->type; //maybe this can be a little speeded up by keeping a list of the types //and comparing integers instead of strings. if(typ==this->roomType){ if(conflictsString!=NULL){ *conflictsString+= (QObject::tr("Space constraint room type not allowed subjects broken for room %1, activity with id %2") .arg(r.internalRoomsList[rm]->name) .arg(r.internalActivitiesList[ac].id)); *conflictsString += ". "; *conflictsString += (QObject::tr("This increases the conflicts total by %1").arg(parity*weight)); *conflictsString += "\n"; } nbroken+=parity; } } } return int (ceil ( weight * nbroken ) );}bool ConstraintRoomTypeNotAllowedSubjects::isRelatedToActivity(Activity* a){ if(a) ; return false;}bool ConstraintRoomTypeNotAllowedSubjects::isRelatedToTeacher(Teacher* t){ if(t) ; return false;}bool ConstraintRoomTypeNotAllowedSubjects::isRelatedToSubject(Subject* s){ if(this->searchNotAllowedSubject(s->name)==true) return true; return false;}bool ConstraintRoomTypeNotAllowedSubjects::isRelatedToSubjectTag(SubjectTag* s){ if(s) ; return false;}bool ConstraintRoomTypeNotAllowedSubjects::isRelatedToStudentsSet(Rules& r, StudentsSet* s){ if(s) ; if(&r) ; return false;}bool ConstraintRoomTypeNotAllowedSubjects::isRelatedToEquipment(Equipment* e){ if(e) ; return false;}bool ConstraintRoomTypeNotAllowedSubjects::isRelatedToBuilding(Building* b){ if(b) ; return false;}bool ConstraintRoomTypeNotAllowedSubjects::isRelatedToRoom(Room* r){ return this->roomType==r->type;}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////ConstraintSubjectRequiresEquipments::ConstraintSubjectRequiresEquipments() : SpaceConstraint(){ this->type=CONSTRAINT_SUBJECT_REQUIRES_EQUIPMENTS;}ConstraintSubjectRequiresEquipments::ConstraintSubjectRequiresEquipments(double w, bool c, const QString& subj) : SpaceConstraint(w, c){ this->type=CONSTRAINT_SUBJECT_REQUIRES_EQUIPMENTS; this->subjectName=subj;}bool ConstraintSubjectRequiresEquipments::computeInternalStructure(Rules& r){ //This procedure computes the internal list of all the activities //which correspond to the subject of the constraint. //Computes also the internal list of indices of equipments this->_nActivities=0; for(int ac=0; ac<r.nInternalActivities; ac++) if(r.internalActivitiesList[ac].subjectName == this->subjectName){ assert(this->_nActivities<MAX_ACTIVITIES_FOR_A_SUBJECT); this->_activities[this->_nActivities++]=ac; } this->_nEquipments=0; for(int eq=0; eq<r.nInternalEquipments; eq++) if(this->searchRequiredEquipment(r.internalEquipmentsList[eq]->name)){ assert(this->_nEquipments<MAX_EQUIPMENTS_FOR_A_CONSTRAINT); this->_equipments[this->_nEquipments++]=eq; } return true;}void ConstraintSubjectRequiresEquipments::addRequiredEquipment(const QString& equip){ this->equipmentsNames.append(equip);}int ConstraintSubjectRequiresEquipments::removeRequiredEquipment(const QString& equip){ int tmp=this->equipmentsNames.remove(equip); return tmp;}bool ConstraintSubjectRequiresEquipments::searchRequiredEquipment(const QString& equip){ int tmp=this->equipmentsNames.findIndex(equip); if(tmp>=0) return true; else return false;}QString ConstraintSubjectRequiresEquipments::getXmlDescription(Rules& r){ if(&r!=NULL) ; QString s="<ConstraintSubjectRequiresEquipments>\n"; s+=" <Weight>"+QString::number(weight)+"</Weight>\n"; s+=" <Compulsory>"; s+=yesNo(this->compulsory); s+="</Compulsory>\n"; s+=" <Subject>"+protect(this->subjectName)+"</Subject>\n"; for(QStringList::Iterator it=this->equipmentsNames.begin(); it!=this->equipmentsNames.end(); it++) s+=" <Equipment>"+protect(*it)+"</Equipment>\n"; s+="</ConstraintSubjectRequiresEquipments>\n"; return s;}QString ConstraintSubjectRequiresEquipments::getDescription(Rules& r){ if(&r!=NULL) ; QString s="Subject requires equipments"; s+=", "; s+=QObject::tr("W:%1").arg(this->weight);s+=", "; s+=QObject::tr("C:%1").arg(yesNoTranslated(this->compulsory));s+=", "; s+=QObject::tr("S:%1").arg(this->subjectName);s+=","; for(QStringList::Iterator it=this->equipmentsNames.begin(); it!=this->equipmentsNames.end(); it++) s+=QObject::tr("E:%1").arg(*it); s+=","; return s;}QString ConstraintSubjectRequiresEquipments::getDetailedDescription(Rules& r){ if(&r!=NULL) ; QString s=QObject::tr("Space constraint"); s+="\n"; s+=QObject::tr("Subject requires equipments"); s+="\n"; s+=QObject::tr("Weight=%1").arg(this->weight);s+="\n"; s+=QObject::tr("Compulsory=%1").arg(yesNoTranslated(this->compulsory));s+="\n"; s+=QObject::tr("Subject=%1").arg(this->subjectName);s+="\n"; for(QStringList::Iterator it=this->equipmentsNames.begin(); it!=this->equipmentsNames.end(); it++){ s+=QObject::tr("Equipment=%1").arg(*it); s+="\n"; } return s;}//critical function here - must be optimized for speedint ConstraintSubjectRequiresEquipments::fitness( SpaceChromosome& c, Rules& r, const int days[/*MAX_ACTIVITIES*/], const int hours[/*MAX_ACTIVITIES*/], QString* conflictsString){ //if the matrix roomsMatrix is already calculated, do not calculate it again! if(crt_chrom!=&c || crt_rules!=&r){ rooms_conflicts = c.getRoomsMatrix(r, days, hours, roomsMatrix); crt_chrom = &c; crt_rules = &r; } //Calculates the number of conflicts. //The fastest way seems to iterate over all activities //involved in this constraint (share the subject of this constraint), //find the scheduled room and check to see if this //room is accepted or not. int nbroken; //without logging
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -