Package org.moeaframework.core
Class Solution
java.lang.Object
org.moeaframework.core.Solution
- All Implemented Interfaces:
Serializable
,Displayable
,Formattable<Solution>
- Direct Known Subclasses:
FutureSolution
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
ModifierConstructorDescriptionSolution
(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.protected
Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAttributes
(Map<String, Serializable> attributes) Adds all attributes to this solution in the specifiedMap
.Returns the contents of this object as aTabularData
instance, which can be used to save, print, or format the data in various ways.double
chebyshevDistance
(Solution otherSolution) Computes the Chebyshev distance (or Linf-norm) between two solutions in objective space.void
Removes 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.double
distanceToNearestSolution
(Population population) Returns the Euclidean distance, in objective space, from this solution to the nearest solution in the population.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.double
euclideanDistance
(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, ornull
if no value has been associated with the key.Returns theMap
containing this solution's attributes.double
getConstraint
(int index) Returns the constraint at the specified index.double[]
Returns an array containing the constraints of this solution.int
Returns the number of constraints defined by this solution.int
Returns the number of objectives defined by this solution.int
Returns the number of variables defined by this solution.double
getObjective
(int index) Returns the objective at the specified index.double[]
Returns an array containing the objectives of this solution.double
Returns the sum of constraint violations, taking the absolute value of each constraint.getVariable
(int index) Returns the variable at the specified index.boolean
hasAttribute
(String key) Returnstrue
if the specified key exists in this solution's attributes;false
otherwise.boolean
Returnstrue
if all constraints are satisfied;false
otherwise.double
manhattanDistance
(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.void
setConstraint
(int index, double constraint) Sets the constraint at the specified index.void
setConstraints
(double[] constraints) Sets all constraints of this solution.void
setObjective
(int index, double objective) Sets the objective at the specified index.void
setObjectives
(double[] objectives) Sets all objectives of this solution.void
setVariable
(int index, Variable variable) Sets the variable at the specified index.boolean
Returnstrue
if any of the constraints are violated;false
otherwise.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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 ofx
and 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.
- Returns:
- an independent copy of this solution
-
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
-
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
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 setobjective
- 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
- ifobjectives.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
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())
-
isFeasible
public boolean isFeasible()Returnstrue
if all constraints are satisfied;false
otherwise. This is the opposite ofviolatesConstraints()
.- Returns:
true
if all constraints are satisfied;false
otherwise
-
violatesConstraints
public boolean violatesConstraints()Returnstrue
if any of the constraints are violated;false
otherwise. This is the opposite ofisFeasible()
.- 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
- ifconstraints.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 setconstraint
- the new value of the constraint being set- Throws:
IndexOutOfBoundsException
- if the index is out of range(index < 0) || (index >= getNumberOfConstraints())
-
getAttribute
Returns the value of the attribute that is associated with the specified key, ornull
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
Associates the specified value with the specified key. Returns the old value associated with the key, ornull
if 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
null
if 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, ornull
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
Returnstrue
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
Returns theMap
containing this solution's attributes.- Returns:
- the
Map
containing this solution's attributes
-
addAttributes
Adds all attributes to this solution in the specifiedMap
.- Parameters:
attributes
- theMap
containing 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:Formattable
Returns the contents of this object as aTabularData
instance, which can be used to save, print, or format the data in various ways.- Specified by:
asTabularData
in interfaceFormattable<Solution>
- Returns:
- the
TabularData
instance
-