コード例 #1
0
	int32 Vector::scan(char* key, uint32 sz, uint32* index, Comparator* c)
	{
		Comparator* cUse = NULL;
		int32 ret = 0;

		if (!c && !m_comp)
			return FAILURE;

		if (!c)
			cUse = m_comp;
		else
			cUse = c;

		for (uint32 j = 0; j < getCount(); j++) {
			VectorSlot* vs = (VectorSlot*) m_pvDB[j];
			if (vs) {
				ret = cUse->compare(vs->dbData.toString(),
								vs->dbData.getCount(), key, sz);
				if (ret == SUCCESS) {
					if (index)
						*index = j;
					return SUCCESS;
				}
			}
		}

		return FAILURE;
	}
コード例 #2
0
ファイル: EqualSolutions.cpp プロジェクト: wkoder/mocde
/**
 * Compares two solutions.
 * @param solution1 Object representing the first <code>Solution</code>.
 * @param solution2 Object representing the second <code>Solution</code>.
 * @return -1, or 0, or 1, or 2 if solution1 is dominates solution2, solution1
 * and solution2 are equals, or solution1 is greater than solution2,
 * respectively.
 */
int EqualSolutions::compare(Solution * solution1, Solution * solution2) {

  if (solution1==NULL)
    return 1;
  else if (solution2 == NULL)
    return -1;

  int dominate1 ; // dominate1 indicates if some objective of solution1
                  // dominates the same objective in solution2. dominate2
  int dominate2 ; // is the complementary of dominate1.

  dominate1 = 0 ;
  dominate2 = 0 ;

  int flag;
  double value1, value2;
  for (int i = 0; i < solution1->getNumberOfObjectives(); i++) {
    Comparator * c = new ObjectiveComparator(i);
    flag = c->compare(solution1,solution2);
    delete c;
    value1 = solution1->getObjective(i);
    value2 = solution2->getObjective(i);

    if (value1 < value2) {
      flag = -1;
    } else if (value1 > value2) {
      flag = 1;
    } else {
      flag = 0;
    }

    if (flag == -1) {
      dominate1 = 1;
    }

    if (flag == 1) {
      dominate2 = 1;
    }
  }

  if (dominate1== 0 && dominate2 ==0) {
    return 0; //No one dominate the other
  }

  if (dominate1 == 1) {
    return -1; // solution1 dominate
  } else if (dominate2 == 1) {
    return 1;    // solution2 dominate
  }

  return 2;

} // compare
コード例 #3
0
 long binarySearch(const S searched,
                   const std::size_t searchLength,
                   const T& searchedFor,
                   const Comparator<T>& comp)
 {
     std::size_t low(0);
     std::size_t high(searchedLength);
     while( low < high ) {
         std::size_t mid(low + ((high - low) >> 1));
         switch(comp.compare(searched[mid], searcedFor))
         {
             case ORDER_INCREASING:
                 low = mid + 1;
                 break;
             case ORDER_DECREASING:
                 high = mid;
                 break;
             case ORDER_SAME:
                 return static_cast<long>(mid);
         }
     }
     return -1 - static_cast<long>(low);
 }
コード例 #4
0
ファイル: GDE3.cpp プロジェクト: ajnebro/MO-Phylogenetics
/*
 * Runs the GDE3 algorithm.
 * @return a <code>SolutionSet</code> that is a set of non dominated solutions
 * as a result of the algorithm execution
 */
SolutionSet * GDE3::execute() {

  int populationSize;
  int maxIterations;
  int evaluations;
  int iterations;

  SolutionSet * population;
  SolutionSet * offspringPopulation;

  Distance * distance;
  Comparator * dominance;

  Operator * crossoverOperator;
  Operator * selectionOperator;

  distance  = new Distance();
  dominance = new DominanceComparator();

  Solution ** parent;

  //Read the parameters
  populationSize = *(int *) getInputParameter("populationSize");
  maxIterations  = *(int *) getInputParameter("maxIterations");

  //Initialize the variables
  population  = new SolutionSet(populationSize);
  evaluations = 0;
  iterations  = 0;

  //Read the operators
  crossoverOperator = operators_["crossover"];
  selectionOperator = operators_["selection"];

  // Create the initial solutionSet
  Solution * newSolution;
  for (int i = 0; i < populationSize; i++) {
    newSolution = new Solution(problem_);
    problem_->evaluate(newSolution);
    problem_->evaluateConstraints(newSolution);
    evaluations++;
    population->add(newSolution);
  } //for

  // Generations ...
  while (iterations < maxIterations) {
    // Create the offSpring solutionSet
    offspringPopulation  = new SolutionSet(populationSize * 2);

    for (int i = 0; i < populationSize; i++){
      // Obtain parents. Two parameters are required: the population and the
      //                 index of the current individual
      void ** object1 = new void*[2];
      object1[0] = population;
      object1[1] = &i;
      parent = (Solution **) (selectionOperator->execute(object1));
      delete[] object1;

      Solution * child ;
      // Crossover. Two parameters are required: the current individual and the
      //            array of parents
      void ** object2 = new void*[2];
      object2[0] = population->get(i);
      object2[1] = parent;
      child = (Solution *) (crossoverOperator->execute(object2));
      delete[] object2;
      delete[] parent;

      problem_->evaluate(child) ;
      problem_->evaluateConstraints(child);
      evaluations++ ;

      // Dominance test
      int result  ;
      result = dominance->compare(population->get(i), child) ;
      if (result == -1) { // Solution i dominates child
        offspringPopulation->add(new Solution(population->get(i)));
        delete child;
      } // if
      else if (result == 1) { // child dominates
        offspringPopulation->add(child) ;
      } // else if
      else { // the two solutions are non-dominated
        offspringPopulation->add(child) ;
        offspringPopulation->add(new Solution(population->get(i)));
      } // else
    } // for

    // Ranking the offspring population
    Ranking * ranking = new Ranking(offspringPopulation);

    int remain = populationSize;
    int index  = 0;
    SolutionSet * front = NULL;
    for (int i = 0; i < populationSize; i++) {
      delete population->get(i);
    }
    population->clear();

    // Obtain the next front
    front = ranking->getSubfront(index);

    while ((remain > 0) && (remain >= front->size())){
      //Assign crowding distance to individuals
      distance->crowdingDistanceAssignment(front,problem_->getNumberOfObjectives());
      //Add the individuals of this front
      for (int k = 0; k < front->size(); k++ ) {
        population->add(new Solution(front->get(k)));
      } // for

      //Decrement remain
      remain = remain - front->size();

      //Obtain the next front
      index++;
      if (remain > 0) {
        front = ranking->getSubfront(index);
      } // if
    } // while

    // remain is less than front(index).size, insert only the best one
    if (remain > 0) {  // front contains individuals to insert
      while (front->size() > remain) {
         distance->crowdingDistanceAssignment(front,problem_->getNumberOfObjectives());
         Comparator * crowdingComparator = new CrowdingComparator();
         int indexWorst = front->indexWorst(crowdingComparator);
         delete crowdingComparator;
         delete front->get(indexWorst);
         front->remove(indexWorst);
      }
      for (int k = 0; k < front->size(); k++) {
        population->add(new Solution(front->get(k)));
      }

      remain = 0;
    } // if

    delete ranking;
    delete offspringPopulation;

    iterations ++ ;
  } // while

  delete dominance;
  delete distance;

  // Return the first non-dominated front
  Ranking * ranking = new Ranking(population);
  SolutionSet * result = new SolutionSet(ranking->getSubfront(0)->size());
  for (int i=0;i<ranking->getSubfront(0)->size();i++) {
    result->add(new Solution(ranking->getSubfront(0)->get(i)));
  }
  delete ranking;
  delete population;

  return result;

} // execute