Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SOCP.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include "SOCP.hh"
26 
27 extern "C" {
28 #include "socp/socp.h"
29 }
30 
31 using namespace BIAS;
32 
34 {
35  MaxIters_ = 100;
36  AbsTolerance_ = 1e-6;
37  RelTolerance_ = 1e-4;
38  TargetValue_ = 0.0;
39  Nu_ = 10.0;
40 }
41 
43 {
44 }
45 
46 int SOCP::Compute(int L, int m, const std::vector<int> &N,
47  const Vector<double> &f,
48  const std::vector< Matrix<double> > &A,
49  const std::vector< Vector<double> > &b,
50  const std::vector< Vector<double> > &c,
51  const std::vector<double> &d,
52  Vector<double> &x, std::vector< Vector<double> > &z,
53  std::vector<double> &w)
54 {
55 #ifdef BIAS_DEBUG
56  // Check sizes of input matrices and vectors
57  BIASASSERT((int)f.Size() == m);
58  BIASASSERT((int)x.Size() == m);
59  BIASASSERT((int)N.size() == L);
60  BIASASSERT((int)A.size() == L);
61  BIASASSERT((int)b.size() == L);
62  BIASASSERT((int)c.size() == L);
63  BIASASSERT((int)d.size() == L);
64  BIASASSERT((int)z.size() == L);
65  BIASASSERT((int)w.size() == L);
66  for (int i = 0; i < L; i++) {
67  BIASASSERT((int)A[i].GetRows() == N[i] && (int)A[i].GetCols() == m);
68  BIASASSERT((int)b[i].Size() == N[i]);
69  BIASASSERT((int)c[i].Size() == m);
70  BIASASSERT((int)z[i].Size() == N[i]);
71  }
72 #endif
73 
74  int n = 0;
75  int *in_N = new int[L];
76  for (int i = 0; i < L; i++)
77  {
78  n += N[i];
79  in_N[i] = N[i] + 1;
80  }
81  double *in_f = new double[m];
82  double *in_Ac = new double[(n+L)*m];
83  double *in_bd = new double[n+L];
84  double *inout_x = new double[m];
85  double *inout_zw = new double[n+L];
86  double *ptr = in_Ac;
87  for (int i = 0; i < L; i++) {
88  for (int col = 0; col < m; col++) {
89  for (int row = 0; row < N[i]; row++)
90  *(ptr++) = A[i][row][col];
91  *(ptr++) = c[i][col];
92  }
93  }
94  ptr = in_bd;
95  for (int i = 0; i < L; i++) {
96  for (int row = 0; row < N[i]; row++)
97  *(ptr++) = b[i][row];
98  *(ptr++) = d[i];
99  }
100  for (int j = 0; j < m; j++) {
101  in_f[j] = f[j];
102  inout_x[j] = x[j];
103  }
104  ptr = inout_zw;
105  for (int i = 0; i < L; i++) {
106  for (int row = 0; row < N[i]; row++)
107  *(ptr++) = z[i][row];
108  *(ptr++) = w[i];
109  }
110 
111  // Compute workspace size
112  int dblSize, intSize, mhist, nhist;
113  socp_getwork(L, in_N, m, MaxIters_, 0, &mhist, &nhist, &dblSize, &intSize);
114  double* dblWorkspace = new double[dblSize];
115  int* intWorkspace = new int[intSize];
116 
117  int iter = MaxIters_, info = 0;
118  int res = socp(L, in_N, m, in_f, in_Ac, in_bd, inout_x, inout_zw,
119  AbsTolerance_, RelTolerance_, TargetValue_, &iter, Nu_,
120  &info, 0, (double*)0, dblWorkspace, intWorkspace);
121 
122  // Write results back
123  for (int j = 0; j < m; j++)
124  x[j] = inout_x[j];
125  ptr = inout_zw;
126  for (int i = 0; i < L; i++) {
127  for (int row = 0; row < N[i]; row++)
128  z[i][row] = *(ptr++);
129  w[i] = *(ptr++);
130  }
131 
132  // Release memory
133  delete[] in_N;
134  delete[] in_f;
135  delete[] in_Ac;
136  delete[] in_bd;
137  delete[] inout_x;
138  delete[] inout_zw;
139  delete[] dblWorkspace;
140  delete[] intWorkspace;
141 
142  return (res != 0) ? -1 : 0;
143 }
144 
145 int SOCP::Compute(int m, int n, const Vector<double> &f,
146  const Matrix<double> &A, const Vector<double> &b,
147  const Vector<double> &c, double d,
148  Vector<double> &x, Vector<double> &z, double &w)
149 {
150 #ifdef BIAS_DEBUG
151  // Check sizes of input matrix and vectors
152  BIASASSERT((int)f.Size() == m);
153  BIASASSERT((int)x.Size() == m);
154  BIASASSERT((int)A.GetRows() == n && (int)A.GetCols() == m);
155  BIASASSERT((int)b.Size() == n);
156  BIASASSERT((int)c.Size() == m);
157  BIASASSERT((int)z.Size() == n)
158 #endif
159 
160  const int L = 1;
161  int in_N = n + 1;
162  double *in_f = new double[m];
163  double *in_Ac = new double[(n+L)*m];
164  double *in_bd = new double[n+L];
165  double *inout_x = new double[m];
166  double *inout_zw = new double[n+L];
167  double *ptr = in_Ac;
168  for (int col = 0; col < m; col++) {
169  for (int row = 0; row < n; row++)
170  *(ptr++) = A[row][col];
171  *(ptr++) = c[col];
172  }
173  ptr = in_bd;
174  for (int row = 0; row < n; row++)
175  *(ptr++) = b[row];
176  *(ptr++) = d;
177  for (int j = 0; j < m; j++) {
178  in_f[j] = f[j];
179  inout_x[j] = x[j];
180  }
181  ptr = inout_zw;
182  for (int row = 0; row < n; row++)
183  *(ptr++) = z[row];
184  *(ptr++) = w;
185 
186  // Compute workspace size
187  int dblSize, intSize, mhist, nhist;
188  socp_getwork(L, &in_N, m, MaxIters_, 0, &mhist, &nhist, &dblSize, &intSize);
189  double* dblWorkspace = new double[dblSize];
190  int* intWorkspace = new int[intSize];
191 
192  int iter = MaxIters_, info = 0;
193  int res = socp(L, &in_N, m, in_f, in_Ac, in_bd, inout_x, inout_zw,
194  AbsTolerance_, RelTolerance_, TargetValue_, &iter, Nu_,
195  &info, 0, (double*)0, dblWorkspace, intWorkspace);
196 
197  // Write results back
198  for (int j = 0; j < m; j++)
199  x[j] = inout_x[j];
200  ptr = inout_zw;
201  for (int row = 0; row < n; row++)
202  z[row] = *(ptr++);
203  w = *(ptr++);
204 
205  // Release memory
206  delete[] in_f;
207  delete[] in_Ac;
208  delete[] in_bd;
209  delete[] inout_x;
210  delete[] inout_zw;
211  delete[] dblWorkspace;
212  delete[] intWorkspace;
213 
214  return (res != 0) ? -1 : 0;
215 }
unsigned int Size() const
length of the vector
Definition: Vector.hh:143
unsigned int GetRows() const
Definition: Matrix.hh:202
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
unsigned int GetCols() const
Definition: Matrix.hh:204
~SOCP()
Release memory used.
Definition: SOCP.cpp:42
SOCP()
Create SOCP solver with default parameters.
Definition: SOCP.cpp:33