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

📄 mapgene.java

📁 jGAp 遗传算法 提不错的一款软件 最新的更新
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

              Class valueType = Class.forName(valueClass);
              Constructor valueC = valueType.getConstructor(new Class[]{String.class});
              Object valueObject = valueC.newInstance(new Object[]{element});
              addAllele(keyObject, valueObject);
              lastWasOpening = 0;
            } catch (Exception cex) {
              throw new UnsupportedRepresentationException("Invalid class: "
                  + keyClass);
            }
          }
          else {
            throw new IllegalStateException("Closing bracket missing");
          }
        }
        else {
          if (element.startsWith("(")) {
            keyClass = element.substring(1);
            lastWasOpening = 1;
          }
          else {
            throw new IllegalStateException("Opening bracket missing");
          }
        }
      }
      if (lastWasOpening != 0) {
        throw new IllegalStateException("Elements missing");
      }
    }
  }

  /**
   * Retrieves a string representation of this Gene that includes any
   * information required to reconstruct it at a later time, such as its
   * value and internal state. This string will be used to represent this
   * Gene in XML persistence. This is an optional method but, if not
   * implemented, XML persistence and possibly other features will not be
   * available. An UnsupportedOperationException should be thrown if no
   * implementation is provided.
   *
   * @return string representation of this Gene's current state
   * @throws UnsupportedOperationException to indicate that no implementation
   * is provided for this method
   *
   * @author Neil Rostan
   * @author Klaus Meffert
   * @since 2.4
   */
  public String getPersistentRepresentation()
      throws UnsupportedOperationException {
    // The persistent representation includes the value and the allele
    // assignment.
    // ---------------------------------------------------------------
    Iterator it = m_geneMap.keySet().iterator();
    StringBuffer strbf = new StringBuffer();
    boolean first = true;
    while (it.hasNext()) {
      if (!first) {
        strbf.append(",");
      }
      Object key = it.next();
      Object value = m_geneMap.get(key);
      strbf.append("(" + key.getClass().getName() + "," + key.toString() + "," +
                   value.getClass().getName() + "," + value.toString() + ")");
      first = false;
    }
    return m_value.toString() + MapGene.PERSISTENT_FIELD_DELIMITER +
        strbf.toString();
  }

  /**
   * Sets the value (allele) of this Gene to the new given value. This class
   * expects the value to be an instance of current type (e.g. Integer).
   *
   * @param a_newValue the new value of this Gene instance
   *
   * @author Johnathan Kool
   * @since 2.4
   */
  public void setAllele(Object a_newValue) {
    // ignore null value as it should have no effect here (otherwise problematic
    // in conjunction with newGene)
    if (a_newValue == null) {
      return;
    }
    if (m_geneMap.keySet().isEmpty()) {
      m_value = a_newValue;
    }
    else if (m_geneMap.keySet().contains(a_newValue)) {
      m_value = m_geneMap.get(a_newValue);
    }
    else {
      throw new IllegalArgumentException("Allele value being set ("
                                         + a_newValue
                                         + ") is not an element of the set of"
                                         + " permitted values.");
    }
  }

  /**
   * Compares this NumberGene with the specified object (which must also
   * be a NumberGene) for order, which is determined by the number
   * value of this Gene compared to the one provided for comparison.
   *
   * @param a_other the NumberGene to be compared to this NumberGene
   * @return a negative integer, zero, or a positive integer as this object
   * is less than, equal to, or greater than the object provided for comparison
   *
   * @throws ClassCastException if the specified object's type prevents it from
   * being compared to this Gene
   *
   * @author Klaus Meffert
   * @author Johnathan Kool
   * @since 2.4
   */
  public int compareTo(Object a_other) {
    MapGene otherGene = (MapGene) a_other;
    // First, if the other gene (or its value) is null, then this is
    // the greater allele. Otherwise, just use the overridden compareToNative
    // method to perform the comparison.
    // ---------------------------------------------------------------
    if (otherGene == null) {
      return 1;
    }
    else if (otherGene.m_value == null) {
      // If our value is not null, then we're the greater gene.
      // ------------------------------------------------------
      if (m_value != null) {
        return 1;
      }
    }
    try {
      int size1 = m_geneMap.size();
      int size2 = otherGene.m_geneMap.size();
      if (size1 != size2) {
        if (size1 < size2) {
          return -1;
        }
        else {
          return 1;
        }
      }
      else {
        // Compare geneMap keys and values.
        Iterator it1 = m_geneMap.keySet().iterator();
//        Iterator it2 = otherGene.m_geneMap.keySet().iterator();
        while (it1.hasNext()) {
          Object key1 = it1.next();
          if (!otherGene.m_geneMap.keySet().contains(key1)) {
            Object key2 = otherGene.m_geneMap.keySet().iterator().next();
            if (Comparable.class.isAssignableFrom(key1.getClass())
                && Comparable.class.isAssignableFrom(key2.getClass())) {
              return ( (Comparable) key1).compareTo(key2);
            }
            else {
              // Arbitrarily return -1
              return -1;
            }
          }
          Object value1 = m_geneMap.get(key1);
          Object value2 = otherGene.m_geneMap.get(key1);
          if (value1 == null && value2 != null) {
            return -1;
          }
          else if (value1 == null && value2 != null) {
            return -1;
          }
          else if (!value1.equals(value2)) {
            if (value2 == null) {
              return 1;
            }
            else {
              if (Comparable.class.isAssignableFrom(value1.getClass())
                  && Comparable.class.isAssignableFrom(value2.getClass())) {
                return ( (Comparable) value1).compareTo(value2);
              }
              else {
                // Arbitrarily return -1
                return -1;
              }
            }
          }
        }
      }
      if (m_value == null) {
        if (otherGene.m_value != null) {
          return 1;
        }
        else {
          return 0;
        }
      }
      Method method = m_value.getClass().getMethod("compareTo",
          new Class[] {otherGene.m_value.getClass()});
      Integer i = (Integer) method.invoke(m_value,
                                          new Object[] {otherGene.m_value});
      return i.intValue();
    }
    catch (InvocationTargetException ex) {
      ex.printStackTrace();
      throw new IllegalArgumentException("CompareTo method of the Gene value" +
                                         " object cannot be invoked.");
    }
    catch (IllegalArgumentException ex) {
      ex.printStackTrace();
      throw new IllegalArgumentException("The value object of the Gene does" +
                                         " not have a compareTo method.  It" +
                                         " cannot be compared.");
    }
    catch (IllegalAccessException ex) {
      ex.printStackTrace();
      throw new IllegalArgumentException("The compareTo method of the Gene" +
                                         " value object cannot be accessed ");
    }
    catch (SecurityException ex) {
      ex.printStackTrace();
      throw new IllegalArgumentException("The compareTo method of the Gene" +
                                         " value object cannot be accessed." +
                                         "  Insufficient permission levels.");
    }
    catch (NoSuchMethodException ex) {
      ex.printStackTrace();
      throw new IllegalArgumentException("The value object of the Gene does" +
                                         " not have a compareTo method.  It" +
                                         " cannot be compared.");
    }
  }

  /**
   * @return the internal value of the gene
   * @since 2.4
   */
  protected Object getInternalValue() {
    return m_value;
  }

  /**
   * Modified hashCode() function to return different hashcodes for differently
   * ordered genes in a chromosome
   * @return -1 if no allele set, otherwise value return by BaseGene.hashCode()
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public int hashCode() {
    if (getInternalValue() == null) {
      return -71;
    }
    else {
      return super.hashCode();
    }
  }

  /**
   * Retrieves a string representation of this Gene's value that may be useful
   * for display purposes.
   *
   * @return a string representation of this Gene's value
   *
   * @author Klaus Meffert
   * @since 2.4
   */
  public String toString() {
    String result = "[";
    if (m_geneMap.size() < 1) {
      result += "null";
    }
    else {
      Set keys = m_geneMap.keySet();
      Iterator keyIterator = keys.iterator();
      boolean firstTime = true;
      while (keyIterator.hasNext()) {
        if (!firstTime) {
          result += ",";
        }
        else {
          firstTime = false;
        }
        Object key = keyIterator.next();
        String keyString;
        if (key == null) {
          keyString = "null";
        }
        else {
          keyString = key.toString();
        }
        result += "(" + keyString + ",";
        Object value = m_geneMap.get(key);
        String valueString;
        if (value == null) {
          valueString = "null";
        }
        else {
          valueString = value.toString();
        }
        result += valueString + ")";
      }
    }
    result += "]";
    return result;
  }
}

⌨️ 快捷键说明

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