Logo Search packages:      
Sourcecode: fet version File versions

int TimeSpaceChromosome::hardFitness ( Rules r,
QString *  conflictsString = NULL 
)

Computes the unsatisfied compulsory constraints (stores the result in the variable _hardFitness, so that it is not recomputed when needed again later). ATTENTION: if the rules change, the user has to reset _hardFitness to -1

If conflictsString is not null, then this function will append at this string an explanation of the conflicts.

Reimplemented from TimeChromosome.

Definition at line 161 of file timespacechromosome.cpp.

References _hardFitness, _softFitness, SpaceConstraint::compulsory, TimeConstraint::compulsory, SpaceConstraint::fitness(), TimeConstraint::fitness(), Rules::fixedDay, Rules::fixedHour, Rules::fixedRoom, Rules::initialized, Rules::internalSpaceConstraintsList, Rules::internalStructureComputed, Rules::internalTimeConstraintsList, Rules::nDaysPerWeek, Rules::nInternalActivities, Rules::nInternalSpaceConstraints, Rules::nInternalTimeConstraints, SpaceChromosome::rooms, Rules::sameDay, Rules::sameHour, and TimeChromosome::times.

Referenced by TimeSpacePopulation::sort().

                                                                      {
      assert(r.initialized);
      assert(r.internalStructureComputed);

      if(this->_hardFitness>=0 && conflictsString==NULL)
      //If you want to see the log, you have to recompute the fitness, even if it is
      //already computed
            return this->_hardFitness;
            
      //Repair the chromosome - we enter here with the assumption that
      //the time constraints of type ConstraintActivityPreferredTime,
      //ConstraintActivitiesSameTime and Constraint2ActivitiesConsecutive
      //do not contradict one with each other.
      //I had good reasons here not to repair activities that are scheduled too late
      //(that is, have the hour+duration>nHoursPerDay.
      //The reason is that there might be a mutation by swapping 2 activities,
      //and I want it to consider all the variants.
      //I might be wrong :-)
      
      //1)preferred times
      for(int i=0; i<r.nInternalActivities; i++){
            if(r.fixedDay[i]>=0 && r.fixedHour[i]>=0){
                  this->times[i] = r.fixedDay[i] + r.fixedHour[i] * r.nDaysPerWeek;
            }
            else if(r.fixedDay[i]>=0 && this->times[i]!=UNALLOCATED_TIME){
                  this->times[i] = r.fixedDay[i] + (this->times[i]/r.nDaysPerWeek)*r.nDaysPerWeek;
            }
            else if(r.fixedHour[i]>=0 && this->times[i]!=UNALLOCATED_TIME){
                  this->times[i] = (this->times[i]%r.nDaysPerWeek) + r.fixedHour[i]*r.nDaysPerWeek;
            }
      }
      //2)same starting day and/or hour
      for(int i=0; i<r.nInternalActivities; i++){
            if(r.sameDay[i]>=0 && r.sameHour[i]>=0 && this->times[r.sameDay[i]]!=UNALLOCATED_TIME && this->times[r.sameHour[i]]!=UNALLOCATED_TIME){
                  int d = this->times[r.sameDay[i]] % r.nDaysPerWeek;
                  int h = this->times[r.sameHour[i]] / r.nDaysPerWeek;
                  this->times[i] = d + h * r.nDaysPerWeek;
                  if(r.fixedDay[i]>=0)
                        assert(r.fixedDay[i]==d);
                  if(r.fixedHour[i]>=0)
                        assert(r.fixedHour[i]==h);
            }
            if(r.sameDay[i]>=0 && this->times[i]!=UNALLOCATED_SPACE && this->times[r.sameDay[i]]!=UNALLOCATED_TIME){
                  int d = this->times[r.sameDay[i]] % r.nDaysPerWeek;
                  int h = this->times[i] / r.nDaysPerWeek;
                  this->times[i] = d + h * r.nDaysPerWeek;
                  if(r.fixedDay[i]>=0)
                        assert(r.fixedDay[i]==d);
            }
            if(r.sameHour[i]>=0 && this->times[i]!=UNALLOCATED_SPACE && this->times[r.sameHour[i]]!=UNALLOCATED_TIME){
                  int d = this->times[i] % r.nDaysPerWeek;
                  int h = this->times[r.sameHour[i]] / r.nDaysPerWeek;
                  this->times[i] = d + h * r.nDaysPerWeek;
                  if(r.fixedHour[i]>=0)
                        assert(r.fixedHour[i]==h);
            }
      }
      
      //repairing - space constraints
      for(int i=0; i<r.nInternalActivities; i++)
            if(r.fixedRoom[i]>=0)
                  this->rooms[i]=r.fixedRoom[i];
      
      this->_hardFitness=0;
      //here we must not have compulsory activity preferred time nor 
      //compulsory activities same time
      //Also, here I compute soft fitness (for faster results,
      //I do not want to pass again through the constraints)
      this->_softFitness=0;
      for(int i=0; i<r.nInternalTimeConstraints; i++)
            if(r.internalTimeConstraintsList[i]->compulsory==true)
                  this->_hardFitness += r.internalTimeConstraintsList[i]->fitness(*this, r, conflictsString);
            else
                  //not logged here
                  this->_softFitness += r.internalTimeConstraintsList[i]->fitness(*this, r, NULL);
                  
      int days[MAX_ACTIVITIES], hours[MAX_ACTIVITIES];
      for(int i=0; i<r.nInternalActivities; i++)
            if(this->times[i]!=UNALLOCATED_TIME)
            {
                  days[i]=this->times[i]%r.nDaysPerWeek;
                  hours[i]=this->times[i]/r.nDaysPerWeek;
            }
            else{
                  days[i]=UNALLOCATED_TIME;
                  hours[i]=UNALLOCATED_TIME;    
            }
                  
      for(int i=0; i<r.nInternalSpaceConstraints; i++)
            if(r.internalSpaceConstraintsList[i]->compulsory==true)
                  this->_hardFitness += r.internalSpaceConstraintsList[i]->fitness(*this, r, days, hours, conflictsString);
            else
                  //not logged here
                  this->_softFitness += r.internalSpaceConstraintsList[i]->fitness(*this, r, days, hours, NULL);

      return this->_hardFitness;
}


Generated by  Doxygen 1.6.0   Back to index