Borderbasix

solvermosek.hpp
Go to the documentation of this file.
1 
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 #include "declarations.h"
6 
7 //#include<iostream>
8 //#include<fstream>
9 //#include<iomanip>
10 
11 #include "mosek.h" /* Include the MOSEK definition file. */
12 //#include "sdpinclude.hpp"
13 
14 //#define NUMCON 14 /* Number of constraints. */
15 //#define NUMBARVAR 4 /* Number of semidefinite variables *//
16 
17 
18 #include "../arithm/Scl.hpp"
19 #include "../arithm/Scl_mpf.hpp"
20 
21 
22 static void MSKAPI printstr(void *handle,
23  MSKCONST char str[])
24 {
25  printf("%s",str);
26 } /* printstr */
27 
28 template<typename T>
29 int solvermosek1(int n,int k,T *a, constraintmatrixv<T> *constraints,int *listsize,int dimcon,double *&solmosek,double *&solprimal, int &solst,double *&soldual,int &dimsoldual)
30 {
31 
32 
33  MSKrescodee r;
34 
35 
36  MSKint32t NUMCON;
37  MSKint32t NUMBARVAR;
38  MSKint32t DIMBARVAR[]={}; /* Dimension of semidefinite cone */
39  MSKint64t LENBARVAR[]={}; /* Number of scalar SD variables */
40 
41  struct sparseblockv<T> *p,*q;
42 
43 // MSKboundkeye bkc[] = { MSK_BK_FX, MSK_BK_FX, MSK_BK_FX, MSK_BK_FX,MSK_BK_FX, MSK_BK_FX, MSK_BK_FX, MSK_BK_FX };
44 // double blc[] = { 0,0,0,0,0,0,1,0};
45 // double buc[] = { 0,0,0,0,0,0,1,0};
46 
47 //
48 
49 // double blc[] = { 0,0,0,1.5,1.0,1.5,1.5,1.5};
50 // double buc[] = { 0,0,0,1.5,1.0,1.5,1.5,1.5};
51 
52  MSKint32t *barc_i,
53  *barc_j;
54  double *barc_v;
55 
56 
57 
58  MSKint32t *bara_i,
59  *bara_j;
60  double *bara_v;
61 
62  MSKint32t i,j,l,m,numb;
63  MSKint64t idx,num=0,numa;
64 
65  double falpha=1.0;
66 
67 
68  double *barx1;
69  double *bars1;
70  double *yy,*primalobj;
71 
72 
73 
74 
75  cout<<"dentro de mosek"<<endl;
76  NUMCON=k-1;
77  NUMBARVAR=1;
78  DIMBARVAR[0]=n;
79  cout<<"n"<<n<<endl;
80  for(i=0;i<dimcon;i++) DIMBARVAR[0]+=listsize[i];
81  LENBARVAR[0]=DIMBARVAR[0]*(DIMBARVAR[0]+1)/2;
82 
83  cout<<"dentro de mosek avant task"<<endl;
84  cout<<"NUMCON"<<NUMCON<<endl;
85  cout<<"DIMBARVAR[0]"<<DIMBARVAR[0]<<endl;
86  cout<<"LENBARVAR[0]"<<LENBARVAR[0]<<endl;
87  MSKenv_t env = NULL;
88  MSKtask_t task = NULL;
89 
90  /* Create the mosek environment. */
91  r = MSK_makeenv(&env,NULL);
92 
93  if ( r==MSK_RES_OK )
94  {
95  /* Create the optimization task. */
96  r = MSK_maketask(env,NUMCON,0,&task);
97 
98  if ( r==MSK_RES_OK )
99  {
100  MSK_linkfunctotaskstream(task,MSK_STREAM_LOG,NULL,printstr);
101 
102  /* Append 'NUMCON' empty constraints.
103  The constraints will initially have no bounds. */
104  if ( r == MSK_RES_OK )
105  r = MSK_appendcons(task,NUMCON);
106 
107 
108 
109  /* Append 'NUMBARVAR' semidefinite variables. */
110  if ( r == MSK_RES_OK ) {
111  r = MSK_appendbarvars(task, NUMBARVAR,DIMBARVAR);
112  }
113 
114  /* Optionally add a constant term to the objective. */
115  if ( r ==MSK_RES_OK )
116  r = MSK_putcfix(task,0.0);
117 
118 
119  cout<<"dentro de mosek avant task2"<<endl;
120  /* Set the linear term barc_j in the objective.*/
121  q=constraints[k].blocks;
122  num=q->numentries;
123 
124  barc_i = (MSKint32t*) MSK_calloctask(task,num,sizeof(MSKintt));
125  barc_j = (MSKint32t*) MSK_calloctask(task,num,sizeof(MSKintt));
126  barc_v= (double*) MSK_calloctask(task,num,sizeof(MSKrealt));
127 
128  for (l=0;l<num;l++)
129  {
130  barc_i[l]=q->jindices[l+1]-1;
131  barc_j[l]=q->iindices[l+1]-1;
132  //put this cast from float to double (doesn't work cast simple to double)
133 
134  barc_v[l]= Scl<MPF>::castDouble(q->entries[l+1])*(-1);
135 
136 // cout<<"barc_i[l]"<<barc_i[l]<<endl;
137 // cout<<"barc_j[l]"<<barc_j[l]<<endl;
138  // cout<<"barc_v[l]"<<barc_v[l]<<endl;
139 
140 
141 
142  }
143 
144  cout<<"dentro de mosek avant task2bis"<<"num"<<num<<endl;
145 
146  if ( r == MSK_RES_OK )
147  r = MSK_appendsparsesymmat(task,
148  DIMBARVAR[0],
149  num,
150  barc_i,
151  barc_j,
152  barc_v,
153  &idx);
154 
155  if ( r == MSK_RES_OK )
156  r = MSK_putbarcj(task, 0, 1, &idx, &falpha);
157 
158 
159  cout<<"dentro de mosek avant task3"<<endl;
160 
161  /* Set the bounds on constraints.
162  for i=1, ...,NUMCON : blc[i] <= constraint i <= buc[i] */
163  for(i=0; i<NUMCON && r==MSK_RES_OK; ++i)
164  {
165  // cout<<"a[i+1]"<<a[i+1]<<endl;
166  r = MSK_putconbound(task,
167  i, /* Index of constraint.*/
168  MSK_BK_FX, /* Bound key.*/
169  Scl<MPF>::castDouble(a[i+1]), /* Numerical value of lower bound.*/
170  Scl<MPF>::castDouble(a[i+1])); /* Numerical value of upper bound.*/
171  }
172 
173  cout<<"dentro de mosek avant task4"<<endl;
174  /* Add the first constraint of barA */
175  numa=0;
176 
177  for (i=1;i<=NUMCON;i++)
178  {
179  p=constraints[i].blocks;
180 
181  numa+=p->numentries;
182  }
183 
184  bara_i = (MSKint32t*) MSK_calloctask(task,numa,sizeof(MSKintt));
185  bara_j = (MSKint32t*) MSK_calloctask(task,numa,sizeof(MSKintt));
186  bara_v= (double*) MSK_calloctask(task,numa,sizeof(MSKrealt));
187  m=0;
188  for (i=1;i<=NUMCON;i++)
189  {
190  p=constraints[i].blocks;
191 
192 
193  for (l=0;l<p->numentries;l++)
194  {
195 
196  bara_i[l+m]=p->jindices[l+1]-1;
197  bara_j[l+m]=p->iindices[l+1]-1;
198  bara_v[l+m]=Scl<MPF>::castDouble(p->entries[l+1]);
199 
200 
201 // cout<<"bara_i[l+m]"<<bara_i[l+m]<<endl;
202 // cout<<"bara_j[l+m]"<<bara_j[l+m]<<endl;
203 // cout<<"bara_v[l+m]"<<bara_v[l+m]<<endl;
204 
205 
206 
207  }
208  m+=p->numentries;
209  }
210  numb=0;
211  for (i=1;i<=NUMCON;i++)
212  {
213  p=constraints[i].blocks;
214 
215  if ( r==MSK_RES_OK )
216  r = MSK_appendsparsesymmat(task,
217  DIMBARVAR[0],
218  p->numentries,
219  bara_i+numb,
220  bara_j+numb,
221  bara_v+numb,
222  &idx);
223 
224  if ( r==MSK_RES_OK )
225  r = MSK_putbaraij(task, i-1, 0, 1, &idx, &falpha);
226 
227  numb+=p->numentries;
228 
229 
230 
231  }
232 
233  cout<<"dentro de mosek avant task5"<<endl;
234 
235 
236 
237 
238 
239 
240 
241  /* Maximize objective function. */
242  if (r == MSK_RES_OK)
243  r = MSK_putobjsense(task, MSK_OBJECTIVE_SENSE_MAXIMIZE);
244 
245  if ( r==MSK_RES_OK )
246  {
247 
248  cout<<"gap"<<MSK_putdouparam(task,MSK_DPAR_INTPNT_CO_TOL_REL_GAP,1.0e-6)<<endl;
249 // // MSK_putdouparam(task,MSK_DPAR_INTPNT_CO_TOL_NEAR_REL,1.0);
250 // MSK_putdouparam(task,MSK_DPAR_INTPNT_CO_TOL_INFEAS,1.0e-3);
251 // MSK_putdouparam(task,MSK_DPAR_INTPNT_CO_TOL_DFEAS,1.0e-3);
252  // MSK_putdouparam(task,MSK_DPAR_INTPNT_CO_TOL_NEAR_REL,1.0e+100);
253  // MSK_putdouparam(task,MSK_DPAR_INTPNT_CO_TOL_PFEAS,1.0e-100);
254  //cout<<"quelle type de probleme"<<MSK_putintparam(task,MSK_IPAR_INTPNT_SOLVE_FORM,MSK_SOLVE_PRIMAL)<<endl;
255  //cout<<"pfeas"<<MSK_putdouparam(task,MSK_DPAR_INTPNT_CO_TOL_PFEAS,1.0e-5)<<endl;
256  //cout<<"dfeas"<<MSK_putdouparam(task,MSK_DPAR_INTPNT_CO_TOL_DFEAS,1.0e-5)<<endl;
257 
259  /* Run optimizer */
260  r = MSK_optimizetrm(task,&trmcode);
261 
262  /* Print a summary containing information
263  about the solution for debugging purposes*/
264  MSK_solutionsummary (task,MSK_STREAM_MSG);
265 
266  if ( r==MSK_RES_OK )
267  {
268 
269 
271 
272 
273  MSK_getsolsta (task,MSK_SOL_ITR,&solsta);
274  //int solst;
275  solst=solsta;
276  cout<<"solsta"<<solst<<endl;
277  dimsoldual= LENBARVAR[0];
278 
279  switch(solsta)
280  {
281  case MSK_SOL_STA_OPTIMAL:
283  case MSK_SOL_STA_UNKNOWN:
284 
285  barx1 = (double*) MSK_calloctask(task,LENBARVAR[0],sizeof(MSKrealt));
286  bars1 = (double*) MSK_calloctask(task,LENBARVAR[0],sizeof(MSKrealt));
287  yy = (double*) MSK_calloctask(task,NUMCON,sizeof(MSKrealt));
288  solmosek=(double*) MSK_calloctask(task,NUMCON+1,sizeof(MSKrealt));
289  primalobj=(double*) MSK_calloctask(task,1,sizeof(MSKrealt));
290  solprimal=(double*) MSK_calloctask(task,1,sizeof(MSKrealt));
291  soldual = (double*) MSK_calloctask(task,LENBARVAR[0],sizeof(MSKrealt));
292 
293  // obtain primal solution, semidefinite matrix
294  MSK_getbarxj(task,
295  MSK_SOL_ITR, /* Request the interior solution. */
296  0,
297  barx1);
298 
299  // obtain dual solution, semidefinite matrix
300  MSK_getbarsj(task,
301  MSK_SOL_ITR, /* Request the interior solution. */
302  0,
303  bars1);
304  MSK_gety(task,
305  MSK_SOL_ITR, /* Request the interior solution. */
306  yy);
307 
308  MSK_getprimalobj(task,
309  MSK_SOL_ITR,
310  primalobj);
311 
312  printf("Optimal primal solution\n");
313 
314 // for(i=0; i<LENBARVAR[0]; ++i)
315 // printf("barx1[%d]: % e\n",i,barx1[i]);
316 
317  printf("Optimal dual solution\n");
318  for(i=0; i<LENBARVAR[0]; ++i)
319  {
320  // printf("bars1[%d]: % e\n",i,-bars1[i]);
321  soldual[i]=-bars1[i];
322  }
323  solprimal[0]=primalobj[0];
324 
325  cout<<"Primal Solution "<<solprimal[0]<<endl;
326 
327  for(i=0; i<NUMCON; ++i)
328  {
329  // printf("yy[%d]: % e\n",i,yy[i]);
330  solmosek[i]=yy[i];
331  }
332  solmosek[NUMCON]=1.0;
333 
334  MSK_freetask(task,barx1);
335 
336  MSK_freetask(task,bars1);
337  MSK_freetask(task,yy);
338 
339  break;
340 
345  printf("Primal or dual infeasibility certificate found.\n");
346  break;
347 
348  //case MSK_SOL_STA_UNKNOWN:
349  // printf("The status of the solution could not be determined.\n");
350  // break;
351  default:
352  printf("Other solution status.");
353  break;
354  }
355  }
356  else
357  {
358  printf("Error while optimizing.\n");
359  }
360  }
361 
362  if (r != MSK_RES_OK)
363  {
364  /* In case of an error print error code and description. */
365  char symname[MSK_MAX_STR_LEN];
366  char desc[MSK_MAX_STR_LEN];
367 
368  printf("An error occurred while optimizing.\n");
369  MSK_getcodedesc (r,
370  symname,
371  desc);
372  printf("Error %s - '%s'\n",symname,desc);
373  }
374 
375  }
376  /* Delete the task and the associated data. */
377 
378  MSK_deletetask(&task);
379  }
380 
381  /* Delete the environment and the associated data. */
382 
383  MSK_deleteenv(&env);
384 
385  return ( r );
386  } /* main */
387 
void * MSKenv_t
Definition: mosek.h:2225
double MSKrealt
Definition: mosek.h:2239
#define MSKAPI
Definition: mosek.h:39
Definition: mosek.h:380
void * MSKtask_t
Definition: mosek.h:2227
Definition: mosek.h:340
MSKcallbackcodee MSKsoltypee MSKprostae MSKsolstae * solsta
Definition: mosek.h:2689
Definition: mosek.h:341
void *MSKAPI MSK_calloctask(MSKtask_t task, MSKCONST size_t number, MSKCONST size_t size)
int solvermosek1(int n, int k, T *a, constraintmatrixv< T > *constraints, int *listsize, int dimcon, double *&solmosek, double *&solprimal, int &solst, double *&soldual, int &dimsoldual)
Definition: solvermosek.hpp:29
Definition: mosek.h:335
MSKenv_t * env
Definition: mosek.h:2755
static double castDouble(Scl< T > &ref)
MSKrescodee * trmcode
Definition: mosek.h:3828
MSKsoltypee MSKrealt * primalobj
Definition: mosek.h:3069
Definition: mosek.h:1408
MSKconetypee MSKrealt MSKint32t MSKint32t j
Definition: mosek.h:2421
Definition: mosek.h:346
MSKint32t k
Definition: mosek.h:2713
Definition: mosek.h:763
MSKint64t idx
Definition: mosek.h:4022
__mskint32 MSKint32t
Definition: mosek.h:2233
MSKCONST char * str
Definition: mosek.h:2317
Definition: mosek.h:347
__mskint64 MSKint64t
Definition: mosek.h:2235
Definition: mosek.h:1393
#define MSKCONST
Definition: mosek.h:32
MSKcallbackfunc MSKuserhandle_t * handle
Definition: mosek.h:2683
Definition: Scl.hpp:26
#define MSKintt
Definition: mosek.h:2074
enum MSKsolsta_enum MSKsolstae
Definition: mosek.h:2112
Definition: mosek.h:1269
MSKint32t MSKint32t MSKint32t i
Definition: mosek.h:2278
char * symname
Definition: mosek.h:4803
MSKrescodee r
Definition: mosek.h:2321
MSKstreamtypee MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t MSKint32t a
Definition: mosek.h:3833
MSKint32t MSKint32t MSKtask_t * task
Definition: mosek.h:4911
Definition: mosek.h:430
Definition: mosek.h:336
MSKint32t num
Definition: mosek.h:2373
enum MSKrescode_enum MSKrescodee
Definition: mosek.h:2136
Definition: mosek.h:429
Definition: mosek.h:357
Definition: mosek.h:342
Home  |  Download & InstallContributions