1 10 package org.jgap.gp.function; 11 12 import org.jgap.*; 13 import org.jgap.gp.*; 14 import org.jgap.gp.impl.*; 15 16 22 public class IfElse 23 extends CommandGene { 24 25 private final static String CVS_REVISION = "$Revision: 1.8 $"; 26 27 private Class m_type; 28 29 public IfElse(final GPConfiguration a_conf, Class a_type) 30 throws InvalidConfigurationException { 31 this(a_conf, a_type, 0, null); 32 } 33 34 public IfElse(final GPConfiguration a_conf, Class a_type, int a_subReturnType, 35 int[] a_subChildTypes) 36 throws InvalidConfigurationException { 37 super(a_conf, 3, CommandGene.VoidClass, a_subReturnType, a_subChildTypes); 38 m_type = a_type; 39 } 40 41 public String toString() { 42 return "if(&1) then (&2) else(&3)"; 43 } 44 45 51 public String getName() { 52 return "IfElse"; 53 } 54 55 public int execute_int(ProgramChromosome c, int n, Object [] args) { 56 int x = c.execute_int(n, 0, args); 57 int value = 0; 58 if (x >= 0) { 59 value = c.execute_int(n, 1, args); 60 } 61 else { 62 value = c.execute_int(n, 2, args); 63 } 64 return value; 65 } 66 67 public long execute_long(ProgramChromosome c, int n, Object [] args) { 68 long x = c.execute_long(n, 0, args); 69 long value = 0; 70 if (x >= 0) { 71 value = c.execute_long(n, 1, args); 72 } 73 else { 74 value = c.execute_long(n, 2, args); 75 } 76 return value; 77 } 78 79 public float execute_float(ProgramChromosome c, int n, Object [] args) { 80 float x = c.execute_float(n, 0, args); 81 float value = 0; 82 if (x >= 0) { 83 value = c.execute_float(n, 1, args); 84 } 85 else { 86 value = c.execute_float(n, 2, args); 87 } 88 return value; 89 } 90 91 public double execute_double(ProgramChromosome c, int n, Object [] args) { 92 double x = c.execute_double(n, 0, args); 93 double value = 0; 94 if (x >= 0) { 95 value = c.execute_double(n, 1, args); 96 } 97 else { 98 value = c.execute_double(n, 2, args); 99 } 100 return value; 101 } 102 103 public void execute_void(ProgramChromosome c, int n, Object [] args) { 104 check(c); 105 boolean condition; 106 if (m_type == CommandGene.IntegerClass) { 107 condition = c.execute_int(n, 0, args) > 0; 108 } 109 else if (m_type == CommandGene.BooleanClass) { 110 condition = c.execute_boolean(n, 0, args); 111 } 112 else if (m_type == CommandGene.LongClass) { 113 condition = c.execute_long(n, 0, args) > 0; 114 } 115 else if (m_type == CommandGene.DoubleClass) { 116 condition = c.execute_double(n, 0, args) > 0; 117 } 118 else if (m_type == CommandGene.FloatClass) { 119 condition = c.execute_float(n, 0, args) > 0; 120 } 121 else { 122 throw new IllegalStateException ("IfElse: cannot process type "+m_type); 123 } 124 if (condition) { 125 c.execute_void(n, 1, args); 126 } 127 else { 128 c.execute_void(n, 2, args); 129 } 130 } 131 132 142 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 143 if (a_chromNum == 0) { 144 return m_type; 145 } 146 return CommandGene.VoidClass; 147 } 148 } 149 | Popular Tags |