Logo Search packages:      
Sourcecode: fet version File versions

int TimeChromosome::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 in TimeSpaceChromosome.

Definition at line 144 of file timechromosome.cpp.

References _hardFitness, _softFitness, TimeConstraint::compulsory, TimeConstraint::fitness(), Rules::fixedDay, Rules::fixedHour, Rules::initialized, Rules::internalStructureComputed, Rules::internalTimeConstraintsList, Rules::nDaysPerWeek, Rules::nInternalActivities, Rules::nInternalTimeConstraints, Rules::sameDay, Rules::sameHour, and times.

Referenced by TimePopulation::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 ConstraintActivitiesSameStartingHour
      //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);
            }
      }
      
      this->_hardFitness=0;
      //here we must not have compulsory activity preferred time nor 
      //compulsory activities same time and/or hour
      //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);

      return this->_hardFitness;
}


Generated by  Doxygen 1.6.0   Back to index