java.tutorial.old.problem2.SimpleDispencer.java

01 /*
02  * JENES
03  * A time asnd memory efficient Java library for genetic algorithms and more 
04  * Copyright (C) 2011 Intelligentia srl
05  *
06  * This program is free software: you can redistribute it and/or modify
07  * it under the terms of the GNU General Public License as published by
08  * the Free Software Foundation, either version 3 of the License, or
09  * (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
18  */
19 package jenes.tutorials.old.problem2;
20 
21 import jenes.chromosome.Chromosome;
22 import jenes.population.Individual;
23 import jenes.population.Population;
24 import jenes.stage.ExclusiveDispenser;
25 
26 /**
27  * Tutorial showing how to extend <code>GeneticAlgorithm</code> and how to use
28  * the flexible and configurable breeding structure in Jenes.
29  * The problem consists in searching a pattern of integers with a given precision.
30  * Solutions flow through two different crossovers in parallel. Some are processed by
31  * a single point crossover, the other by a double point crossover.
32  * After solutions are mutated.
33  *
34  * This class implements the strategy for dispensing solutions in the two branches.
35  * Odd solutions goes to the first, even to the second.
36  *
37  @author Luigi Troiano
38  @author Pierpaolo Lombardi
39  *
40  @version 1.0
41  *
42  @since 1.0
43  */
44 public class SimpleDispenser<T extends Chromosome> extends ExclusiveDispenser<T> {
45     
46     private int count;
47     
48     public SimpleDispenser(int span) {
49         super(span);
50     }
51     
52     public void preDistribute(Population<T> population){
53         this.count = 0;
54     }
55     
56     @Override
57     public int distribute(Individual<T> ind) {
58         return count++ % span;
59     }
60     
61 }