KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > distr > Culture


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.distr;
11
12 import java.io.*;
13 import java.util.*;
14 import org.apache.commons.lang.builder.*;
15
16 /**
17  * Culture is a memory not being bound to a generation, but possibly persistent
18  * during the whole history of a genotype (over all generations).<p>
19  * Also see http://cs.gmu.edu/~sean/papers/culture-gp96.pdf
20  *
21  * @author Klaus Meffert
22  * @since 2.3
23  */

24 public class Culture
25     implements Serializable, Comparable JavaDoc {
26   /** String containing the CVS revision. Read out via reflection!*/
27   private final static String JavaDoc CVS_REVISION = "$Revision: 1.14 $";
28
29   /**
30    * The storage to use.
31    */

32   private CultureMemoryCell[] m_memory;
33
34   /**
35    * Storage for named indices.
36    */

37   private List m_memoryNames = new Vector();
38
39   /**
40    * Number of memory cells available.
41    */

42   private int m_size;
43
44   /**
45    * Width of a matrixed memory (optional, but necessary to set if you want
46    * to treat the memory as a matrix).
47    */

48   private int m_width;
49
50
51   /**
52    * Constructor.
53    *
54    * @param a_size the size of the memory in cells (CultureMemoryCell instances)
55    *
56    * @author Klaus Meffert
57    * @since 2.3
58    */

59   public Culture(final int a_size) {
60     if (a_size < 1) {
61       throw new IllegalArgumentException JavaDoc("Size must be greater than zero!");
62     }
63     m_size = a_size;
64     m_memory = new CultureMemoryCell[m_size];
65     m_width = 1;
66   }
67
68   /**
69    * Sets a memory cell with a given value. The memory cell will be newly
70    * created for that.
71    *
72    * @param a_index index of the memory cell
73    * @param a_value value to set in the memory
74    * @param a_historySize size of history to use, or less than 1 for turning
75    * history off
76    * @param a_name informative name of the memory cell
77    * @return newly created memory cell set with the given value
78    *
79    * @author Klaus Meffert
80    * @since 2.3
81    */

82   public CultureMemoryCell set(final int a_index, final double a_value,
83                                final int a_historySize, final String JavaDoc a_name) {
84     if (a_index < 0 || a_index >= size()) {
85       throw new IllegalArgumentException JavaDoc("Illegal memory index!");
86     }
87     CultureMemoryCell cell = new CultureMemoryCell(a_name, a_historySize);
88     cell.setDouble(a_value);
89     m_memory[a_index] = cell;
90     return cell;
91   }
92
93   /**
94    * Sets a memory cell with a given value. The memory cell will be newly
95    * created for that.
96    *
97    * @param a_index index of the memory cell
98    * @param a_value value to set in the memory
99    * @param a_historySize size of history to use, or less than 1 for turning
100    * history off
101    * @param a_infotext informative name of the memory cell
102    * @return newly created memory cell set with the given value
103    *
104    * @author Klaus Meffert
105    * @since 3.0
106    */

107   public CultureMemoryCell set(final int a_index, final Object JavaDoc a_value,
108                                final int a_historySize, final String JavaDoc a_infotext) {
109     if (a_index < 0 || a_index >= size()) {
110       throw new IllegalArgumentException JavaDoc("Illegal memory index!");
111     }
112     CultureMemoryCell cell = new CultureMemoryCell(a_infotext, a_historySize);
113     cell.setValue(a_value);
114     m_memory[a_index] = cell;
115     return cell;
116   }
117
118   /**
119    * Sets a memory cell with a given value. The memory cell will be newly
120    * created for that.
121    *
122    * @param a_name named index of the memory cell
123    * @param a_value value to set in the memory
124    * @param a_historySize size of history to use, or less than 1 for turning
125    * history off
126    * @return newly created memory cell set with the given value
127    *
128    * @author Klaus Meffert
129    * @since 3.0
130    */

131   public CultureMemoryCell set(final String JavaDoc a_name, final Object JavaDoc a_value,
132                                final int a_historySize) {
133     if (a_name == null || a_name.length() < 1) {
134       throw new IllegalArgumentException JavaDoc("Illegal memory name!");
135     }
136     int index = m_memoryNames.indexOf(a_name);
137     if (index < 0) {
138       // Create new named index.
139
// -----------------------
140
m_memoryNames.add(a_name);
141       index = m_memoryNames.size() - 1;
142     }
143     CultureMemoryCell cell = new CultureMemoryCell(a_name, a_historySize);
144     cell.setValue(a_value);
145     m_memory[index] = cell;
146     return cell;
147   }
148
149   /**
150    * Retrieves the memory cell at the given index.
151    *
152    * @param a_index index of the memory cell to read out
153    * @return stored memory cell at given index
154    *
155    * @author Klaus Meffert
156    * @since 2.3
157    */

158   public CultureMemoryCell get(final int a_index) {
159     if (a_index < 0 || a_index >= size()) {
160       throw new IllegalArgumentException JavaDoc("Illegal memory index!");
161     }
162     return m_memory[a_index];
163   }
164
165   /**
166    * Retrieves the memory cell at the given index.
167    *
168    * @param a_name name of the memory cell to read out
169    * @return stored memory cell for given name, or null if name unknown
170    *
171    * @author Klaus Meffert
172    * @since 3.0
173    */

174   public CultureMemoryCell get(final String JavaDoc a_name) {
175     if (a_name == null || a_name.length() < 1) {
176       throw new IllegalArgumentException JavaDoc("Illegal memory name!");
177     }
178     int index = m_memoryNames.indexOf(a_name);
179     if (index < 0) {
180       throw new IllegalArgumentException JavaDoc("Memory name unknown: " + a_name);
181     }
182     return m_memory[index];
183   }
184
185   /**
186    * Checks if a memory cell with the given name exists.
187    *
188    * @param a_name the name of the cell to check
189    * @return true: cell excists
190    *
191    * @author Klaus Meffert
192    * @since 3.2
193    */

194   public boolean contains(final String JavaDoc a_name) {
195     if (a_name == null || a_name.length() < 1) {
196       throw new IllegalArgumentException JavaDoc("Illegal memory name!");
197     }
198     int index = m_memoryNames.indexOf(a_name);
199     return index >=0;
200   }
201
202   /**
203    * @return size of the memory
204    *
205    * @author Klaus Meffert
206    * @since 2.3
207    */

208   public int size() {
209     return m_memory.length;
210   }
211
212   /**
213    * @return String representation of the cultural memory
214    *
215    * @author Klaus Meffert
216    * @since 2.3
217    */

218   public String JavaDoc toString() {
219     StringBuffer JavaDoc result = new StringBuffer JavaDoc("[");
220     int len = m_memory.length;
221     if (m_memory[0] == null) {
222       result.append(m_memory[0]);
223     }
224     else {
225       result.append(m_memory[0].toString());
226     }
227     for (int i = 1; i < len; i++) {
228       if (m_memory[i] == null) {
229         result.append(";" + m_memory[i]);
230       }
231       else {
232         result.append(";" + m_memory[i].toString());
233       }
234     }
235     result.append("]");
236     return result.toString();
237   }
238
239   /**
240    * Clears the memory.
241    *
242    * @author Klaus Meffert
243    * @since 3.0
244    */

245   public void clear() {
246     m_memory = new CultureMemoryCell[m_size];
247     m_memoryNames.clear();
248   }
249
250   /**
251    * @return cloned list of symbolic memory names
252    *
253    * @author Klaus Meffert
254    * @since 3.0
255    */

256   public List getMemoryNames() {
257     return new Vector(m_memoryNames);
258   }
259
260   /**
261    * The equals-method.
262    * @param a_other the other object to compare
263    * @return true if the objects are regarded as equal
264    *
265    * @author Klaus Meffert
266    * @since 3.0
267    */

268   public boolean equals(Object JavaDoc a_other) {
269     try {
270       return compareTo(a_other) == 0;
271     } catch (ClassCastException JavaDoc cex) {
272       cex.printStackTrace();
273       return false;
274     }
275   }
276
277   /**
278    * The compareTo-method.
279    *
280    * @param a_other the other object to compare
281    * @return -1, 0, 1
282    *
283    * @author Klaus Meffert
284    * @since 3.0
285    */

286   public int compareTo(Object JavaDoc a_other) {
287     Culture other = (Culture) a_other;
288     if (other == null) {
289       return 1;
290     }
291     // Problem: Vector does not implement Comparable. Thus we call toArray().
292
// ----------------------------------------------------------------------
293
return new CompareToBuilder()
294         .append(m_size, other.m_size)
295         .append(m_memory, other.m_memory)
296         .append(m_memoryNames.toArray(), other.m_memoryNames.toArray())
297         .toComparison();
298   }
299
300   /**
301    * Sets the width of the matrix memory. Important to call before using
302    * setMatrix/getMatrix!
303    *
304    * @param a_width the width the matrix should have
305    *
306    * @author Klaus Meffert
307    * @since 3.2
308    */

309   public void setMatrixWidth(int a_width) {
310     int size = size();
311     if (a_width > size) {
312       throw new IllegalArgumentException JavaDoc("Width must not be greater than the"
313           + " size of the memory ("
314           + size
315           + ") !");
316     }
317     m_width = a_width;
318   }
319
320   /**
321    * Stores a value in the matrix memory. Use setMatrixWidth(int) beforehand!
322    *
323    * @param a_x the first coordinate of the matrix (width)
324    * @param a_y the second coordinate of the matrix (height)
325    * @param a_value the value to store
326    * @return created or used memory cell
327    *
328    * @author Klaus Meffert
329    * @since 3.2
330    */

331   public CultureMemoryCell setMatrix(int a_x, int a_y, Object JavaDoc a_value) {
332     int index = a_x * m_width + a_y;
333     CultureMemoryCell cell = m_memory[index];
334     if (cell == null) {
335       cell = new CultureMemoryCell(a_x+"_"+a_y, -1);
336     }
337     cell.setValue(a_value);
338     m_memory[index] = cell;
339     return cell;
340   }
341
342   /**
343    * Reads a value from the matrix memory that was previously stored with
344    * setMatrix(...).
345    *
346    * @param a_x the first coordinate of the matrix (width)
347    * @param a_y the second coordinate of the matrix (height)
348    * @return read value
349    *
350    * @author Klaus Meffert
351    * @since 3.2
352    */

353   public CultureMemoryCell getMatrix(int a_x, int a_y) {
354     int index = a_x * m_width + a_y;
355     return get(index);
356   }
357 }
358
Popular Tags