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