Logo Search packages:      
Sourcecode: fet version File versions

bool Rules::computeInternalStructure (  ) 

Internal structure initializer.

After any modification of the activities or students or teachers or constraints, there is a need to call this subroutine

Definition at line 79 of file rules.cpp.

References ConstraintActivitiesSameRoom::_activities, ConstraintTeacherRequiresRoom::_activities, ConstraintActivitiesSameStartingHour::_activities, ConstraintActivitiesSameStartingTime::_activities, ConstraintActivityPreferredRoom::_activity, ConstraintTeacherRequiresRoom::_nActivities, ConstraintActivityPreferredRoom::_room, ConstraintTeacherRequiresRoom::_room, activitiesList, ConstraintActivityPreferredTime::activityIndex, SpaceConstraint::compulsory, SpaceConstraint::computeInternalStructure(), Activity::computeInternalStructure(), ConstraintActivityPreferredTime::day, equipmentsList, fixedDay, fixedHour, fixedRoom, ConstraintActivityPreferredTime::hour, initialized, internalStructureComputed, ConstraintActivitiesSameRoom::n_activities, ConstraintActivitiesSameStartingHour::n_activities, ConstraintActivitiesSameStartingTime::n_activities, nDaysPerWeek, nHoursPerDay, nHoursPerWeek, roomsList, sameDay, sameHour, sameRoom, searchEquipment(), spaceConstraintsList, subjectsList, teachersList, timeConstraintsList, SpaceConstraint::type, and yearsList.

{
      //The order is important - firstly the teachers, subjects and students.
      //After that, the equipments.
      //After that, the rooms.
      //After that, the activities.
      //After that, the time constraints.
      //After that, initialization of the matrix "roomHasEquipment".
      //After that, the space constraints.

      int i;

      assert(this->initialized);

      //days and hours
      assert(this->nHoursPerDay>0);
      assert(this->nDaysPerWeek>0);
      this->nHoursPerWeek=this->nHoursPerDay*this->nDaysPerWeek;

      //teachers
      Teacher* tch;
      this->nInternalTeachers=this->teachersList.count();
      assert(this->nInternalTeachers<=MAX_TEACHERS);
      for(tch=this->teachersList.first(), i=0; tch; tch=this->teachersList.next(), i++)
            this->internalTeachersList[i]=tch;
      assert(i==this->nInternalTeachers);

      //subjects
      Subject* sbj;
      this->nInternalSubjects=this->subjectsList.count();
      assert(this->nInternalSubjects<=MAX_SUBJECTS);  
      for(sbj=this->subjectsList.first(), i=0; sbj; sbj=this->subjectsList.next(), i++)
            this->internalSubjectsList[i]=sbj;
      assert(i==this->nInternalSubjects);

      //students
      this->nInternalSubgroups=0;
      assert(this->yearsList.count()<=(uint)(MAX_YEARS));
      for(StudentsYear* sty=this->yearsList.first(); sty; sty=this->yearsList.next()){
            //if this year has no groups, insert something to simulate the whole year
            if(sty->groupsList.count()==0){
                  StudentsGroup* tmpGroup = new StudentsGroup();
                  tmpGroup->name = sty->name+" WHOLE YEAR";
                  tmpGroup->numberOfStudents = sty->numberOfStudents;
                  sty->groupsList.append(tmpGroup);
            }
            assert(sty->groupsList.count()<=(uint)(MAX_GROUPS_PER_YEAR));
            for(StudentsGroup* stg=sty->groupsList.first(); stg; stg=sty->groupsList.next()){
                  //if this group has no subgroups, insert something to simulate the whole group
                  if(stg->subgroupsList.count()==0){
                        StudentsSubgroup* tmpSubgroup = new StudentsSubgroup();
                        tmpSubgroup->name = stg->name+" WHOLE GROUP";
                        tmpSubgroup->numberOfStudents=stg->numberOfStudents;
                        stg->subgroupsList.append(tmpSubgroup);
                  }
                  assert(stg->subgroupsList.count()<=(uint)(MAX_SUBGROUPS_PER_GROUP));
                  for(StudentsSubgroup* sts=stg->subgroupsList.first(); sts; sts=stg->subgroupsList.next()){
                        bool existing=false;
                        for(int i=0; i<this->nInternalSubgroups; i++)
                              if(this->internalSubgroupsList[i]->name==sts->name){
                                    existing=true;
                                    break;
                              }
                        if(!existing){
                              assert(this->nInternalSubgroups<MAX_TOTAL_SUBGROUPS);
                              this->internalSubgroupsList[this->nInternalSubgroups++]=sts;
                        }
                  }
            }
      }

      //equipments internal list
      this->nInternalEquipments=0;
      assert(this->equipmentsList.count()<=(uint)(MAX_EQUIPMENTS));
      for(Equipment* eq=this->equipmentsList.first(); eq; eq=this->equipmentsList.next())
            this->internalEquipmentsList[this->nInternalEquipments++]=eq;
      assert((uint)(this->nInternalEquipments)==this->equipmentsList.count());

      //rooms internal list
      this->nInternalRooms=0;
      assert(this->roomsList.count()<=(uint)(MAX_ROOMS));
      for(Room* rm=this->roomsList.first(); rm; rm=this->roomsList.next())
            this->internalRoomsList[this->nInternalRooms++]=rm;
      assert((uint)(this->nInternalRooms)==this->roomsList.count());

      //activities
      Activity* act;
      assert(this->activitiesList.count()<=(uint)(MAX_ACTIVITIES));
      for(act=this->activitiesList.first(); act; act=this->activitiesList.next())
            act->computeInternalStructure(*this);

      this->nInternalActivities=this->activitiesList.count();
      assert(this->nInternalActivities<=MAX_ACTIVITIES);
      for(act=this->activitiesList.first(), i=0; act; act=this->activitiesList.next(), i++)
            this->internalActivitiesList[i]=*act;

      //time constraints
      assert(this->timeConstraintsList.count()<=(uint)(MAX_TIME_CONSTRAINTS));
      TimeConstraint* tctr;
      for(tctr=this->timeConstraintsList.first(); tctr; tctr=this->timeConstraintsList.next())
            tctr->computeInternalStructure(*this);

      for(int i=0; i<this->nInternalActivities; i++){
            this->fixedDay[i]=-1;
            this->fixedHour[i]=-1;
            this->sameDay[i]=-1;
            this->sameHour[i]=-1;
      }
      //getting rid of compulsory preferred time-s, same starting hour & same starting time-s - 
      //these will be used separately to repair the chromosomes (it was practically
      //found that this is the best method).    
      for(tctr=this->timeConstraintsList.first(), i=0; tctr; tctr=this->timeConstraintsList.next()){
            if(tctr->type==CONSTRAINT_ACTIVITY_PREFERRED_TIME && tctr->compulsory==true){
                  ConstraintActivityPreferredTime* c = (ConstraintActivityPreferredTime*)tctr;
                  this->fixedDay[c->activityIndex] = c->day;
                  this->fixedHour[c->activityIndex] = c->hour;
            }
            else if(tctr->type==CONSTRAINT_ACTIVITIES_SAME_STARTING_TIME && tctr->compulsory==true){
                  ConstraintActivitiesSameStartingTime* c=(ConstraintActivitiesSameStartingTime*)tctr;
                  int ai1=c->_activities[0];
                  for(int i=1; i<c->n_activities; i++){
                        int ai2=c->_activities[i];
                        this->sameDay[ai2]=ai1;
                        this->sameHour[ai2]=ai1;
                  }
            }
            else if(tctr->type==CONSTRAINT_ACTIVITIES_SAME_STARTING_HOUR && tctr->compulsory==true){
                  ConstraintActivitiesSameStartingHour* c=(ConstraintActivitiesSameStartingHour*)tctr;
                  int ai1=c->_activities[0];
                  for(int i=1; i<c->n_activities; i++){
                        int ai2=c->_activities[i];
                        this->sameHour[ai2]=ai1;
                  }
            }
            else{
                  this->internalTimeConstraintsList[i++]=tctr;
            }
      }

      //all activities will depend on only one "head"
      for(int i=0; i<this->nInternalActivities; i++){
            for(int j=0; j<this->nInternalActivities; j++){
                  int k=this->sameDay[j];
                  if(k>=0 && this->sameDay[k]>=0)
                        this->sameDay[j]=this->sameDay[k];
                  k=this->sameHour[j];
                  if(k>=0 && this->sameHour[k]>=0)
                        this->sameHour[j]=this->sameHour[k];
            }
      }
      //make the head have the fixed day & hour
      for(int i=0; i<this->nInternalActivities; i++){
            if(this->fixedDay[i]>=0 && this->sameDay[i]>=0){
                  bool t=this->fixedDay[this->sameDay[i]]==this->fixedDay[i] || this->fixedDay[this->sameDay[i]]==-1;
                  if(!t){
                        QMessageBox::warning(NULL, QObject::tr("FET information"), 
                               QObject::tr("Activity with id=%1 has fixed day on %2 and must be scheduled on the same\n"
                               " day with act. with id=%3, which is impossible. Please check your constraints").arg(this->internalActivitiesList[i].id)
                               .arg(this->fixedDay[i]).arg(this->internalActivitiesList[this->sameDay[i]].id));
                        this->internalStructureComputed=false;
                        return false;
                  }

                  assert(this->fixedDay[this->sameDay[i]]==this->fixedDay[i] || this->fixedDay[this->sameDay[i]]==-1);
                  this->fixedDay[this->sameDay[i]]=this->fixedDay[i];
            }
            if(this->fixedHour[i]>=0 && this->sameHour[i]>=0){
                  bool t=this->fixedHour[this->sameHour[i]]==this->fixedHour[i] || this->fixedHour[this->sameHour[i]]==-1;
                  if(!t){
                        QMessageBox::warning(NULL, QObject::tr("FET information"), 
                               QObject::tr("Activity with id=%1 has fixed hour on %2 and must be scheduled on the same\n"
                               " hour with act. with id=%3, which is impossible. Please check your constraints").arg(this->internalActivitiesList[i].id)
                               .arg(this->fixedHour[i]).arg(this->internalActivitiesList[this->sameHour[i]].id));
                        this->internalStructureComputed=false;
                        return false;
                  }

                  assert(this->fixedHour[this->sameHour[i]]==this->fixedHour[i] || this->fixedHour[this->sameHour[i]]==-1);
                  this->fixedHour[this->sameHour[i]]=this->fixedHour[i];
            }
      }
      //make all have the fixed day & hour
      for(int i=0; i<this->nInternalActivities; i++){
            if(this->sameDay[i]>=0){
                  assert(this->fixedDay[i]==this->fixedDay[this->sameDay[i]] || this->fixedDay[i]==-1);
                  this->fixedDay[i]=this->fixedDay[this->sameDay[i]];
            }
            if(this->sameHour[i]>=0){
                  assert(this->fixedHour[i]==this->fixedHour[this->sameHour[i]] || this->fixedHour[i]==-1);
                  this->fixedHour[i]=this->fixedHour[this->sameHour[i]];
            }
      }
      
      this->nInternalTimeConstraints=i;
      assert(this->nInternalTimeConstraints<=MAX_TIME_CONSTRAINTS);
      
      //roomHasEquipment
      for(int rm=0; rm<this->nInternalRooms; rm++){
            for(int eq=0; eq<this->nInternalEquipments; eq++){
                  this->roomHasEquipment[rm][eq] = 
                   (this->internalRoomsList[rm])->searchEquipment(this->internalEquipmentsList[eq]->name);
            }
      }

      //space constraints
      SpaceConstraint* sctr;
      assert(this->spaceConstraintsList.count()<=(uint)(MAX_SPACE_CONSTRAINTS));

      for(sctr=this->spaceConstraintsList.first(); sctr; sctr=this->spaceConstraintsList.next())
            sctr->computeInternalStructure(*this);
            
      for(int i=0; i<this->nInternalActivities; i++){
            this->fixedRoom[i]=-1;
            this->sameRoom[i]=-1;
      }

      for(sctr=this->spaceConstraintsList.first(), i=0; sctr; sctr=this->spaceConstraintsList.next()){
            if(sctr->type==CONSTRAINT_TEACHER_REQUIRES_ROOM && sctr->compulsory==true){
                  //works by repairing
                  ConstraintTeacherRequiresRoom* ctrr=(ConstraintTeacherRequiresRoom*) sctr;
                  for(int i=0; i<ctrr->_nActivities; i++)
                        this->fixedRoom[ctrr->_activities[i]]=ctrr->_room;
            }
            else if(sctr->type==CONSTRAINT_ACTIVITY_PREFERRED_ROOM && sctr->compulsory==true){
                  //works by repairing
                  ConstraintActivityPreferredRoom* capr=(ConstraintActivityPreferredRoom*) sctr;
                  this->fixedRoom[capr->_activity]=capr->_room;
            }
            else if(sctr->type==CONSTRAINT_ACTIVITIES_SAME_ROOM && sctr->compulsory==true){
                  ConstraintActivitiesSameRoom* c=(ConstraintActivitiesSameRoom*)sctr;
                  int ai1=c->_activities[0];
                  for(int i=1; i<c->n_activities; i++){
                        int ai2=c->_activities[i];
                        this->sameRoom[ai2]=ai1;
                  }
            }
            else
                  this->internalSpaceConstraintsList[i++]=sctr;
      }
      this->nInternalSpaceConstraints=i;
      assert(this->nInternalSpaceConstraints<=MAX_SPACE_CONSTRAINTS);

      //all activities will depend on only one "head"
      for(int i=0; i<this->nInternalActivities; i++){
            for(int j=0; j<this->nInternalActivities; j++){
                  int k=this->sameRoom[j];
                  if(k>=0 && this->sameRoom[k]>=0)
                        this->sameRoom[j]=this->sameRoom[k];
            }
      }
      //make the head have the fixed room
      for(int i=0; i<this->nInternalActivities; i++){
            if(this->fixedRoom[i]>=0 && this->sameRoom[i]>=0){
                  bool t=this->fixedRoom[this->sameRoom[i]]==this->fixedRoom[i] || this->fixedRoom[this->sameRoom[i]]==-1;
                  if(!t){
                        QMessageBox::warning(NULL, QObject::tr("FET information"), 
                               QObject::tr("Activity with id=%1 has fixed room in %2 and must be scheduled in the same\n"
                               " room with act. with id=%3, which is impossible. Please check your constraints").arg(this->internalActivitiesList[i].id)
                               .arg(this->fixedRoom[i]).arg(this->internalActivitiesList[this->sameRoom[i]].id));
                        this->internalStructureComputed=false;
                        return false;
                  }

                  assert(this->fixedRoom[this->sameRoom[i]]==this->fixedRoom[i] || this->fixedRoom[this->sameRoom[i]]==-1);
                  this->fixedRoom[this->sameRoom[i]]=this->fixedRoom[i];
            }
      }
      //make all have the fixed room
      for(int i=0; i<this->nInternalActivities; i++){
            if(this->sameRoom[i]>=0){
                  assert(this->fixedRoom[i]==this->fixedRoom[this->sameRoom[i]] || this->fixedRoom[i]==-1);
                  this->fixedRoom[i]=this->fixedRoom[this->sameRoom[i]];
            }
      }

      //done.
      this->internalStructureComputed=true;
      
      return true;
}


Generated by  Doxygen 1.6.0   Back to index