Class RealVariable

java.lang.Object
org.moeaframework.core.variable.AbstractVariable
org.moeaframework.core.variable.RealVariable
All Implemented Interfaces:
Serializable, Copyable<Variable>, Defined, Named, Variable

public class RealVariable extends AbstractVariable
Decision variable for real values.
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
    RealVariable(double lowerBound, double upperBound)
    Constructs a real variable in the range lowerBound <= x <= upperBound.
    RealVariable(String name, double lowerBound, double upperBound)
    Constructs a real variable in the range lowerBound <= x <= upperBound with the given name.
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates and returns a copy of this object.
    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
     
    Returns the string representation, or definition, of this object.
    double
    Returns the lower bound of this decision variable.
    static double[]
    getReal(Solution solution)
    Returns the array of floating-point decision variables stored in a solution.
    static double[]
    getReal(Solution solution, int startIndex, int endIndex)
    Returns the array of floating-point decision variables stored in a solution between the specified indices.
    static double
    getReal(Variable variable)
    Returns the value stored in a floating-point decision variable.
    double
    Returns the upper bound of this decision variable.
    double
    Returns the current value of this decision variable.
    int
     
    void
    Randomly assign the value of this variable.
    static void
    setReal(Solution solution, double[] values)
    Sets the values of all floating-point decision variables stored in the solution.
    static void
    setReal(Solution solution, int startIndex, int endIndex, double[] values)
    Sets the values of the floating-point decision variables stored in a solution between the specified indices.
    static void
    setReal(Variable variable, double value)
    Sets the value of a floating-point decision variable.
    void
    setValue(double value)
    Sets the value of this decision variable.
    Returns a human-readable representation of this value.
    withValue(double value)
    Calls setValue(double) and returns this instance, mainly allowing one to create and set the value of a decision variable on one line.

    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

    • RealVariable

      public RealVariable(double lowerBound, double upperBound)
      Constructs a real variable in the range lowerBound <= x <= upperBound.
      Parameters:
      lowerBound - the lower bound of this decision variable, inclusive
      upperBound - the upper bound of this decision variable, inclusive
    • RealVariable

      public RealVariable(String name, double lowerBound, double upperBound)
      Constructs a real variable in the range lowerBound <= x <= upperBound with the given name.
      Parameters:
      name - the name of this decision variable
      lowerBound - the lower bound of this decision variable, inclusive
      upperBound - the upper bound of this decision variable, inclusive
  • Method Details

    • getValue

      public double getValue()
      Returns the current value of this decision variable.
      Returns:
      the current value of this decision variable
    • setValue

      public void setValue(double value)
      Sets the value of this decision variable. The value can be set to 0d/0d to indicate no value is assigned.
      Parameters:
      value - the new value for this decision variable
      Throws:
      IllegalArgumentException - if the value is out of bounds (value < getLowerBound()) || (value > getUpperBound())
    • withValue

      public RealVariable withValue(double value)
      Calls setValue(double) and returns this instance, mainly allowing one to create and set the value of a decision variable on one line.
      Parameters:
      value - the new value for this decision variable
      Returns:
      this decision variable
      Throws:
      IllegalArgumentException - if the value is out of bounds (value < getLowerBound()) || (value > getUpperBound())
    • getLowerBound

      public double getLowerBound()
      Returns the lower bound of this decision variable.
      Returns:
      the lower bound of this decision variable, inclusive
    • getUpperBound

      public double getUpperBound()
      Returns the upper bound of this decision variable.
      Returns:
      the upper bound of this decision variable, inclusive
    • copy

      public RealVariable 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
    • 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
    • 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.
    • 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
    • getReal

      public static double getReal(Variable variable)
      Returns the value stored in a floating-point decision variable.
      Parameters:
      variable - the decision variable
      Returns:
      the value stored in a floating-point decision variable
      Throws:
      IllegalArgumentException - if the decision variable is not of type RealVariable
    • getReal

      public static double[] getReal(Solution solution)
      Returns the array of floating-point decision variables stored in a solution. The solution must contain only floating-point decision variables.
      Parameters:
      solution - the solution
      Returns:
      the array of floating-point decision variables stored in a solution
      Throws:
      IllegalArgumentException - if any decision variable contained in the solution is not of type RealVariable
    • getReal

      public static double[] getReal(Solution solution, int startIndex, int endIndex)
      Returns the array of floating-point decision variables stored in a solution between the specified indices. The decision variables located between the start and end index must all be floating-point decision variables.
      Parameters:
      solution - the solution
      startIndex - the start index (inclusive)
      endIndex - the end index (exclusive)
      Returns:
      the array of floating-point decision variables stored in a solution between the specified indices
      Throws:
      IllegalArgumentException - if any decision variable contained in the solution between the start and end index is not of type RealVariable
    • setReal

      public static void setReal(Variable variable, double value)
      Sets the value of a floating-point decision variable.
      Parameters:
      variable - the decision variable
      value - the value to assign the floating-point decision variable
      Throws:
      IllegalArgumentException - if the decision variable is not of type RealVariable
      IllegalArgumentException - if the value is out of bounds (value < getLowerBound()) || (value > getUpperBound())
    • setReal

      public static void setReal(Solution solution, double[] values)
      Sets the values of all floating-point decision variables stored in the solution. The solution must contain only floating-point decision variables.
      Parameters:
      solution - the solution
      values - the array of floating-point values to assign the solution
      Throws:
      IllegalArgumentException - if any decision variable contained in the solution is not of type RealVariable
      IllegalArgumentException - if any of the values are out of bounds (value < getLowerBound()) || (value > getUpperBound())
    • setReal

      public static void setReal(Solution solution, int startIndex, int endIndex, double[] values)
      Sets the values of the floating-point decision variables stored in a solution between the specified indices. The decision variables located between the start and end index must all be floating-point decision variables.
      Parameters:
      solution - the solution
      startIndex - the start index (inclusive)
      endIndex - the end index (exclusive)
      values - the array of floating-point values to assign the decision variables
      Throws:
      IllegalArgumentException - if any decision variable contained in the solution between the start and end index is not of type RealVariable
      IllegalArgumentException - if an invalid number of values are provided
      IllegalArgumentException - if any of the values are out of bounds (value < getLowerBound()) || (value > getUpperBound())