⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 weightedrouletteselector.java

📁 jGAp 遗传算法 提不错的一款软件 最新的更新
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      // selected slot.
      // ----------------------------------------------------------
      boolean found;
      if (isFitter2_1) {
        // Introduced DELTA to fix bug 1449651
        found = selectedSlot - currentSlot <= DELTA;
      }
      else {
        // Introduced DELTA to fix bug 1449651
        found = Math.abs(currentSlot - selectedSlot) <= DELTA;
      }
      if (found) {
        // Remove one instance of the chromosome from the wheel by
        // decrementing the slot counter by the fitness value resp.
        // resetting the counter if doublette chromosomes are not
        // allowed.
        // -------------------------------------------------------
        if (!getDoubletteChromosomesAllowed()) {
          m_totalNumberOfUsedSlots -= a_counterValues[i];
          a_counterValues[i] = 0;
        }
        else {
          a_counterValues[i] -= a_fitnessValues[i];
          m_totalNumberOfUsedSlots -= a_fitnessValues[i];
        }
        // Introduced DELTA to fix bug 1449651
        if (Math.abs(m_totalNumberOfUsedSlots) < DELTA) {
          m_totalNumberOfUsedSlots = 0.0d;
        }
        // Now return our selected Chromosome.
        // -----------------------------------
        return a_chromosomes[i];
      }
      else {
        currentSlot += a_counterValues[i];
      }
    }
    // We have reached here because there were rounding errors when
    // computing with doubles or because the last entry is the right one.
    // ------------------------------------------------------------------
    return a_chromosomes[a_counterValues.length - 1];
  }

  /**
   * Empty out the working pool of Chromosomes.
   *
   * @author Neil Rotstan
   * @since 1.0
   */
  public synchronized void empty() {
    // Put all of the old SlotCounters into the pool so that we can
    // reuse them later instead of constructing new ones.
    // ------------------------------------------------------------
    m_counterPool.releaseAllObjects(m_wheel.values());
    // Now clear the wheel and reset the internal state.
    // -------------------------------------------------
    m_wheel.clear();
    m_totalNumberOfUsedSlots = 0;
  }

  private void scaleFitnessValues() {
    // First, add up all the fitness values. While we're doing this,
    // keep track of the largest fitness value we encounter.
    // -------------------------------------------------------------
    double largestFitnessValue = 0.0;
    BigDecimal totalFitness = ZERO_BIG_DECIMAL;
    Iterator counterIterator = m_wheel.values().iterator();
    while (counterIterator.hasNext()) {
      SlotCounter counter = (SlotCounter) counterIterator.next();
      if (counter.getFitnessValue() > largestFitnessValue) {
        largestFitnessValue = counter.getFitnessValue();
      }
      BigDecimal counterFitness = new BigDecimal(counter.getFitnessValue());
      totalFitness = totalFitness.add(counterFitness.multiply(
          new BigDecimal(counter.getCounterValue())));
    }
    // Now divide the total fitness by the largest fitness value to
    // compute the scaling factor.
    // ------------------------------------------------------------
    if (largestFitnessValue > 0.000000d
        && totalFitness.floatValue() > 0.0000001d) {
      double scalingFactor =
          totalFitness.divide(new BigDecimal(largestFitnessValue),
                              BigDecimal.ROUND_HALF_UP).doubleValue();
      // Divide each of the fitness values by the scaling factor to
      // scale them down.
      // ----------------------------------------------------------
      counterIterator = m_wheel.values().iterator();
      while (counterIterator.hasNext()) {
        SlotCounter counter = (SlotCounter) counterIterator.next();
        counter.scaleFitnessValue(scalingFactor);
      }
    }
  }

  /**
   * @return always false as some Chromosome's could be returnd multiple times
   *
   * @author Klaus Meffert
   * @since 2.0
   */
  public boolean returnsUniqueChromosomes() {
    return false;
  }

  /**
   * Not supported by this selector! Please do not use it.
   *
   * @param a_doublettesAllowed do not use
   *
   * @author Klaus Meffert
   * @since 2.0
   */
  public void setDoubletteChromosomesAllowed(final boolean a_doublettesAllowed) {
    throw new IllegalStateException("Weighted roulette selector does not"
                                    +" support this parameter,"
                                   +" please do not use it!");
  }

  /**
   * @return TRUE: doublette chromosomes allowed to be added by the selector
   *
   * @author Klaus Meffert
   * @since 2.0
   */
  public boolean getDoubletteChromosomesAllowed() {
    return true;
  }

  /**
   * @return deep clone of this instance
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public Object clone() {
    try {
      WeightedRouletteSelector result = new WeightedRouletteSelector(
          getConfiguration());
      result.m_wheel = (THashMap) m_wheel.clone();
      result.m_config = new WeightedRouletteSelConfig();
      result.m_config.m_doublettesAllowed = m_config.m_doublettesAllowed;
      return result;
    } catch (InvalidConfigurationException iex) {
      throw new CloneException(iex);
    }
  }

  public boolean equals(Object o) {
    WeightedRouletteSelector other = (WeightedRouletteSelector) o;
    if (other == null) {
      return false;
    }
    if (m_totalNumberOfUsedSlots != other.m_totalNumberOfUsedSlots) {
      return false;
    }
    if (other.m_config == null) {
      return false;
    }
    if (m_config.m_doublettesAllowed != other.m_config.m_doublettesAllowed) {
      return false;
    }
    if (other.m_counterPool == null) {
      return false;
    }
    if (!m_wheel.equals(other.m_wheel)) {
      return false;
    }
    return true;
  }

  class WeightedRouletteSelConfig
      implements Serializable {
    /**
     * Allows or disallows doublette chromosomes to be added to the selector
     */
    public boolean m_doublettesAllowed;
  }
}
/**
 * Implements a counter that is used to keep track of the total number of
 * slots that a single Chromosome is occupying in the roulette wheel. Since
 * all equal copies of a chromosome have the same fitness value, the increment
 * method always adds the fitness value of the chromosome. Following
 * construction of this class, the reset() method must be invoked to provide
 * the initial fitness value of the Chromosome for which this SlotCounter is
 * to be associated. The reset() method may be reinvoked to begin counting
 * slots for a new Chromosome.
 *
 * @author Neil Rotstan
 * @since 1.0
 */
class SlotCounter {
  /**
   * The fitness value of the Chromosome for which we are keeping count of
   * roulette wheel slots. Although this value is constant for a Chromosome,
   * it's not declared final here so that the slots can be reset and later
   * reused for other Chromosomes, thus saving some memory and the overhead
   * of constructing them from scratch.
   */
  private double m_fitnessValue;

  /**
   * The current number of Chromosomes represented by this counter.
   */
  private int m_count;

  /**
   * Resets the internal state of this SlotCounter instance so that it can
   * be used to count slots for a new Chromosome.
   *
   * @param a_initialFitness the fitness value of the Chromosome for which
   * this instance is acting as a counter
   *
   * @author Neil Rotstan
   * @since 1.0
   */
  public void reset(final double a_initialFitness) {
    m_fitnessValue = a_initialFitness;
    m_count = 1;
  }

  /**
   * Retrieves the fitness value of the chromosome for which this instance
   * is acting as a counter.
   *
   * @return the fitness value that was passed in at reset time
   *
   * @author Neil Rotstan
   * @since 1.0
   */
  public double getFitnessValue() {
    return m_fitnessValue;
  }

  /**
   * Increments the value of this counter by the fitness value that was
   * passed in at reset time.
   *
   * @author Neil Rotstan
   * @since 1.0
   */
  public void increment() {
    m_count++;
  }

  /**
   * Retrieves the current value of this counter: ie, the number of slots
   * on the roulette wheel that are currently occupied by the Chromosome
   * associated with this SlotCounter instance.
   *
   * @return the current value of this counter
   */
  public int getCounterValue() {
    return m_count;
  }

  /**
   * Scales this SlotCounter's fitness value by the given scaling factor.
   *
   * @param a_scalingFactor the factor by which the fitness value is to be
   * scaled
   *
   * @author Neil Rotstan
   * @since 1.0
   */
  public void scaleFitnessValue(final double a_scalingFactor) {
    m_fitnessValue /= a_scalingFactor;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -