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

📄 compositegene.java

📁 一个开源的用java开发的遗传算法的封装好的工程
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  /**
   * Retrieves the value represented by this Gene. All values returned
   * by this class will be Vector instances. Each element of the Vector
   * represents the allele of the corresponding gene in the CompositeGene's
   * container
   *
   * @return the value of this Gene
   *
   * @author Klaus Meffert
   * @since 1.1
   */
  public Object getAllele() {
    List alleles = new Vector();
    Gene gene;
    int size = m_genes.size();
    for (int i = 0; i < size; i++) {
      gene = (Gene) m_genes.get(i);
      alleles.add(gene.getAllele());
    }
    return alleles;
  }

  /**
   * Sets the value of the contained Genes to the new given value. This class
   * expects the value to be of a Vector type. Each element of the Vector
   * must conform with the type of the gene in the CompositeGene's container
   * at the corresponding position.
   *
   * @param a_newValue the new value of this Gene instance
   *
   * @author Klaus Meffert
   * @since 1.1
   */
  public void setAllele(Object a_newValue) {
    if (! (a_newValue instanceof List)) {
      throw new IllegalArgumentException(
          "The expected type of the allele"
          + " is a List descendent.");
    }
    if (getConstraintChecker() != null) {
      if (!getConstraintChecker().verify(this, a_newValue, null, -1)) {
        return;
      }
    }
    List alleles = (List) a_newValue;
    Gene gene;
    for (int i = 0; i < alleles.size(); i++) {
      gene = (Gene) m_genes.get(i);
      gene.setAllele(alleles.get(i));
    }
  }

  /**
   * Provides an implementation-independent means for creating new Gene
   * instances.
   *
   * @return a new Gene instance of the same type and with the same setup as
   * this concrete Gene
   *
   * @author Klaus Meffert
   * @since 1.1
   */
  protected Gene newGeneInternal() {
    try {
      CompositeGene compositeGene = new CompositeGene(getConfiguration());
      compositeGene.setConstraintChecker(getConstraintChecker());
      Gene gene;
      int geneSize = m_genes.size();
      for (int i = 0; i < geneSize; i++) {
        gene = (Gene) m_genes.get(i);
        compositeGene.addGene(gene.newGene(), false);
      }
      return compositeGene;
    }
    catch (InvalidConfigurationException iex) {
      throw new IllegalStateException(iex.getMessage());
    }
  }

  /**
   * Compares this CompositeGene with the specified object for order. A
   * false value is considered to be less than a true value. A null value
   * is considered to be less than any non-null value.
   *
   * @param a_other the CompositeGene to be compared
   * @return a negative integer, zero, or a positive integer as this object
   * is less than, equal to, or greater than the specified object
   *
   * @throws ClassCastException if the specified object's type prevents it
   * from being compared to this CompositeGene
   *
   * @author Klaus Meffert
   * @since 1.1
   */
  public int compareTo(Object a_other) {
    // First, if the other gene (or its value) is null, then this is
    // the greater allele. Otherwise, just use the contained genes' compareTo
    // method to perform the comparison.
    // ---------------------------------------------------------------
    if (a_other == null) {
      return 1;
    }
    if (! (a_other instanceof CompositeGene)) {
      return this.getClass().getName().compareTo(a_other.getClass().getName());
    }
    CompositeGene otherCompositeGene = (CompositeGene) a_other;
    if (otherCompositeGene.isEmpty()) {
      // If our value is also null, then we're the same. Otherwise,
      // this is the greater gene.
      // ----------------------------------------------------------
      if (isEmpty()) {
        return 0;
      }
      else {
        return 1;
      }
    }
    else {
      // Compare each gene against each other.
      // -------------------------------------
      int numberGenes = Math.min(size(), otherCompositeGene.size());
      Gene gene1;
      Gene gene2;
      for (int i = 0; i < numberGenes; i++) {
        gene1 = geneAt(i);
        gene2 = otherCompositeGene.geneAt(i);
        int result = gene1.compareTo(gene2);
        if (result != 0) {
          return result;
        }
      }
      // If everything is equal until now the CompositeGene with more
      // contained genes wins.
      // ------------------------------------------------------------
      if (size() == otherCompositeGene.size()) {
        if (isCompareApplicationData()) {
          return compareApplicationData(getApplicationData(),
                                        otherCompositeGene.getApplicationData());
        }
        else {
          return 0;
        }
      }
      else {
        if (size() > otherCompositeGene.size()) {
          return 1;
        }
        else {
          return -1;
        }
      }
    }
  }

  /**
   * Retrieves a string representation of this CompositeGene's value that
   * may be useful for display purposes.
   * @return string representation of this CompositeGene's value. Every
   * contained gene's string representation is delimited by the given
   * delimiter
   *
   * @author Neil Rotstan
   * @author Klaus Meffert
   * @author Audrius Meskauskas
   * @since 1.1
   */
  public String toString() {
    if (m_genes.isEmpty()) {
      return "CompositeGene=null";
    }
    else {
      String result = "CompositeGene=(";
      Gene gene;
      for (int i = 0; i < m_genes.size(); i++) {
        gene = (Gene) m_genes.get(i);
        result += gene;
        if (i < m_genes.size() - 1) {
          result += GENE_DELIMITER;
        }
      }
      return result + ")";
    }
  }

  /**
   * @return true: no genes contained, false otherwise
   *
   * @author Klaus Meffert
   * @since 1.1
   */
  public boolean isEmpty() {
    return m_genes.isEmpty() ? true : false;
  }

  /**
   * @param a_index index to return the gene at
   * @return the gene at the given index
   *
   * @author Klaus Meffert
   * @since 1.1
   */
  public Gene geneAt(int a_index) {
    return (Gene) m_genes.get(a_index);
  }

  /**
   * @return the number of genes contained
   *
   * @author Klaus Meffert
   * @since 1.1
   */
  public int size() {
    return m_genes.size();
  }

  /**
   * Checks whether a specific gene is already contained. The determination
   * will be done by checking for identity and not using the equal method!
   *
   * @param gene the gene under test
   * @return true: the given gene object is contained
   *
   * @author Klaus Meffert
   * @since 1.1
   */
  public boolean containsGeneByIdentity(Gene gene) {
    boolean result;
    int size = size();
    if (size < 1) {
      result = false;
    }
    else {
      result = false;
      for (int i = 0; i < size; i++) {
        //check for identity
        //------------------
        if (geneAt(i) == gene) {
          result = true;
          break;
        }
      }
    }
    return result;
  }

  /**
   * Don't use this method, is makes no sense here. It is just there to
   * satisfy the Gene interface. Instead, loop over all contained genes and
   * call their applyMutation method.
   * @param a_index does not matter here
   * @param a_percentage does not matter here
   *
   * @author Klaus Meffert
   * @since 1.1
   */
  public void applyMutation(int a_index, double a_percentage) {
    // problem here: size() of CompositeGene not equal to (different)
    // sizes of contained genes.
    // Solution: Don't use CompositeGene.applyMutation, instead loop
    //           over all contained genes and call their method
    // -------------------------------------------------------------
    throw new RuntimeException("applyMutation may not be called for "
                               + "a CompositeGene. Call this method for each"
                               + " gene contained in the CompositeGene.");
  }

  /**
   * Splits the input a_string into individual gene representations.
   *
   * @param a_string the string to split
   * @return the elements of the returned array are the persistent
   * representation strings of the gene's components
   * @throws UnsupportedRepresentationException
   *
   * @author Audrius Meskauskas
   * @since 2.0
   */
  protected static final List split(String a_string)
      throws UnsupportedRepresentationException {
    List a = Collections.synchronizedList(new ArrayList());
    StringTokenizer st = new StringTokenizer
        (a_string, GENE_DELIMITER_HEADING + GENE_DELIMITER_CLOSING, true);
    while (st.hasMoreTokens()) {
      if (!st.nextToken().equals(GENE_DELIMITER_HEADING)) {
        throw new UnsupportedRepresentationException(a_string + " no open tag");
      }
      String n = st.nextToken();
      if (n.equals(GENE_DELIMITER_CLOSING)) {
        a.add(""); /* Empty token */
      }
      else {
        a.add(n);
        if (!st.nextToken().equals(GENE_DELIMITER_CLOSING)) {
          throw new UnsupportedRepresentationException
              (a_string + " no close tag");
        }
      }
    }
    return a;
  }

  /**
   * Retrieves the hash code value for this Gene.
   *
   * @return this Gene's hash code
   *
   * @author Klaus Meffert
   * @since 2.2
   */
  public int hashCode() {
    int hashCode = 1;
    int geneHashcode;
    for (int i = 0; i < size(); i++) {
      geneHashcode = geneAt(i).hashCode();
      hashCode = 31 * hashCode + geneHashcode;
    }
    return hashCode;
  }

  /**
   * This method is not called internally because BaseGene.getAllele() is
   * overridden here!
   * @return always null
   */
  protected Object getInternalValue() {
    return null;
  }

  @Override
  public String getBusinessKey() {
    Iterator iter = m_genes.iterator();
    Gene gene;
    StringBuffer b = new StringBuffer();
    while (iter.hasNext()) {
      gene = (Gene) iter.next();
      b.append(GENE_DELIMITER_HEADING);
      if (IBusinessKey.class.isAssignableFrom(gene.getClass())) {
        b.append(((IBusinessKey)gene).getBusinessKey());
      }
      else {
        // Fall back to suboptimal solution.
        // ---------------------------------
        b.append(gene.getPersistentRepresentation());
      }
      b.append(GENE_DELIMITER_CLOSING);
    }
    return b.toString();
  }
}

⌨️ 快捷键说明

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