gpconfiguration.java

来自「jgap3.2 遗传算法工具包,嘿嘿,笨鸟先飞哦」· Java 代码 · 共 702 行 · 第 1/2 页

JAVA
702
字号
   * @author Klaus Meffert
   * @since 3.0
   */
  public void storeInMemory(String a_name, Object a_value) {
    m_memory.set(a_name, a_value, -1);
  }

  /**
   * Stores a value in the internal matrix memory.
   *
   * @param a_x the first coordinate of the matrix (width)
   * @param a_y the second coordinate of the matrix (height)
   * @param a_value the value to store
   * @return created or used memory cell
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public CultureMemoryCell storeMatrixMemory(int a_x, int a_y, Object a_value) {
    return m_memory.setMatrix(a_x, a_y, a_value);
  }

  /**
   * Reads a value from the internal matrix memory.
   *
   * @param a_x the first coordinate of the matrix (width)
   * @param a_y the second coordinate of the matrix (height)
   * @return read value
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public Object readMatrixMemory(int a_x, int a_y) {
    return m_memory.getMatrix(a_x, a_y).getCurrentValue();
  }

  /**
   * Reads a value from the internal memory.
   *
   * @param a_name named index of the memory cell to read out
   * @return read value
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public Object readFromMemory(String a_name) {
    return m_memory.get(a_name).getCurrentValue();
  }

  /**
   * @param a_name the name of the cell to evaluate
   * @return the value of a memory cell, if it exsists. Otherwise returns null.
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public Object readFromMemoryIfExists(String a_name) {
    CultureMemoryCell cell = null;
    try {
      cell = m_memory.get(a_name);
    } catch (IllegalArgumentException iex) {
      // Memory name not found: OK.
      // --------------------------
      ;
    }
    if (cell == null) {
      return null;
    }
    return cell.getCurrentValue();
  }

  /**
   * Stores a value in the internal indexed memory.
   *
   * @param a_index index of the cell
   * @param a_value the value to store
   * @return created or used memory cell
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public CultureMemoryCell storeIndexedMemory(int a_index, Object a_value) {
    return m_memory.set(a_index, a_value, -1, "noname");
  }

  /**
   * Reads a value from the internal indexed memory.
   *
   * @param a_index index of the cell
   * @return read value (maybe null )
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public Object readIndexedMemory(int a_index) {
    CultureMemoryCell cell = m_memory.get(a_index);
    if (cell == null) {
      return null;
    }
    else {
      return cell.getCurrentValue();
    }
  }

  /**
   * Clears the memory.
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void clearMemory() {
    m_memory.clear();
  }

  public GPFitnessFunction getGPFitnessFunction() {
    return m_objectiveFunction;
  }

  /**
   * Sets the fitness function to be used for this genetic algorithm.
   * The fitness function is responsible for evaluating a given
   * Chromosome and returning a positive integer that represents its
   * worth as a candidate solution. These values are used as a guide by the
   * natural to determine which Chromosome instances will be allowed to move
   * on to the next round of evolution, and which will instead be eliminated.
   *
   * @param a_functionToSet fitness function to be used
   *
   * @throws InvalidConfigurationException if the fitness function is null, or
   * if this Configuration object is locked.
   *
   * @author Neil Rotstan
   * @since 1.1
   */
  public synchronized void setFitnessFunction(GPFitnessFunction a_functionToSet)
      throws InvalidConfigurationException {
    verifyChangesAllowed();
    // Sanity check: Make sure that the given fitness function isn't null.
    // -------------------------------------------------------------------
    if (a_functionToSet == null) {
      throw new InvalidConfigurationException(
          "The FitnessFunction instance may not be null.");
    }
    // Ensure that no other fitness function has been set in a different
    // configuration object within the same thread!
    // -----------------------------------------------------------------
    checkProperty(PROPERTY_FITFUNC_INST, a_functionToSet,
                  "Fitness function has already been set differently.");
    m_objectiveFunction = a_functionToSet;
  }

  public boolean isStrictProgramCreation() {
    return m_strictProgramCreation;
  }

  public void setStrictProgramCreation(boolean a_strict) {
    m_strictProgramCreation = a_strict;
  }

  public int getProgramCreationMaxtries() {
    return m_programCreationMaxTries;
  }

  public void setProgramCreationMaxTries(int a_maxtries) {
    m_programCreationMaxTries = a_maxtries;
  }

  /**
   * @return the fitness evaluator set
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public IGPFitnessEvaluator getGPFitnessEvaluator() {
    return m_fitnessEvaluator;
  }

  /**
   * Validates a_node in the context of a_chrom. Considers the recursion level
   * (a_recursLevel), the type needed (a_type) for the node, the functions
   * available (a_functionSet) and the depth of the whole chromosome needed
   * (a_depth), and whether grow mode is used (a_grow is true) or not.
   *
   * @param a_chrom the chromosome that will contain the node, if valid
   * @param a_node the node selected and to be validated
   * @param a_rootNode root node of the node to be validated (may be null)
   * @param a_tries number of times the validator has been called, useful for
   * stopping by returning true if the number exceeds a limit
   * @param a_num the chromosome's index in the individual of this chromosome
   * @param a_recurseLevel level of recursion
   * @param a_type the return type of the node needed
   * @param a_functionSet the array of available functions
   * @param a_depth the needed depth of the program chromosome
   * @param a_grow true: use grow mode, false: use full mode
   * @param a_childIndex index of the child in the parent node to which it
   * belongs (-1 if node is root node)
   *
   * @return true: node is valid; false: node is invalid
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public boolean validateNode(ProgramChromosome a_chrom, CommandGene a_node,
                              CommandGene a_rootNode, int a_tries, int a_num,
                              int a_recurseLevel, Class a_type,
                              CommandGene[] a_functionSet, int a_depth,
                              boolean a_grow, int a_childIndex) {
    if (m_nodeValidator == null) {
      return true;
    }
    return m_nodeValidator.validate(a_chrom, a_node, a_rootNode, a_tries, a_num,
                                    a_recurseLevel, a_type, a_functionSet,
                                    a_depth, a_grow, a_childIndex);
  }

  /**
   * Sets the node validator. Also see method validateNode.
   *
   * @param a_nodeValidator sic
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public void setNodeValidator(INodeValidator a_nodeValidator) {
    m_nodeValidator = a_nodeValidator;
  }

  /**
   * @return the node validator set
   *
   * @author Klaus Meffert
   * @since 3.0
   */
  public INodeValidator getNodeValidator() {
    return m_nodeValidator;
  }

  /**
   * Compares this entity against the specified object.
   *
   * @param a_other the object to compare against
   * @return true: if the objects are the same, false otherwise
   *
   * @author Klaus Meffert
   * @since 3.1
   */
  public boolean equals(Object a_other) {
    try {
      return compareTo(a_other) == 0;
    } catch (ClassCastException cex) {
      return false;
    }
  }

  public int compareTo(Object a_other) {
    if (a_other == null) {
      return 1;
    }
    else {
      GPConfiguration other = (GPConfiguration) a_other;
      return new CompareToBuilder()
          .append(m_objectiveFunction, other.m_objectiveFunction)
          .append(m_crossoverProb, other.m_crossoverProb)
          .append(m_reproductionProb, other.m_reproductionProb)
          .append(m_newChromsPercent, other.m_newChromsPercent)
          .append(m_maxCrossoverDepth, other.m_maxCrossoverDepth)
          .append(m_maxInitDepth, other.m_maxInitDepth)
          .append(m_selectionMethod.getClass(),
                  other.m_selectionMethod.getClass())
          .append(m_crossMethod.getClass(), other.m_crossMethod.getClass())
          .append(m_programCreationMaxTries, other.m_programCreationMaxTries)
          .append(m_strictProgramCreation, other.m_strictProgramCreation)
          .append(m_fitnessEvaluator.getClass(),
                  other.m_fitnessEvaluator.getClass())
          .toComparison();
    }
  }

  /**
   *
   * @return see ProgramChromosome.growOrFull(...) and GPGenotype.evolve()
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public boolean isMaxNodeWarningPrinted() {
    return m_warningPrinted;
  }

  /**
   * See ProgramChromosome.growOrFull(...) and GPGenotype.evolve().
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public void flagMaxNodeWarningPrinted() {
    m_warningPrinted = true;
  }

  /**
   *
   * @param a_program IGPProgram
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public void setPrototypeProgram(IGPProgram a_program) {
    m_prototypeProgram = a_program;
  }

  /**
   * @return prototype program set (maybe null if not setted previously)
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public IGPProgram getPrototypeProgram() {
    return m_prototypeProgram;
  }

  /**
   * @return capacity of the memory in cells
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public int getMemorySize() {
    return m_memory.size();
  }

  public GPProgramInfo readProgramCache(GPProgram a_prog) {
    GPProgramInfo pci = new GPProgramInfo(a_prog, true);
    pci.setFound(false);
    return (GPProgramInfo)m_programCache.get(pci.getToStringNorm());
  }

  public GPProgramInfo putToProgramCache(GPProgram a_prog) {
    GPProgramInfo pci = new GPProgramInfo(a_prog, true);
    return (GPProgramInfo)m_programCache.put(pci.getToStringNorm(), pci);
  }

  public boolean isUseProgramCache() {
    return m_useProgramCache;
  }

  public void setUseProgramCache(boolean a_useCache) {
    m_useProgramCache = a_useCache;
  }
}
/**@todo introduce lock for configuration*/

⌨️ 快捷键说明

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