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

📄 doublegenetest.java

📁 java实现的遗传算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licencing information please see the file license.txt included with JGAP
 * or have a look at the top of class org.jgap.Chromosome which representatively
 * includes the JGAP license policy applicable for any file delivered with JGAP.
 */
package org.jgap.impl;

import java.util.*;
import org.jgap.*;
import junit.framework.*;

/**
 * Tests the DoubleGene class
 *
 * @author Klaus Meffert
 * @since 1.1
 */
public class DoubleGeneTest
    extends JGAPTestCase {
  /** String containing the CVS revision. Read out via reflection!*/
  private static final String CVS_REVISION = "$Revision: 1.24 $";

  //delta for distinguishing whether a value is to be interpreted as zero
  private static final double DELTA = 0.0001d;

  public void setUp() {
    Genotype.setConfiguration(null);
  }

  public static Test suite() {
    TestSuite suite = new TestSuite(DoubleGeneTest.class);
    return suite;
  }

  /**
   * @author Klaus Meffert
   */
  public void testConstruct_0() {
    Gene gene = new DoubleGene(1.1d, 100.0d);
    //following should be possible without exception
    gene.setAllele(new Double(101.1d));
  }

  /**
   * @author Klaus Meffert
   */
  public void testConstruct_1() {
    Gene gene = new DoubleGene();
    gene.setAllele(new Double(Double.MAX_VALUE));
  }

  /**
   * @author Klaus Meffert
   */
  public void testConstruct_2() {
    Gene gene = new DoubleGene();
    gene.setAllele(new Double( - (Double.MAX_VALUE / 2)));
  }

  /**
   * @author Klaus Meffert
   */
  public void testToString_0() {
    Gene gene = new DoubleGene(1.2d, 99.7d);
    gene.setAllele(new Double(47.3d));
    assertEquals("DoubleGene(1.2,99.7)=47.3", gene.toString());
  }

  /**
   * @author Klaus Meffert
   */
  public void testToString_1() {
    Gene gene = new DoubleGene( -100.0d, 100.0d);
    gene.setAllele(new Double( -88.75286d));
    assertEquals("DoubleGene(-100.0,100.0)=-88.75286", gene.toString());
  }

  /**
   * @author Klaus Meffert
   */
  public void testGetAllele_0() {
    Gene gene = new DoubleGene(1.9d, 100.4d);
    gene.setAllele(new Double(33.0d));
    assertEquals(new Double(33.0d), gene.getAllele());
  }

  /**
   * @author Klaus Meffert
   */
  public void testGetAllele_1() {
    Gene gene = new DoubleGene(1.8d, 100.1d);
    gene.setAllele(new Double(1.9d));
    assertEquals(new Double(1.9d), gene.getAllele());
  }

  /**
   * @author Klaus Meffert
   */
  public void testGetAllele_2() {
    Gene gene = new DoubleGene(1.0d, 100.0d);
    gene.setAllele(new Double(100.0d));
    assertEquals(new Double(100.0d), gene.getAllele());
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_0() {
    Gene gene1 = new DoubleGene(1.1d, 100.2d);
    Gene gene2 = new DoubleGene(1.1d, 100.2d);
    assertTrue(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_1() {
    Gene gene1 = new DoubleGene(1.9d, 100.4d);
    assertFalse(gene1.equals(null));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_2() {
    Gene gene1 = new DoubleGene(11.2d, 100.7d);
    assertFalse(gene1.equals(new BooleanGene()));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_3() {
    Gene gene1 = new DoubleGene(1.0d, 100.7d);
    assertFalse(gene1.equals(new Vector()));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_4() {
    Gene gene1 = new DoubleGene(1.2d, 100.3d);
    Gene gene2 = new DoubleGene(1.2d, 99.5d);
    assertTrue(gene1.equals(gene2));
    assertTrue(gene2.equals(gene1));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_5() {
    Gene gene1 = new FixedBinaryGene(5);
    Gene gene2 = new DoubleGene(1, 99);
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_6() {
    Gene gene1 = new DoubleGene(1, 99);
    Gene gene2 = new BooleanGene();
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   * @author Klaus Meffert
   */
  public void testEquals_7() {
    Gene gene1 = new DoubleGene(1, 99);
    Gene gene2 = new IntegerGene();
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   * @author Klaus Meffert
   * @since 2.4
   */
  public void testEquals_8() {
    Gene gene1 = new DoubleGene(1.2d, 100.3d);
    gene1.setAllele(new Double(1));
    Gene gene2 = new DoubleGene(1.2d, 99.5d);
    gene2.setAllele(new Double(-1));
    assertFalse(gene1.equals(gene2));
    assertFalse(gene2.equals(gene1));
  }

  /**
   * @author Klaus Meffert
   */
  public void testDoubleValue_0() {
    DoubleGene gene1 = new DoubleGene(1.0d, 10000.0d);
    gene1.setAllele(new Double(4711.0d));
    assertEquals(4711.0d, gene1.doubleValue(), DELTA);
  }

  /**
   * @author Klaus Meffert
   */
  public void testDoubleValue_1() {
    DoubleGene gene1 = new DoubleGene(1.765d, 10000.0d);
    gene1.setAllele(null);
    try {
      assertEquals(0.0d, gene1.doubleValue(), DELTA);
      fail();
    }
    catch (NullPointerException nullex) {
      ; //this is OK
    }
  }

  /**
   * Set Allele to null, no exception should occur
   * @author Klaus Meffert
   */
  public void testSetAllele_0() {
    Gene gene1 = new DoubleGene(1.0d, 10000.0d);
    gene1.setAllele(null);
  }

  /**
   * @author Klaus Meffert
   */
  public void testSetAllele_1() {
    Gene gene1 = new DoubleGene(1.0d, 10000.0d);
    try {
      gene1.setAllele("22");
      fail();
    }
    catch (ClassCastException classex) {
      ; //this is OK
    }
  }

  /**
   * @author Klaus Meffert
   */
  public void testSetAllele_2() {
    Gene gene1 = new DoubleGene(1.0d, 10000.0d);
    try {
      gene1.setAllele(new Integer(22));
      fail();
    }
    catch (ClassCastException classex) {
      ; //this is OK
    }
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testNewGene_0()
      throws Exception {
    DoubleGene gene1 = new DoubleGene(1.0d, 10000.0d);
    IGeneConstraintChecker checker = new GeneConstraintChecker();
    gene1.setConstraintChecker(checker);
    gene1.setAllele(new Double(4711.0d));
    Double lower1 = (Double) privateAccessor.getField(gene1,
        "m_lowerBounds");
    Double upper1 = (Double) privateAccessor.getField(gene1,
        "m_upperBounds");
    DoubleGene gene2 = (DoubleGene)gene1.newGene();
    Double lower2 = (Double) privateAccessor.getField(gene2,
        "m_lowerBounds");
    Double upper2 = (Double) privateAccessor.getField(gene2,
        "m_upperBounds");
    assertEquals(lower1, lower2);
    assertEquals(upper1, upper2);
    assertEquals(checker, gene2.getConstraintChecker());
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_0()
      throws Exception {
    Gene gene1 = new DoubleGene(2.05d, 7.53d);
    gene1.setAllele(new Double(4.5d));
    String pres1 = gene1.getPersistentRepresentation();
    Gene gene2 = new DoubleGene();
    gene2.setValueFromPersistentRepresentation(pres1);
    String pres2 = gene2.getPersistentRepresentation();
    assertEquals(pres1, pres2);
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_1()
      throws Exception {
    Gene gene1 = new DoubleGene(2.05d, 7.53d);
    gene1.setValueFromPersistentRepresentation(null);
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_2()
      throws Exception {
    Gene gene1 = new DoubleGene(2.05d, 7.53d);
    try {
      gene1.setValueFromPersistentRepresentation("2.3");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_3()
      throws Exception {
    Gene gene1 = new DoubleGene(2.05d, 7.53d);
    try {
      gene1.setValueFromPersistentRepresentation("2.3" +
                                                 DoubleGene.
                                                 PERSISTENT_FIELD_DELIMITER +
                                                 "4.6" +
                                                 DoubleGene.
                                                 PERSISTENT_FIELD_DELIMITER +
                                                 "6,5");
      fail();
    }
    catch (UnsupportedRepresentationException uex) {
      ; //this is OK
    }
  }

  /**
   *
   * @throws Exception
   * @author Klaus Meffert
   */
  public void testPersistentRepresentation_4()
      throws Exception {
    Gene gene1 = new DoubleGene(2.05d, 7.53d);

⌨️ 快捷键说明

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