/** * @throws Exception * @author Klaus Meffert * @since 3.0 */ public void testSetEventManager_2() throws Exception { Configuration conf = new Configuration(); conf.setEventManager(new EventManager()); try { conf.setEventManager(new TestEventManager()); fail(); } catch (RuntimeException rex) {; // this is OK } }
/** * @throws Exception * @author Klaus Meffert */ public void testGetter_0() throws Exception { Configuration conf = new Configuration(); Genotype.setStaticConfiguration(conf); assertEquals(false, conf.isLocked()); FitnessFunction fitFunc = new StaticFitnessFunction(2); conf.setFitnessFunction(fitFunc); Gene gene = new BooleanGene(conf); Chromosome sample = new Chromosome(conf, gene, 55); conf.setSampleChromosome(sample); NaturalSelector natSel = new WeightedRouletteSelector(); conf.addNaturalSelector(natSel, false); RandomGenerator randGen = new StockRandomGenerator(); conf.setRandomGenerator(randGen); IEventManager evMan = new EventManager(); conf.setEventManager(evMan); GeneticOperator mutOp = new MutationOperator(conf); conf.addGeneticOperator(mutOp); GeneticOperator croOp = new CrossoverOperator(conf); conf.addGeneticOperator(croOp); conf.setPopulationSize(7); assertEquals(fitFunc, conf.getFitnessFunction()); assertEquals(natSel, conf.getNaturalSelectors(false).get(0)); assertEquals(randGen, conf.getRandomGenerator()); assertEquals(sample, conf.getSampleChromosome()); assertEquals(evMan, conf.getEventManager()); assertEquals(7, conf.getPopulationSize()); assertEquals(2, conf.getGeneticOperators().size()); assertEquals(mutOp, conf.getGeneticOperators().get(0)); assertEquals(croOp, conf.getGeneticOperators().get(1)); }
/** @author Klaus Meffert */ public void testSetEventManager_0() { Configuration conf = new Configuration(); try { conf.setEventManager(null); fail(); } catch (InvalidConfigurationException invex) {; // this is OK } }
public void run() { Configuration conf = new Configuration(); try { conf.setEventManager(new EventManager()); Thread.sleep(100); } catch (Exception ex) { ex.printStackTrace(); } }
/** * @throws Exception * @author Klaus Meffert */ public void testVerifyStateIsValid_6() throws Exception { Configuration conf = new Configuration(); assertEquals(false, conf.isLocked()); conf.setFitnessFunction(new StaticFitnessFunction(2)); Gene gene = new BooleanGene(conf); conf.setSampleChromosome(new Chromosome(conf, gene, 5)); conf.addNaturalSelector(new WeightedRouletteSelector(conf), true); conf.setRandomGenerator(new StockRandomGenerator()); conf.setEventManager(new EventManager()); conf.setFitnessEvaluator(new DefaultFitnessEvaluator()); conf.addGeneticOperator(new MutationOperator(conf)); conf.setPopulationSize(1); conf.verifyStateIsValid(); }
/** * @throws Exception * @author Klaus Meffert */ public void testVerifyStateIsValid_4() throws Exception { Configuration conf = new Configuration(); assertEquals(false, conf.isLocked()); conf.setFitnessFunction(new StaticFitnessFunction(2)); Gene gene = new BooleanGene(conf); conf.setSampleChromosome(new Chromosome(conf, gene, 5)); conf.addNaturalSelector(new WeightedRouletteSelector(conf), false); conf.setRandomGenerator(new StockRandomGenerator()); conf.setEventManager(new EventManager()); try { conf.verifyStateIsValid(); fail(); } catch (InvalidConfigurationException invex) {; // this is OK } }
/** * @throws Exception * @author Klaus Meffert */ public void testVerifyStateIsValid_7() throws Exception { Configuration conf = new Configuration(); assertEquals(false, conf.isLocked()); conf.setFitnessFunction(new StaticFitnessFunction(2)); Gene gene = new BooleanGene(conf); conf.setSampleChromosome(new Chromosome(conf, gene, 5)); conf.addNaturalSelector(new WeightedRouletteSelector(conf), true); conf.setRandomGenerator(new StockRandomGenerator()); conf.setEventManager(new EventManager()); conf.addGeneticOperator(new MutationOperator(conf)); conf.setPopulationSize(1); try { conf.verifyStateIsValid(); fail(); } catch (IllegalArgumentException illex) {; // this is OK } }
/** * Cloning multiple times in a row must be possible. * * @throws Exception * @author Klaus Meffert * @since 3.2 */ public void testClone_1() throws Exception { Configuration conf = new Configuration(); conf.setFitnessFunction(new StaticFitnessFunction(2)); Gene gene = new BooleanGene(conf); conf.setSampleChromosome(new Chromosome(conf, gene, 5)); conf.addNaturalSelector(new WeightedRouletteSelector(conf), true); conf.setRandomGenerator(new StockRandomGenerator()); conf.setEventManager(new EventManager()); conf.setFitnessEvaluator(new DefaultFitnessEvaluator()); conf.addGeneticOperator(new MutationOperator(conf)); conf.setPopulationSize(1); Configuration theClone1 = (Configuration) conf.clone(); Configuration theClone2 = (Configuration) conf.clone(); assertEquals(conf, theClone1); assertEquals(conf, theClone2); assertEquals(theClone1, theClone2); }
/** * Clone after settings locked. * * @throws Exception * @author Klaus Meffert * @since 3.2 */ public void testClone_2() throws Exception { Configuration conf = new Configuration(); conf.setFitnessFunction(new StaticFitnessFunction(2)); Gene gene = new BooleanGene(conf); conf.setSampleChromosome(new Chromosome(conf, gene, 5)); conf.addNaturalSelector(new WeightedRouletteSelector(conf), true); conf.setRandomGenerator(new StockRandomGenerator()); conf.setEventManager(new EventManager()); conf.setFitnessEvaluator(new DefaultFitnessEvaluator()); conf.addGeneticOperator(new MutationOperator(conf)); conf.addGeneticOperator(new CrossoverOperator(conf, 2)); conf.setPopulationSize(1); conf.lockSettings(); Configuration theClone = (Configuration) conf.clone(); assertEquals(conf, theClone); assertEquals(2, theClone.getGeneticOperators().size()); assertEquals(MutationOperator.class, theClone.getGeneticOperators().get(0).getClass()); assertEquals(CrossoverOperator.class, theClone.getGeneticOperators().get(1).getClass()); }
/** * @throws Exception * @author Klaus Meffert */ public void testLock_1() throws Exception { Configuration conf = new Configuration(); conf.setFitnessFunction(new TestFitnessFunction()); Gene gene = new BooleanGene(conf); Chromosome sample = new Chromosome(conf, gene, 55); conf.setSampleChromosome(sample); conf.addNaturalSelector(new WeightedRouletteSelector(conf), false); conf.setRandomGenerator(new GaussianRandomGenerator()); conf.setEventManager(new EventManager()); conf.setFitnessEvaluator(new DefaultFitnessEvaluator()); conf.addGeneticOperator(new MutationOperator(conf)); conf.setPopulationSize(1); conf.lockSettings(); assertEquals(true, conf.isLocked()); try { conf.verifyChangesAllowed(); fail(); } catch (InvalidConfigurationException invex) {; // this is OK } }
public static void main(String[] args) throws InvalidConfigurationException { // Reading data from xml try { new InputData().readFromFile(XML_TEST_FILENAME); } catch (SAXException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } catch (ParserConfigurationException e) { System.out.println(e.getMessage()); } // Configuration conf = new DefaultConfiguration(); Configuration conf = new Configuration("myconf"); TimetableFitnessFunction fitnessFunction = new TimetableFitnessFunction(); InitialConstraintChecker timetableConstraintChecker = new InitialConstraintChecker(); // Creating genes Gene[] testGenes = new Gene[CHROMOSOME_SIZE]; for (int i = 0; i < CHROMOSOME_SIZE; i++) { testGenes[i] = new GroupClassTeacherLessonTimeSG( conf, new Gene[] { new GroupGene(conf, 1), new ClassGene(conf, 1), new TeacherGene(conf, 1), new LessonGene(conf, 1), new TimeGene(conf, 1) }); } System.out.println("=================================="); // Creating chromosome Chromosome testChromosome; testChromosome = new Chromosome(conf, testGenes); testChromosome.setConstraintChecker(timetableConstraintChecker); // Setup configuration conf.setSampleChromosome(testChromosome); conf.setPopulationSize(POPULATION_SIZE); conf.setFitnessFunction(fitnessFunction); // add fitness function BestChromosomesSelector myBestChromosomesSelector = new BestChromosomesSelector(conf); conf.addNaturalSelector(myBestChromosomesSelector, false); conf.setRandomGenerator(new StockRandomGenerator()); conf.setEventManager(new EventManager()); conf.setFitnessEvaluator(new DefaultFitnessEvaluator()); CrossoverOperator myCrossoverOperator = new CrossoverOperator(conf); conf.addGeneticOperator(myCrossoverOperator); TimetableMutationOperator myMutationOperator = new TimetableMutationOperator(conf); conf.addGeneticOperator(myMutationOperator); conf.setKeepPopulationSizeConstant(false); // Creating genotype // Population pop = new Population(conf, testChromosome); // Genotype population = new Genotype(conf, pop); Genotype population = Genotype.randomInitialGenotype(conf); System.out.println("Our Chromosome: \n " + testChromosome.getConfiguration().toString()); System.out.println("------------evolution-----------------------------"); // Begin evolution Calendar cal = Calendar.getInstance(); start_t = cal.getTimeInMillis(); for (int i = 0; i < MAX_EVOLUTIONS; i++) { System.out.println( "generation#: " + i + " population size:" + (Integer) population.getPopulation().size()); if (population.getFittestChromosome().getFitnessValue() >= THRESHOLD) break; population.evolve(); } cal = Calendar.getInstance(); finish_t = cal.getTimeInMillis(); System.out.println("--------------end of evolution--------------------"); Chromosome fittestChromosome = (Chromosome) population.getFittestChromosome(); System.out.println( "-------------The best chromosome---fitness=" + fittestChromosome.getFitnessValue() + "---"); System.out.println(" Group Class Time"); for (int i = 0; i < CHROMOSOME_SIZE; i++) { GroupClassTeacherLessonTimeSG s = (GroupClassTeacherLessonTimeSG) fittestChromosome.getGene(i); System.out.println( "Gene " + i + " contains: " + (Integer) s.geneAt(GROUP).getAllele() + " " + (Integer) s.geneAt(CLASS).getAllele() + " " + (Integer) s.geneAt(TEACHER).getAllele() + " " + (Integer) s.geneAt(LESSON).getAllele() + " " + (Integer) s.geneAt(TIME).getAllele()); // GroupGene gg = (GroupGene)s.geneAt(GROUP); // System.out.println("gg's idGroup"+gg.getAllele()+" gg.getGroupSize()"+ gg.getGroupSize() ); } System.out.println("Elapsed time:" + (double) (finish_t - start_t) / 1000 + "s"); // Display the best solution OutputData od = new OutputData(); od.printToConsole(fittestChromosome); // Write population to the disk try { od.printToFile(population, GENOTYPE_FILENAME, BEST_CHROMOSOME_FILENAME); } catch (IOException e) { System.out.println("IOException raised! " + e.getMessage()); } }
/** * @throws Exception * @author Klaus Meffert * @since 3.0 */ public void testSetEventManager_1() throws Exception { Configuration conf = new Configuration(); conf.setEventManager(new EventManager()); conf.setEventManager(new EventManager()); }