📄 strictpermutation.java
字号:
/*--- formatted by Jindent 2.1, (www.c-lab.de/~jindent) ---*//** * StrictPermutation * * location: net.openai.ai.ga.cell.encoding.StrictPermutation * */package net.openai.ai.ga.cell.encoding;/** * The <code>StrictPermutation</code> class stores a permutation of values from * 0 to (length-1). All values are represented, however it is their order * that is of interest. This class supports cross-over and mutation. * * the maximum for the list in * @author Jared Grubb * @version %I%, %G% * @since JDK1.3 */public class StrictPermutation { /** * The list of values */ private int[] valList; /** * The name of this permutation */ private String name; /** * Creates a new <code>StrictPermutation</code> with the specified length * and given name. * * @param name a <code>String</code> representing the string's name * @param length the number of elements in this list */ public StrictPermutation(String name, int length) { setLength(length); setListLoosely(null); } /** * Creates a new <code>StrictPermutation</code> initialized with the * specified capacity and starting with the values in the passed integer * array so far as is possible using <code>setListLoosely</code>. * * @param name a <code>String</code> representing the string's name * @param capacity the number of elements in this list * @param value the initial value for the binary string */ public StrictPermutation(String name, int length, int[] vals) { setLength(length); setListLoosely(vals); } /** * Creates and returns a clone of the given <code>StrictPermutation</code>. * Throws <code>NullPointerException</code> when passed <code>null</code>. * * @param toClone the binary string to clone */ public StrictPermutation(StrictPermutation toClone) { this(toClone.getName(), toClone.getLength(), toClone.getList()); } /** * Returns the length of this permutation list * * @returns the length of the permutation */ public int getLength() { return this.valList.length; } /** * Sets the length of this permutation. Destroys any previous list and * initializes a new one. * * @param length the length of the permutation */ public void setLength(int size) { if (size < 0) { // Throw exception } this.valList = new int[size]; for (int i = 0; i < size; i++) { this.valList[i] = i; } } /** * Returns the name of this permutation list * * @returns a <code>String</code> of the name of this permutation */ public String getName() { return this.name; } /** * Sets the name of this permutation * * @param name the <code>String</code> for the new name of this list */ public void setName(String name) { this.name = name; } /** * Returns this permutation as a <code>int[]</code>. * * @returns a <code>int[]</code> of this permutation */ public int[] getList() { return this.valList; } /** * Sets the permutation to the values in the given list. * * @param list an <code>int[]</code> value to duplicate */ public void setList(int[] list) { if (list.length != this.valList.length) { // Throw exception return; } boolean[] check = new boolean[this.valList.length]; for (int i = 0; i < this.valList.length; i++) { check[i] = false; } for (int i = 0; i < this.valList.length; i++) { if (check[list[i]] == true) { // Throw exception?? return; } this.valList[i] = list[i]; check[list[i]] = true; } } /** * Sets the permutation to the values in the given list when possible. * Calls setListLoosely(list, 0); * * @param list an <code>int[]</code> value to try to duplicate */ public void setListLoosely(int[] list) { setListLoosely(list, 0); } /** * Sets the permutation to the values in the given list when possible * starting from a given position. All elements before this position are * left alone. If a duplicate is found, it is skipped and not added a * second time. If not all values have been added into the list, then they * are added sequentially to fill the list. When (start<0) or (start>length) * an exception is thrown. * * @param list an <code>int[]</code> value to try to duplicate * @param start the position to start from */ public void setListLoosely(int[] list, int start) { if (start < 0 || start > this.valList.length) { // Throw exception return; } boolean[] check = new boolean[this.valList.length]; for (int i = 0; i < this.valList.length; i++) { check[i] = false; } for (int i = 0; i < start; i++) { check[this.valList[i]] = true; } // Try to copy this list int j = start; if (list != null) { for (int i = 0; i < list.length; i++) { // Make sure it's a good value first if (list[i] < 0 || list[i] >= this.valList.length) { continue; // Add it if not already done } if (check[list[i]] == false) { this.valList[j++] = list[i]; check[list[i]] = true; if (j > this.valList.length) { break; } } } } int i = 0; // Add all the rest of the numbers for (; j < this.valList.length; j++) { while (check[i] == true) { i++; } this.valList[j] = i; check[i] = true; } } /** * Returns the value of a certain element in the permutation * * @param element the element to return * @returns the value of the permutation at this point */ public int getElement(int element) { if (element < 0 || element > this.valList.length) { // Throw exception return 0; } return valList[element]; } /** * Returns a new <code>StrictPermutation</code> based on two other * permutations of the same length. A cross-over point is picked at * random and all elements before this point are the same as the first * parent's and all all other elements are added in the same order as they * are in the second permutation when possible. Neither parent is altered * during this call. * * @param first the first <code>StrictPermutation</code> * @param second the second <code>StrictPermutation</code> * @returns a new <code>StrictPermutation</code> */ public static StrictPermutation cross(StrictPermutation first, StrictPermutation second) { if (first.getLength() != second.getLength()) { // Throw exception return null; } StrictPermutation n = new StrictPermutation(first); int crossover_pt = ((int) (Math.random() * (double) (first.getLength()))); n.setListLoosely(second.getList(), crossover_pt); return n; } /** * Mutates a given number of elements in the permutation. The mutation is * the exchange of two elements in the list. A non-positive argument * results in no change in the string. * * @param amount the number of elements to exchange ("mutate") */ public void mutate(int amount) { int f, s, temp; for (int i = 0; i < amount; i++) { f = (int) ((Math.random() * (double) (this.getLength()))); s = (int) ((Math.random() * (double) (this.getLength()))); temp = this.valList[f]; this.valList[f] = this.valList[s]; this.valList[s] = temp; } } /** * Method declaration * * * @return * * @see */ public String toString() { StringBuffer sb = new StringBuffer(String.valueOf(this.valList[0])); for (int i = 1; i < this.valList.length; i++) { sb.append(" " + this.valList[i]); } return sb.toString(); } }/*--- formatting done in "Sun Java Convention" style on 12-28-2000 ---*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -