1 10 package org.jgap.impl.salesman; 11 12 import org.jgap.*; 13 import org.jgap.impl.*; 14 import junit.framework.*; 15 16 23 public class TravellingSalesmanTest 24 extends JGAPTestCase { 25 26 private final static String CVS_REVISION = "$Revision: 1.13 $"; 27 28 public static Test suite() { 29 TestSuite suite = new TestSuite(TravellingSalesmanTest.class); 30 return suite; 31 } 32 33 public void setUp() { 34 super.setUp(); 35 Configuration.reset(); 36 } 37 38 public void tearDown() 39 throws Exception { 40 super.tearDown(); 41 } 42 43 49 public void testSampleTravellingSalesmanApp() 50 throws Exception { 51 int oks = 0; 53 for (int i = 0; i < 7; i++) { 54 TravellingSalesmanForTest t = new TravellingSalesmanForTest(); 55 IChromosome optimal = t.findOptimalPath(null); 56 if (Integer.MAX_VALUE / 2 - optimal.getFitnessValue() <= 7) { 57 oks++; 58 } 59 Configuration.reset(); 60 } 61 if (oks < 6) { 62 fail("Less than 6 cities computed correctly!"); 63 } 64 } 65 66 72 public void testSetAcceptableCost_0() throws Exception { 73 TravellingSalesmanForTest t = new TravellingSalesmanForTest(); 74 assertEquals(-1, t.getAcceptableCost()); 75 t.setAcceptableCost(47); 76 assertEquals(47, t.getAcceptableCost()); 77 } 78 79 85 public void testSetStartOffset_0() throws Exception { 86 TravellingSalesmanForTest t = new TravellingSalesmanForTest(); 87 assertEquals(1, t.getStartOffset()); 88 t.setStartOffset(47); 89 assertEquals(47, t.getStartOffset()); 90 } 91 92 134 public class TravellingSalesmanForTest 135 extends Salesman { 136 137 public static final int CITIES = 7; 138 139 146 public IChromosome createSampleChromosome(Object a_initial_data) { 147 try { 148 Gene[] genes = new Gene[CITIES]; 149 for (int i = 0; i < genes.length; i++) { 150 genes[i] = new IntegerGene(getConfiguration(), 0, CITIES - 1); 151 genes[i].setAllele(new Integer (i)); 152 } 153 IChromosome sample = new Chromosome(getConfiguration(), genes); 154 shuffle(genes); 159 return sample; 164 } 165 catch (InvalidConfigurationException iex) { 166 throw new IllegalStateException (iex.getMessage()); 167 } 168 } 169 170 179 public double distance(Gene a_from, Gene a_to) { 180 IntegerGene a = (IntegerGene) a_from; 181 IntegerGene b = (IntegerGene) a_to; 182 int A = a.intValue(); 183 int B = b.intValue(); 184 if (A == 0 && B == CITIES - 1) { 185 return 1; 186 } 187 if (B == 0 && A == CITIES - 1) { 188 return 1; 189 } 190 return Math.abs(A - B); 191 } 192 193 } 194 } 195 | Popular Tags |