Class Grammar

All Implemented Interfaces:
Serializable, Copyable<Variable>, Defined, Named, Variable

public class Grammar extends AbstractVariable
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:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.moeaframework.core.Defined

    Defined.ConstructorComparator
  • Field Summary

    Fields inherited from class org.moeaframework.core.variable.AbstractVariable

    name
  • Constructor Summary

    Constructors
    Constructor
    Description
    Grammar(int size)
    Constructs a grammar variable with the specified initial size.
    Grammar(String name, int size)
    Constructs a grammar variable with the specified initial size and name.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the string representation of this grammar produced by evaluating the context-free grammar with the codon array.
    Creates and returns a copy of this object.
    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.
    void
    decode(String value)
    Parses and loads the value of this variable from a string.
    Encodes the value of this variable as a string.
    boolean
     
    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.
    Returns the string representation, or definition, of this object.
    int
    Returns the number of values that each codon can represent.
    int
     
    void
    insert(int insertIndex, int[] array)
    Inserts the specified array into this grammar's integer codon representation at the specified insert index.
    void
    Randomly assign the value of this variable.
    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
    Returns the length of the integer codon representation of this grammar.
    int[]
    Returns the integer codon representation of this grammar.
    Returns a human-readable representation of this value.

    Methods inherited from class org.moeaframework.core.variable.AbstractVariable

    getName

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • 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(String name, int size)
      Constructs a grammar variable with the specified initial size and name.
      Parameters:
      name - the name of this decision variable
      size - the initial size of this grammar
  • Method Details

    • 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()))
    • build

      public String build(ContextFreeGrammar cfg)
      Returns the string representation of this grammar produced by evaluating the context-free grammar with the codon array.
      Parameters:
      cfg - the context-free grammar
      Returns:
      the string representation of the grammar, or null if no valid grammar was produced
      See Also:
    • copy

      public Grammar copy()
      Description copied from interface: Copyable
      Creates and returns a copy of this object. 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).
      Returns:
      the copy
    • 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()
      Overrides:
      hashCode in class AbstractVariable
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class AbstractVariable
    • randomize

      public void randomize()
      Description copied from interface: Variable
      Randomly assign the value of this variable.
    • getDefinition

      public String getDefinition()
      Description copied from interface: Defined
      Returns the string representation, or definition, of this object.
      Returns:
      the definition
    • toString

      public String toString()
      Description copied from interface: Variable
      Returns a human-readable representation of this value.
      Specified by:
      toString in interface Variable
      Overrides:
      toString in class Object
      Returns:
      the value of this variable formatted as a string
    • encode

      public String encode()
      Description copied from interface: Variable
      Encodes the value of this variable as a string. This should reflect the internal representation of the value and not necessarily the actual value. For example, a binary-encoded integer should display the bits and not the integer value.

      Implementations should make an effort to display the value in a meaningful format, but that is not required. Instead, use Variable.toString() if a human-readable format is required.

      This method along with Variable.decode(String) are used primarily for storing values in text files. To make parsing easier, the resulting string must:

      1. Only contain ASCII characters
      2. Contain no whitespace (no spaces, tabs, newlines, etc.)
      Returns:
      the encoded value as a string
    • decode

      public void decode(String value)
      Description copied from interface: Variable
      Parses and loads the value of this variable from a string. This must be able to process any string produced by Variable.encode().
      Parameters:
      value - the value as a string