1 10 package org.jgap.gp; 11 12 import org.jgap.gp.terminal.*; 13 import org.jgap.gp.impl.*; 14 import org.jgap.*; 15 16 22 public abstract class BaseGPChromosome 23 implements IGPChromosome { 24 25 private final static String CVS_REVISION = "$Revision: 1.3 $"; 26 27 30 private 31 GPConfiguration m_configuration; 32 33 36 private IGPProgram m_ind; 37 38 public BaseGPChromosome(GPConfiguration a_configuration) 39 throws InvalidConfigurationException { 40 if (a_configuration == null) { 41 throw new InvalidConfigurationException( 42 "Configuration to be set must not" 43 + " be null!"); 44 } 45 m_configuration = a_configuration; 46 } 47 48 public BaseGPChromosome(GPConfiguration a_configuration, IGPProgram a_ind) 49 throws InvalidConfigurationException { 50 this(a_configuration); 51 m_ind = a_ind; 52 } 53 54 60 public IGPProgram getIndividual() { 61 return m_ind; 62 } 63 64 72 public void setIndividual(IGPProgram a_ind) { 73 m_ind = a_ind; 74 } 75 76 87 public int getTerminal(int a_index) { 88 CommandGene[] functions = getFunctions(); 89 int len = functions.length; 90 for (int j = 0; j < len && functions[j] != null; j++) { 91 if (functions[j].getArity(m_ind) == 0) { 92 if (--a_index < 0) { 93 return j; 94 } 95 } 96 } 97 return -1; 98 } 99 100 111 public int getFunction(int a_index) { 112 CommandGene[] functions = getFunctions(); 113 int len = functions.length; 114 for (int j = 0; j < len && functions[j] != null; j++) { 115 if (functions[j].getArity(m_ind) != 0) { 116 if (--a_index < 0) { 117 return j; 118 } 119 } 120 } 121 return -1; 122 } 123 124 138 public int getTerminal(int a_index, Class a_type, int a_subType) { 139 CommandGene[] functions = getFunctions(); 140 int len = functions.length; 141 for (int j = 0; j < len && functions[j] != null; j++) { 142 if ( (a_subType == 0 || functions[j].getSubReturnType() == a_subType) 143 && functions[j].getReturnType() == a_type 144 && functions[j].getArity(m_ind) == 0) { 145 if (--a_index < 0) { 146 return j; 147 } 148 } 149 } 150 return -1; 151 } 152 153 167 public int getFunction(int a_index, Class a_type, int a_subType) { 168 CommandGene[] functions = getFunctions(); 169 int len = functions.length; 170 for (int j = 0; j < len && functions[j] != null; j++) { 171 if (functions[j].getReturnType() == a_type 172 && (a_subType == 0 || a_subType == functions[j].getSubReturnType()) 173 && functions[j].getArity(m_ind) != 0) { 174 if (--a_index < 0) { 175 return j; 176 } 177 } 178 } 179 return -1; 180 } 181 182 188 public int numTerminals() { 189 int count = 0; 190 CommandGene[] functions = getFunctions(); 191 int len = functions.length; 192 for (int i = 0; i < len && functions[i] != null; i++) { 193 if (functions[i].getArity(m_ind) == 0) { 194 count++; 195 } 196 } 197 return count; 198 } 199 200 206 public int numFunctions() { 207 int count = 0; 208 CommandGene[] functions = getFunctions(); 209 int len = functions.length; 210 for (int i = 0; i < len && functions[i] != null; i++) { 211 if (functions[i].getArity(m_ind) != 0) { 212 count++; 213 } 214 } 215 return count; 216 } 217 218 228 public int numTerminals(Class a_type, int a_subType) { 229 int count = 0; 230 CommandGene[] functions = getFunctions(); 231 int len = functions.length; 232 for (int i = 0; i < len && functions[i] != null; i++) { 233 if (functions[i].getArity(m_ind) == 0 234 && functions[i].getReturnType() == a_type 235 && (a_subType == 0 || functions[i].getSubReturnType() == a_subType)) { 236 count++; 237 } 238 } 239 return count; 240 } 241 242 252 public int numFunctions(Class a_type, int a_subType) { 253 int count = 0; 254 CommandGene[] functions = getFunctions(); 255 int len = functions.length; 256 for (int i = 0; i < len && functions[i] != null; i++) { 257 if (functions[i].getArity(m_ind) != 0 258 && functions[i].getReturnType() == a_type 259 && (a_subType == 0 || functions[i].getSubReturnType() == a_subType)) { 260 count++; 261 } 262 } 263 return count; 264 } 265 266 276 public CommandGene getNode(int a_index) { 277 if (a_index >= getFunctions().length || getFunctions()[a_index] == null) { 278 return null; 279 } 280 return getFunctions()[a_index]; 281 } 282 283 292 public int getCommandOfClass(int a_n, Class a_class) { 293 CommandGene[] functions = getFunctions(); 294 int len = functions.length; 295 for (int j = 0; j < len && functions[j] != null; j++) { 296 if (functions[j].getClass() == a_class) { 297 if (--a_n < 0) { 298 return j; 299 } 300 } 301 } 302 return -1; 303 } 304 305 314 public int getVariableWithReturnType(int a_n, Class a_returnType) { 315 CommandGene[] functions = getFunctions(); 316 int len = functions.length; 317 for (int j = 0; j < len && functions[j] != null; j++) { 318 if (functions[j].getClass() == Variable.class) { 319 Variable v = (Variable) functions[j]; 320 if (v.getReturnType() == a_returnType) { 321 if (--a_n < 0) { 322 return j; 323 } 324 } 325 } 326 } 327 return -1; 328 } 329 330 public GPConfiguration getGPConfiguration() { 331 return m_configuration; 332 } 333 334 } 335 | Popular Tags |