001 /*
002 * CISELab, Computational and Intelligent Systems Engineering Laboratory
003 * Department of Engineering
004 * University of Sannio, 82100 Benevento (ITALY)
005 * web-site: www.ciselab.org
006 *
007 * JENES, A Java Library for Genetic Algorithms
008 * Copyright (C) 2009, Luigi Troiano
009 *
010 * This library is free software; you can redistribute it and/or
011 * modify it under the terms of the GNU Lesser General Public
012 * License as published by the Free Software Foundation; either
013 * version 2.1 of the License, or (at your option) any later version.
014 *
015 * This library is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018 * Lesser General Public License for more details.
019 *
020 * You should have received a copy of the GNU Lesser General Public
021 * License along with this library; if not, write to the Free Software
022 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
023 02110-1301 USA
024 *
025 * COPYRIGHT DISCLAIMER
026 * CISELab, hereby disclaims all copyright interest in the
027 * library `JENES' (A Java Library for Genetic Algorithms)
028 * written by Luigi Troiano et al.
029 *
030 * Luigi Troiano, 1 January 2009
031 * CISELab Coordinator
032 *
033 */
034 package jenes.tutorials.problem5;
035
036 import java.util.HashSet;
037 import java.util.Set;
038
039 import jenes.Random;
040 import jenes.chromosome.GenericAlleleSet;
041
042 public class IntegerAlleleSet extends GenericAlleleSet<Integer> {
043
044 public IntegerAlleleSet(Set<Integer> set) {
045 super(set);
046 }
047
048 /**
049 * Builds an IntegerAlleleSet with random values within the range [lowerBound,upperBound]
050 * <p>
051 * @param size the allala set cardinality
052 * @param lowerBound the min value to choose
053 * @param upperBound the max value to choose
054 * @return a new IntegerAlleleSet
055 */
056 public static IntegerAlleleSet createRandom(int size, int lowerBound, int upperBound ) {
057 HashSet<Integer> values = new HashSet<Integer>();
058 int s0 = upperBound - lowerBound + 1;
059 if( size > s0 ) size = s0;
060
061 Random rand = Random.getInstance();
062
063 for( int i = 0; i < s0; ++i ) {
064
065 int chosen = values.size();
066 double coin = ((double)size-chosen)/(s0-i);
067 boolean justEnough = s0-i == size-chosen;
068
069 if( justEnough || rand.nextBoolean(coin) ) {
070 values.add(lowerBound + i);
071 }
072 }
073
074 return new IntegerAlleleSet(values);
075 }
076
077 /**
078 * Builds a new IntegerAlleleSet with uniformly distributed values within the range [lowerBound,upperBound]
079 * <p>
080 * @param size the allala set cardinality
081 * @param lowerBound the min value to choose
082 * @param upperBound the max value to choose
083 * @return a new IntegerAlleleSet
084 */
085 public static IntegerAlleleSet createUniform(int size, int lowerBound, int upperBound ) {
086 HashSet<Integer> values = new HashSet<Integer>();
087 int s0 = upperBound - lowerBound + 1;
088 if( size > s0 ) size = s0;
089
090 double step = 1.0/(upperBound - lowerBound);
091 for( double x = lowerBound; x <= upperBound; x += step ) {
092 int i = (int) Math.round(x);
093 values.add(i);
094 }
095
096 return new IntegerAlleleSet(values);
097 }
098
099 }
|
|
|