public class Grammar extends java.lang.Object implements Variable
ContextFreeGrammar.build(int[])
.ContextFreeGrammar
,
Serialized FormConstructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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. |
void |
decode(java.lang.String value)
Parses and loads the value of this variable from a string.
|
java.lang.String |
encode()
Encodes the value of this variable as a string.
|
boolean |
equals(java.lang.Object obj) |
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() |
void |
insert(int insertIndex,
int[] array)
Inserts the specified array into this grammar's integer codon
representation at the specified insert index.
|
void |
randomize()
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 |
size()
Returns the length of the integer codon representation of this grammar.
|
int[] |
toArray()
Returns the integer codon representation of this grammar.
|
public Grammar(int size)
size
- the initial size of this grammarpublic Grammar(int[] codon)
codon
- the integer codon representation for this grammarpublic int getMaximumValue()
[0,
maximumValue-1]
public void setMaximumValue(int maximumValue)
[0,
maximumValue-1]
.maximumValue
- the number of values that each codon can representpublic int[] toArray()
public void fromArray(int[] codon)
codon
- the new integer codon representation for this grammarjava.lang.IllegalArgumentException
- if any codon value is out of range (
(value < 0) || (value >= getMaximumValue())
)public int size()
public void set(int index, int value)
index
- the index of the codon to be assignedvalue
- the new value for the specified indexjava.lang.ArrayIndexOutOfBoundsException
- if the index is out of range (
(index < 0) || (index >= size())
)java.lang.IllegalArgumentException
- if the value is out of range (
(value < 0) || (value >= getMaximumValue())
)public int get(int index)
index
- the index of the codon value to be returnedjava.lang.ArrayIndexOutOfBoundsException
- if the index is out of range (
(index < 0) || (index >= size())
)public Grammar copy()
Variable
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)
.public int[] cut(int start, int end)
[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]
.start
- the start index of the cut operationend
- the end index of the cut operationpublic void insert(int insertIndex, int[] array)
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]
.insertIndex
- the index where the specified array is to be insertedarray
- the array of integer codons to be insertedpublic int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public void randomize()
Variable
public java.lang.String encode()
Variable
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.)public void decode(java.lang.String value)
Variable
Variable.encode()
.Copyright 2009-2024 David Hadka and other contributors. All rights reserved.
Licensed under the GNU Lesser General Public License.
Return to the MOEA Framework homepage.