示例#1
0
  // :::::::::::::::::::::::::::  Copyright(c) M@nso  2016  :::::::::::::::::::
  ///////////////////////////////////////////////////////////////////////////
  public static void main(String[] args) {
    Random rnd = new Random(1223);
    SimplePopulation mp = new MultiPopulation();
    mp.setParameters("500");
    mp.setRandomGenerator(rnd);

    Solution i = new OneMax();
    i.setRandomGenerator(rnd);
    i.setParameters("1000");
    mp.createRandom(i);

    for (int j = 0; j < mp.getNumberOfIndividuals(); j++) {
      Solution i1 = mp.removeRandom();
      Solution i2 = mp.removeRandom();
      if (i1.compareTo(i2) > 0) {
        mp.addIndividual(i1);
        mp.addIndividual(i1);
      } else {
        mp.addIndividual(i2);
        mp.addIndividual(i2);
      }
    }
    Recombination x = new M_Uniforme();
    x.setRandomGenerator(rnd);

    for (int j = 0; j < mp.getNumberOfIndividuals(); j++) {
      System.out.println(" i " + j);
      mp = x.execute(mp);
      mp.evaluate();
    }

    System.out.println("" + mp);
  }
示例#2
0
 @Override
 public SimplePopulation execute(SimplePopulation parents) {
   // clean population
   SimplePopulation newPopulation = parents.getCleanClone();
   Solution indiv1;
   Solution indiv2;
   // get list of individuals
   List<Solution> pop = parents.getList(); // raw list of population (parents.pop)
   int index = 0;
   while (index < pop.size()) { // iterate all the individuals
     // first parent
     indiv1 = pop.get(index++);
     if (index == pop.size() || random.nextDouble() >= pCrossover) { // insert clone
       newPopulation.addIndividual(indiv1);
       continue;
     } // END: if
     // second parent
     indiv2 = pop.get(index++);
     // ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
     // -------------------- multiset recombination ----------------------
     // ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
     // number of recombinations
     int numberOfRecombinations = Math.min(indiv1.getNumberOfCopies(), indiv2.getNumberOfCopies());
     // insert clones of the individual with greather number of copies
     if (indiv1.getNumberOfCopies() > indiv2.getNumberOfCopies()) {
       Solution clone = indiv1.getClone();
       clone.setNumberOfCopies(indiv1.getNumberOfCopies() - numberOfRecombinations);
       newPopulation.addIndividual(clone); // add clone
     } else if (indiv2.getNumberOfCopies() > indiv1.getNumberOfCopies()) {
       Solution clone = indiv2.getClone();
       clone.setNumberOfCopies(indiv2.getNumberOfCopies() - numberOfRecombinations);
       newPopulation.addIndividual(clone); // add clone
     }
     double prob = 0.5;
     // recombine multiset individuals
     for (int nCuts = 1; nCuts <= numberOfRecombinations; nCuts++) {
       // clone multiset individuals to simple individuals
       Solution clone1 = indiv1.getClone();
       clone1.setNumberOfCopies(1);
       Solution clone2 = indiv2.getClone();
       clone2.setNumberOfCopies(1);
       // execute crossover
       doCrossover((BinaryString) clone1, (BinaryString) clone2, prob / nCuts);
       // add individuals
       newPopulation.addIndividual(clone1);
       newPopulation.addIndividual(clone2);
     }
   } // END :  list
   return newPopulation;
 }
示例#3
0
 public SimplePopulation getPopulation(BinaryString template) {
   SimplePopulation pop = new SimplePopulation();
   for (Individual ind : individuals) {
     pop.addIndividual(ind.getSolution(template));
   }
   return pop;
 }
示例#4
0
 public SimplePopulation sampleNewPopulation(SimplePopulation pop) {
   SimplePopulation newPop = pop.getCleanClone();
   BinaryString template = (BinaryString) pop.getIndividual(0);
   for (int i = 0; i < offspringSize; i++) {
     newPop.addIndividual(generateInstance(template));
   }
   return newPop;
 }