Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleSOCP.cpp
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3 Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9 BIAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13 
14 BIAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18 
19 You should have received a copy of the GNU Lesser General Public License
20 along with BIAS; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 
24 
25 /** @example ExampleSOCP.cpp
26  @relates SOCP
27  @ingroup g_examples
28  @brief Example for Second Order Cone Programming.
29  Similar to Matlab example provided by M. S. Lobo et al.
30  from http://www.stanford.edu/~boyd/old_software/socp/
31 
32  Minimize x + y subject to (x+1)^2 + (y-1)^2 <= 1.
33 
34  @author esquivel 05/2012
35 */
36 
37 #include <bias_config.h>
38 
39 #include <MathAlgo/SOCP.hh>
40 #include <Base/Math/Random.hh>
41 
42 using namespace BIAS;
43 using namespace std;
44 
45 int main(int args, char **arg)
46 {
47  // Specify SOCP instance with single constraint
48  const int m = 2; // number of variables
49  const int n = 2; // dimension of constraint
50  Vector<double> f(m);
51  Matrix<double> A(n, m, MatrixZero);
52  Vector<double> b(n), c(m);
53  double d;
54  f[0] = f[1] = 1;
55  A[0][0] = A[1][1] = 1;
56  b[0] = 1;
57  b[1] = -1;
58  c[0] = c[1] = 0;
59  d = 1;
60 
61  // Create initial primal and dual solution
62  Vector<double> x(m), z(n);
63  double w;
64  x[0] = -0.5;
65  x[1] = 1;
66  z[0] = 1;
67  z[1] = 1;
68  w = 4;
69 
70  // Solve SOCP instance
71  SOCP socp;
72  socp.SetNu(5.0);
73  socp.SetMaxIterations(50);
74  socp.SetRelativeTolerance(0.0);
75  socp.SetAbsoluteTolerance(1e-4);
76  socp.SetTargetValue(0.0);
77  int res = socp.Compute(m, n, f, A, b, c, d, x, z, w);
78 
79  // Show results
80  cout << "Return code of SOCP solver = " << res << endl;
81  cout << "Primal problem : " << endl;
82  cout << " Final primal solution x = " << x << endl;
83  cout << " Target value of f'x = " << f.ScalarProduct(x) << endl;
84  cout << " Constraint evaluation for solution x : " << endl
85  << " |Ax + b| = " << BIAS::Vector<double>(A*x+b).NormL2()
86  << " <= c'x + d = " << c.ScalarProduct(x) + d << endl;
87  cout << "Dual problem : " << endl;
88  cout << " Final dual solution z = " << z << ", w = " << w << endl;
89  cout << " Target value of -(b'z + dw) = " << -(b.ScalarProduct(z) + d*w) << endl;
90  cout << " Constraint evaluation for solution z, w : " << endl
91  << " A'z + wc = " << BIAS::Vector<double>(A.Transpose()*z + w*c)
92  << " = f = " << f << endl;
93  cout << " |z| = " << z.NormL2() << " <= w = " << w << endl;
94  return 0;
95 }
96 
T ScalarProduct(const Vector< T > &argvec) const
scalar product (inner product) of two vectors returning a scalr
Definition: Vector.hh:355
double NormL2() const
Return the L2 norm: sqrt(a^1 + a^2 + ...)
Definition: Vector.hh:416
Wrapper for Second-Order Cone Programming implementation, by Miguel S.
Definition: SOCP.hh:57
void SetNu(double nu)
Set parameter nu that controls the rate of convergence.
Definition: SOCP.hh:78
void SetTargetValue(double target)
Set target value used in convergence criterion only when relative tolerance given is &lt; 0...
Definition: SOCP.hh:87
void SetRelativeTolerance(double tol)
Set relative tolerance used to determine convergence.
Definition: SOCP.hh:103
void SetAbsoluteTolerance(double tol)
Set absolute tolerance used to determine convergence.
Definition: SOCP.hh:95
int Compute(int L, int m, const std::vector< int > &N, const Vector< double > &f, const std::vector< Matrix< double > > &A, const std::vector< Vector< double > > &b, const std::vector< Vector< double > > &c, const std::vector< double > &d, Vector< double > &x, std::vector< Vector< double > > &z, std::vector< double > &w)
Solve Second-Order Cone Programming instance with L constraints.
Definition: SOCP.cpp:46
void SetMaxIterations(int iter)
Set maximal number of iterations performed by SOCP algorithm.
Definition: SOCP.hh:69