001 package org.jaga.individualRepresentation.greycodedNumbers;
002
003 import org.jaga.util.*;
004
005 /**
006 * TODO: Complete these comments.
007 *
008 * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
009 *
010 * <p><u>Company:</u> University College London and JAGA.Org
011 * (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
012 * </p>
013 *
014 * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
015 * This program is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU General Public License as published by
017 * the Free Software Foundation, ONLY if you include a note of the original
018 * author(s) in any redistributed/modified copy.<br/>
019 * This program is distributed in the hope that it will be useful,
020 * but WITHOUT ANY WARRANTY; without even the implied warranty of
021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
022 * GNU General Public License for more details.<br/>
023 * You should have received a copy of the GNU General Public License
024 * along with this program; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
026 * or see http://www.gnu.org/licenses/gpl.html</p>
027 *
028 * @author Greg Paperin (greg@jaga.org)
029 *
030 * @version JAGA public release 1.0 beta
031 */
032
033 public class NDecimalsIndividual extends NNumbersGreycodedIndivudual {
034
035 private double decimalScale = 1000000.0; // 10^6 i.e., 6 digits
036
037 public NDecimalsIndividual(int size, double decimalScale, int precision) {
038 super(size, precision);
039 this.decimalScale = decimalScale;
040 }
041
042 public double getDecimalScale() {
043 return decimalScale;
044 }
045
046 public double getDoubleValue(int valueIndex) {
047 BitString clear = getClearBitCode(valueIndex);
048 double f = 1.0;
049 double val = 0.0;
050 for (int i = getPrecision() - 1; i >= 1; i--, f *= 2.0)
051 val += (clear.get(i) ? 1.0 : 0.0) * f;
052 if (clear.get(0))
053 val = -val;
054 val /= decimalScale;
055 return val;
056 }
057
058 public void setDoubleValue(int valueIndex, double value) {
059 double setVal = Math.rint(value * decimalScale);
060 if (setVal >= Long.MAX_VALUE || setVal <= Long.MIN_VALUE)
061 throw new IllegalArgumentException("Absolute decimal value too big");
062 BitString clear = new BitString(getPrecision());
063 clear.set(0, setVal < 0);
064 setVal = Math.abs(setVal);
065 double f = 1.0;
066 for (int i = getPrecision() - 1; i >= 1; i--, f *= 2.0)
067 clear.set(i, (setVal % (2.0 * f) >= f));
068 setClearBitCode(valueIndex, clear);
069 }
070
071 public String toString() {
072 final int size = getSize();
073 StringBuffer s = new StringBuffer("{size=");
074 s.append(size);
075 s.append("; repr=");
076 s.append(getBitStringRepresentation().toString());
077 s.append("; vals=(");
078 for (int i = 0; i < size; i++) {
079 s.append(getDoubleValue(i));
080 if (i < size - 1)
081 s.append(", ");
082 else
083 s.append(")");
084 }
085 if (null == getFitness()) {
086 s.append("; fitness-unknown}");
087 } else {
088 s.append("; fit=");
089 s.append(getFitness().toString());
090 s.append("}");
091 }
092 return s.toString();
093 }
094
095 }