1 10 package org.jgap.impl; 11 12 import java.util.*; 13 import org.jgap.*; 14 15 27 public class SetGene 28 extends BaseGene implements IPersistentRepresentation { 29 30 private final static String CVS_REVISION = "$Revision: 1.15 $"; 31 32 private HashSet m_geneSet = new HashSet(); 33 34 private Object m_value; 35 36 42 public SetGene() 43 throws InvalidConfigurationException { 44 this(Genotype.getStaticConfiguration()); 45 } 46 47 54 public SetGene(final Configuration a_conf) 55 throws InvalidConfigurationException { 56 super(a_conf); 57 } 58 59 protected Gene newGeneInternal() { 60 try { 61 return new SetGene(getConfiguration()); 62 } 63 catch (InvalidConfigurationException iex) { 64 throw new IllegalStateException (iex.getMessage()); 65 } 66 } 67 68 73 public void addAllele(final Object a_value) { 74 m_geneSet.add(a_value); 75 } 76 77 82 public void addAlleles(final Collection a_alleles) { 83 m_geneSet.addAll(a_alleles); 84 } 85 86 91 public void removeAlleles(final Object a_key) { 92 m_geneSet.remove(a_key); 93 } 94 95 103 public void setToRandomValue(final RandomGenerator a_numberGenerator) { 104 105 m_value = m_geneSet.toArray()[a_numberGenerator.nextInt( 106 m_geneSet.size())]; 107 } 108 109 125 public void applyMutation(final int a_index, final double a_percentage) { 126 RandomGenerator rn; 127 if (getConfiguration() != null) { 128 rn = getConfiguration().getRandomGenerator(); 129 } 130 else { 131 rn = getConfiguration().getJGAPFactory().createRandomGenerator(); 132 } 133 setToRandomValue(rn); 134 } 135 136 152 public void setValueFromPersistentRepresentation(String a_representation) 153 throws UnsupportedRepresentationException { 154 if (a_representation != null) { 155 StringTokenizer tokenizer = 156 new StringTokenizer(a_representation, 157 PERSISTENT_FIELD_DELIMITER); 158 if (tokenizer.countTokens() < 3) { 162 throw new UnsupportedRepresentationException( 163 "The format of the given persistent representation " + 164 "is not recognized: it must contain at least three tokens."); 165 } 166 String valueRepresentation = tokenizer.nextToken(); 167 169 if (valueRepresentation.equals("null")) { 171 m_value = null; 172 } 173 else { 174 try { 175 m_value = 176 new Integer (Integer.parseInt(valueRepresentation)); 177 } 178 catch (NumberFormatException e) { 179 throw new UnsupportedRepresentationException( 180 "The format of the given persistent representation " + 181 "is not recognized: field 1 does not appear to be " + 182 "an integer value."); 183 } 184 } 185 Integer allele; 188 while (tokenizer.hasMoreTokens()) { 189 try { 190 allele = new Integer (Integer.parseInt(tokenizer.nextToken())); 191 m_geneSet.add(allele); 192 } 193 catch (NumberFormatException e) { 194 throw new UnsupportedRepresentationException( 195 "The format of the given persistent representation " 196 + "is not recognized: a member of the list of eligible values " 197 + "does not appear to be an integer value."); 198 } 199 } 200 } 201 } 202 203 219 public String getPersistentRepresentation() 220 throws 221 UnsupportedOperationException { 222 Iterator it = m_geneSet.iterator(); 226 StringBuffer strbf = new StringBuffer (); 227 while (it.hasNext()) { 228 strbf.append(PERSISTENT_FIELD_DELIMITER); 229 strbf.append(it.next().toString()); 230 } 231 return m_value.toString() + strbf.toString(); 232 } 233 234 242 public void setAllele(Object a_newValue) { 243 if (m_geneSet.contains(a_newValue)) { 244 m_value = a_newValue; 245 } 246 else { 247 throw new IllegalArgumentException ("Allele value being set is not an " 248 + "element of the set of permitted" 249 + " values."); 250 } 251 } 252 253 269 public int compareTo(Object other) { 270 SetGene otherGene = (SetGene) other; 271 if (otherGene == null) { 276 return 1; 277 } 278 else if (otherGene.m_value == null) { 279 return m_value == null ? 0 : 1; 283 } 284 else { 285 ICompareToHandler handler = getConfiguration().getJGAPFactory(). 286 getCompareToHandlerFor(m_value, m_value.getClass()); 287 if (handler != null) { 288 try { 289 return ( (Integer ) handler.perform(m_value, null, otherGene.m_value)). 290 intValue(); 291 } 292 catch (Exception ex) { 293 throw new Error (ex); 294 } 295 } 296 else { 297 return 0; 298 } 299 } 300 } 301 302 306 protected Object getInternalValue() { 307 return m_value; 308 } 309 310 318 public int hashCode() { 319 if (getInternalValue() == null) { 320 return -67; 321 } 322 else { 323 return super.hashCode(); 324 } 325 } 326 } 327 | Popular Tags |