/**
  * @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));
 }
 /**
  * @throws Exception
  * @author Klaus Meffert
  */
 public void testAddGeneticOperator_2() throws Exception {
   Configuration conf = new Configuration();
   conf.addGeneticOperator(new MutationOperator(conf));
   assertEquals(1, conf.getGeneticOperators().size());
   conf.addGeneticOperator(new MutationOperator(conf));
   assertEquals(2, conf.getGeneticOperators().size());
 }
 /** @author Klaus Meffert */
 public void testAddGeneticOperator_0() {
   Configuration conf = new Configuration();
   try {
     conf.addGeneticOperator(null);
     fail();
   } catch (InvalidConfigurationException invex) {; // this is OK
   }
 }
 /** @author Klaus Meffert */
 public void testAddGeneticOperator_1() {
   Configuration conf = new Configuration();
   try {
     conf.lockSettings();
     conf.addGeneticOperator(new MutationOperator());
     fail();
   } catch (InvalidConfigurationException invex) {; // this is OK
   }
 }
 /**
  * @throws Exception
  * @author Klaus Meffert
  * @since 3.2
  */
 public void testClone_0() 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);
   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 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_5() 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));
   try {
     conf.verifyStateIsValid();
     fail();
   } catch (InvalidConfigurationException invex) {; // this is OK
   }
 }
 /**
  * @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
   }
 }
Exemplo n.º 9
0
  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 2.4
  */
 public void testToString_0() throws Exception {
   Configuration conf = new ConfigurationForTesting();
   conf.addGeneticOperator(new MutationOperator(conf));
   conf.addNaturalSelector(new WeightedRouletteSelector(conf), true);
   conf.addNaturalSelector(new WeightedRouletteSelector(conf), false);
   conf.addNaturalSelector(new BestChromosomesSelector(conf), false);
   String s = trimString(conf.toString());
   String eventmgr =
       conf.getEventManager() != null ? conf.getEventManager().getClass().getName() : conf.S_NONE;
   String genops = "";
   if (conf.getGeneticOperators().size() < 1) {
     genops = conf.S_NONE;
   } else {
     for (int i = 0; i < conf.getGeneticOperators().size(); i++) {
       if (i > 0) {
         genops += "; ";
       }
       genops += conf.getGeneticOperators().get(i).getClass().getName();
       ;
     }
   }
   // natural selectors (pre)
   String natselsPre = "";
   int natsize = conf.getNaturalSelectors(true).size();
   if (natsize < 1) {
     natselsPre = conf.S_NONE;
   } else {
     for (int i = 0; i < natsize; i++) {
       if (i > 0) {
         natselsPre += "; ";
       }
       natselsPre += " " + conf.getNaturalSelectors(true).get(i).getClass().getName();
     }
   }
   // natural selectors (post)
   String natselsPost = "";
   natsize = conf.getNaturalSelectors(false).size();
   if (natsize < 1) {
     natselsPost = conf.S_NONE;
   } else {
     for (int i = 0; i < natsize; i++) {
       if (i > 0) {
         natselsPost += "; ";
       }
       natselsPost += " " + conf.getNaturalSelectors(false).get(i).getClass().getName();
     }
   }
   assertEquals(
       trimString(
           conf.S_CONFIGURATION
               + ":"
               + conf.S_CONFIGURATION_NAME
               + ":"
               + conf.getName()
               + " "
               + conf.S_POPULATION_SIZE
               + ":"
               + conf.getPopulationSize()
               + " "
               + conf.S_MINPOPSIZE
               + ":"
               + conf.getMinimumPopSizePercent()
               + " "
               + conf.S_CHROMOSOME_SIZE
               + ":"
               + conf.getChromosomeSize()
               + " "
               + conf.S_SAMPLE_CHROM
               + ":"
               + conf.S_SIZE
               + ":"
               + conf.getSampleChromosome().size()
               + " "
               + conf.S_TOSTRING
               + ":"
               + conf.getSampleChromosome().toString()
               + " "
               + conf.S_RANDOM_GENERATOR
               + ":"
               + conf.getRandomGenerator().getClass().getName()
               + " "
               + conf.S_EVENT_MANAGER
               + ":"
               + eventmgr
               + " "
               + conf.S_CONFIGURATION_HANDLER
               + ":"
               + conf.getConfigurationHandler().getName()
               + " "
               + conf.S_FITNESS_FUNCTION
               + ":"
               + conf.getFitnessFunction().getClass().getName()
               + " "
               + conf.S_FITNESS_EVALUATOR
               + ":"
               + conf.getFitnessEvaluator().getClass().getName()
               + " "
               + conf.S_GENETIC_OPERATORS
               + ":"
               + genops
               + " "
               + conf.S_NATURAL_SELECTORS
               + "("
               + conf.S_PRE
               + ")"
               + ":"
               + natselsPre
               + " "
               + conf.S_NATURAL_SELECTORS
               + "("
               + conf.S_POST
               + ")"
               + ":"
               + natselsPost
               + " "
           //                            + conf.S_POPCONSTANT_SELECTOR + ":"
           //                            + "null" + " "
           ),
       s);
 }