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