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 Equals 23 extends MathCommand { 24 25 private final static String CVS_REVISION = "$Revision: 1.6 $"; 26 27 private Class m_type; 28 29 public Equals(final GPConfiguration a_conf, Class a_type) 30 throws InvalidConfigurationException { 31 this(a_conf, a_type, 0, null); 32 } 33 34 public Equals(final GPConfiguration a_conf, Class a_type, int a_subReturnType, 35 int[] a_subChildTypes) 36 throws InvalidConfigurationException { 37 super(a_conf, 2, CommandGene.BooleanClass, a_subReturnType, a_subChildTypes); 38 m_type = a_type; 39 } 40 41 public String toString() { 42 return "Equals(&1, &2)"; 43 } 44 45 51 public String getName() { 52 return "Equals"; 53 } 54 55 public boolean execute_boolean(ProgramChromosome c, int n, Object [] args) { 56 if (m_type == CommandGene.BooleanClass) { 57 return c.execute_boolean(n, 0, args) == c.execute_boolean(n, 1, args); 58 } 59 else if (m_type == CommandGene.IntegerClass) { 60 return c.execute_int(n, 0, args) == c.execute_int(n, 1, args); 61 } 62 else if (m_type == CommandGene.LongClass) { 63 return c.execute_long(n, 0, args) == c.execute_long(n, 1, args); 64 } 65 else if (m_type == CommandGene.DoubleClass) { 66 return Math.abs(c.execute_double(n, 0, args) - 67 c.execute_double(n, 1, args)) < DELTA; 68 } 69 else if (m_type == CommandGene.FloatClass) { 70 return Math.abs(c.execute_float(n, 0, args) - 71 c.execute_float(n, 1, args)) < DELTA; 72 } 73 else if (m_type == CommandGene.VoidClass) { 74 return c.execute_object(n, 0, args).equals(c.execute_object(n, 1, args)); 75 } 76 throw new UnsupportedOperationException ("Unsupported type " + m_type + 77 " for Equals-command!"); 78 } 79 80 public Class getChildType(IGPProgram a_ind, int a_index) { 81 return m_type; 82 } 83 } 84 | Popular Tags |