Class Solution

java.lang.Object
org.moeaframework.core.Solution
All Implemented Interfaces:
Serializable, Displayable, Formattable<Solution>
Direct Known Subclasses:
FutureSolution

public class Solution extends Object implements Formattable<Solution>, Serializable
A solution to an optimization problem, storing the decision variables, objectives, constraints and attributes. Attributes are arbitrary (key, value) pairs; they are instance-specific and are not carried over in the copy constructor.

Solutions should only be constructed in Problem.newSolution() or cloned from an existing solution with copy(). This ensures the solutions and configured correctly for the given optimization problem.

See Also:
  • Constructor Details

    • Solution

      public Solution(int numberOfVariables, int numberOfObjectives)
      Constructs a solution with the specified number of variables and objectives with no constraints.
      Parameters:
      numberOfVariables - the number of variables defined by this solution
      numberOfObjectives - the number of objectives defined by this solution
    • Solution

      public Solution(int numberOfVariables, int numberOfObjectives, int numberOfConstraints)
      Constructs a solution with the specified number of variables, objectives and constraints.
      Parameters:
      numberOfVariables - the number of variables defined by this solution
      numberOfObjectives - the number of objectives defined by this solution
      numberOfConstraints - the number of constraints defined by this solution
    • Solution

      protected Solution(Solution solution)
      Copy constructor.
      Parameters:
      solution - the solution being copied
  • Method Details

    • copy

      public Solution copy()
      Returns an independent copy of this solution. 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)

      Note that a solution's attributes are not copied, as the attributes are generally specific to each instance.

      Returns:
      an independent copy of this solution
    • deepCopy

      public Solution deepCopy()
      Similar to copy() except all attributes are also copied. As a result, this method tends to be significantly slower than copy() if many large objects are stored as attributes.
      Returns:
      an independent copy of this solution
    • getNumberOfObjectives

      public int getNumberOfObjectives()
      Returns the number of objectives defined by this solution.
      Returns:
      the number of objectives defined by this solution
    • getNumberOfVariables

      public int getNumberOfVariables()
      Returns the number of variables defined by this solution.
      Returns:
      the number of variables defined by this solution
    • getNumberOfConstraints

      public int getNumberOfConstraints()
      Returns the number of constraints defined by this solution.
      Returns:
      the number of constraints defined by this solution
    • getObjective

      public double getObjective(int index)
      Returns the objective at the specified index.
      Parameters:
      index - index of the objective to return
      Returns:
      the objective at the specified index
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0) || (index >= getNumberOfObjectives())
    • getVariable

      public Variable getVariable(int index)
      Returns the variable at the specified index.
      Parameters:
      index - index of the variable to return
      Returns:
      the variable at the specified index
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0) || (index >= getNumberOfVariables())
    • setObjective

      public void setObjective(int index, double objective)
      Sets the objective at the specified index.
      Parameters:
      index - index of the objective to set
      objective - the new value of the objective being set
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0) || (index >= getNumberOfObjectives())
    • setObjectives

      public void setObjectives(double[] objectives)
      Sets all objectives of this solution.
      Parameters:
      objectives - the new objectives for this solution
      Throws:
      IllegalArgumentException - if objectives.length != getNumberOfObjectives()
    • getObjectives

      public double[] getObjectives()
      Returns an array containing the objectives of this solution. Modifying the returned array will not modify the internal state of this solution.
      Returns:
      an array containing the objectives of this solution
    • setVariable

      public void setVariable(int index, Variable variable)
      Sets the variable at the specified index.
      Parameters:
      index - index of the variable being set
      variable - the new value of the variable being set
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0) || (index >= getNumberOfVariables())
    • isFeasible

      public boolean isFeasible()
      Returns true if all constraints are satisfied; false otherwise. This is the opposite of violatesConstraints().
      Returns:
      true if all constraints are satisfied; false otherwise
    • violatesConstraints

      public boolean violatesConstraints()
      Returns true if any of the constraints are violated; false otherwise. This is the opposite of isFeasible().
      Returns:
      true if any of the constraints are violated; false otherwise
    • getSumOfConstraintViolations

      public double getSumOfConstraintViolations()
      Returns the sum of constraint violations, taking the absolute value of each constraint. Consequently, larger values reflect larger constraint violations.
      Returns:
      the sum of constraint violations
    • getConstraint

      public double getConstraint(int index)
      Returns the constraint at the specified index.
      Parameters:
      index - index of the variable to be returned
      Returns:
      the constraint at the specified index
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0) || (index >= getNumberOfConstraints())
    • setConstraints

      public void setConstraints(double[] constraints)
      Sets all constraints of this solution.
      Parameters:
      constraints - the new constraints for this solution
      Throws:
      IllegalArgumentException - if constraints.length != getNumberOfConstraints()
    • getConstraints

      public double[] getConstraints()
      Returns an array containing the constraints of this solution. Modifying the returned array will not modify the internal state of this solution.
      Returns:
      an array containing the constraints of this solution
    • setConstraint

      public void setConstraint(int index, double constraint)
      Sets the constraint at the specified index.
      Parameters:
      index - the index of the constraint being set
      constraint - the new value of the constraint being set
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0) || (index >= getNumberOfConstraints())
    • getAttribute

      public Object getAttribute(String key)
      Returns the value of the attribute that is associated with the specified key, or null if no value has been associated with the key.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      the value associated with the specified key, or null if no value has been associated with the key
    • setAttribute

      public Object setAttribute(String key, Serializable value)
      Associates the specified value with the specified key. Returns the old value associated with the key, or null if no prior value has been associated with the key.
      Parameters:
      key - the key with which the specified value is to be associated
      value - the value to be associated with the specified key
      Returns:
      the old value associated with the key, or null if no prior value has been associated with the key
    • removeAttribute

      public Object removeAttribute(String key)
      Removes the specified key and its associated value from this solution. Returns the old value associated with the key, or null if no prior value has been associated with the key.
      Parameters:
      key - the key to be removed
      Returns:
      the old value associated with the key, or null if no prior value has been associated with the key
    • hasAttribute

      public boolean hasAttribute(String key)
      Returns true if the specified key exists in this solution's attributes; false otherwise.
      Parameters:
      key - the key whose presence is being tested
      Returns:
      true if the specified key exists in this solution's attributes; false otherwise
    • getAttributes

      public Map<String,Serializable> getAttributes()
      Returns the Map containing this solution's attributes.
      Returns:
      the Map containing this solution's attributes
    • addAttributes

      public void addAttributes(Map<String,Serializable> attributes)
      Adds all attributes to this solution in the specified Map.
      Parameters:
      attributes - the Map containing the attributes to be added to this solution
    • clearAttributes

      public void clearAttributes()
      Removes all keys and values from this solution's attributes.
    • euclideanDistance

      public double euclideanDistance(Solution otherSolution)
      Computes the Euclidean distance (or L2)-norm) between two solutions in objective space.
      Parameters:
      otherSolution - the other solution
      Returns:
      the Euclidean distance in objective space
      Throws:
      IllegalArgumentException - if the solutions have differing numbers of objectives
    • manhattanDistance

      public double manhattanDistance(Solution otherSolution)
      Computes the Manhattan distance (or L1-norm) between two solutions in objective space.
      Parameters:
      otherSolution - the other solution
      Returns:
      the Manhattan distance in objective space
      Throws:
      IllegalArgumentException - if the solutions have differing numbers of objectives
    • chebyshevDistance

      public double chebyshevDistance(Solution otherSolution)
      Computes the Chebyshev distance (or Linf-norm) between two solutions in objective space.
      Parameters:
      otherSolution - the other solution
      Returns:
      the Chebyshev distance in objective space
      Throws:
      IllegalArgumentException - if the solutions have differing numbers of objectives
    • distanceToNearestSolution

      public double distanceToNearestSolution(Population population)
      Returns the Euclidean distance, in objective space, from this solution to the nearest solution in the population.
      Parameters:
      population - the population
      Returns:
      the Euclidean distance
    • distanceToNearestSolution

      public double distanceToNearestSolution(Population population, DistanceMeasure<Solution> distanceMeasure)
      Returns the distance, as calculated by the given distance measure, from this solution to the nearest solution in the population.
      Parameters:
      population - the population
      distanceMeasure - the measure of distance between two solutions
      Returns:
      the distance
    • asTabularData

      public TabularData<Solution> asTabularData()
      Description copied from interface: Formattable
      Returns the contents of this object as a TabularData instance, which can be used to save, print, or format the data in various ways.
      Specified by:
      asTabularData in interface Formattable<Solution>
      Returns:
      the TabularData instance