1 10 package org.jgap.gp.function; 11 12 import org.jgap.*; 13 import org.jgap.gp.*; 14 import org.apache.commons.lang.builder.*; 15 import org.jgap.gp.impl.*; 16 import org.jgap.util.*; 17 18 27 public class SubProgram 28 extends CommandGene implements ICloneable { 29 30 private final static String CVS_REVISION = "$Revision: 1.12 $"; 31 32 35 private int m_subtrees; 36 37 40 private Class [] m_types; 41 42 public SubProgram(final GPConfiguration a_conf, Class [] a_types) 43 throws InvalidConfigurationException { 44 this(a_conf, a_types, 0, null); 45 } 46 47 public SubProgram(final GPConfiguration a_conf, Class [] a_types, 48 int a_subReturnType, int[] a_subChildTypes) 49 throws InvalidConfigurationException { 50 super(a_conf, a_types.length, a_types[a_types.length - 1], a_subReturnType, 51 a_subChildTypes); 52 if (a_types.length < 1) { 53 throw new IllegalArgumentException ("Number of subtrees must be >= 1"); 54 } 55 m_types = a_types; 56 m_subtrees = a_types.length; 57 } 58 59 public String toString() { 60 String ret = "sub["; 61 for (int i = 1; i < m_subtrees; i++) { 62 ret += "&" + i + " --> "; 63 } 64 ret += "&" + m_subtrees + "]"; 65 return ret; 66 } 67 68 74 public String getName() { 75 return "Sub program"; 76 } 77 78 public int execute_int(ProgramChromosome c, int n, Object [] args) { 79 check(c); 80 int value = -1; 81 for (int i = 0; i < m_subtrees; i++) { 82 if (i < m_subtrees - 1) { 83 c.execute_void(n, i, args); 84 } 85 else { 86 value = c.execute_int(n, i, args); 87 } 88 } 93 return value; 94 } 95 96 public void execute_void(ProgramChromosome c, int n, Object [] args) { 97 check(c); 98 for (int i = 0; i < m_subtrees; i++) { 99 c.execute_void(n, i, args); 100 } 101 } 102 103 public long execute_long(ProgramChromosome c, int n, Object [] args) { 104 check(c); 105 long value = -1; 106 for (int i = 0; i < m_subtrees; i++) { 107 value = c.execute_long(n, i, args); 108 } 109 return value; 110 } 111 112 public float execute_float(ProgramChromosome c, int n, Object [] args) { 113 check(c); 114 float value = -1; 115 for (int i = 0; i < m_subtrees; i++) { 116 value = c.execute_float(n, i, args); 117 } 118 return value; 119 } 120 121 public double execute_double(ProgramChromosome c, int n, Object [] args) { 122 check(c); 123 double value = -1; 124 for (int i = 0; i < m_subtrees; i++) { 125 value = c.execute_double(n, i, args); 126 } 127 return value; 128 } 129 130 public Object execute_object(ProgramChromosome c, int n, Object [] args) { 131 check(c); 132 Object value = null; 133 for (int i = 0; i < m_subtrees; i++) { 134 value = c.execute_object(n, i, args); 135 } 136 return value; 137 } 138 139 public boolean isValid(ProgramChromosome a_program) { 140 return true; 141 } 142 143 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 144 return m_types[a_chromNum]; 145 } 146 147 156 public int compareTo(Object a_other) { 157 if (a_other == null) { 158 return 1; 159 } 160 else { 161 SubProgram other = (SubProgram) a_other; 162 return new CompareToBuilder() 163 .append(m_types, other.m_types) 164 .toComparison(); 165 } 166 } 167 168 177 public boolean equals(Object a_other) { 178 if (a_other == null) { 179 return false; 180 } 181 else { 182 try { 183 SubProgram other = (SubProgram) a_other; 184 return new EqualsBuilder() 185 .append(m_types, other.m_types) 186 .isEquals(); 187 } catch (ClassCastException cex) { 188 return false; 189 } 190 } 191 } 192 193 199 public Object clone() { 200 try { 201 int[] subChildTypes = getSubChildTypes(); 202 if (subChildTypes != null) { 203 subChildTypes = (int[])subChildTypes.clone(); 204 } 205 SubProgram result = new SubProgram(getGPConfiguration(), m_types, 206 getSubReturnType(), subChildTypes); 207 result.m_subtrees = m_subtrees; 208 result.m_types = (Class [])m_types.clone(); 209 return result; 210 } catch (Throwable t) { 211 throw new CloneException(t); 212 } 213 } 214 } 215 | Popular Tags |