MOEA Framework 2.5
API Specification

org.moeaframework.core.variable
Class Grammar

java.lang.Object
  extended by org.moeaframework.core.variable.Grammar
All Implemented Interfaces:
Serializable, Variable

public class Grammar
extends Object
implements Variable

Decision variable for grammars. This class represents the grammar as a variable-length integer codon which is subsequently converted into a grammar using ContextFreeGrammar.build(int[]).

See Also:
ContextFreeGrammar, Serialized Form

Constructor Summary
Grammar(int size)
          Constructs a grammar variable with the specified initial size.
Grammar(int[] codon)
          Constructs a grammar variable with the specified integer codon representation.
 
Method Summary
 Grammar copy()
          Returns an independent copy of this decision variable.
 int[] cut(int start, int end)
          Removes the indices in the range [start, end] from the integer codon representation, returning array of the values removed by this cut operation.
 boolean equals(Object obj)
          Indicates whether some other object is "equal to" this one.
 void fromArray(int[] codon)
          Sets the integer codon representation of this grammar.
 int get(int index)
          Returns the value at the specified index in the integer codon representation of this grammar.
 int getMaximumValue()
          Returns the number of values that each codon can represent.
 int hashCode()
          Returns a hash code value for the object.
 void insert(int insertIndex, int[] array)
          Inserts the specified array into this grammar's integer codon representation at the specified insert index.
 void set(int index, int value)
          Sets the specified index in the integer codon representation of this grammar to the specified value.
 void setMaximumValue(int maximumValue)
          Sets the number of values that each codon can represent.
 int size()
          Returns the length of the integer codon representation of this grammar.
 int[] toArray()
          Returns the integer codon representation of this grammar.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Grammar

public Grammar(int size)
Constructs a grammar variable with the specified initial size.

Parameters:
size - the initial size of this grammar

Grammar

public Grammar(int[] codon)
Constructs a grammar variable with the specified integer codon representation.

Parameters:
codon - the integer codon representation for this grammar
Method Detail

getMaximumValue

public int getMaximumValue()
Returns the number of values that each codon can represent. Each index in the codon array can be assigned a value in the range [0, maximumValue-1]

Returns:
the number of values that each codon can represent

setMaximumValue

public void setMaximumValue(int maximumValue)
Sets the number of values that each codon can represent. Each index in the codon array can be assigned a value in the range [0, maximumValue-1].

Parameters:
maximumValue - the number of values that each codon can represent

toArray

public int[] toArray()
Returns the integer codon representation of this grammar. The returned object is a clone of the internal storage, and thus can be modified independently of this instance.

Returns:
the integer codon representation of this grammar

fromArray

public void fromArray(int[] codon)
Sets the integer codon representation of this grammar. The stored object is a clone of the argument, and thus can be modified independently of this instance.

Parameters:
codon - the new integer codon representation for this grammar
Throws:
IllegalArgumentException - if any codon value is out of range ( (value < 0) || (value >= getMaximumValue()))

size

public int size()
Returns the length of the integer codon representation of this grammar.

Returns:
the length of the integer codon representation of this grammar

set

public void set(int index,
                int value)
Sets the specified index in the integer codon representation of this grammar to the specified value.

Parameters:
index - the index of the codon to be assigned
value - the new value for the specified index
Throws:
ArrayIndexOutOfBoundsException - if the index is out of range ( (index < 0) || (index >= size()))
IllegalArgumentException - if the value is out of range ( (value < 0) || (value >= getMaximumValue()))

get

public int get(int index)
Returns the value at the specified index in the integer codon representation of this grammar.

Parameters:
index - the index of the codon value to be returned
Returns:
the value at the specified index in the integer codon representation of this grammar
Throws:
ArrayIndexOutOfBoundsException - if the index is out of range ( (index < 0) || (index >= size()))

copy

public Grammar copy()
Description copied from interface: Variable
Returns an independent copy of this decision variable. It is required that x.copy() is completely independent from x. This means any method invoked on x.copy() in no way alters the state of x and vice versa. It is typically the case that x.copy().getClass() == x.getClass() and x.copy().equals(x).

Specified by:
copy in interface Variable
Returns:
an independent copy of this decision variable

cut

public int[] cut(int start,
                 int end)
Removes the indices in the range [start, end] from the integer codon representation, returning array of the values removed by this cut operation. For example,
 Grammar grammar = new Grammar(new int[] { 0, 1, 2, 3, 4, 5 });
 int[] removed = grammar.cut(2, 4);
 
results in grammar representing the array [2, 3, 4] and removed containing [0, 1, 5].

Parameters:
start - the start index of the cut operation
end - the end index of the cut operation
Returns:
the array of values removed by this cut operation

insert

public void insert(int insertIndex,
                   int[] array)
Inserts the specified array into this grammar's integer codon representation at the specified insert index. For example,
 Grammar grammar = new Grammar(new int[] { 0, 1, 2, 3, 4, 5 });
 grammar.insert(2, new int[] { 6, 7 });
 
results in grammar representing the array [0, 1, 6, 7, 2, 3, 4, 5].

Parameters:
insertIndex - the index where the specified array is to be inserted
array - the array of integer codons to be inserted

hashCode

public int hashCode()
Description copied from class: java.lang.Object
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

The general contract of hashCode is:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

Overrides:
hashCode in class Object
Returns:
a hash code value for this object.
See Also:
Object.equals(java.lang.Object), Hashtable

equals

public boolean equals(Object obj)
Description copied from class: java.lang.Object
Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Overrides:
equals in class Object
Parameters:
obj - the reference object with which to compare.
Returns:
true if this object is the same as the obj argument; false otherwise.
See Also:
Object.hashCode(), Hashtable

MOEA Framework 2.5
API Specification

Copyright 2009-2015 MOEA Framework. All rights reserved.
Licensed under the GNU Lesser General Public License.
Return to the MOEA Framework homepage. Hosted by Get MOEA Framework at SourceForge.net. Fast, secure and Free Open Source software downloads