jenes.tutorials.old.problem5.IntegerAlleleSet.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.problem5;
20 
21 import java.util.HashSet;
22 import java.util.Set;
23 
24 import jenes.utils.Random;
25 import jenes.chromosome.GenericAlleleSet;
26 
27 /**
28  * Tutorial illustrating the use of object-oriented chromosomes, whose
29  * allele set can be defined by the user for each gene.
30  *
31  * In this example the chromosomes are combinations of colors. We aim at finding
32  * the vector of colors closest to a given sequence.
33  *
34  * This class defines a set of integers.
35  *
36  @author Luigi Troiano
37  @author Pierpaolo Lombardi
38  *
39  @version 1.0
40  @since 1.0
41  */
42 public class IntegerAlleleSet extends GenericAlleleSet<Integer> {
43     
44     public IntegerAlleleSet(Set<Integer> set) {
45         super(set);
46     }
47     
48     /**
49      * Builds an IntegerAlleleSet with random values within the range [lowerBound,upperBound]
50      <p>
51      @param size the allala set cardinality
52      @param lowerBound the min value to choose
53      @param upperBound the max value to choose
54      @return a new IntegerAlleleSet
55      */
56     public static IntegerAlleleSet createRandom(int size,  int lowerBound, int upperBound ) {
57         HashSet<Integer> values = new HashSet<Integer>();
58         int s0 = upperBound - lowerBound + 1;
59         ifsize > s0 size = s0;
60         
61         Random rand = Random.getInstance();
62         
63         forint i = 0; i < s0; ++i  ) {
64             
65             int chosen = values.size();
66             double coin = ((double)size-chosen)/(s0-i);
67             boolean justEnough = s0-i == size-chosen;
68             
69             ifjustEnough || rand.nextBoolean(coin) ) {
70                 values.add(lowerBound + i);
71             }
72         }
73         
74         return new IntegerAlleleSet(values);
75     }
76     
77     /**
78      * Builds a new IntegerAlleleSet with uniformly distributed values within the range [lowerBound,upperBound]
79      <p>
80      @param size the allala set cardinality
81      @param lowerBound the min value to choose
82      @param upperBound the max value to choose
83      @return a new IntegerAlleleSet
84      */
85     public static IntegerAlleleSet createUniform(int size,  int lowerBound, int upperBound ) {
86         HashSet<Integer> values = new HashSet<Integer>();
87         int s0 = upperBound - lowerBound + 1;
88         ifsize > s0 size = s0;
89         
90         double step = 1.0/(upperBound - lowerBound);
91         fordouble x = lowerBound; x <= upperBound; x += step ) {
92             int i = (intMath.round(x);
93             values.add(i);
94         }
95         
96         return new IntegerAlleleSet(values);
97     }
98     
99 }