Skip to content

Commit b216c09

Browse files
committed
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.
1 parent f41b8b9 commit b216c09

File tree

4 files changed

+68
-21
lines changed

4 files changed

+68
-21
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ OpenCV4Tegra/
66
.sw[a-z]
77
.*.swp
88
tags
9+
build/

modules/optim/CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
set(the_description "Generic optimization")
2-
ocv_define_module(optim opencv_imgproc)
3-
#ocv_define_module(optim core)
2+
ocv_define_module(optim opencv_core)

modules/optim/include/opencv2/optim.hpp

+45-18
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,71 @@
4343
#ifndef __OPENCV_OPTIM_HPP__
4444
#define __OPENCV_OPTIM_HPP__
4545

46+
#include <iostream>
4647
#include "opencv2/core.hpp"
4748
#include "opencv2/core/mat.hpp"
4849

4950
/*! \namespace cv
5051
Namespace where all the C++ OpenCV functionality resides
5152
*/
52-
namespace cv
53+
namespace cv{namespace optim
5354
{
54-
55-
/* //! restores the damaged image areas using one of the available intpainting algorithms */
56-
class Solver : public Algorithm /* Algorithm is the base OpenCV class */
55+
//! generic class for optimization algorithms */
56+
class CV_EXPORTS Solver : public Algorithm /* Algorithm is the base OpenCV class */
5757
{
58-
class Function
58+
public:
59+
class CV_EXPORTS Function
5960
{
6061
public:
61-
virtual ~Function();
62+
virtual ~Function(){}
6263
virtual double calc(InputArray args) const = 0;
63-
//virtual double calc(InputArray args, OutputArray grad) const = 0;
64+
};
65+
class CV_EXPORTS Constraints
66+
{
67+
public:
68+
virtual ~Constraints(){}
6469
};
6570

66-
// could be reused for all the generic algorithms like downhill simplex.
67-
virtual void solve(InputArray x0, OutputArray result) const = 0;
71+
//! could be reused for all the generic algorithms like downhill simplex. Return value is the maximum value of a function*/
72+
virtual double solve(const Function& F,const Constraints& C, OutputArray result) const = 0;
6873

69-
virtual void setTermCriteria(const TermCriteria& criteria) = 0;
70-
virtual TermCriteria getTermCriteria() = 0;
74+
/*virtual void setTermCriteria(const TermCriteria& criteria) = 0;
75+
virtual TermCriteria getTermCriteria() = 0;*/
7176

7277
// more detailed API to be defined later ...
73-
7478
};
7579

76-
class LPSolver : public Solver
80+
class CV_EXPORTS LPSolver : public Solver
7781
{
7882
public:
79-
virtual void solve(InputArray coeffs, InputArray constraints, OutputArray result) const = 0;
80-
// ...
81-
};
83+
class CV_EXPORTS LPFunction:public Solver::Function
84+
{
85+
cv::Mat z;
86+
public:
87+
//! Note, that this class is supposed to be immutable, so it's ok to make only a shallow copy of z_in.*/
88+
LPFunction(cv::Mat z_in):z(z_in){}
89+
~LPFunction(){};
90+
const cv::Mat& getz()const{return z;}
91+
double calc(InputArray args)const;
92+
};
8293

83-
Ptr<LPSolver> createLPSimplexSolver();
84-
}// cv
94+
//!This class represents constraints for linear problem. There are two matrix stored: m-by-n matrix A and n-by-1 column-vector b.
95+
//!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
96+
//!this form and **we shall create various constructors for this class that will perform these conversions**.
97+
class CV_EXPORTS LPConstraints:public Solver::Constraints
98+
{
99+
cv::Mat A,b;
100+
public:
101+
~LPConstraints(){};
102+
//! 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.*/
103+
LPConstraints(cv::Mat A_in, cv::Mat b_in):A(A_in),b(b_in){}
104+
const cv::Mat& getA()const{return A;}
105+
const cv::Mat& getb()const{return b;}
106+
};
107+
108+
LPSolver(){}
109+
double solve(const Function& F,const Constraints& C, OutputArray result)const;
110+
};
111+
}}// cv
85112

86113
#endif

modules/optim/src/lpsolver.cpp

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11
#include "precomp.hpp"
2-
#include "opencv2/optim.hpp"
2+
3+
namespace cv{namespace optim{
4+
5+
double LPSolver::solve(const Function& F,const Constraints& C, OutputArray result)const{
6+
printf("call to solve\n");
7+
8+
//TODO: sanity check and throw exception, if appropriate
9+
10+
//TODO: copy A,b,z
11+
12+
//TODO: run simplex algo
13+
14+
return 0.0;
15+
}
16+
17+
double LPSolver::LPFunction::calc(InputArray args)const{
18+
printf("call to LPFunction::calc()\n");
19+
return 0.0;
20+
}
21+
22+
}}

0 commit comments

Comments
 (0)