Beispiel #1
0
/**
 * Runs of the SMPSO algorithm.
 * @return a <code>SolutionSet</code> that is a set of non dominated solutions
 * as a result of the algorithm execution
 */
SolutionSet * PSO::execute() {

  initParams();

  success_ = false;
  globalBest_ =  NULL;

  //->Step 1 (and 3) Create the initial population and evaluate
  for (int i = 0; i < particlesSize_; i++) {
    Solution * particle = new Solution(problem_);
    problem_->evaluate(particle);
    evaluations_ ++;
    particles_->add(particle);
    if ((globalBest_ == NULL) || (particle->getObjective(0) < globalBest_->getObjective(0))) {
      if (globalBest_!= NULL) {
        delete globalBest_;
      }
      globalBest_ = new Solution(particle);
    }
  }

  //-> Step2. Initialize the speed_ of each particle to 0
  for (int i = 0; i < particlesSize_; i++) {
    speed_[i] = new double[problem_->getNumberOfVariables()];
    for (int j = 0; j < problem_->getNumberOfVariables(); j++) {
    speed_[i][j] = 0.0;
    }
  }

  //-> Step 6. Initialize the memory of each particle
  for (int i = 0; i < particles_->size(); i++) {
    Solution * particle = new Solution(particles_->get(i));
    localBest_[i] = particle;
  }

  //-> Step 7. Iterations ..
  while (iteration_ < maxIterations_) {
    int * bestIndividualPtr = (int*)findBestSolution_->execute(particles_);
    int bestIndividual = *bestIndividualPtr;
    delete bestIndividualPtr;
    computeSpeed(iteration_, maxIterations_);

    //Compute the new positions for the particles_
    computeNewPositions();

    //Mutate the particles_
    //mopsoMutation(iteration_, maxIterations_);

    //Evaluate the new particles_ in new positions
    for (int i = 0; i < particles_->size(); i++) {
      Solution * particle = particles_->get(i);
      problem_->evaluate(particle);
      evaluations_ ++;
    }

    //Actualize the memory of this particle
    for (int i = 0; i < particles_->size(); i++) {
     //int flag = comparator_.compare(particles_.get(i), localBest_[i]);
     //if (flag < 0) { // the new particle is best_ than the older remember
     if ((particles_->get(i)->getObjective(0) < localBest_[i]->getObjective(0))) {
       Solution * particle = new Solution(particles_->get(i));
       delete localBest_[i];
       localBest_[i] = particle;
     } // if
     if ((particles_->get(i)->getObjective(0) < globalBest_->getObjective(0))) {
       Solution * particle = new Solution(particles_->get(i));
       delete globalBest_;
       globalBest_ = particle;
     } // if

    }
    iteration_++;
  }

  // Return a population with the best individual
  SolutionSet * resultPopulation = new SolutionSet(1);
  int * bestIndexPtr = (int *)findBestSolution_->execute(particles_);
  int bestIndex = *bestIndexPtr;
  delete bestIndexPtr;
  cout << "Best index = " << bestIndex << endl;
  Solution * s = particles_->get(bestIndex);
  resultPopulation->add(new Solution(s));

  // Free memory
  deleteParams();

  return resultPopulation;
} // execute
Beispiel #2
0
/**
 * Runs of the SMPSO algorithm.
 * @return a <code>SolutionSet</code> that is a set of non dominated solutions
 * as a result of the algorithm execution
 */
SolutionSet *SMPSOhv::execute() {

  initParams();

  success = false;
  //->Step 1 (and 3) Create the initial population and evaluate
  for (int i = 0; i < swarmSize; i++){
    Solution *particle = new Solution(problem_);
    problem_->evaluate(particle);
    problem_->evaluateConstraints(particle);
    particles->add(particle);
  }

  //-> Step2. Initialize the speed of each particle to 0
  for (int i = 0; i < swarmSize; i++) {
    speed[i] = new double[problem_->getNumberOfVariables()];
    for (int j = 0; j < problem_->getNumberOfVariables(); j++) {
      speed[i][j] = 0.0;
    }
  }

  // Step4 and 5
  for (int i = 0; i < particles->size(); i++){
    Solution *particle = new Solution(particles->get(i));
    if (leaders->add(particle) == false){
      delete particle;
    }
  }

  //-> Step 6. Initialize the memory of each particle
  for (int i = 0; i < particles->size(); i++){
    Solution *particle = new Solution(particles->get(i));
    best[i] = particle;
  }

  //Crowding the leaders_
  //distance->crowdingDistanceAssignment(leaders, problem_->getNumberOfObjectives());
  leaders->computeHVContribution();

  //-> Step 7. Iterations ..
  while (iteration < maxIterations) {
    //Compute the speed_
    computeSpeed(iteration, maxIterations);

    //Compute the new positions for the particles_
    computeNewPositions();

    //Mutate the particles_
    mopsoMutation(iteration,maxIterations);

    //Evaluate the new particles in new positions
    for (int i = 0; i < particles->size(); i++){
      Solution *particle = particles->get(i);
      problem_->evaluate(particle);
      problem_->evaluateConstraints(particle);
    }

    //Update the archive
    for (int i = 0; i < particles->size(); i++) {
      Solution *particle = new Solution(particles->get(i));
      if (leaders->add(particle) == false) {
        delete particle;
      }
    }

    //Update the memory of this particle
    for (int i = 0; i < particles->size(); i++) {
      int flag = dominance->compare(particles->get(i), best[i]);
      if (flag != 1) { // the new particle is best_ than the older remembered
        Solution *particle = new Solution(particles->get(i));
        delete best[i];
        best[i] = particle;
      }
    }

    iteration++;
  }

  // Build the solution set result
  SolutionSet * result = new SolutionSet(leaders->size());
  for (int i=0;i<leaders->size();i++) {
    result->add(new Solution(leaders->get(i)));
  }

  // Free memory
  deleteParams();

  return result;
} // execute