Created skeleton for simplex method.

Added LPSolver class together with two nested classes: LPFunction and
LPConstraints. These represent function to be maximized and constraints
imposed respectively. They are implementations of interfaces Function
and Constraints respectively (latter ones are nested classes of Solver
interface, which is generic interface for all optimization algorithms to
be implemented within this project).

The next step is to implement the simplex algorithm! First, we shall
implement it for the case of constraints of the form Ax<=b and x>=0.
Then, we shall extend the sets of problems that can be handled by the
conversion to the one we've handled already. Finally, we shale
concentrate on numerical stability and efficiency.
This commit is contained in:
Alex Leontiev 2013-06-24 20:27:11 +03:00
parent f41b8b90ff
commit b216c0940c
4 changed files with 68 additions and 21 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ OpenCV4Tegra/
.sw[a-z]
.*.swp
tags
build/

View File

@ -1,3 +1,2 @@
set(the_description "Generic optimization")
ocv_define_module(optim opencv_imgproc)
#ocv_define_module(optim core)
ocv_define_module(optim opencv_core)

View File

@ -43,44 +43,71 @@
#ifndef __OPENCV_OPTIM_HPP__
#define __OPENCV_OPTIM_HPP__
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/core/mat.hpp"
/*! \namespace cv
Namespace where all the C++ OpenCV functionality resides
*/
namespace cv
namespace cv{namespace optim
{
/* //! restores the damaged image areas using one of the available intpainting algorithms */
class Solver : public Algorithm /* Algorithm is the base OpenCV class */
//! generic class for optimization algorithms */
class CV_EXPORTS Solver : public Algorithm /* Algorithm is the base OpenCV class */
{
class Function
public:
class CV_EXPORTS Function
{
public:
virtual ~Function();
virtual ~Function(){}
virtual double calc(InputArray args) const = 0;
//virtual double calc(InputArray args, OutputArray grad) const = 0;
};
class CV_EXPORTS Constraints
{
public:
virtual ~Constraints(){}
};
// could be reused for all the generic algorithms like downhill simplex.
virtual void solve(InputArray x0, OutputArray result) const = 0;
//! could be reused for all the generic algorithms like downhill simplex. Return value is the maximum value of a function*/
virtual double solve(const Function& F,const Constraints& C, OutputArray result) const = 0;
virtual void setTermCriteria(const TermCriteria& criteria) = 0;
virtual TermCriteria getTermCriteria() = 0;
/*virtual void setTermCriteria(const TermCriteria& criteria) = 0;
virtual TermCriteria getTermCriteria() = 0;*/
// more detailed API to be defined later ...
};
class LPSolver : public Solver
class CV_EXPORTS LPSolver : public Solver
{
public:
virtual void solve(InputArray coeffs, InputArray constraints, OutputArray result) const = 0;
// ...
};
class CV_EXPORTS LPFunction:public Solver::Function
{
cv::Mat z;
public:
//! Note, that this class is supposed to be immutable, so it's ok to make only a shallow copy of z_in.*/
LPFunction(cv::Mat z_in):z(z_in){}
~LPFunction(){};
const cv::Mat& getz()const{return z;}
double calc(InputArray args)const;
};
Ptr<LPSolver> createLPSimplexSolver();
}// cv
//!This class represents constraints for linear problem. There are two matrix stored: m-by-n matrix A and n-by-1 column-vector b.
//!What this represents is the set of constraints Ax\leq b and x\geq 0. It can be shown that any set of linear constraints can be converted
//!this form and **we shall create various constructors for this class that will perform these conversions**.
class CV_EXPORTS LPConstraints:public Solver::Constraints
{
cv::Mat A,b;
public:
~LPConstraints(){};
//! Note, that this class is supposed to be immutable, so it's ok to make only a shallow copy of A_in and b_in.*/
LPConstraints(cv::Mat A_in, cv::Mat b_in):A(A_in),b(b_in){}
const cv::Mat& getA()const{return A;}
const cv::Mat& getb()const{return b;}
};
LPSolver(){}
double solve(const Function& F,const Constraints& C, OutputArray result)const;
};
}}// cv
#endif

View File

@ -1,2 +1,22 @@
#include "precomp.hpp"
#include "opencv2/optim.hpp"
namespace cv{namespace optim{
double LPSolver::solve(const Function& F,const Constraints& C, OutputArray result)const{
printf("call to solve\n");
//TODO: sanity check and throw exception, if appropriate
//TODO: copy A,b,z
//TODO: run simplex algo
return 0.0;
}
double LPSolver::LPFunction::calc(InputArray args)const{
printf("call to LPFunction::calc()\n");
return 0.0;
}
}}