Package org.moeaframework.core
Class Solution
java.lang.Object
org.moeaframework.core.Solution
- All Implemented Interfaces:
Serializable,Copyable<Solution>,Displayable,Formattable<Solution>
- Direct Known Subclasses:
FutureSolution
public class Solution
extends Object
implements Copyable<Solution>, 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 Summary
ConstructorsModifierConstructorDescriptionSolution(int numberOfVariables, int numberOfObjectives) Constructs a solution with the specified number of variables and objectives with no constraints.Solution(int numberOfVariables, int numberOfObjectives, int numberOfConstraints) Constructs a solution with the specified number of variables, objectives and constraints.protectedCopy constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAttributes(Map<String, Serializable> attributes) Adds all attributes to this solution in the specifiedMap.Returns the contents of this object as aTabularDatainstance, which can be used to save, print, or format the data in various ways.doublechebyshevDistance(Solution otherSolution) Computes the Chebyshev distance (or Linf-norm) between two solutions in objective space.voidRemoves all keys and values from this solution's attributes.copy()Returns an independent copy of this solution.deepCopy()Similar tocopy()except all attributes are also copied.doubledistanceToNearestSolution(Population population) Returns the Euclidean distance, in objective space, from this solution to the nearest solution in the population.doubledistanceToNearestSolution(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.doubleeuclideanDistance(Solution otherSolution) Computes the Euclidean distance (or L2)-norm) between two solutions in objective space.getAttribute(String key) Returns the value of the attribute that is associated with the specified key, ornullif no value has been associated with the key.Returns theMapcontaining this solution's attributes.double[]Returns an array containing the canonical objective values of this solution.getConstraint(int index) Returns the constraint at the specified index.doublegetConstraintValue(int index) Returns the constraint value at the specified index.double[]Returns an array containing the constraint values of this solution.intReturns the number of constraints defined by this solution.intReturns the number of objectives defined by this solution.intReturns the number of variables defined by this solution.getObjective(int index) Returns the objective at the specified index.doublegetObjectiveValue(int index) Returns the objective value at the specified index.double[]Returns an array containing the objective values of this solution.doubleReturns the sum of constraint violations, taking the absolute value of each constraint.getVariable(int index) Returns the variable at the specified index.booleanhasAttribute(String key) Returnstrueif the specified key exists in this solution's attributes;falseotherwise.booleanReturnstrueif all constraints are satisfied;falseotherwise.doublemanhattanDistance(Solution otherSolution) Computes the Manhattan distance (or L1-norm) between two solutions in objective space.removeAttribute(String key) Removes the specified key and its associated value from this solution.setAttribute(String key, Serializable value) Associates the specified value with the specified key.voidsetConstraint(int index, Constraint constraint) Sets the constraint at the specified index.voidsetConstraints(double[] constraints) Deprecated.voidsetConstraintValue(int index, double constraint) Sets the constraint value at the specified index.voidsetConstraintValues(double[] constraints) Sets all constraint values of this solution.voidsetObjective(int index, Objective objective) Sets the objective at the specified index.voidsetObjectives(double[] objectives) Deprecated.usesetObjectiveValues(double[])insteadvoidsetObjectiveValue(int index, double objective) Sets the objective value at the specified index.voidsetObjectiveValues(double[] objectives) Sets all objective values of this solution.voidsetVariable(int index, Variable variable) Sets the variable at the specified index.booleanReturnstrueif any of the constraints are violated;falseotherwise.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.moeaframework.util.format.Displayable
display
-
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 solutionnumberOfObjectives- 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 solutionnumberOfObjectives- the number of objectives defined by this solutionnumberOfConstraints- the number of constraints defined by this solution
-
Solution
Copy constructor.- Parameters:
solution- the solution being copied
-
-
Method Details
-
copy
Returns an independent copy of this solution. It is required thatx.copy()is completely independent fromx. This means any method invoked onx.copy()in no way alters the state ofxand vice versa. It is typically the case thatx.copy().getClass() == x.getClass()andx.copy().equals(x)Note that a solution's attributes are not copied, as the attributes are generally specific to each instance.
-
deepCopy
Similar tocopy()except all attributes are also copied. As a result, this method tends to be significantly slower thancopy()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
-
getVariable
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())
-
setVariable
Sets the variable at the specified index.- Parameters:
index- index of the variable being setvariable- the new value of the variable being set- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0) || (index >= getNumberOfVariables())
-
getObjective
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())
-
setObjective
Sets the objective at the specified index.- Parameters:
index- index of the objective to setobjective- the new value of the objective being set- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0) || (index >= getNumberOfObjectives())
-
getObjectiveValue
public double getObjectiveValue(int index) Returns the objective value at the specified index.- Parameters:
index- index of the objective to return- Returns:
- the objective value at the specified index
- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0) || (index >= getNumberOfObjectives())
-
setObjectiveValue
public void setObjectiveValue(int index, double objective) Sets the objective value at the specified index.- Parameters:
index- index of the objective to setobjective- the new value of the objective being set- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0) || (index >= getNumberOfObjectives())
-
setObjectives
Deprecated.usesetObjectiveValues(double[])insteadSets all objective values of this solution.- Parameters:
objectives- the new objective values for this solution- Throws:
IllegalArgumentException- ifobjectives.length != getNumberOfObjectives()
-
setObjectiveValues
public void setObjectiveValues(double[] objectives) Sets all objective values of this solution.- Parameters:
objectives- the new objective values for this solution- Throws:
IllegalArgumentException- ifobjectives.length != getNumberOfObjectives()
-
getObjectiveValues
public double[] getObjectiveValues()Returns an array containing the objective values of this solution. Modifying the returned array will not modify the internal state of this solution.- Returns:
- an array containing the objective values of this solution
-
getCanonicalObjectiveValues
public double[] getCanonicalObjectiveValues()Returns an array containing the canonical objective values of this solution. SeeObjective.getCanonicalValue()for more details.- Returns:
- an array containing the canonical objective values of this solution
-
isFeasible
public boolean isFeasible()Returnstrueif all constraints are satisfied;falseotherwise. This is the opposite ofviolatesConstraints().- Returns:
trueif all constraints are satisfied;falseotherwise
-
violatesConstraints
public boolean violatesConstraints()Returnstrueif any of the constraints are violated;falseotherwise. This is the opposite ofisFeasible().- Returns:
trueif any of the constraints are violated;falseotherwise
-
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
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())
-
setConstraint
Sets the constraint at the specified index.- Parameters:
index- the index of the constraint being setconstraint- the new value of the constraint being set- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0) || (index >= getNumberOfConstraints())
-
getConstraintValue
public double getConstraintValue(int index) Returns the constraint value at the specified index.- Parameters:
index- index of the variable to be returned- Returns:
- the constraint value at the specified index
- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0) || (index >= getNumberOfConstraints())
-
setConstraintValue
public void setConstraintValue(int index, double constraint) Sets the constraint value at the specified index.- Parameters:
index- the index of the constraint being setconstraint- the new value of the constraint being set- Throws:
IndexOutOfBoundsException- if the index is out of range(index < 0) || (index >= getNumberOfConstraints())
-
setConstraints
Deprecated.usesetConstraintValues(double[])insteadSets all constraint values of this solution.- Parameters:
constraints- the new constraint values for this solution- Throws:
IllegalArgumentException- ifconstraints.length != getNumberOfConstraints()
-
setConstraintValues
public void setConstraintValues(double[] constraints) Sets all constraint values of this solution.- Parameters:
constraints- the new constraint values for this solution- Throws:
IllegalArgumentException- ifconstraints.length != getNumberOfConstraints()
-
getConstraintValues
public double[] getConstraintValues()Returns an array containing the constraint values of this solution. Modifying the returned array will not modify the internal state of this solution.- Returns:
- an array containing the constraint values of this solution
-
getAttribute
Returns the value of the attribute that is associated with the specified key, ornullif 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
nullif no value has been associated with the key
-
setAttribute
Associates the specified value with the specified key. Returns the old value associated with the key, ornullif no prior value has been associated with the key.- Parameters:
key- the key with which the specified value is to be associatedvalue- the value to be associated with the specified key- Returns:
- the old value associated with the key, or
nullif no prior value has been associated with the key
-
removeAttribute
Removes the specified key and its associated value from this solution. Returns the old value associated with the key, ornullif 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
nullif no prior value has been associated with the key
-
hasAttribute
Returnstrueif the specified key exists in this solution's attributes;falseotherwise.- Parameters:
key- the key whose presence is being tested- Returns:
trueif the specified key exists in this solution's attributes;falseotherwise
-
getAttributes
Returns theMapcontaining this solution's attributes.- Returns:
- the
Mapcontaining this solution's attributes
-
addAttributes
Adds all attributes to this solution in the specifiedMap.- Parameters:
attributes- theMapcontaining the attributes to be added to this solution
-
clearAttributes
public void clearAttributes()Removes all keys and values from this solution's attributes. -
euclideanDistance
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
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
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
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 populationdistanceMeasure- the measure of distance between two solutions- Returns:
- the distance
-
asTabularData
Description copied from interface:FormattableReturns the contents of this object as aTabularDatainstance, which can be used to save, print, or format the data in various ways.- Specified by:
asTabularDatain interfaceFormattable<Solution>- Returns:
- the
TabularDatainstance
-
setConstraintValues(double[])instead