MOEA Framework 2.12
API Specification

org.moeaframework.core
Class Solution

java.lang.Object
  extended by org.moeaframework.core.Solution
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
FutureSolution

public class Solution
extends Object
implements 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:
Serialized Form

Constructor Summary
  Solution(double[] objectives)
          Constructs a solution with no variables and the specified objectives.
  Solution(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 Solution(Solution solution)
          Copy constructor.
 
Method Summary
 void addAttributes(Map<String,Object> attributes)
          Adds all attributes to this solution in the specified Map.
 void clearAttributes()
          Removes all keys and values from this solution's attributes.
 Solution copy()
          Returns an independent copy of this solution.
 Solution deepCopy()
          Similar to copy() except all attributes are also copied.
 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.
 Map<String,Serializable> getAttributes()
          Returns the Map containing this solution's attributes.
 double getConstraint(int index)
          Returns the constraint at the specified index.
 double[] getConstraints()
          Returns an array containing the constraints of this solution.
 int getNumberOfConstraints()
          Returns the number of constraints defined by this solution.
 int getNumberOfObjectives()
          Returns the number of objectives defined by this solution.
 int getNumberOfVariables()
          Returns the number of variables defined by this solution.
 double getObjective(int index)
          Returns the objective at the specified index.
 double[] getObjectives()
          Returns an array containing the objectives of this solution.
 Variable getVariable(int index)
          Returns the variable at the specified index.
 boolean hasAttribute(String key)
          Returns true if the specified key exists in this solution's attributes; false otherwise.
 Object removeAttribute(String key)
          Removes the specified key and its associated value from this solution.
 Object 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 violatesConstraints()
          Returns true 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
 

Constructor Detail

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

public Solution(double[] objectives)
Constructs a solution with no variables and the specified objectives. This is intended for creating reference set solutions.

Parameters:
objectives - the objectives to be stored in this solution

Solution

protected Solution(Solution solution)
Copy constructor.

Parameters:
solution - the solution being copied
Method Detail

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())

violatesConstraints

public boolean violatesConstraints()
Returns true if any of the constraints are violated; false otherwise.

Returns:
true if any of the constraints are violated; false otherwise

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,Object> 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.


MOEA Framework 2.12
API Specification

Copyright 2009-2016 MOEA Framework. All rights reserved.
Licensed under the GNU Lesser General Public License.
Return to the MOEA Framework homepage. Visit us on Github!