jenes.tutorials.problem5.DoubleAlleleSet

01 /*
02  * JENES
03  * A time and 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.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 an allele set made of doubles.
35  *
36  @author Luigi Troiano
37  *
38  @version 1.0
39  @since 1.0
40  */
41 
42 public class DoubleAlleleSet extends GenericAlleleSet<Double> {
43     
44     public DoubleAlleleSet(Set<Double> set) {
45         super(set);
46     }
47     
48     /**
49      * Builds a DoubleAlleleSet 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 DoubleAlleleSet
55      */
56     public static DoubleAlleleSet createRandom(int size,  double lowerBound, double upperBound ) {
57         HashSet<Double> values = new HashSet<Double>();
58         Random rand = Random.getInstance();
59         
60         forint i = 0; i < size; ++i  ) {
61             values.add(rand.nextDouble(lowerBound, upperBound+Double.MIN_VALUE));
62         }
63         
64         return new DoubleAlleleSet(values);
65     }
66     
67     /**
68      * Builds a new DoubleAlleleSet with uniformly distributed values within the range [lowerBound,upperBound]
69      <p>
70      @param size the allala set cardinality
71      @param lowerBound the min value to choose
72      @param upperBound the max value to choose
73      @return a new DoubleAlleleSet
74      */
75     public static DoubleAlleleSet createUniform(int size,  int lowerBound, int upperBound ) {
76         HashSet<Double> values = new HashSet<Double>();
77         
78         double step = 1.0/upperBound - lowerBound;
79         fordouble x = lowerBound; x <= upperBound; x += step ) {
80             values.add(x);
81         }
82         
83         return new DoubleAlleleSet(values);
84     }
85     
86     
87 }