|
WebCab Optimization v2.6 (J2SE Edition) |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object | +--webcab.lib.math.optimization.EasySolver
This class offers a declarative means by which you can find the solution to
uni or multi dimensional (non-linear) optimization problems. In order to find the
solution of linear problems, also known as linear programming problems we refer the
reader to LinearProgramming.
To solve an optimization problem all you will need to do is define the optimization
problem you wish to solve (as described below) and then call the intelligent
solve. Our intelligent solver will inspect your given problem at
hand and then select the most appropriate algorithm from all the available business
classes, populate the algorithm with appropriate algorithm and problem specific
parameters and then evaluate the solution.
By following the below steps you will be able to quickly get your optimization problem solver up and running.
All you will to do is:
setFunction
setLocal(boolean) or setGlobal(boolean) respectively.
setExtremumType(webcab.lib.math.optimization.ExtremumTypes). The default
extremum sought is maximum.
setConstraints, and in the multi-dimensional case
by using: addGreaterThanInequality, addLessThanInequality,
addEqualityConstraint, addLowerBoundConstraint,
addUpperBoundConstraint, removeAllConstraints.
Once the Object function has been set you are able to read-off a number of its qualitative properties (and hence check that it has been set correctly) by using the following:
Though it is only strictly necessary to provide an initial point for constrained multidimensional optimization problems we advise the user to always set an initial point in the case of local unidimensional and multidimensional optimization. In the case of global (i.e. constrained) unidimensional optimization problems it is the number of subintervals which we advise the user to set.
Even if it is not clear which point and number of intervals to considered, through a process of try and error you should be able to quickly find suitable values. You are able to set the either the initial point in the uni and multi dimensional case or the number of subintervals using one of the following set methods:
setInitialPoint(double) - Provide the initial
point for unidimensional unconstrained optimization problems.
setNoSubIntervals - For the
constrained unidimensional case sets the number of intermediate points on the interval
considered and hence in doing so provides a set of initial points.
setInitialPoint(double[]) - Provide the initial
point for multidimensional optimization problems. For constrained optimization problems
you must provide an initial points since no default value in this case is provided.
Following these four steps you are able to define any unidimensional or multidimensional
optimization problem with or without linear constraints by first calling solve,
after which you will be able to read of the properties of the solution found using:
getUniDimensionalSolution - Returns the coordinate
value on the real line of the location of the extremum of a unidimensional optimization
problem considered.
getMultiDimensionalSolution - Returns the coordinate
point of the location of the extremum of a multidimensional optimization problem considered.
getValueAtExtremum - Returns the value of the object function of the
unidimensional or multidimensional optimization problem considered.
Moreover, once solve has been called you are able to ascertain which
particular algorithm has been applied by calling: getMethodUsed.
Remark: We provide a number of clients examples which illustrate exactly how in practice you are able to write a client using this class which finds the solution to a given optimization problem.
In the above section we provide the minimum steps required in order to solve
an optimization problem. In nearly all cases the solve method will
select the optimal algorithm to use, and set values for the tolerance and the maximum
number of iterations to there default values, namely 0.00001, and 300
respectively.
However, in order to offer the flexibility to fine tune your client applications we enable the tolerance and maximum number of iterations to be set which are the exiting conditions of the algorithms. That is, either the solution will be found to a given level of tolerance and the algorithm will exit, or the maximum number of iterations will be exceeded before the solution is found to the given level of tolerance which will also result in the algorithm exiting. Clearly, the desirable outcome is for the solution to be found and hence the number of iterations should be chosen generously.
The two model parameters (or exiting conditions) of the tolerance and maximum number of iterations can be set and finely tuned using:
setTolerance controls the precision with
which the solution you are looking for is evaluated.
setMaxIterations
maximum number of iterations with which can be used.
Remark: If you wish to override solve method selection or use
a particular algorithm for a given problem then we refer you to the MultiDimensionalSolver and UniDimensionalSolver classs.
Within this component we will categorize sufficiently optimization problems such that we are able to:
In particular, we will categorize Optimization problems in accordance with:
In addition to these problem specific parameters you also have the option if you wish to set the following algorithm parameters which are also exiting conditions:
Uni or Multi Dimensionality and Object Functions
Optimization problems can be unidimensional (that is, the object function has only one variable) or multidimensional (that is, the object function has more than one variable). We provide specialized algorithms which deal with the uni-dimensional and multidimensional cases. Optimization problems are also categorized by the qualitative properties of there object functions and for the following types of functions we provide specialized algorithms:
LinearProgramming for an extensive treatment of
linear programming problems including the Simplex algorithm and duality based techniques.
This includes Linear Programming where the linear object function is constrained by linear
constraints.
EasySolver sineOptimizationProblem = new EasySolver ();
sineOptimizationProblem.setFunction (new SineFunction ());
sineOptimizationProblem.solve ();
double localMaximum = sineOptimizationProblem.getUniDimensionalSolution ();
The initial point for local uni-dimensional extremum problems is
0. You can change it by calling the setInitialPoint method.
| Constructor Summary | |
EasySolver()
Creates an instance of this class. |
|
| Method Summary | |
void |
addEqualityConstraint(double[] coefficients,
double equalToValue)
Adds an equality constraint to the multidimensional optimization problem being considered. |
void |
addGreaterThanInequality(double[] coefficients,
double greaterThanValue)
Adds a linear constraint of `greater than type' to the multidimensional optimization problem being considered. |
void |
addLessThanInequality(double[] coefficients,
double lessThanValue)
Adds a linear constraint of `less than type' to the multidimensional optimization problem being considered. |
void |
addLowerBoundConstraint(int variableIndex,
double greaterThanValue)
Adds a lower bound constraint to the multidimensional optimization problem being considered. |
void |
addUpperBoundConstraint(int variableIndex,
double lessThanValue)
Adds an upper bound constraint to the multidimensional optimization problem being considered. |
ExtremumTypes |
getExtremumType()
Returns the type of extremum -- minimum or maximum -- the solver is looking for when calculating a solution to the given problem. |
Function |
getFunction()
Returns the instance of the function which was sent to the setFunction method. |
double |
getLowerBound()
Returns the value of the lower bound of the interval over which the solution of the constrained optimization problem is sought. |
int |
getMaxIterations()
Returns the maximum number of iterations which can be used by solve,
of the underlying algorithm in order to find the solution of the optimization problem
to the given level of accuracy specified by setTolerance. |
String |
getMethodUsed()
Returns a text description of the method used in calculating the solution to the given optimization problem. |
double[] |
getMultiDimensionalInitialPoint()
Returns the array representing the points in the multidimensional space where the search for the extremum of the multidimensional optimization problem is performed. |
double[] |
getMultiDimensionalSolution()
After the solution for a uni-dimensional function has been calculated (i.e. after you called the solve
method), this method will return the n-dimensional point at which
the solution was found. |
int |
getNoSubIntervals()
Returns the number of intermediate values used in the case of finding the solution of the unidimensional global optimization problem. |
double |
getTolerance()
Returns the precision with which solve, looks for solutions. |
double |
getUniDimensionalInitialPoint()
Returns the coordinate value of the point on the real line from which the solution of the unidimensional optimization problem is sought. |
double |
getUniDimensionalSolution()
After the solution for a uni-dimensional function has been calculated (i.e. after you called the solve
method), this method will return the point at which the solution
was found. |
double |
getUpperBound()
Returns the value of the upper bound of the interval over which the solution of the constrained optimization problem is sought. |
double |
getValueAtExtremum()
After the solution for your function has been calculated (i.e. |
boolean |
isDifferentiable()
Returns true if your the function to be optimized
is differentiable, or false if it is not
differentiable or you cannot provide its differential. |
boolean |
isFunctionSet()
Indicates whether a function has been set for optimization by the client. |
boolean |
isGlobal()
Indicates whether the optimization solver is currently set to look for global extrema. |
boolean |
isLocal()
Indicates whether the optimization solver is currently set to look for local extrema. |
boolean |
isMultiDimensional()
Returns true if the your optimization problem is
multi-dimensional, or false if it is
uni-dimensional. |
boolean |
isSolutionCalculated()
Indicates whether a solution to the given optimization problem has been found. |
boolean |
isUniDimensional()
Returns true if the your optimization problem is
uni-dimensional, or false if it is
multi-dimensional. |
void |
removeAllConstraints()
Removes all the multidimensional constraints which have been set using: (double[] coefficients, double greaterThanValue) addGreaterThanInequality,
(double[] coefficients, double lessThanValue) addLessThanInequality,
(double[] coefficients, double equalToValue) addEqualityConstraint,
(int variableIndex, double greaterThanValue) addLowerBoundConstraint,
(int variableIndex, double lessThanValue) addUpperBoundConstraint. |
void |
setConstraints(double lowerBound,
double upperBound)
Sets the upper and lower bounds of a Unidimensional Constrained optimization problem. |
void |
setExtremumType(ExtremumTypes extremumType)
Selects the type of extremum you are looking for, minimum or maximum. |
void |
setFunction(Function instanceOfFunction)
Allows you to set the function you wish to optimize. |
void |
setGlobal(boolean global)
Set whether the nature of the sought extremum is global. |
void |
setInitialPoint(double initialPoint)
Sets the initial point from which the solution of the (local) Unidimensional Optimization problem will be sought. |
void |
setInitialPoint(double[] initialPoint)
Sets the initial point from which the multidimensional optimization problem will be applied. |
void |
setLocal(boolean local)
Allows you to set whether the nature of the sought extremum is local. |
void |
setMaxIterations(int maxIterations)
The maximum number of iterations with which solve, can use of the
underlying algorithm in order to find the solution of the optimization problem to
the given level of tolerance specified by setTolerance.
|
void |
setNoSubIntervals(int noSubIntervals)
Sets the number of intermediate "safety" points to be used in the case of solving a unidimensional global optimization problem. |
void |
setTolerance(double tolerance)
Controls the precision with which the solution you are looking for is evaluated. |
void |
solve()
Call this method after setting the optimization problem which you are considering; this will involve setting the object function using setFunction and the other properties of this
class that are relevant to your problem.
|
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public EasySolver()
| Method Detail |
public boolean isMultiDimensional()
Returns true if the your optimization problem is
multi-dimensional, or false if it is
uni-dimensional.
The uni-dimensional or multi-dimensional nature of your
optimization problem is derived directly from the type of function
you send to the setFunction method. If your
function class implements the MultiDimensionalSolver interface,
your problem will be treated as multi-dimensional; if your class
implements the UniDimensionalSolver interface, your problem will
be treated as uni-dimensional.
public boolean isUniDimensional()
Returns true if the your optimization problem is
uni-dimensional, or false if it is
multi-dimensional.
The uni-dimensional or multi-dimensional nature of your
optimization problem is derived directly from the type of function
you send to the setFunction method. If your
class implements the UniDimensionalSolver interface, your problem
will be treated as uni-dimensional; if your function class
implements the MultiDimensionalSolver interface, your problem will
be treated as multi-dimensional.
public boolean isDifferentiable()
Returns true if your the function to be optimized
is differentiable, or false if it is not
differentiable or you cannot provide its differential.
The differentiability of the optimized function is determined
from the type of your function, as sent to the setFunction method before invoking this method.
If your function class implements the Derivative or
Gradient interface (according to whether your function is
uni or multi-dimensional), then your function is differentiable. If
it implements only the UniDimensionalSolver or MultiDimensionalSolver
interface, then it is not going to be treated as
differentiable.
public boolean isFunctionSet()
Indicates whether a function has been set for optimization by the client.
This method returns true if called after a
successful call to the setFunction method. If
the setFunction method was never called or the last
call was unsuccessful (an exception was thrown), this method will
return false, to indicate that the function has not
been set yet.
setFunctionpublic void setLocal(boolean local)
true value to this
method, this optimization solver will look for a local extremum,
next time it looks for a solution.
setGlobalpublic boolean isLocal()
setLocal
and setGlobal setGlobal methods.
The default value for this property is true.
isGlobalpublic void setGlobal(boolean global)
true value to this
method, this optimization solver will look for a global extremum,
next time it looks for a solution.
setLocalpublic boolean isGlobal()
setLocal
and setGlobal methods.
The default value for this property is false.
isLocal
public void setFunction(Function instanceOfFunction)
throws ReferencedServiceException
UniDimensionalFunction or MultiDimensionalFunction.
After having set the function, you can set other properties such
as the initial point and when you are done,
you can make a call to the solve method in
order to find a solution.
In order to provide a function which is to be used within the algorithms contained within this class you are required to implement one of the following interfaces:
UniDimensionalFunction - In order to provide
a uni-dimensional function for which the derivative is not known.
Derivative - In order to provide a uni-dimensional function
for which the derivative is known. Note that the Derivative interface
inherits the UniDimensionalFunction interface.
MultiDimensionalFunction - In order to provide
a multi-dimensional function for which the gradient is not known.
Gradient - In order to provide a multi-dimensional function
for which the gradient is known. Note that the Gradient interface
inherits from the MultiDimensionalFunction interface.
For further explicit details concerning how these interfaces are implemented (including source code) please see documentation accompanying the relevant interface.
ReferencedServiceException - Thrown by one of the optimization classes
while attempting to pass on the given function to it.solvepublic Function getFunction()
setFunction method.
public void setExtremumType(ExtremumTypes extremumType)
Selects the type of extremum you are looking
for, minimum or maximum.
The two values you can choose from are located in the ExtremumTypes enumeration class, which lists these values as
constants. By default, the type of extremum to optimize for is
set to maximum.
public ExtremumTypes getExtremumType()
setExtremumType method.
setExtremumType(webcab.lib.math.optimization.ExtremumTypes)
public void setConstraints(double lowerBound,
double upperBound)
Note: By default to solution of a unidimensional optimization problem is sought over the maximum possible range.
lowerBound - the coordinate value on the real line of the beginning of the interval over which the extremum is sought.upperBound - the coordinate value on the real line of the end of the interval over which the extremum is sought.public double getLowerBound()
setConstraints(double, double)public double getUpperBound()
setConstraints(double, double)
public void addGreaterThanInequality(double[] coefficients,
double greaterThanValue)
For an optimization problem in n-dimensional space, you are able
add the following linear constraint:
coeff0 * x0 + coeff1 * x1 + ..... + coeffn-1 * xn-1 >= greaterThanValue,
where coeffi, is the coefficient of the coordinate variable
xi and greaterThanValue is just a constant.
In order to pass this linear constraint as parameters to this method you need to set:
{ coeff0, coeff0, ..., coeffn-1}
greaterThanValue
Very similar method except allow the addition of
`less than' linear constraints to multidimensional optimization problems.,
Adds equality constraints to multidimensional optimization
problem being considered.,
Adds lower bound constraints to a multidimensional
optimization problem being considered.,
Adds lower bound constraints to a multidimensional
optimization problem being considered.,
Removes all the constraints which have been set for a
multidimensional optimization problem being considered.
public void addLessThanInequality(double[] coefficients,
double lessThanValue)
For an optimization problem in n-dimensional space, you are able
add the following linear constraint:
coeff0 * x0 + coeff1 * x1 + ..... + coeffn-1 * xn-1 <= greaterThanValue,
where coeffi, is the coefficient of the coordinate variable
xi and greaterThanValue is just a constant.
In order to pass this linear constraint as parameters to this method you need to set:
{ coeff0, coeff0, ..., coeffn-1}
greaterThanValue
Very similar method except allow the addition of
`greater than' linear constraints to multidimensional optimization problems.,
Adds equality constraints to multidimensional optimization
problem being considered.,
Adds lower bound constraints to a multidimensional
optimization problem being considered.,
Adds lower bound constraints to a multidimensional
optimization problem being considered.,
Removes all the constraints which have been set for a
multidimensional optimization problem being considered.
public void addEqualityConstraint(double[] coefficients,
double equalToValue)
For an optimization problem in n-dimensional space, you are able
add the following equality constraint:
coeff0 * x0 + coeff1 * x1 + ..... + coeffn-1 * xn-1 = equalToValue,
where coeffi, is the coefficient of the coordinate variable
xi and equalToValue is just a constant.
In order to pass this linear constraint as parameters to this method you need to set:
{ coeff0, coeff0, ..., coeffn-1}
equalToValue
Add a `greater than' linear constraints to multidimensional optimization problem being considered.,
Add a `less than' linear constraints to multidimensional optimization problem being considered.,
Adds a lower bound constraints to a multidimensional
optimization problem being considered.,
Adds an upper bound constraint to a multidimensional
optimization problem being considered.,
Removes all the constraints which have been set for a
multidimensional optimization problem being considered.
public void addLowerBoundConstraint(int variableIndex,
double greaterThanValue)
For an optimization problem in n-dimensional space, you are able
add the following lower bound inequality constraint:
xk >= greaterThanValue,
where xk, is the k+1-th coordinate variable where k
take one of the values {0, 1, ..., n-1}, and greaterThanValue is just a constant.
In order to pass this lower bound constraint as parameters to this method you need to set:
k
greaterThanValue
Add a `greater than' linear constraints to multidimensional optimization problem being considered.,
Add a `less than' linear constraints to multidimensional optimization problem being considered.,
Adds equality constraints to multidimensional optimization
problem being considered.,
Adds an upper bound constraint to a multidimensional
optimization problem being considered.,
Removes all the constraints which have been set for a
multidimensional optimization problem being considered.
public void addUpperBoundConstraint(int variableIndex,
double lessThanValue)
For an optimization problem in n-dimensional space, you are able
to add the following upper bound inequality constraint:
xk <= lessThanValue,
where xk, is the k+1-th coordinate variable where k
take one of the values {0, 1, ..., n-1}, and lessThanValue is just a constant.
In order to pass this upper bound constraint as parameters to this method you need to set:
k
lessThanValue
Add a `greater than' linear constraints to multidimensional optimization problem being considered.,
Add a `less than' linear constraints to multidimensional optimization problem being considered.,
Adds equality constraints to multidimensional optimization
problem being considered.,
Adds a lower bound constraints to a multidimensional
optimization problem being considered.,
Removes all the constraints which have been set for a
multidimensional optimization problem being considered.public void removeAllConstraints()
(double[] coefficients, double greaterThanValue) addGreaterThanInequality,
(double[] coefficients, double lessThanValue) addLessThanInequality,
(double[] coefficients, double equalToValue) addEqualityConstraint,
(int variableIndex, double greaterThanValue) addLowerBoundConstraint,
(int variableIndex, double lessThanValue) addUpperBoundConstraint.
Add a `greater than' linear constraints to multidimensional optimization problem being considered.,
Add a `less than' linear constraints to multidimensional optimization problem being considered.,
Adds equality constraints to multidimensional optimization
problem being considered.,
Adds a lower bound constraints to a multidimensional
optimization problem being considered.,
Adds an upper bound constraint to a multidimensional
optimization problem being considered.,
Removes all the constraints which have been set for a
multidimensional optimization problem being considered.public void setTolerance(double tolerance)
1e-5, or 0.00001).
You can set this to a smaller number for more accurate results, or use a larger number to increase the speed of the optimization.
public double getTolerance()
solve, looks for solutions. By
default, the precision is set to 5 decimal places, but you can
change it by calling the setTolerance method.
Note: The tolerance acts as an exiting condition and whenever
a solution is sought by solve, then either a solution to a
given level of tolerance will be found and the algorithm will exit or the
maximum number of iterations given by setMaxIterations,
will be reached.
setTolerancepublic void setMaxIterations(int maxIterations)
solve, can use of the
underlying algorithm in order to find the solution of the optimization problem to
the given level of tolerance specified by setTolerance.
Note: The tolerance acts as an exiting condition and whenever
a solution is sought by solve, then either a solution to a
given level of tolerance will be found and the algorithm will exit or the
maximum number of iterations given by setMaxIterations,
will be reached.
maxIterations - the maximum number of iterative steps taken before the algorithm exits and a TooManyMultiDimensionalIterationsException is thrown. A reasonable number to use for this parameter is 300.getMaxIterations()public int getMaxIterations()
solve,
of the underlying algorithm in order to find the solution of the optimization problem
to the given level of accuracy specified by setTolerance.
public void setNoSubIntervals(int noSubIntervals)
The purpose of the parameters is to ensure that the advance steps do not get to
large. The larger the parameter given the less likely we are to miss any local extremum
in the search for the global extremum. We provide this parameter with a default value of
100, which is a reasonable value in most instances. However, as is the
case with most model parameters this parameter is provided to allow you to fine tune
the algorithm for your particular problem at hand. If you decide to fine tune this
parameter then it a certain amount of trial and error may be necessary.
Remark: In this instance the number of subintervals play the role of the initial point within local optimizations problems.
noSubIntervals - the number of intermediate "safety" points to be used, in order to ensure that the advance steps do not get to large. Note that the larger the parameter the less likely we are to miss any local extremum in the search for the global extremum.public int getNoSubIntervals()
public void setInitialPoint(double initialPoint)
solve,
to be able to run since in such instances the default point of the origin will
be used.
Remark: The selection of the initial point ideally should be made with reference to the given problem at hand. In particular, by using insight into the particular problem at hand you may be able to select of points which is close to the extremum sought which will increase the speed with which the solution is found.
initialPoint - a double which corresponds to the coordinate value of a point on the real line from which the iterative search for the extremum will begin.public double getUniDimensionalInitialPoint()
public void setInitialPoint(double[] initialPoint)
solve.
Remark: The selection of the initial point ideally should be made with reference to the given problem at hand. In particular, by using insight into the particular problem at hand you may be able to select of points which is close to the extremum sought which will increase the speed with which the solution is found.
initialPoint - an array of doubles where the k-th term corresponds to the coordinate value in the k-th coordinate variable of the initial point from which the search for the extremum is performed.public double[] getMultiDimensionalInitialPoint()
public void solve()
throws ReferencedServiceException
setFunction and the other properties of this
class that are relevant to your problem.
This method will attempt to solve your optimization problem
according to the given information regarding your type of function, type of
extremum, and other specific parameters.
If successful, this method will return quietly and store internally the value of the solution and the value of the function at the solution point.
To retrieve the actual values of the point where the solution was
found call either getUniDimensionalSolution or getMultiDimensionalSolution depending on whether your function is
uni or multi-dimensional.
To retrieve the value of the extremum, call the getValueAtExtremum method.
ReferencedServiceException - Thrown when one of the
optimization algorithms in another class fails to find a
solution.Retrieving the uni-dimensional solution,
Retrieving the multi-dimensional solution,
Retrieving the value of the extremum,
Setting the functionpublic String getMethodUsed()
This method can be called after a call to the solve
method, regardless of whether a solution was found or not. If the function
is changed, subsequent calls to this method should be made only after
calling solve again.
public boolean isSolutionCalculated()
Indicates whether a solution to the given optimization problem has been found.
This method returns `true' if called after a
successful call to the solve
method. If the calculateSolution method was never
called since the last function set (see isFunctionSet), or the last call was unsuccessful, this method will
return `false', to indicate that no solution is
available yet.
public double getUniDimensionalSolution()
After the solution for a uni-dimensional function has been
calculated (i.e. after you called the solve
method), this method will return the point at which the solution
was found.
getValueAtExtremum()public double[] getMultiDimensionalSolution()
After the solution for a uni-dimensional function has been
calculated (i.e. after you called the solve
method), this method will return the n-dimensional point at which
the solution was found.
The n-dimensional solution point (x1,
x2, ..., xn) is described by a
one-dimensional array of numbers (double values), with
as many elements as number of dimensions (i.e. n). The
first element of the array corresponds to the coordinate on the
first dimension x1, the second element is the
position on the second dimension x2, and so
on.
Note: The indexing of the array starts from
0, so that in order to access the coordinate on the
first dimension, you will need to read off the element at the
zero-th index. This means that in order to access the
ith dimension, you would read off the
element whose index is i-1 in the returned array, where
i ranges from 1 to n.
getValueAtExtremum()public double getValueAtExtremum()
After the solution for your function has been calculated (i.e.
after you called the solve method), this method will
return the value of the function at the point of the solution.
|
WebCab Optimization v2.6 (J2SE Edition) |
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||