Logo Search packages:      
Sourcecode: fet version File versions

int ConstraintBasicCompulsorySpace::fitness ( SpaceChromosome c,
Rules r,
const int  days[],
const int  hours[],
QString *  conflictsString = NULL 
) [virtual]

The function that calculates the fitness of a space chromosome, according to this constraint. We need the rules to compute this fitness factor. We need also the allocation of the activities on days and hours. If conflictsString!=NULL, it will be initialized with a text explaining where this restriction is broken.

Implements SpaceConstraint.

Definition at line 136 of file spaceconstraint.cpp.

References Room::capacity, Rules::daysOfTheWeek, SpaceChromosome::getRoomsMatrix(), Rules::hoursOfTheDay, Activity::id, Rules::internalActivitiesList, Rules::internalRoomsList, Rules::internalStructureComputed, Room::name, Rules::nDaysPerWeek, Rules::nHoursPerDay, Rules::nInternalActivities, Rules::nInternalRooms, Activity::nTotalStudents, Activity::parity, SpaceChromosome::rooms, and SpaceConstraint::weight.

      {

      assert(r.internalStructureComputed);

      int roomsConflicts;

      //This constraint fitness calculation routine is called firstly,
      //so we can compute the rooms conflicts faster this way.
      if(crt_chrom!=&c || crt_rules!=&r){
            roomsConflicts = c.getRoomsMatrix(r, days, hours, roomsMatrix);

            crt_chrom = &c;
            crt_rules = &r;
      }
      else{
            cout<<"Warning here ****"<<endl;
            roomsConflicts=-1;
      }

      int i;

      int unallocated; //unallocated activities
      int nre; //number of room exhaustions
      int nor; //number of overwhelmed rooms

      //part without logging....................................................................
if(conflictsString==NULL){
      //Unallocated activities
      unallocated=0;
      nor=0;
      for(i=0; i<r.nInternalActivities; i++)
            if(c.rooms[i]==UNALLOCATED_SPACE){
                  //Firstly, we consider a big clash each unallocated activity.
                  //Needs to be very a large constant, bigger than any other broken constraint.
                  unallocated += /*r.internalActivitiesList[i].duration * r.internalActivitiesList[i].nSubgroups * */ 10000;
                  //(an unallocated activity for a year is more important than an unallocated activity for a subgroup)
            }
            else{
                  //The capacity of each room must be respected
                  //(the number of students must be less than the capacity)
                  int rm=c.rooms[i];
                  if(r.internalActivitiesList[i].nTotalStudents>r.internalRoomsList[rm]->capacity){
                        if(r.internalActivitiesList[i].parity==PARITY_WEEKLY)
                              nor+=2;
                        else
                              nor++;
                  }
            }

      //Below, for rooms, please remember that 2 means a weekly activity
      //and 1 bi-weekly one. So, is the matrix roomsMatrix[rooms][day][hour]==2,
      //it is ok.

      //Calculates the number of rooms exhaustion (when a room is occupied
      //for more than one activity at the same time)
      /*nre=0;
      for(i=0; i<r.nInternalRooms; i++)
            for(int j=0; j<r.nDaysPerWeek; j++)
                  for(int k=0; k<r.nHoursPerDay; k++){
                        int tmp=roomsMatrix[i][j][k]-2;
                        if(tmp>0){
                              if(conflictsString!=NULL){
                                    (*conflictsString)+="Space constraint basic ";
                                    (*conflictsString)+="compulsory: rooms with name ";
                                    (*conflictsString)+=r.internalRoomsList[i]->name;
                                    (*conflictsString)+=" has more than one allocated activity on";
                                    (*conflictsString)+=" day ";
                                    (*conflictsString)+=r.daysOfTheWeek[j];
                                    (*conflictsString)+=", hour ";
                                    (*conflictsString)+=QString::number(k);
                                    (*conflictsString)+=". This increases the fitness factor with ";
                                    (*conflictsString)+=QString::number(tmp*weight);
                                    (*conflictsString)+="\n";
                              }
                              nre+=tmp;
                        }
                  }*/
      nre = roomsConflicts;
}
      //part with logging....................................................................
else{
      //Unallocated activities
      unallocated=0;
      nor=0;
      for(i=0; i<r.nInternalActivities; i++)
            if(c.rooms[i]==UNALLOCATED_SPACE){
                  //Firstly, we consider a big clash each unallocated activity.
                  //Needs to be very a large constant, bigger than any other broken constraint.
                  unallocated += /*r.internalActivitiesList[i].duration * r.internalActivitiesList[i].nSubgroups * */ 10000;
                  //(an unallocated activity for a year is more important than an unallocated activity for a subgroup)
                  if(conflictsString!=NULL){
                        (*conflictsString) += QObject::tr("Space constraint basic compulsory: unallocated activity with id=%1").arg(r.internalActivitiesList[i].id);
                        (*conflictsString) += QObject::tr(" - this increases the conflicts factor with %1")
                              .arg(weight* /*r.internalActivitiesList[i].duration * r.internalActivitiesList[i].nSubgroups * */ 10000);
                        (*conflictsString) += "\n";
                  }
            }
            else{
                  //The capacity of each room must be respected
                  //(the number of students must be less than the capacity)
                  int rm=c.rooms[i];
                  if(r.internalActivitiesList[i].nTotalStudents>r.internalRoomsList[rm]->capacity){
                        int tmp;
                        if(r.internalActivitiesList[i].parity==PARITY_WEEKLY)
                              tmp=2;
                        else
                              tmp=1;

                        nor+=tmp;

                        if(conflictsString!=NULL){
                              QString s;
                              s=QObject::tr("Space constraint basic compulsory: room %1 has allocated activity with id %2 and the capacity of the room is overloaded")
                              .arg(r.internalRoomsList[rm]->name).arg(r.internalActivitiesList[i].id);
                              (*conflictsString) += s;
                              (*conflictsString) += "\n";
                        }
                  }
            }

      //Below, for rooms, please remember that 2 means a weekly activity
      //and 1 bi-weekly one. So, is the matrix roomsMatrix[rooms][day][hour]==2,
      //it is ok.

      //Calculates the number of rooms exhaustion (when a room is occupied
      //for more than one activity at the same time)
      nre=0;
      for(i=0; i<r.nInternalRooms; i++)
            for(int j=0; j<r.nDaysPerWeek; j++)
                  for(int k=0; k<r.nHoursPerDay; k++){
                        int tmp=roomsMatrix[i][j][k]-2;
                        if(tmp>0){
                              if(conflictsString!=NULL){
                                    (*conflictsString)+=QObject::tr("Space constraint basic compulsory: room with name %1 has more than one allocated activity on day %2, hour %3.")
                                          .arg(r.internalRoomsList[i]->name)
                                          .arg(r.daysOfTheWeek[j])
                                          .arg(r.hoursOfTheDay[k]);
                                    (*conflictsString)+=" ";
                                    (*conflictsString)+=QObject::tr("This increases the conflicts factor with %1").arg(tmp*weight);
                                    (*conflictsString)+="\n";
                              }
                              nre+=tmp;
                        }
                  }
}
      if(roomsConflicts!=-1)
            assert(nre==roomsConflicts); //just a check, works only on logged fitness calculation

      return int (ceil ( weight * (unallocated + nre + nor) ) ); //fitness factor
}


Generated by  Doxygen 1.6.0   Back to index