5234
|
1 /* |
|
2 |
|
3 Copyright (C) 2005 Nicolo' Giorgetti |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
5232
|
26 |
|
27 #include <cfloat> |
|
28 #include <csetjmp> |
|
29 #include <ctime> |
|
30 |
5234
|
31 #include "defun-dld.h" |
|
32 #include "error.h" |
5235
|
33 #include "gripes.h" |
|
34 #include "oct-map.h" |
5234
|
35 #include "oct-obj.h" |
5235
|
36 #include "pager.h" |
|
37 |
|
38 #if defined (HAVE_GLPK) |
5232
|
39 |
5234
|
40 extern "C" { |
|
41 #include <glpk.h> |
5232
|
42 } |
|
43 |
|
44 #define NIntP 17 |
|
45 #define NRealP 10 |
|
46 |
5234
|
47 int lpxIntParam[NIntP] = { |
5240
|
48 0, |
5234
|
49 1, |
|
50 0, |
|
51 1, |
|
52 0, |
|
53 -1, |
|
54 0, |
|
55 200, |
|
56 1, |
|
57 2, |
|
58 0, |
|
59 1, |
|
60 0, |
|
61 0, |
|
62 2, |
|
63 2, |
|
64 1 |
5232
|
65 }; |
|
66 |
5234
|
67 int IParam[NIntP] = { |
|
68 LPX_K_MSGLEV, |
|
69 LPX_K_SCALE, |
|
70 LPX_K_DUAL, |
|
71 LPX_K_PRICE, |
|
72 LPX_K_ROUND, |
|
73 LPX_K_ITLIM, |
|
74 LPX_K_ITCNT, |
|
75 LPX_K_OUTFRQ, |
|
76 LPX_K_MPSINFO, |
|
77 LPX_K_MPSOBJ, |
|
78 LPX_K_MPSORIG, |
|
79 LPX_K_MPSWIDE, |
|
80 LPX_K_MPSFREE, |
|
81 LPX_K_MPSSKIP, |
|
82 LPX_K_BRANCH, |
|
83 LPX_K_BTRACK, |
|
84 LPX_K_PRESOL |
5232
|
85 }; |
|
86 |
|
87 |
5234
|
88 double lpxRealParam[NRealP] = { |
|
89 0.07, |
|
90 1e-7, |
|
91 1e-7, |
|
92 1e-9, |
|
93 -DBL_MAX, |
|
94 DBL_MAX, |
|
95 -1.0, |
|
96 0.0, |
|
97 1e-6, |
|
98 1e-7 |
5232
|
99 }; |
|
100 |
5234
|
101 int RParam[NRealP] = { |
|
102 LPX_K_RELAX, |
|
103 LPX_K_TOLBND, |
|
104 LPX_K_TOLDJ, |
|
105 LPX_K_TOLPIV, |
|
106 LPX_K_OBJLL, |
|
107 LPX_K_OBJUL, |
|
108 LPX_K_TMLIM, |
|
109 LPX_K_OUTDLY, |
|
110 LPX_K_TOLINT, |
|
111 LPX_K_TOLOBJ |
5232
|
112 }; |
|
113 |
|
114 jmp_buf mark; //-- Address for long jump to jump to |
5234
|
115 int fperr; //-- Global error number |
5232
|
116 |
|
117 |
5234
|
118 int |
5235
|
119 glpk_fault_hook (void * /* info */, char *msg) |
5234
|
120 { |
5240
|
121 error ("CRITICAL ERROR in GLPK: %s", msg); |
5234
|
122 longjmp (mark, -1); |
5232
|
123 } |
|
124 |
5234
|
125 int |
5235
|
126 glpk_print_hook (void * /* info */, char *msg) |
5232
|
127 { |
5240
|
128 message (0, "%s\n", msg); |
5234
|
129 return 1; |
5232
|
130 } |
|
131 |
|
132 |
5234
|
133 int |
|
134 glpk (int sense, int n, int m, double *c, int nz, int *rn, int *cn, |
|
135 double *a, double *b, char *ctype, int *freeLB, double *lb, |
|
136 int *freeUB, double *ub, int *vartype, int isMIP, int lpsolver, |
|
137 int save_pb, double *xmin, double *fmin, double *status, |
|
138 double *lambda, double *redcosts, double *time, double *mem) |
5232
|
139 { |
5240
|
140 int errnum; |
5234
|
141 int typx = 0; |
|
142 int method; |
|
143 |
|
144 clock_t t_start = clock(); |
|
145 |
|
146 lib_set_fault_hook (NULL, glpk_fault_hook); |
5232
|
147 |
5234
|
148 if (lpxIntParam[0] > 1) |
|
149 lib_set_print_hook (NULL, glpk_print_hook); |
|
150 |
|
151 LPX *lp = lpx_create_prob (); |
|
152 |
5232
|
153 |
5234
|
154 //-- Set the sense of optimization |
|
155 if (sense == 1) |
|
156 lpx_set_obj_dir (lp, LPX_MIN); |
|
157 else |
|
158 lpx_set_obj_dir (lp, LPX_MAX); |
|
159 |
|
160 //-- If the problem has integer structural variables switch to MIP |
|
161 if (isMIP) |
|
162 lpx_set_class (lp, LPX_MIP); |
5232
|
163 |
5234
|
164 lpx_add_cols (lp, n); |
|
165 for (int i = 0; i < n; i++) |
|
166 { |
5232
|
167 //-- Define type of the structural variables |
5234
|
168 if (! freeLB[i] && ! freeUB[i]) |
|
169 lpx_set_col_bnds (lp, i+1, LPX_DB, lb[i], ub[i]); |
|
170 else |
|
171 { |
|
172 if (! freeLB[i] && freeUB[i]) |
|
173 lpx_set_col_bnds (lp, i+1, LPX_LO, lb[i], ub[i]); |
|
174 else |
|
175 { |
|
176 if (freeLB[i] && ! freeUB[i]) |
|
177 lpx_set_col_bnds (lp, i+1, LPX_UP, lb[i], ub[i]); |
|
178 else |
|
179 lpx_set_col_bnds (lp, i+1, LPX_FR, lb[i], ub[i]); |
|
180 } |
|
181 } |
|
182 |
|
183 // -- Set the objective coefficient of the corresponding |
5232
|
184 // -- structural variable. No constant term is assumed. |
5234
|
185 lpx_set_obj_coef(lp,i+1,c[i]); |
5232
|
186 |
5234
|
187 if (isMIP) |
|
188 lpx_set_col_kind (lp, i+1, vartype[i]); |
|
189 } |
|
190 |
|
191 lpx_add_rows (lp, m); |
|
192 |
|
193 for (int i = 0; i < m; i++) |
|
194 { |
5232
|
195 /* If the i-th row has no lower bound (types F,U), the |
|
196 corrispondent parameter will be ignored. |
|
197 If the i-th row has no upper bound (types F,L), the corrispondent |
|
198 parameter will be ignored. |
|
199 If the i-th row is of S type, the i-th LB is used, but |
|
200 the i-th UB is ignored. |
|
201 */ |
5234
|
202 |
|
203 switch (ctype[i]) |
|
204 { |
|
205 case 'F': |
|
206 typx = LPX_FR; |
|
207 break; |
|
208 |
|
209 case 'U': |
|
210 typx = LPX_UP; |
|
211 break; |
|
212 |
|
213 case 'L': |
|
214 typx = LPX_LO; |
|
215 break; |
5232
|
216 |
5234
|
217 case 'S': |
|
218 typx = LPX_FX; |
|
219 break; |
5232
|
220 |
5234
|
221 case 'D': |
|
222 typx = LPX_DB; |
|
223 break; |
|
224 } |
|
225 |
|
226 lpx_set_row_bnds (lp, i+1, typx, b[i], b[i]); |
|
227 |
|
228 } |
|
229 |
|
230 lpx_load_matrix (lp, nz, rn, cn, a); |
5232
|
231 |
5234
|
232 if (save_pb) |
|
233 { |
|
234 if (lpx_write_cpxlp (lp, "outpb.lp") != 0) |
|
235 { |
5240
|
236 error ("__glpk__: unable to write problem"); |
5234
|
237 longjmp (mark, -1); |
|
238 } |
|
239 } |
|
240 |
|
241 //-- scale the problem data (if required) |
|
242 //-- if (scale && (!presol || method == 1)) lpx_scale_prob(lp); |
|
243 //-- LPX_K_SCALE=IParam[1] LPX_K_PRESOL=IParam[16] |
|
244 if (lpxIntParam[1] && (! lpxIntParam[16] || lpsolver != 1)) |
|
245 lpx_scale_prob (lp); |
|
246 |
|
247 //-- build advanced initial basis (if required) |
|
248 if (lpsolver == 1 && ! lpxIntParam[16]) |
|
249 lpx_adv_basis (lp); |
|
250 |
|
251 for(int i = 0; i < NIntP; i++) |
|
252 lpx_set_int_parm (lp, IParam[i], lpxIntParam[i]); |
5232
|
253 |
5234
|
254 for (int i = 0; i < NRealP; i++) |
|
255 lpx_set_real_parm (lp, RParam[i], lpxRealParam[i]); |
|
256 |
|
257 if (lpsolver == 1) |
|
258 method = 'S'; |
|
259 else |
|
260 method = 'T'; |
5232
|
261 |
5234
|
262 switch (method) |
|
263 { |
|
264 case 'S': |
|
265 { |
|
266 if (isMIP) |
|
267 { |
|
268 method = 'I'; |
5240
|
269 errnum = lpx_simplex (lp); |
|
270 errnum = lpx_integer (lp); |
5234
|
271 } |
|
272 else |
5240
|
273 errnum = lpx_simplex(lp); |
5234
|
274 } |
5232
|
275 break; |
5234
|
276 |
|
277 case 'T': |
5240
|
278 errnum = lpx_interior(lp); |
5234
|
279 break; |
|
280 |
|
281 default: |
|
282 insist (method != method); |
|
283 } |
|
284 |
5240
|
285 /* errnum assumes the following results: |
|
286 errnum = 0 <=> No errors |
|
287 errnum = 1 <=> Iteration limit exceeded. |
|
288 errnum = 2 <=> Numerical problems with basis matrix. |
5234
|
289 */ |
5240
|
290 if (errnum == LPX_E_OK) |
5234
|
291 { |
|
292 if (isMIP) |
|
293 { |
|
294 *status = static_cast<double> (lpx_mip_status (lp)); |
|
295 *fmin = lpx_mip_obj_val (lp); |
|
296 } |
|
297 else |
|
298 { |
|
299 if (lpsolver == 1) |
|
300 { |
|
301 *status = static_cast<double> (lpx_get_status (lp)); |
|
302 *fmin = lpx_get_obj_val (lp); |
|
303 } |
|
304 else |
|
305 { |
|
306 *status = static_cast<double> (lpx_ipt_status (lp)); |
|
307 *fmin = lpx_ipt_obj_val (lp); |
|
308 } |
|
309 } |
5232
|
310 |
5234
|
311 if (isMIP) |
|
312 { |
|
313 for (int i = 0; i < n; i++) |
|
314 xmin[i] = lpx_mip_col_val (lp, i+1); |
|
315 } |
|
316 else |
|
317 { |
|
318 /* Primal values */ |
|
319 for (int i = 0; i < n; i++) |
|
320 { |
|
321 if (lpsolver == 1) |
|
322 xmin[i] = lpx_get_col_prim (lp, i+1); |
|
323 else |
|
324 xmin[i] = lpx_ipt_col_prim (lp, i+1); |
|
325 } |
5232
|
326 |
5234
|
327 /* Dual values */ |
|
328 for (int i = 0; i < m; i++) |
|
329 { |
|
330 if (lpsolver == 1) |
|
331 lambda[i] = lpx_get_row_dual (lp, i+1); |
|
332 else |
|
333 lambda[i] = lpx_ipt_row_dual (lp, i+1); |
|
334 } |
|
335 |
|
336 /* Reduced costs */ |
|
337 for (int i = 0; i < lpx_get_num_cols (lp); i++) |
|
338 { |
|
339 if (lpsolver == 1) |
|
340 redcosts[i] = lpx_get_col_dual (lp, i+1); |
|
341 else |
|
342 redcosts[i] = lpx_ipt_col_dual (lp, i+1); |
|
343 } |
|
344 } |
|
345 |
|
346 *time = static_cast<double> (clock () - t_start) / CLOCKS_PER_SEC; |
|
347 *mem = static_cast<double> (lib_env_ptr () -> mem_tpeak); |
|
348 |
|
349 lpx_delete_prob (lp); |
|
350 return 0; |
|
351 } |
|
352 |
|
353 lpx_delete_prob (lp); |
|
354 |
5240
|
355 *status= static_cast<double> (errnum); |
5234
|
356 |
5240
|
357 return errnum; |
5232
|
358 } |
|
359 |
5235
|
360 #endif |
|
361 |
5240
|
362 #define OCTAVE_GLPK_GET_REAL_PARAM(NAME, IDX) \ |
|
363 do \ |
|
364 { \ |
|
365 if (PARAM.contains (NAME)) \ |
|
366 { \ |
|
367 Cell tmp = PARAM.contents (NAME); \ |
|
368 \ |
|
369 if (! tmp.is_empty ()) \ |
|
370 { \ |
|
371 lpxRealParam[IDX] = tmp(0).scalar_value (); \ |
|
372 \ |
|
373 if (error_state) \ |
|
374 { \ |
|
375 error ("glpk: invalid value in param." NAME); \ |
|
376 return retval; \ |
|
377 } \ |
|
378 } \ |
|
379 else \ |
|
380 { \ |
|
381 error ("glpk: invalid value in param." NAME); \ |
|
382 return retval; \ |
|
383 } \ |
|
384 } \ |
|
385 } \ |
|
386 while (0) |
|
387 |
|
388 #define OCTAVE_GLPK_GET_INT_PARAM(NAME, VAL) \ |
|
389 do \ |
|
390 { \ |
|
391 if (PARAM.contains (NAME)) \ |
|
392 { \ |
|
393 Cell tmp = PARAM.contents (NAME); \ |
|
394 \ |
|
395 if (! tmp.is_empty ()) \ |
|
396 { \ |
|
397 VAL = tmp(0).int_value (); \ |
|
398 \ |
|
399 if (error_state) \ |
|
400 { \ |
|
401 error ("glpk: invalid value in param." NAME); \ |
|
402 return retval; \ |
|
403 } \ |
|
404 } \ |
|
405 else \ |
|
406 { \ |
|
407 error ("glpk: invalid value in param." NAME); \ |
|
408 return retval; \ |
|
409 } \ |
|
410 } \ |
|
411 } \ |
|
412 while (0) |
|
413 |
5235
|
414 DEFUN_DLD (__glpk__, args, , |
5234
|
415 "__glpk__: internal interface for the GLPK library.\n\ |
|
416 You should be using using glpk instead") |
5232
|
417 { |
5234
|
418 // The list of values to return. See the declaration in oct-obj.h |
|
419 octave_value_list retval; |
5232
|
420 |
5235
|
421 #if defined (HAVE_GLPK) |
|
422 |
5234
|
423 int nrhs = args.length (); |
|
424 |
5240
|
425 if (nrhs != 9) |
5234
|
426 { |
5240
|
427 print_usage ("__glpk__"); |
5232
|
428 return retval; |
5234
|
429 } |
|
430 |
5237
|
431 //-- 1nd Input. A column array containing the objective function |
|
432 //-- coefficients. |
|
433 int mrowsc = args(0).rows(); |
5234
|
434 |
5237
|
435 Matrix C (args(0).matrix_value ()); |
5240
|
436 |
|
437 if (error_state) |
|
438 { |
|
439 error ("__glpk__: invalid value of C"); |
|
440 return retval; |
|
441 } |
|
442 |
5234
|
443 double *c = C.fortran_vec (); |
5232
|
444 |
5237
|
445 //-- 2nd Input. A matrix containing the constraints coefficients. |
5234
|
446 // If matrix A is NOT a sparse matrix |
|
447 // if(!mxIsSparse(A_IN)){ |
5237
|
448 Matrix A (args(1).matrix_value ()); // get the matrix |
5240
|
449 |
|
450 if (error_state) |
|
451 { |
|
452 error ("__glpk__: invalid value of A"); |
|
453 return retval; |
|
454 } |
|
455 |
|
456 int mrowsA = A.rows (); |
5234
|
457 Array<int> rn (mrowsA*mrowsc+1); |
|
458 Array<int> cn (mrowsA*mrowsc+1); |
|
459 ColumnVector a (mrowsA*mrowsc+1, 0.0); |
|
460 |
5235
|
461 volatile int nz = 0; |
5234
|
462 for (int i = 0; i < mrowsA; i++) |
|
463 { |
|
464 for (int j = 0; j < mrowsc; j++) |
|
465 { |
|
466 if (A(i,j) != 0) |
|
467 { |
|
468 nz++; |
|
469 rn(nz) = i + 1; |
|
470 cn(nz) = j + 1; |
|
471 a(nz) = A(i,j); |
|
472 } |
|
473 } |
|
474 } |
|
475 |
5232
|
476 // DON'T DELETE THIS PART... REPRESENTS THE SPARSE MATRICES MANIPULATION |
|
477 // }else{ |
|
478 // int i,j; |
|
479 // int *jc,*ir; |
|
480 // double *pr; |
|
481 // int nelc,count,row; |
|
482 // |
|
483 // /* NOTE: nnz is the actual number of nonzeros and is stored as the |
|
484 // last element of the jc array where the size of the jc array is the |
|
485 // number of columns + 1 */ |
|
486 // nz = *(mxGetJc(A_IN) + mrowsc); |
|
487 // jc = mxGetJc(A_IN); |
|
488 // ir = mxGetIr(A_IN); |
|
489 // pr = mxGetPr(A_IN); |
|
490 // |
|
491 // rn=(int *)calloc(nz+1,sizeof(int)); |
|
492 // cn=(int *)calloc(nz+1,sizeof(int)); |
|
493 // a=(double *)calloc(nz+1,sizeof(double)); |
|
494 // |
|
495 // count=0; row=0; |
|
496 // for(i=1;i<=mrowsc;i++){ |
|
497 // nelc=jc[i]-jc[i-1]; |
|
498 // for(j=0;j<nelc;j++){ |
|
499 // count++; |
|
500 // rn[count]=ir[row]+1; |
|
501 // cn[count]=i; |
|
502 // a[count]=pr[row]; |
|
503 // row++; |
|
504 // } |
|
505 // } |
|
506 // } |
|
507 |
5237
|
508 //-- 3rd Input. A column array containing the right-hand side value |
5234
|
509 // for each constraint in the constraint matrix. |
5237
|
510 Matrix B (args(2).matrix_value ()); |
5240
|
511 |
|
512 if (error_state) |
|
513 { |
|
514 error ("__glpk__: invalid value of b"); |
|
515 return retval; |
|
516 } |
|
517 |
5234
|
518 double *b = B.fortran_vec (); |
|
519 |
5237
|
520 //-- 4th Input. An array of length mrowsc containing the lower |
5234
|
521 //-- bound on each of the variables. |
5237
|
522 Matrix LB (args(3).matrix_value ()); |
5240
|
523 |
|
524 if (error_state) |
|
525 { |
|
526 error ("__glpk__: invalid value of lb"); |
|
527 return retval; |
|
528 } |
|
529 |
5234
|
530 double *lb = LB.fortran_vec (); |
|
531 |
|
532 //-- LB argument, default: Free |
|
533 Array<int> freeLB (mrowsc); |
|
534 for (int i = 0; i < mrowsc; i++) |
|
535 { |
|
536 if (isinf (lb[i])) |
|
537 { |
|
538 freeLB(i) = 1; |
|
539 lb[i] = -octave_Inf; |
|
540 } |
|
541 else |
|
542 freeLB(i) = 0; |
|
543 } |
|
544 |
5237
|
545 //-- 5th Input. An array of at least length numcols containing the upper |
5234
|
546 //-- bound on each of the variables. |
5237
|
547 Matrix UB (args(4).matrix_value ()); |
5234
|
548 |
5240
|
549 if (error_state) |
|
550 { |
|
551 error ("__glpk__: invalid value of ub"); |
|
552 return retval; |
|
553 } |
|
554 |
5234
|
555 double *ub = UB.fortran_vec (); |
5232
|
556 |
5234
|
557 Array<int> freeUB (mrowsc); |
|
558 for (int i = 0; i < mrowsc; i++) |
|
559 { |
|
560 if (isinf (ub[i])) |
|
561 { |
|
562 freeUB(i) = 1; |
|
563 ub[i] = octave_Inf; |
|
564 } |
|
565 else |
|
566 freeUB(i) = 0; |
|
567 } |
|
568 |
5237
|
569 //-- 6th Input. A column array containing the sense of each constraint |
|
570 //-- in the constraint matrix. |
|
571 charMatrix CTYPE (args(5).char_matrix_value ()); |
5240
|
572 |
|
573 if (error_state) |
|
574 { |
|
575 error ("__glpk__: invalid value of ctype"); |
|
576 return retval; |
|
577 } |
|
578 |
5237
|
579 char *ctype = CTYPE.fortran_vec (); |
|
580 |
|
581 //-- 7th Input. A column array containing the types of the variables. |
|
582 charMatrix VTYPE (args(6).char_matrix_value ()); |
5232
|
583 |
5240
|
584 if (error_state) |
|
585 { |
|
586 error ("__glpk__: invalid value of vtype"); |
|
587 return retval; |
|
588 } |
|
589 |
5234
|
590 Array<int> vartype (mrowsc); |
5235
|
591 volatile int isMIP = 0; |
5234
|
592 for (int i = 0; i < mrowsc ; i++) |
|
593 { |
|
594 if (VTYPE(i,0) == 'I') |
|
595 { |
|
596 isMIP = 1; |
|
597 vartype(i) = LPX_IV; |
|
598 } |
|
599 else |
5235
|
600 vartype(i) = LPX_CV; |
5234
|
601 } |
|
602 |
5237
|
603 //-- 8th Input. Sense of optimization. |
|
604 volatile int sense; |
|
605 double SENSE = args(7).scalar_value (); |
5240
|
606 |
|
607 if (error_state) |
|
608 { |
|
609 error ("__glpk__: invalid value of sense"); |
|
610 return retval; |
|
611 } |
|
612 |
5237
|
613 if (SENSE >= 0) |
|
614 sense = 1; |
|
615 else |
|
616 sense = -1; |
|
617 |
5234
|
618 //-- 9th Input. A structure containing the control parameters. |
|
619 Octave_map PARAM = args(8).map_value (); |
|
620 |
5240
|
621 if (error_state) |
|
622 { |
|
623 error ("__glpk__: invalid value of param"); |
|
624 return retval; |
|
625 } |
|
626 |
5234
|
627 //-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
628 //-- Integer parameters |
|
629 //-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
630 |
|
631 //-- Level of messages output by the solver |
5240
|
632 OCTAVE_GLPK_GET_INT_PARAM ("msglev", lpxIntParam[0]); |
|
633 if (lpxIntParam[0] < 0 || lpxIntParam[0] > 3) |
5234
|
634 { |
5240
|
635 error ("__glpk__: param.msglev must be 0 (no output [default]) or 1 (error messages only) or 2 (normal output) or 3 (full output)"); |
|
636 return retval; |
|
637 } |
5232
|
638 |
5234
|
639 //-- scaling option |
5240
|
640 OCTAVE_GLPK_GET_INT_PARAM ("scale", lpxIntParam[1]); |
|
641 if (lpxIntParam[1] < 0 || lpxIntParam[1] > 2) |
5234
|
642 { |
5240
|
643 error ("__glpk__: param.scale must be 0 (no scaling) or 1 (equilibration scaling [default]) or 2 (geometric mean scaling)"); |
|
644 return retval; |
5234
|
645 } |
5232
|
646 |
5234
|
647 //-- Dual dimplex option |
5240
|
648 OCTAVE_GLPK_GET_INT_PARAM ("dual", lpxIntParam[2]); |
|
649 if (lpxIntParam[2] < 0 || lpxIntParam[2] > 1) |
5234
|
650 { |
5240
|
651 error ("__glpk__: param.dual must be 0 (do NOT use dual simplex [default]) or 1 (use dual simplex)"); |
|
652 return retval; |
5234
|
653 } |
5232
|
654 |
5234
|
655 //-- Pricing option |
5240
|
656 OCTAVE_GLPK_GET_INT_PARAM ("price", lpxIntParam[3]); |
|
657 if (lpxIntParam[3] < 0 || lpxIntParam[3] > 1) |
5234
|
658 { |
5240
|
659 error ("__glpk__: param.price must be 0 (textbook pricing) or 1 (steepest edge pricing [default])"); |
|
660 return retval; |
|
661 } |
5232
|
662 |
5234
|
663 //-- Solution rounding option |
5240
|
664 OCTAVE_GLPK_GET_INT_PARAM ("round", lpxIntParam[4]); |
|
665 if (lpxIntParam[4] < 0 || lpxIntParam[4] > 1) |
5234
|
666 { |
5240
|
667 error ("__glpk__: param.round must be 0 (report all primal and dual values [default]) or 1 (replace tiny primal and dual values by exact zero)"); |
|
668 return retval; |
5234
|
669 } |
|
670 |
|
671 //-- Simplex iterations limit |
5240
|
672 OCTAVE_GLPK_GET_INT_PARAM ("itlim", lpxIntParam[5]); |
5232
|
673 |
5234
|
674 //-- Simplex iterations count |
5240
|
675 OCTAVE_GLPK_GET_INT_PARAM ("itcnt", lpxIntParam[6]); |
5234
|
676 |
|
677 //-- Output frequency, in iterations |
5240
|
678 OCTAVE_GLPK_GET_INT_PARAM ("outfrq", lpxIntParam[7]); |
5234
|
679 |
|
680 //-- Branching heuristic option |
5240
|
681 OCTAVE_GLPK_GET_INT_PARAM ("branch", lpxIntParam[14]); |
|
682 if (lpxIntParam[14] < 0 || lpxIntParam[14] > 2) |
5234
|
683 { |
5240
|
684 error ("__glpk__: param.branch must be (MIP only) 0 (branch on first variable) or 1 (branch on last variable) or 2 (branch using a heuristic by Driebeck and Tomlin [default]"); |
|
685 return retval; |
|
686 } |
5232
|
687 |
5234
|
688 //-- Backtracking heuristic option |
5240
|
689 OCTAVE_GLPK_GET_INT_PARAM ("btrack", lpxIntParam[15]); |
|
690 if (lpxIntParam[15] < 0 || lpxIntParam[15] > 2) |
5234
|
691 { |
5240
|
692 error ("__glpk__: param.btrack must be (MIP only) 0 (depth first search) or 1 (breadth first search) or 2 (backtrack using the best projection heuristic [default]"); |
|
693 return retval; |
|
694 } |
5232
|
695 |
5234
|
696 //-- Presolver option |
5240
|
697 OCTAVE_GLPK_GET_INT_PARAM ("presol", lpxIntParam[16]); |
|
698 if (lpxIntParam[16] < 0 || lpxIntParam[16] > 1) |
5234
|
699 { |
5240
|
700 error ("__glpk__: param.presol must be 0 (do NOT use LP presolver) or 1 (use LP presolver [default])"); |
|
701 return retval; |
5234
|
702 } |
|
703 |
5237
|
704 //-- LPsolver option |
|
705 volatile int lpsolver = 1; |
5240
|
706 OCTAVE_GLPK_GET_INT_PARAM ("lpsolver", lpsolver); |
|
707 if (lpsolver < 1 || lpsolver > 2) |
5237
|
708 { |
5240
|
709 error ("__glpk__: param.lpsolver must be 1 (simplex method) or 2 (interior point method)"); |
|
710 return retval; |
5237
|
711 } |
|
712 |
|
713 //-- Save option |
|
714 volatile int save_pb = 0; |
5240
|
715 OCTAVE_GLPK_GET_INT_PARAM ("save", save_pb); |
|
716 save_pb = save_pb != 0; |
5237
|
717 |
5234
|
718 //-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
719 //-- Real parameters |
|
720 //-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
721 |
|
722 //-- Ratio test option |
5240
|
723 OCTAVE_GLPK_GET_REAL_PARAM ("relax", 0); |
5232
|
724 |
5234
|
725 //-- Relative tolerance used to check if the current basic solution |
|
726 //-- is primal feasible |
5240
|
727 OCTAVE_GLPK_GET_REAL_PARAM ("tolbnd", 1); |
5234
|
728 |
|
729 //-- Absolute tolerance used to check if the current basic solution |
|
730 //-- is dual feasible |
5240
|
731 OCTAVE_GLPK_GET_REAL_PARAM ("toldj", 2); |
5232
|
732 |
5234
|
733 //-- Relative tolerance used to choose eligible pivotal elements of |
|
734 //-- the simplex table in the ratio test |
5240
|
735 OCTAVE_GLPK_GET_REAL_PARAM ("tolpiv", 3); |
5234
|
736 |
5240
|
737 OCTAVE_GLPK_GET_REAL_PARAM ("objll", 4); |
5234
|
738 |
5240
|
739 OCTAVE_GLPK_GET_REAL_PARAM ("objul", 5); |
5232
|
740 |
5240
|
741 OCTAVE_GLPK_GET_REAL_PARAM ("tmlim", 6); |
5234
|
742 |
5240
|
743 OCTAVE_GLPK_GET_REAL_PARAM ("outdly", 7); |
5234
|
744 |
5240
|
745 OCTAVE_GLPK_GET_REAL_PARAM ("tolint", 8); |
5234
|
746 |
5240
|
747 OCTAVE_GLPK_GET_REAL_PARAM ("tolobj", 9); |
5234
|
748 |
|
749 //-- Assign pointers to the output parameters |
|
750 ColumnVector xmin (mrowsc); |
|
751 ColumnVector fmin (1); |
|
752 ColumnVector status (1); |
|
753 ColumnVector lambda (mrowsA); |
|
754 ColumnVector redcosts (mrowsc); |
|
755 ColumnVector time (1); |
|
756 ColumnVector mem (1); |
5232
|
757 |
5234
|
758 int jmpret = setjmp (mark); |
5235
|
759 |
5234
|
760 if (jmpret == 0) |
5235
|
761 glpk (sense, mrowsc, mrowsA, c, nz, rn.fortran_vec (), |
|
762 cn.fortran_vec (), a.fortran_vec (), b, ctype, |
|
763 freeLB.fortran_vec (), lb, freeUB.fortran_vec (), |
|
764 ub, vartype.fortran_vec (), isMIP, lpsolver, |
|
765 save_pb, xmin.fortran_vec (), fmin.fortran_vec (), |
|
766 status.fortran_vec (), lambda.fortran_vec (), |
|
767 redcosts.fortran_vec (), time.fortran_vec (), |
|
768 mem.fortran_vec ()); |
5232
|
769 |
5234
|
770 Octave_map extra; |
|
771 |
5238
|
772 if (! isMIP) |
|
773 { |
|
774 extra.assign ("lambda", octave_value (lambda)); |
|
775 extra.assign ("redcosts", octave_value (redcosts)); |
|
776 } |
|
777 |
5234
|
778 extra.assign ("time", octave_value (time)); |
|
779 extra.assign ("mem", octave_value (mem)); |
5232
|
780 |
5234
|
781 retval(3) = extra; |
|
782 retval(2) = octave_value(status); |
|
783 retval(1) = octave_value(fmin); |
|
784 retval(0) = octave_value(xmin); |
|
785 |
5235
|
786 #else |
|
787 |
|
788 gripe_not_supported ("glpk"); |
|
789 |
|
790 #endif |
|
791 |
5234
|
792 return retval; |
5232
|
793 } |