1 10 package org.jgap.data; 11 12 import org.jgap.*; 13 import org.jgap.impl.*; 14 import junit.framework.*; 15 16 23 public class DataTreeBuilderTest 24 extends JGAPTestCase { 25 26 private final static String CVS_REVISION = "$Revision: 1.11 $"; 27 28 private final static int NUM_CHROMS = 5; 30 31 private final static int NUM_GENES = 2; 33 34 public static Test suite() { 35 TestSuite suite = new TestSuite(DataTreeBuilderTest.class); 36 return suite; 37 } 38 39 public void setUp() { 40 super.setUp(); 41 Configuration.reset(); 42 } 43 44 49 public void testRepresentGenotypeAsDocument_0() 50 throws Exception { 51 Configuration conf = new DefaultConfiguration(); 53 conf.setFitnessFunction(new StaticFitnessFunction(5)); 54 conf.setPopulationSize(NUM_CHROMS); 55 conf.setSampleChromosome(new Chromosome(conf, new Gene[] { 56 new IntegerGene(conf, 1, 5), 57 new IntegerGene(conf, 1, 3)})); 58 Chromosome[] chroms = new Chromosome[NUM_CHROMS]; 61 for (int i = 0; i < NUM_CHROMS; i++) { 62 chroms[i] = new Chromosome(conf, new Gene[] { 63 new IntegerGene(conf, 1, 5), 64 new IntegerGene(conf, 1, 10)}); 65 chroms[i].getGene(0).setAllele(new Integer (i + 1)); 66 chroms[i].getGene(1).setAllele(new Integer (i + 1)); 67 } 68 Population popul = new Population(conf, chroms); 69 Genotype genotype = new Genotype(conf, popul); 70 IDataCreators doc = DataTreeBuilder.getInstance(). 72 representGenotypeAsDocument(genotype); 73 IDataElementList tree = doc.getTree(); 75 assertTrue(tree.getLength() == 1); 77 IDataElement element = tree.item(0); 78 assertEquals("genotype", element.getTagName()); 80 IDataElementList chromList = element.getChildNodes(); 81 assertEquals(NUM_CHROMS, chromList.getLength()); 82 for (int i = 0; i < NUM_CHROMS; i++) { 84 IDataElement chrom = chromList.item(i); 85 assertTrue(chrom.getTagName().equals("chromosome")); 86 IDataElementList genesList = chrom.getChildNodes(); 87 assertTrue(genesList.getLength() == 1); 88 IDataElement genes = genesList.item(0); 89 assertTrue(genes.getTagName().equals("genes")); 90 IDataElementList geneList = genes.getChildNodes(); 91 assertTrue(geneList.getLength() == NUM_GENES); 92 for (int j = 0; j < NUM_GENES; j++) { 94 IDataElement gene = geneList.item(j); 95 assertTrue(gene.getTagName().equals("gene")); 96 IDataElementList alleleList = gene.getChildNodes(); 97 assertTrue(alleleList.getLength() == 1); 98 IDataElement allele = alleleList.item(0); 99 assertTrue(allele.getTagName().equals("allele")); 100 assertTrue(allele.getAttribute("value"). 101 equals(chroms[i].getGene(j). 102 getPersistentRepresentation())); 103 } 104 } 105 } 106 107 112 public void testRepresentChromosomeAsDocument_0() 113 throws Exception { 114 Configuration conf = new DefaultConfiguration(); 115 Chromosome chrom = new Chromosome(conf, new Gene[] { 116 new IntegerGene(conf, 1, 5), 117 new IntegerGene(conf, 1, 10)}); 118 chrom.getGene(0).setAllele(new Integer (1)); 119 chrom.getGene(1).setAllele(new Integer ( -3)); 120 IDataCreators doc = DataTreeBuilder.getInstance(). 122 representChromosomeAsDocument(chrom); 123 IDataElementList tree = doc.getTree(); 125 assertTrue(tree.getLength() == 1); 127 IDataElement element = tree.item(0); 128 assertTrue(element.getTagName().equals("chromosome")); 130 IDataElementList chromsList = element.getChildNodes(); 131 assertEquals(1, chromsList.getLength()); 132 IDataElement genes = chromsList.item(0); 133 assertTrue(genes.getTagName().equals("genes")); 134 IDataElementList geneList = genes.getChildNodes(); 135 assertTrue(geneList.getLength() == NUM_GENES); 136 for (int j = 0; j < NUM_GENES; j++) { 138 IDataElement gene = geneList.item(j); 139 assertTrue(gene.getTagName().equals("gene")); 140 IDataElementList alleleList = gene.getChildNodes(); 141 assertTrue(alleleList.getLength() == 1); 142 IDataElement allele = alleleList.item(0); 143 assertTrue(allele.getTagName().equals("allele")); 144 assertTrue(allele.getAttribute("value"). 145 equals(chrom.getGene(j). 146 getPersistentRepresentation())); 147 } 148 } 149 } 150 | Popular Tags |