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

📄 gpconfiguration.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   * @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)
   * @param a_fullProgram true: whole program is available in a_chrom
   *
   * @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,
                              boolean a_fullProgram) {
    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,
                                    a_fullProgram);
  }

  /**
   * 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;
  }

  /**
   * Stores a Variable.
   *
   * @param a_var the Variable to store
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public void putVariable(Variable a_var) {
    m_variables.put(a_var.getName(), a_var);
  }

  /**
   * @param a_varName name of variable to retriebe
   * @return Variable instance or null, if not found
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public Variable getVariable(String a_varName) {
    return (Variable) m_variables.get(a_varName);
  }

  /**
   * @return deep clone of this instance
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public Object clone() {
    return newInstanceGP(getId(), getName());
  }

  /**
   * Creates a new GPConfiguration instance by cloning. Allows to preset the
   * ID and the name.
   *
   * @param a_id new ID for clone
   * @param a_name new name for clone
   * @return deep clone of this instance
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public GPConfiguration newInstanceGP(String a_id, String a_name) {
    try {
      GPConfiguration result = new GPConfiguration(getName());
      // Clone JGAPFactory first because it helps in cloning other objects.
      // ------------------------------------------------------------------
      if (m_factory instanceof ICloneable) {
        result.m_factory = (IJGAPFactory) ( (ICloneable) m_factory).clone();
      }
      else {
        // We must fallback to a standardized solution.
        // --------------------------------------------
        m_factory = new JGAPFactory(false);
        result.m_factory = (IJGAPFactory) ( (JGAPFactory) m_factory).clone();
      }
      if (result.m_factory == null) {
        throw new IllegalStateException("JGAPFactory must not be null!");
      }
      if (m_objectiveFunction != null) {
        result.m_objectiveFunction = m_objectiveFunction;
      }
      result.m_crossoverProb = m_crossoverProb;
      result.m_reproductionProb = m_reproductionProb;
      result.m_newChromsPercent = m_newChromsPercent;
      result.m_functionProb = m_functionProb;
      result.m_maxCrossoverDepth = m_maxCrossoverDepth;
      result.m_maxInitDepth = m_maxInitDepth;
      result.m_minInitDepth = m_minInitDepth;
      result.m_strictProgramCreation = m_strictProgramCreation;
      result.m_programCreationMaxTries = m_programCreationMaxTries;
      result.m_selectionMethod = (INaturalGPSelector) doClone(m_selectionMethod);
      result.m_crossMethod = (CrossMethod) doClone(m_crossMethod);
      result.m_fitnessEvaluator = (IGPFitnessEvaluator) doClone(
          m_fitnessEvaluator);
      result.m_nodeValidator = (INodeValidator) doClone(m_nodeValidator);
      result.m_useProgramCache = m_useProgramCache;
      // Configurable data.
      // ------------------
//      result.m_config = new ConfigurationConfigurable();
      // Identificative data.
      // --------------------
      result.setName(a_name);
      result.setId(a_id);
      result.makeThreadKey(); // Must be called after m_id is set
      return result;
    } catch (Throwable t) {
      throw new CloneException(t);
    }
  }

  /**
   * @return the JGAP factory registered
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  public IJGAPFactory getJGAPFactory() {
    return m_factory;
  }

  /**
   * When deserializing, do specific initializations.
   *
   * @param a_inputStream the ObjectInputStream provided for deserialzation
   *
   * @throws IOException
   * @throws ClassNotFoundException
   *
   * @author Klaus Meffert
   * @since 3.2
   */
  private void readObject(ObjectInputStream a_inputStream)
      throws IOException, ClassNotFoundException {
    //always perform the default de-serialization first
    a_inputStream.defaultReadObject();
    try {
      init(false);
    } catch (InvalidConfigurationException iex) {
      iex.printStackTrace();
      throw new IOException(iex.toString());
    }
  }

  /**
   *
   * @param a_strategy IGPInitStrategy
   *
   * @author Klaus Meffert
   * @since 3.2.1
   */
  public void setInitStrategy(IGPInitStrategy a_strategy) {
    m_initStrategy = a_strategy;
  }

  /**
   *
   * @return IGPInitStrategy
   *
   * @author Klaus Meffert
   * @since 3.2.1
   */
  public IGPInitStrategy getInitStrategy() {
    return  m_initStrategy;
  }
}

⌨️ 快捷键说明

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