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