The algorithms in this section minimize or maximize function value within specified constraints or without any constraints.
Solve given (noninteger) linear programming problem using the Simplex Algorithm (Simplex Method). What we mean here by “linear programming problem” (or LP problem, for short) can be formulated as:
Where is fixed 1byn rowvector, is fixed mbyn matrix, is fixed mby1 column vector and is an arbitrary nby1 column vector, which satisfies the constraints.
Simplex algorithm is one of many algorithms that are designed to handle this sort of problems efficiently. Although it is not optimal in theoretical sense (there exist algorithms that can solve any problem written as above in polynomial type, while simplex method degenerates to exponential time for some special cases), it is wellstudied, easy to implement and is shown to work well for reallife purposes.
The particular implementation is taken almost verbatim from Introduction to Algorithms, third edition by T. H. Cormen, C. E. Leiserson, R. L. Rivest and Clifford Stein. In particular, the Bland’s rule (http://en.wikipedia.org/wiki/Bland%27s_rule) is used to prevent cycling.
Parameters: 


Returns:  One of the return codes: 
//!the return codes for solveLP() function
enum
{
SOLVELP_UNBOUNDED = 2, //problem is unbounded (target function can achieve arbitrary high values)
SOLVELP_UNFEASIBLE = 1, //problem is unfeasible (there are no points that satisfy all the constraints imposed)
SOLVELP_SINGLE = 0, //there is only one maximum for target function
SOLVELP_MULTI = 1 //there are multiple maxima for target function  the arbitrary one is returned
};
This class is used to perform the nonlinear nonconstrained minimization of a function, defined on an ndimensional Euclidean space, using the NelderMead method, also known as downhill simplex method. The basic idea about the method can be obtained from (http://en.wikipedia.org/wiki/NelderMead_method). It should be noted, that this method, although deterministic, is rather a heuristic and therefore may converge to a local minima, not necessary a global one. It is iterative optimization technique, which at each step uses an information about the values of a function evaluated only at n+1 points, arranged as a simplex in ndimensional space (hence the second name of the method). At each step new point is chosen to evaluate function at, obtained value is compared with previous ones and based on this information simplex changes it’s shape , slowly moving to the local minimum. Thus this method is using only function values to make decision, on contrary to, say, Nonlinear Conjugate Gradient method (which is also implemented in optim).
Algorithm stops when the number of function evaluations done exceeds termcrit.maxCount, when the function values at the vertices of simplex are within termcrit.epsilon range or simplex becomes so small that it can enclosed in a box with termcrit.epsilon sides, whatever comes first, for some defined by user positive integer termcrit.maxCount and positive noninteger termcrit.epsilon.
class CV_EXPORTS Solver : public Algorithm
{
public:
class CV_EXPORTS Function
{
public:
virtual ~Function() {}
virtual double calc(const double* x) const = 0;
virtual void getGradient(const double* /*x*/,double* /*grad*/) {}
};
virtual Ptr<Function> getFunction() const = 0;
virtual void setFunction(const Ptr<Function>& f) = 0;
virtual TermCriteria getTermCriteria() const = 0;
virtual void setTermCriteria(const TermCriteria& termcrit) = 0;
// x contain the initial point before the call and the minima position (if algorithm converged) after. x is assumed to be (something that
// after getMat() will return) rowvector or columnvector. *It's size and should
// be consisted with previous dimensionality data given, if any (otherwise, it determines dimensionality)*
virtual double minimize(InputOutputArray x) = 0;
};
class CV_EXPORTS DownhillSolver : public Solver
{
public:
//! returns rowvector, even if the columnvector was given
virtual void getInitStep(OutputArray step) const=0;
//!This should be called at least once before the first call to minimize() and step is assumed to be (something that
//! after getMat() will return) rowvector or columnvector. *It's dimensionality determines the dimensionality of a problem.*
virtual void setInitStep(InputArray step)=0;
};
It should be noted, that DownhillSolver is a derivative of the abstract interface Solver, which in turn is derived from the Algorithm interface and is used to encapsulate the functionality, common to all nonlinear optimization algorithms in the optim module.
Getter for the optimized function. The optimized function is represented by Solver::Function interface, which requires derivatives to implement the sole method calc(double*) to evaluate the function.
Returns:  Smartpointer to an object that implements Solver::Function interface  it represents the function that is being optimized. It can be empty, if no function was given so far. 

Setter for the optimized function. It should be called at least once before the call to DownhillSolver::minimize(), as default value is not usable.
Parameters: 


Getter for the previously set terminal criteria for this algorithm.
Returns:  Deep copy of the terminal criteria used at the moment. 

Set terminal criteria for downhill simplex method. Two things should be noted. First, this method is not necessary to be called before the first call to DownhillSolver::minimize(), as the default value is sensible. Second, the method will raise an error if termcrit.type!=(TermCriteria::MAX_ITER+TermCriteria::EPS), termcrit.epsilon<=0 or termcrit.maxCount<=0. That is, both epsilon and maxCount should be set to positive values (noninteger and integer respectively) and they represent tolerance and maximal number of function evaluations that is allowed.
Algorithm stops when the number of function evaluations done exceeds termcrit.maxCount, when the function values at the vertices of simplex are within termcrit.epsilon range or simplex becomes so small that it can enclosed in a box with termcrit.epsilon sides, whatever comes first.
Parameters: 


Returns the initial step that will be used in downhill simplex algorithm. See the description of corresponding setter (follows next) for the meaning of this parameter.
Parameters: 


Sets the initial step that will be used in downhill simplex algorithm. Step, together with initial point (givin in DownhillSolver::minimize) are two ndimensional vectors that are used to determine the shape of initial simplex. Roughly said, initial point determines the position of a simplex (it will become simplex’s centroid), while step determines the spread (size in each dimension) of a simplex. To be more precise, if are the initial step and initial point respectively, the vertices of a simplex will be: and for where denotes projections of the initial step of nth coordinate (the result of projection is treated to be vector given by , where form canonical basis)
Parameters: 


The main method of the DownhillSolver. It actually runs the algorithm and performs the minimization. The sole input parameter determines the centroid of the starting simplex (roughly, it tells where to start), all the others (terminal criteria, initial step, function to be minimized) are supposed to be set via the setters before the call to this method or the default values (not always sensible) will be used.
Parameters: 


Returns:  The value of a function at the point found. 
This function returns the reference to the readytouse DownhillSolver object. All the parameters are optional, so this procedure can be called even without parameters at all. In this case, the default values will be used. As default value for terminal criteria are the only sensible ones, DownhillSolver::setFunction() and DownhillSolver::setInitStep() should be called upon the obtained object, if the respective parameters were not given to createDownhillSolver(). Otherwise, the two ways (give parameters to createDownhillSolver() or miss them out and call the DownhillSolver::setFunction() and DownhillSolver::setInitStep()) are absolutely equivalent (and will drop the same errors in the same way, should invalid input be detected).
Parameters: 


This class is used to perform the nonlinear nonconstrained minimization of a function with known gradient , defined on an ndimensional Euclidean space, using the Nonlinear Conjugate Gradient method. The implementation was done based on the beautifully clear explanatory article An Introduction to the Conjugate Gradient Method Without the Agonizing Pain by Jonathan Richard Shewchuk. The method can be seen as an adaptation of a standard Conjugate Gradient method (see, for example http://en.wikipedia.org/wiki/Conjugate_gradient_method) for numerically solving the systems of linear equations.
It should be noted, that this method, although deterministic, is rather a heuristic method and therefore may converge to a local minima, not necessary a global one. What is even more disastrous, most of its behaviour is ruled by gradient, therefore it essentially cannot distinguish between local minima and maxima. Therefore, if it starts sufficiently near to the local maximum, it may converge to it. Another obvious restriction is that it should be possible to compute the gradient of a function at any point, thus it is preferable to have analytic expression for gradient and computational burden should be born by the user.
The latter responsibility is accompilished via the getGradient(const double* x,double* grad) method of a Solver::Function interface (which represents function that is being optimized). This method takes point a point in ndimensional space (first argument represents the array of coordinates of that point) and comput its gradient (it should be stored in the second argument as an array).
class CV_EXPORTS Solver : public Algorithm { public: class CV_EXPORTS Function { public: virtual ~Function() {} virtual double calc(const double* x) const = 0; virtual void getGradient(const double* /*x*/,double* /*grad*/) {} }; virtual Ptr<Function> getFunction() const = 0; virtual void setFunction(const Ptr<Function>& f) = 0; virtual TermCriteria getTermCriteria() const = 0; virtual void setTermCriteria(const TermCriteria& termcrit) = 0; // x contain the initial point before the call and the minima position (if algorithm converged) after. x is assumed to be (something that // after getMat() will return) rowvector or columnvector. *It's size and should // be consisted with previous dimensionality data given, if any (otherwise, it determines dimensionality)* virtual double minimize(InputOutputArray x) = 0; }; class CV_EXPORTS ConjGradSolver : public Solver{ };Note, that class ConjGradSolver thus does not add any new methods to the basic Solver interface.
Getter for the optimized function. The optimized function is represented by Solver::Function interface, which requires derivatives to implement the method calc(double*) to evaluate the function. It should be emphasized once more, that since Nonlinear Conjugate Gradient method requires gradient to be computable in addition to the function values, getGradient(const double* x,double* grad) method of a Solver::Function interface should be also implemented meaningfully.
Returns:  Smartpointer to an object that implements Solver::Function interface  it represents the function that is being optimized. It can be empty, if no function was given so far. 

Setter for the optimized function. It should be called at least once before the call to ConjGradSolver::minimize(), as default value is not usable.
Parameters: 


Getter for the previously set terminal criteria for this algorithm.
Returns:  Deep copy of the terminal criteria used at the moment. 

Set terminal criteria for downhill simplex method. Two things should be noted. First, this method is not necessary to be called before the first call to ConjGradSolver::minimize(), as the default value is sensible. Second, the method will raise an error if termcrit.type!=(TermCriteria::MAX_ITER+TermCriteria::EPS) and termcrit.type!=TermCriteria::MAX_ITER. This means that termination criteria has to restrict maximum number of iterations to be done and may optionally allow algorithm to stop earlier if certain tolerance is achieved (what we mean by “tolerance is achieved” will be clarified below). If termcrit restricts both tolerance and maximum iteration number, both termcrit.epsilon and termcrit.maxCount should be positive. In case, if termcrit.type==TermCriteria::MAX_ITER, only member termcrit.maxCount is required to be positive and in this case algorithm will just work for required number of iterations.
In current implementation, “tolerance is achieved” means that we have arrived at the point where the norm of the gradient is less than the tolerance value.
Parameters: 


The main method of the ConjGradSolver. It actually runs the algorithm and performs the minimization. The sole input parameter determines the centroid of the starting simplex (roughly, it tells where to start), all the others (terminal criteria and function to be minimized) are supposed to be set via the setters before the call to this method or the default values (not always sensible) will be used. Sometimes it may throw an error, if these default values cannot be used (say, you forgot to set the function to minimize and default value, that is, empty function, cannot be used).
Parameters: 


Returns:  The value of a function at the point found. 
This function returns the reference to the readytouse ConjGradSolver object. All the parameters are optional, so this procedure can be called even without parameters at all. In this case, the default values will be used. As default value for terminal criteria are the only sensible ones, ConjGradSolver::setFunction() should be called upon the obtained object, if the function was not given to createConjGradSolver(). Otherwise, the two ways (submit it to createConjGradSolver() or miss it out and call the ConjGradSolver::setFunction()) are absolutely equivalent (and will drop the same errors in the same way, should invalid input be detected).
Parameters: 

