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 a set of integers.
35 *
36 * @author Luigi Troiano
37 *
38 * @version 1.0
39 * @since 1.0
40 */
41 public class IntegerAlleleSet extends GenericAlleleSet<Integer> {
42
43 public IntegerAlleleSet(Set<Integer> set) {
44 super(set);
45 }
46
47 /**
48 * Builds an IntegerAlleleSet with random values within the range [lowerBound,upperBound]
49 * <p>
50 * @param size the allala set cardinality
51 * @param lowerBound the min value to choose
52 * @param upperBound the max value to choose
53 * @return a new IntegerAlleleSet
54 */
55 public static IntegerAlleleSet createRandom(int size, int lowerBound, int upperBound ) {
56 HashSet<Integer> values = new HashSet<Integer>();
57 int s0 = upperBound - lowerBound + 1;
58 if( size > s0 ) size = s0;
59
60 Random rand = Random.getInstance();
61
62 for( int i = 0; i < s0; ++i ) {
63
64 int chosen = values.size();
65 double coin = ((double)size-chosen)/(s0-i);
66 boolean justEnough = s0-i == size-chosen;
67
68 if( justEnough || rand.nextBoolean(coin) ) {
69 values.add(lowerBound + i);
70 }
71 }
72
73 return new IntegerAlleleSet(values);
74 }
75
76 /**
77 * Builds a new IntegerAlleleSet with uniformly distributed values within the range [lowerBound,upperBound]
78 * <p>
79 * @param size the allala set cardinality
80 * @param lowerBound the min value to choose
81 * @param upperBound the max value to choose
82 * @return a new IntegerAlleleSet
83 */
84 public static IntegerAlleleSet createUniform(int size, int lowerBound, int upperBound ) {
85 HashSet<Integer> values = new HashSet<Integer>();
86 int s0 = upperBound - lowerBound + 1;
87 if( size > s0 ) size = s0;
88
89 double step = 1.0/(upperBound - lowerBound);
90 for( double x = lowerBound; x <= upperBound; x += step ) {
91 int i = (int) Math.round(x);
92 values.add(i);
93 }
94
95 return new IntegerAlleleSet(values);
96 }
97
98 }
|
|
|