1 10 package org.jgap.gp.function; 11 12 import org.jgap.*; 13 import org.jgap.gp.*; 14 import org.apache.commons.lang.builder.CompareToBuilder; 15 import org.apache.commons.lang.builder.EqualsBuilder; 16 import org.jgap.gp.impl.*; 17 18 25 public class IncrementMemory 26 extends MathCommand { 27 28 private static final String CVS_REVISION = "$Revision: 1.1 $"; 29 30 private int m_increment; 31 32 private String m_memoryName; 33 34 private int m_initialValue; 35 36 46 public IncrementMemory(final GPConfiguration a_conf, Class a_type, 47 String a_memoryName, int a_initialValue) 48 throws InvalidConfigurationException { 49 this(a_conf, a_type, a_memoryName, a_initialValue, 1); 50 } 51 52 63 public IncrementMemory(final GPConfiguration a_conf, Class a_type, 64 String a_memoryName, int a_initialValue, 65 int a_increment) 66 throws InvalidConfigurationException { 67 this(a_conf, a_type, a_memoryName, a_initialValue, a_increment, 0, 0); 68 } 69 70 public IncrementMemory(final GPConfiguration a_conf, Class a_type, 71 String a_memoryName, int a_initialValue,int a_increment, 72 int a_subReturnType, int a_subChildType) 73 throws InvalidConfigurationException { 74 super(a_conf, 0, a_type, a_subReturnType, a_subChildType); 75 m_increment = a_increment; 76 m_memoryName = a_memoryName; 77 m_initialValue = a_initialValue; 78 } 79 80 public String toString() { 81 if (m_increment == 1) { 82 return "INCMEM(" + m_memoryName + ")"; 83 } 84 else { 85 return "INCMEM(" + m_memoryName + ", " + m_increment + ")"; 86 } 87 } 88 89 95 public String getName() { 96 return "INCMEM(" + m_memoryName + ")"; 97 } 98 99 public int execute_int(ProgramChromosome c, int n, Object [] args) { 100 Integer value = (Integer ) getGPConfiguration().readFromMemoryIfExists( 101 m_memoryName); 102 int valueI; 103 if (value == null) { 104 valueI = m_initialValue; 105 } 106 else { 107 valueI = value.intValue() + 1; 108 } 109 getGPConfiguration().storeInMemory(m_memoryName, new Integer (valueI)); 110 return valueI; 111 } 112 113 122 public int compareTo(Object a_other) { 123 if (a_other == null) { 124 return 1; 125 } 126 else { 127 IncrementMemory other = (IncrementMemory) a_other; 128 return new CompareToBuilder() 129 .append(m_increment, other.m_increment) 130 .append(m_memoryName, other.m_memoryName) 131 .toComparison(); 132 } 133 } 134 135 144 public boolean equals(Object a_other) { 145 if (a_other == null) { 146 return false; 147 } 148 else { 149 try { 150 IncrementMemory other = (IncrementMemory) a_other; 151 return new EqualsBuilder() 152 .append(m_increment, other.m_increment) 153 .append(m_memoryName, other.m_memoryName) 154 .isEquals(); 155 } catch (ClassCastException cex) { 156 return false; 157 } 158 } 159 } 160 } 161 | Popular Tags |