summaryrefslogtreecommitdiff
path: root/sci_gateway/cpp/ecos.cpp
blob: 5dee6a423651e9e4eb0b02d32096ea6c3331224f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright (C) 2016 - IIT Bombay - FOSSEE
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
// Author: Georgey John
// Organization: FOSSEE, IIT Bombay
// Email: toolbox@scilab.in

#include "sci_iofunc.hpp"
extern "C"
{
	#include <api_scilab.h>
	#include <Scierror.h>
	#include <BOOL.h>
	#include <localization.h>
	#include <sciprint.h>
	#include <ecos.h>
	#include <ecos_bb.h>

	// function to convert int to idxint
	idxint * int2idxint(int * sci_int, int n) {
		// int *sci_int;
		idxint *sci_idxint;
		sci_idxint = (idxint *) malloc(n * sizeof(idxint));
		for (int i = 0; i < n; i++) {
			sci_idxint[i] = sci_int[i];
			}
		return(sci_idxint);
	}

	int sci_ecos(char *fname){

		CheckInputArgument(pvApiCtx, 17, 17);
		CheckOutputArgument(pvApiCtx, 5, 5);
		// Error management variable
		SciErr sciErr;
		
		double *cptr=NULL, *Gprptr=NULL, *hptr=NULL, *Aprptr=NULL, *bptr=NULL;
		int  *Girptr=NULL,*Gjcptr=NULL, *Airptr=NULL, *Ajcptr=NULL, *qptr=NULL, mptr=0,nptr=0,pptr=0,nconesptr=0;
		static int  lptr=0, eptr=0;
		unsigned int temp1 = 0,temp2 = 0, iret = 0;
		double *maxit=NULL,*feastol=NULL,*reltol=NULL,*abstol=NULL,*feastol_inacc=NULL,*reltol_inacc=NULL,*abstol_inacc=NULL,*verbose=NULL,*mi_max_iter=NULL,*mi_int_tol=NULL,*mi_abs_eps=NULL,*mi_rel_eps=NULL;
		int c_rows=0, Gpr_rows=0, Gir_rows=0, Gjc_rows=0, h_rows=0, Apr_rows=0, Air_rows=0, Ajc_rows=0, b_rows=0, l_rows=0, q_rows=0, e_rows=0;
		int c_cols=0, Gpr_cols=0, Gir_cols=0, Gjc_cols=0, h_cols=0, Apr_cols=0, Air_cols=0, Ajc_cols=0, b_cols=0, l_cols=0, q_cols=0, e_cols=0;
	
		idxint exitflag = ECOS_FATAL;

		
		//Getting objective matrix 
		if(getDoubleMatrixFromScilab(1, &c_rows, &c_cols, &cptr))
		{
			return 1;
		}

		//Getting Gpr matrix linear inequality constraints 
		
		if(getDoubleMatrixFromScilab(2, &Gpr_rows, &Gpr_cols, &Gprptr))
		{
			return 1;
		}

		//Getting Gjc integer matrix representing coloumn indices of linear inequality constraints 
		if(getIntMatrixFromScilab(3, &Gjc_rows, &Gjc_cols, &Gjcptr))
		{
			return 1;
		}

		//Getting Gir integer matrix representing row indices of linear inequality constraints 
		if(getIntMatrixFromScilab(4, &Gir_rows, &Gir_cols, &Girptr))
		{
			return 1;
		}

		//Getting matrix representing RHS of linear inequality constraints 
		if(getDoubleMatrixFromScilab(5, &h_rows, &h_cols, &hptr))
		{
			return 1;
		}

		//Getting Apr matrix representing linear equality constraints 
		if(getDoubleMatrixFromScilab(6, &Apr_rows, &Apr_cols, &Aprptr))
		{
			return 1;
		}

		//Getting Ajc integer matrix representing coloumn indices linear inequality constraints 
		if(getIntMatrixFromScilab(7, &Ajc_rows, &Ajc_cols, &Ajcptr))
		{
			return 1;
		}

		//Getting Air integer matrix representing row indices of linear inequality constraints 
		if(getIntMatrixFromScilab(8, &Air_rows, &Air_cols, &Airptr))
		{
			return 1;
		}

		//Getting matrix representing RHS of linear inequality constraints 
		if(getDoubleMatrixFromScilab(9, &b_rows, &b_cols, &bptr))
		{
			return 1;
		}

		//Getting Integer representing dimensions of positive orthant
		if(getIntFromScilab(10, &lptr))
		{
			return 1;
		}

		//Getting integer matrix representing dimensions of each cone  
		if(getIntMatrixFromScilab(11, &q_rows, &q_cols, &qptr))
		{
			return 1;
		}

		//Getting integer representing number of exponential cone
		if(getIntFromScilab(12, &eptr))
		{
			return 1;
		}
		
		//Getting matrix representing maximum iteration
		if(getFixedSizeDoubleMatrixInList(13,2,temp1,temp2,&maxit))
		{
			return 1;
		}

		//Getting matrix representing feasible tolerance
		if(getFixedSizeDoubleMatrixInList(13,4,temp1,temp2,&feastol))
		{
			return 1;
		}

		//Getting matrix representing relative tolerance
		if(getFixedSizeDoubleMatrixInList(13,6,temp1,temp2,&reltol))
		{
			return 1;
		}

		//Getting matrix representing absolute tolerance
		if(getFixedSizeDoubleMatrixInList(13,8,temp1,temp2,&abstol))
		{
			return 1;
		}

		//Getting matrix representing feastol_inacc
		if(getFixedSizeDoubleMatrixInList(13,10,temp1,temp2,&feastol_inacc))
		{
			return 1;
		}

		//Getting matrix representing reltol_inacc
		if(getFixedSizeDoubleMatrixInList(13,12,temp1,temp2,&reltol_inacc))
		{
			return 1;
		}

		//Getting matrix representing abstol_inacc
		if(getFixedSizeDoubleMatrixInList(13,14,temp1,temp2,&abstol_inacc))
		{
			return 1;
		}
		//Getting matrix representing verbose mode
		if(getFixedSizeDoubleMatrixInList(13,16,temp1,temp2,&verbose))
		{
			return 1;
		}

		// /ecos_bb options
		// if(getFixedSizeDoubleMatrixInList(13,18,temp1,temp2,&mi_max_iter))
		// {
		// 	return 1;
		// }

		// if(getFixedSizeDoubleMatrixInList(13,20,temp1,temp2,&mi_int_tol))
		// {
		// 	return 1;
		// }

		// if(getFixedSizeDoubleMatrixInList(13,22,temp1,temp2,&mi_rel_eps))
		// {
		// 	return 1;
		// }

		// if(getFixedSizeDoubleMatrixInList(13,24,temp1,temp2,&mi_abs_eps))
		// {
		// 	return 1;
		// }

		// //Getting integer representing number of inequality constraints
		if(getIntFromScilab(14, &mptr))
		{
			return 1;
		}
		
		//Getting integer representing the number of primal variable 
		if(getIntFromScilab(15, &nptr))
		{
			return 1;
		}

		//Getting integer representing the number of equality constraints
		if(getIntFromScilab(16, &pptr))
		{
			return 1;
		}

		//Getting integer representing the number of second order cones
		if(getIntFromScilab(17, &nconesptr))
		{
			return 1;
		}

		// Intializing ECOS variables
		pfloat *Gpr=NULL,*c=NULL,*h=NULL,*Apr=NULL,*b=NULL;
		idxint *Gjc=NULL,*Gir=NULL,*Ajc=NULL,*Air=NULL, n=0, m=0, p=0, l=0, *q=NULL,ncones=0, e=0;
		double *x = NULL, *y = NULL, *s = NULL, *z = NULL;
		const char* infostring;
		
		c = (pfloat *)cptr;

		if (Gpr_rows != 0)
		{
			Gpr = (pfloat *) Gprptr;
			Gjc = int2idxint(Gjcptr,Gjc_rows);
			Gir = int2idxint(Girptr,Gir_rows);
			h = (pfloat *) hptr;
		}		

		if (Apr_rows != 0)
		{
			Apr = (pfloat *)Aprptr;
			Ajc = int2idxint(Ajcptr,Ajc_rows);
			Air = int2idxint(Airptr,Air_rows);
			b = (pfloat *)bptr;
		}
		
		n = (idxint)nptr;
		m = (idxint)mptr;
		p = (idxint)pptr;
		l = (idxint)lptr;
		ncones = (idxint)nconesptr;

		if (ncones != 0)
		{
			q = int2idxint(qptr,ncones);
		}

		e = (idxint)eptr;


		pwork* input_struct;

		// for (int i = 0; i < Gpr_rows; i++)
		// {
		// 	sciprint("%f\t",Gpr[i]);
		// }
		// sciprint("\n");

		// for (int i = 0; i < Gjc_rows; i++)
		// {
		// 	sciprint("%d\t",Gjc[i]);
		// }
		// sciprint("\n");

		// for (int i = 0; i < Gir_rows; i++)
		// {
		// 	sciprint("%d\t",Gir[i]);
		// }
		// sciprint("\n");
		// sciprint("%d\t",n);sciprint("%d\t",m);sciprint("%d\t",p);sciprint("%d\t",l);sciprint("%d\t",ncones);

		// setup ecos 
		input_struct = ECOS_setup(n, m, p, l, ncones, q, e, Gpr, Gjc, Gir, Apr, Ajc, Air,c, h, b);

		// if setup is successful
		if( input_struct != NULL ){
			// passing options to ecos
			input_struct->stgs->feastol 			= (pfloat) *feastol;
			input_struct->stgs->reltol 				= (pfloat) *reltol;
			input_struct->stgs->abstol 				= (pfloat) *abstol;
			input_struct->stgs->feastol_inacc 		= (pfloat) *feastol_inacc;
			input_struct->stgs->abstol_inacc 		= (pfloat) *abstol_inacc;
			input_struct->stgs->reltol_inacc 		= (pfloat) *reltol_inacc;
			input_struct->stgs->maxit 				= (idxint) maxit;
			input_struct->stgs->verbose 			= (idxint) verbose;
       		
       		// solve
	        exitflag = ECOS_solve(input_struct); 

	        // output
	        x = input_struct->x;
			y = input_struct->y;
			s = input_struct->s;
			z = input_struct->z;

			double pcost = (double)input_struct->info->pcost;
			double dcost = (double)input_struct->info->dcost;
			double pres = (double)input_struct->info->pres;
			double dres = (double)input_struct->info->dres;
			double pinf = (double)input_struct->info->pinf;
			double dinf = (double)input_struct->info->dinf;
			double pinfres = (double)input_struct->info->pinfres;
			double dinfres = (double)input_struct->info->dinfres;
			double gap = (double)input_struct->info->gap;
			double rel_gap = (double)input_struct->info->relgap;
			double iter = (double)input_struct->info->iter;

			// #if PROFILING > 1
			// 	double tkktcreate = (double)input_struct->info->tkktcreate;
			// 	double tkktsolve = (double)input_struct->info->tkktsolve;
			// 	double tkktfactor = (double)input_struct->info->tfactor;
			// 	double torder = (double)input_struct->info->torder;
			// 	double ttranspose = (double)input_struct->info->ttranspose;
			// #else
				double tsetup = (double)input_struct->info->tsetup;
				double tsolve = (double)input_struct->info->tsolve;
			// #endif

			// #if PROFILING > 1
			// 	double info[19]={tsetup,tsolve,pcost,dcost,pres,dres,pinf,dinf,pinfres,dinfres,gap,rel_gap,iter,(double) exitflag,tkktcreate,tkktsolve,tfactor,torder,ttranspose};
			// 	int o = 19;
			// #else
				double info[14]={tsetup,tsolve,pcost,dcost,pres,dres,pinf,dinf,pinfres,dinfres,gap,rel_gap,iter,(double) exitflag};
			// 	int o = 14;
			// #endif

			if (returnDoubleMatrixToScilab(1, n, 1, x))
			{
				return 1;
			}

			if (returnDoubleMatrixToScilab(2, p, 1, y))
			{
				return 1;
			}

			if (returnDoubleMatrixToScilab(3, 14, 1, info))
			{
				return 1;
			}

			if (returnDoubleMatrixToScilab(4, m, 1, s))
			{
				return 1;
			}

			if (returnDoubleMatrixToScilab(5, m, 1, z))
			{
				return 1;
			}

			/* clean up memory */
			ECOS_cleanup(input_struct, 0);
	    }
	    else{
	    	// if setup fails
	    	exitflag =-8;
			
			// #if PROFILING > 1
			// 	double info[19]={0,0,0,0,0,0,0,0,0,0,0,0,0,(double) exitflag,0,0,0,0,0};
			// 	int o = 19;
			// #else
				double info[14]={0,0,0,0,0,0,0,0,0,0,0,0,0,(double) exitflag};
			// 	int o = 14;
			// #endif

			if (returnDoubleMatrixToScilab(1, 0, 0, x))
			{
				return 1;
			}

			if (returnDoubleMatrixToScilab(2, 0, 0, y))
			{
				return 1;
			}

			if (returnDoubleMatrixToScilab(3, 14, 1, info))
			{
				return 1;
			}

			if (returnDoubleMatrixToScilab(4, 0, 0, s))
			{
				return 1;
			}

			if (returnDoubleMatrixToScilab(5, 0, 0, z))
			{
				return 1;
			}
	    }	

	}
}