001 package org.jaga.individualRepresentation.greycodedNumbers;
002
003 import org.jaga.definitions.*;
004 import org.jaga.util.*;
005
006
007 /**
008 * TODO: Complete these comments.
009 *
010 * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
011 *
012 * <p><u>Company:</u> University College London and JAGA.Org
013 * (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
014 * </p>
015 *
016 * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
017 * This program is free software; you can redistribute it and/or modify
018 * it under the terms of the GNU General Public License as published by
019 * the Free Software Foundation, ONLY if you include a note of the original
020 * author(s) in any redistributed/modified copy.<br/>
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
024 * GNU General Public License for more details.<br/>
025 * You should have received a copy of the GNU General Public License
026 * along with this program; if not, write to the Free Software
027 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
028 * or see http://www.gnu.org/licenses/gpl.html</p>
029 *
030 * @author Greg Paperin (greg@jaga.org)
031 *
032 * @version JAGA public release 1.0 beta
033 */
034
035 public class NNumbersGreycodedIndivudual implements BinaryEncodedIndividual {
036
037 private BitString representation = null;
038 private int size = -1;
039 private Fitness fitness = null;
040 private int precision = 64;
041
042 private NNumbersGreycodedIndivudual() {
043 throw new java.lang.UnsupportedOperationException("Dont use the default constructor!");
044 }
045
046 public NNumbersGreycodedIndivudual(int size, int precision) {
047 if (1 > size)
048 throw new IllegalArgumentException("Creating an empty individual");
049 if (1 > precision)
050 throw new IllegalArgumentException("Individual cannot us zero length representation");
051 this.size = size;
052 this.precision = precision;
053 this.representation = new BitString(size * precision);
054 }
055
056 public int getPrecision() {
057 return this.precision;
058 }
059
060 public int getSize() {
061 return this.size;
062 }
063
064 public BitString getBitStringRepresentation() {
065 return representation;
066 }
067
068 public void setBitStringRepresentation(BitString bits) {
069 if (bits.getLength() != this.representation.getLength())
070 throw new IllegalArgumentException("Length of the bit string is incorrect (is "
071 + bits.getLength() + " but expected "
072 + this.representation.getLength() + ")");
073 representation = bits;
074 }
075
076 public Fitness getFitness() {
077 return fitness;
078 }
079
080 public void setFitness(Fitness fitness) {
081 this.fitness = fitness;
082 }
083
084 protected BitString getClearBitCode(int valueIndex) {
085 if (valueIndex < 0 || valueIndex > getSize())
086 throw new IndexOutOfBoundsException("value index is " + valueIndex
087 + ", but must be in [0, "
088 + getSize() + "]");
089 BitString valRep = representation.substring(valueIndex * precision, (valueIndex+1) * precision);
090 BitString clear = grey2bin(valRep);
091 return clear;
092 }
093
094 protected void setClearBitCode(int valueIndex, BitString clear) {
095 if (valueIndex < 0 || valueIndex > getSize())
096 throw new IndexOutOfBoundsException("value index is " + valueIndex
097 + ", but must be in [0, "
098 + getSize() + "]");
099 BitString grey = bin2grey(clear);
100 representation.set(valueIndex * precision, (valueIndex+1) * precision, grey);
101 }
102
103 protected BitString grey2bin(BitString grey) {
104 int len = grey.getLength();
105 if (len < 2)
106 return grey;
107 BitString bin = new BitString(len);
108 bin.set(0, grey.get(0));
109 for (int i = 1; i < len; i++)
110 bin.set(i, bin.get(i-1) ^ grey.get(i));
111 return bin;
112 }
113
114 protected BitString bin2grey(BitString bin) {
115 int len = bin.getLength();
116 if (len < 2)
117 return bin;
118 BitString grey = new BitString(len);
119 grey.set(0, bin.get(0));
120 for (int i = 1; i < len; i++)
121 grey.set(i, bin.get(i-1) ^ bin.get(i));
122 return grey;
123 }
124
125 /*
126 // TEST:
127 public static void main(String[] args) {
128 BitString s1 = new BitString(4);
129 s1.set(0, false); s1.set(1, false); s1.set(2, false); s1.set(3, false);
130 System.out.println(s1);
131 System.out.println(bin2grey(s1));
132 System.out.println(grey2bin(bin2grey(s1)));
133 System.out.println();
134
135 s1.set(0, false); s1.set(1, false); s1.set(2, false); s1.set(3, true);
136 System.out.println(s1);
137 System.out.println(bin2grey(s1));
138 System.out.println(grey2bin(bin2grey(s1)));
139 System.out.println();
140
141 s1.set(0, false); s1.set(1, false); s1.set(2, true); s1.set(3, false);
142 System.out.println(s1);
143 System.out.println(bin2grey(s1));
144 System.out.println(grey2bin(bin2grey(s1)));
145 System.out.println();
146
147 s1.set(0, false); s1.set(1, false); s1.set(2, true); s1.set(3, true);
148 System.out.println(s1);
149 System.out.println(bin2grey(s1));
150 System.out.println(grey2bin(bin2grey(s1)));
151 System.out.println();
152
153 s1.set(0, false); s1.set(1, true); s1.set(2, false); s1.set(3, false);
154 System.out.println(s1);
155 System.out.println(bin2grey(s1));
156 System.out.println(grey2bin(bin2grey(s1)));
157 System.out.println();
158
159 s1.set(0, false); s1.set(1, true); s1.set(2, false); s1.set(3, true);
160 System.out.println(s1);
161 System.out.println(bin2grey(s1));
162 System.out.println(grey2bin(bin2grey(s1)));
163 System.out.println();
164
165 s1.set(0, false); s1.set(1, true); s1.set(2, true); s1.set(3, false);
166 System.out.println(s1);
167 System.out.println(bin2grey(s1));
168 System.out.println(grey2bin(bin2grey(s1)));
169 System.out.println();
170
171 s1.set(0, false); s1.set(1, true); s1.set(2, true); s1.set(3, true);
172 System.out.println(s1);
173 System.out.println(bin2grey(s1));
174 System.out.println(grey2bin(bin2grey(s1)));
175 System.out.println();
176
177 s1.set(0, true); s1.set(1, false); s1.set(2, false); s1.set(3, false);
178 System.out.println(s1);
179 System.out.println(bin2grey(s1));
180 System.out.println(grey2bin(bin2grey(s1)));
181 System.out.println();
182 }
183 */
184
185 }