Esempio n. 1
0
  public void init() {
    //		System.out.println("TRIBES.init()");
    // Generate a swarm
    swarm =
        new TribesSwarm(
            this, range, initRange); // TODO initRange is hard coded equal to problem range
    // swarm.generateSwarm(initExplorerNb, initType, m_problem);

    //   swarm.displaySwarm(swarm,out);
    //  print("\n Best after init: "+swarm.Best.position.fitness,out);

    iter = 0;
    adapt = 0;
    informOption = -1;
    //        Hard coded option
    //        -1 = absolute best informant
    //        1 = relative (pseudo-gradient) best informant. For "niching"
    //       See also moveExplorer, which can be modified in order to avoid this parameter

    population.clear();
    population.addAll(swarm.toPopulation());
    population.init(); // necessary to allow for multi-runs

    if (m_Show) show();
  }
Esempio n. 2
0
  public void optimize() {

    int initOption = 0;
    if (iter == 0) { // first iteration!
      if (initRange == null) {
        rangeInitType = 0;
      } else {
        rangeInitType = 1; // 1 means in initRange for a start
      }
      // * initOption Options: 0 - random, 1 - on the bounds, 2 - sunny spell, 3 - around a center
      // * rangeInitType for options 0,1: 1 means use initRange, 0 use default range
      swarm.generateSwarm(initExplorerNb, initOption, rangeInitType, m_problem);
    }
    iter++;

    m_problem.evaluatePopulationStart(population);
    swarm.setSwarmSize();
    // swarm.Best.positionPrev = swarm.Best.position;

    swarm.moveSwarm(
        range, new TribesParam(), informOption, m_problem); // *** HERE IT MOVES and EVALUATES
    // public void moveSwarm(double[][] range, int fitnessSize, TribesParam pb, TribesSwarm swarm,
    //      int informOption, AbstractOptimizationProblem prob) {

    if (Tribes.adaptOption != 0) { // perform adaption
      if (Tribes.adaptOption == 1) { // Just reinitialize the swarm
        adaptThreshold = iter - adapt;
        //   adaptMax=swarmSize;
        adaptMax = swarm.linkNb(swarm);
        if (adaptThreshold >= adaptMax) {
          if (swarm.getBestMemory().getPrevPos().getTotalError()
              <= swarm.getBestMemory().getPos().getTotalError()) {
            adapt = iter; // Memorize at which iteration adaptation occurs

            for (int i = 0; i < swarm.getTribeCnt(); i++) {
              swarm.reinitTribe(i, rangeInitType, m_problem);
            }
          }
        }
      } else //   if(swarm.Best.positionPrev.getTotalError()<=swarm.Best.position.getTotalError())
      {
        // Structural adaptations

        adaptThreshold = iter - adapt;
        //   adaptMax=swarmSize;
        adaptMax = swarm.linkNb(swarm);

        if (adaptThreshold >= adaptMax) {
          adapt = iter; // Memorize at which iteration adaptation occurs
          swarm.adaptSwarm(rangeInitType, m_problem); // Re´alise l'adaptation
        }
      }
    }
    population.clear();
    population.addAll(swarm.toPopulation());
    if (m_Show) plotAll(population);
    m_problem.evaluatePopulationEnd(population);

    //		this.population.incrFunctionCallsby(evals);
    this.population.incrGeneration();
    // this.firePropertyChangedEvent("NextGenerationPerformed");	// This is now done implicitely, as
    // after every evaluation, addEvals is called

    if (TRACE) {
      System.out.println("loop finished after " + population.getFunctionCalls() + " evaluations");
      // for (int i=0; i<population.size(); i++) System.out.println(" *
      // "+((TribesExplorer)population.get(i)).getStringRepresentation());
      System.out.println(
          " best: "
              + population.getBestEAIndividual().getStringRepresentation()
              + " - "
              + population.getBestEAIndividual().getFitness(0));
      System.out.println(
          "swarm contains " + swarm.numParticles() + " particles in iteration " + iter);
    }
  }