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 (); |
5232
|
445 |
5237
|
446 //-- 2nd Input. A matrix containing the constraints coefficients. |
5234
|
447 // If matrix A is NOT a sparse matrix |
|
448 // if(!mxIsSparse(A_IN)){ |
5237
|
449 Matrix A (args(1).matrix_value ()); // get the matrix |
5240
|
450 |
|
451 if (error_state) |
|
452 { |
|
453 error ("__glpk__: invalid value of A"); |
|
454 return retval; |
|
455 } |
|
456 |
|
457 int mrowsA = A.rows (); |
5234
|
458 Array<int> rn (mrowsA*mrowsc+1); |
|
459 Array<int> cn (mrowsA*mrowsc+1); |
|
460 ColumnVector a (mrowsA*mrowsc+1, 0.0); |
|
461 |
5235
|
462 volatile int nz = 0; |
5234
|
463 for (int i = 0; i < mrowsA; i++) |
|
464 { |
|
465 for (int j = 0; j < mrowsc; j++) |
|
466 { |
|
467 if (A(i,j) != 0) |
|
468 { |
|
469 nz++; |
|
470 rn(nz) = i + 1; |
|
471 cn(nz) = j + 1; |
|
472 a(nz) = A(i,j); |
|
473 } |
|
474 } |
|
475 } |
|
476 |
5232
|
477 // DON'T DELETE THIS PART... REPRESENTS THE SPARSE MATRICES MANIPULATION |
|
478 // }else{ |
|
479 // int i,j; |
|
480 // int *jc,*ir; |
|
481 // double *pr; |
|
482 // int nelc,count,row; |
|
483 // |
|
484 // /* NOTE: nnz is the actual number of nonzeros and is stored as the |
|
485 // last element of the jc array where the size of the jc array is the |
|
486 // number of columns + 1 */ |
|
487 // nz = *(mxGetJc(A_IN) + mrowsc); |
|
488 // jc = mxGetJc(A_IN); |
|
489 // ir = mxGetIr(A_IN); |
|
490 // pr = mxGetPr(A_IN); |
|
491 // |
|
492 // rn=(int *)calloc(nz+1,sizeof(int)); |
|
493 // cn=(int *)calloc(nz+1,sizeof(int)); |
|
494 // a=(double *)calloc(nz+1,sizeof(double)); |
|
495 // |
|
496 // count=0; row=0; |
|
497 // for(i=1;i<=mrowsc;i++){ |
|
498 // nelc=jc[i]-jc[i-1]; |
|
499 // for(j=0;j<nelc;j++){ |
|
500 // count++; |
|
501 // rn[count]=ir[row]+1; |
|
502 // cn[count]=i; |
|
503 // a[count]=pr[row]; |
|
504 // row++; |
|
505 // } |
|
506 // } |
|
507 // } |
|
508 |
5237
|
509 //-- 3rd Input. A column array containing the right-hand side value |
5234
|
510 // for each constraint in the constraint matrix. |
5237
|
511 Matrix B (args(2).matrix_value ()); |
5240
|
512 |
|
513 if (error_state) |
|
514 { |
|
515 error ("__glpk__: invalid value of b"); |
|
516 return retval; |
|
517 } |
|
518 |
5234
|
519 double *b = B.fortran_vec (); |
|
520 |
5237
|
521 //-- 4th Input. An array of length mrowsc containing the lower |
5234
|
522 //-- bound on each of the variables. |
5237
|
523 Matrix LB (args(3).matrix_value ()); |
5240
|
524 |
|
525 if (error_state) |
|
526 { |
|
527 error ("__glpk__: invalid value of lb"); |
|
528 return retval; |
|
529 } |
|
530 |
5234
|
531 double *lb = LB.fortran_vec (); |
|
532 |
|
533 //-- LB argument, default: Free |
|
534 Array<int> freeLB (mrowsc); |
|
535 for (int i = 0; i < mrowsc; i++) |
|
536 { |
|
537 if (isinf (lb[i])) |
|
538 { |
|
539 freeLB(i) = 1; |
|
540 lb[i] = -octave_Inf; |
|
541 } |
|
542 else |
|
543 freeLB(i) = 0; |
|
544 } |
|
545 |
5237
|
546 //-- 5th Input. An array of at least length numcols containing the upper |
5234
|
547 //-- bound on each of the variables. |
5237
|
548 Matrix UB (args(4).matrix_value ()); |
5234
|
549 |
5240
|
550 if (error_state) |
|
551 { |
|
552 error ("__glpk__: invalid value of ub"); |
|
553 return retval; |
|
554 } |
|
555 |
5234
|
556 double *ub = UB.fortran_vec (); |
5232
|
557 |
5234
|
558 Array<int> freeUB (mrowsc); |
|
559 for (int i = 0; i < mrowsc; i++) |
|
560 { |
|
561 if (isinf (ub[i])) |
|
562 { |
|
563 freeUB(i) = 1; |
|
564 ub[i] = octave_Inf; |
|
565 } |
|
566 else |
|
567 freeUB(i) = 0; |
|
568 } |
|
569 |
5237
|
570 //-- 6th Input. A column array containing the sense of each constraint |
|
571 //-- in the constraint matrix. |
|
572 charMatrix CTYPE (args(5).char_matrix_value ()); |
5240
|
573 |
|
574 if (error_state) |
|
575 { |
|
576 error ("__glpk__: invalid value of ctype"); |
|
577 return retval; |
|
578 } |
|
579 |
5237
|
580 char *ctype = CTYPE.fortran_vec (); |
|
581 |
|
582 //-- 7th Input. A column array containing the types of the variables. |
|
583 charMatrix VTYPE (args(6).char_matrix_value ()); |
5232
|
584 |
5240
|
585 if (error_state) |
|
586 { |
|
587 error ("__glpk__: invalid value of vtype"); |
|
588 return retval; |
|
589 } |
|
590 |
5234
|
591 Array<int> vartype (mrowsc); |
5235
|
592 volatile int isMIP = 0; |
5234
|
593 for (int i = 0; i < mrowsc ; i++) |
|
594 { |
|
595 if (VTYPE(i,0) == 'I') |
|
596 { |
|
597 isMIP = 1; |
|
598 vartype(i) = LPX_IV; |
|
599 } |
|
600 else |
5235
|
601 vartype(i) = LPX_CV; |
5234
|
602 } |
|
603 |
5237
|
604 //-- 8th Input. Sense of optimization. |
|
605 volatile int sense; |
|
606 double SENSE = args(7).scalar_value (); |
5240
|
607 |
|
608 if (error_state) |
|
609 { |
|
610 error ("__glpk__: invalid value of sense"); |
|
611 return retval; |
|
612 } |
|
613 |
5237
|
614 if (SENSE >= 0) |
|
615 sense = 1; |
|
616 else |
|
617 sense = -1; |
|
618 |
5234
|
619 //-- 9th Input. A structure containing the control parameters. |
|
620 Octave_map PARAM = args(8).map_value (); |
|
621 |
5240
|
622 if (error_state) |
|
623 { |
|
624 error ("__glpk__: invalid value of param"); |
|
625 return retval; |
|
626 } |
|
627 |
5234
|
628 //-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
629 //-- Integer parameters |
|
630 //-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
631 |
|
632 //-- Level of messages output by the solver |
5240
|
633 OCTAVE_GLPK_GET_INT_PARAM ("msglev", lpxIntParam[0]); |
|
634 if (lpxIntParam[0] < 0 || lpxIntParam[0] > 3) |
5234
|
635 { |
5240
|
636 error ("__glpk__: param.msglev must be 0 (no output [default]) or 1 (error messages only) or 2 (normal output) or 3 (full output)"); |
|
637 return retval; |
|
638 } |
5232
|
639 |
5234
|
640 //-- scaling option |
5240
|
641 OCTAVE_GLPK_GET_INT_PARAM ("scale", lpxIntParam[1]); |
|
642 if (lpxIntParam[1] < 0 || lpxIntParam[1] > 2) |
5234
|
643 { |
5240
|
644 error ("__glpk__: param.scale must be 0 (no scaling) or 1 (equilibration scaling [default]) or 2 (geometric mean scaling)"); |
|
645 return retval; |
5234
|
646 } |
5232
|
647 |
5234
|
648 //-- Dual dimplex option |
5240
|
649 OCTAVE_GLPK_GET_INT_PARAM ("dual", lpxIntParam[2]); |
|
650 if (lpxIntParam[2] < 0 || lpxIntParam[2] > 1) |
5234
|
651 { |
5240
|
652 error ("__glpk__: param.dual must be 0 (do NOT use dual simplex [default]) or 1 (use dual simplex)"); |
|
653 return retval; |
5234
|
654 } |
5232
|
655 |
5234
|
656 //-- Pricing option |
5240
|
657 OCTAVE_GLPK_GET_INT_PARAM ("price", lpxIntParam[3]); |
|
658 if (lpxIntParam[3] < 0 || lpxIntParam[3] > 1) |
5234
|
659 { |
5240
|
660 error ("__glpk__: param.price must be 0 (textbook pricing) or 1 (steepest edge pricing [default])"); |
|
661 return retval; |
|
662 } |
5232
|
663 |
5234
|
664 //-- Solution rounding option |
5240
|
665 OCTAVE_GLPK_GET_INT_PARAM ("round", lpxIntParam[4]); |
|
666 if (lpxIntParam[4] < 0 || lpxIntParam[4] > 1) |
5234
|
667 { |
5240
|
668 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)"); |
|
669 return retval; |
5234
|
670 } |
|
671 |
|
672 //-- Simplex iterations limit |
5240
|
673 OCTAVE_GLPK_GET_INT_PARAM ("itlim", lpxIntParam[5]); |
5232
|
674 |
5234
|
675 //-- Simplex iterations count |
5240
|
676 OCTAVE_GLPK_GET_INT_PARAM ("itcnt", lpxIntParam[6]); |
5234
|
677 |
|
678 //-- Output frequency, in iterations |
5240
|
679 OCTAVE_GLPK_GET_INT_PARAM ("outfrq", lpxIntParam[7]); |
5234
|
680 |
|
681 //-- Branching heuristic option |
5240
|
682 OCTAVE_GLPK_GET_INT_PARAM ("branch", lpxIntParam[14]); |
|
683 if (lpxIntParam[14] < 0 || lpxIntParam[14] > 2) |
5234
|
684 { |
5240
|
685 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]"); |
|
686 return retval; |
|
687 } |
5232
|
688 |
5234
|
689 //-- Backtracking heuristic option |
5240
|
690 OCTAVE_GLPK_GET_INT_PARAM ("btrack", lpxIntParam[15]); |
|
691 if (lpxIntParam[15] < 0 || lpxIntParam[15] > 2) |
5234
|
692 { |
5240
|
693 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]"); |
|
694 return retval; |
|
695 } |
5232
|
696 |
5234
|
697 //-- Presolver option |
5240
|
698 OCTAVE_GLPK_GET_INT_PARAM ("presol", lpxIntParam[16]); |
|
699 if (lpxIntParam[16] < 0 || lpxIntParam[16] > 1) |
5234
|
700 { |
5240
|
701 error ("__glpk__: param.presol must be 0 (do NOT use LP presolver) or 1 (use LP presolver [default])"); |
|
702 return retval; |
5234
|
703 } |
|
704 |
5237
|
705 //-- LPsolver option |
|
706 volatile int lpsolver = 1; |
5240
|
707 OCTAVE_GLPK_GET_INT_PARAM ("lpsolver", lpsolver); |
|
708 if (lpsolver < 1 || lpsolver > 2) |
5237
|
709 { |
5240
|
710 error ("__glpk__: param.lpsolver must be 1 (simplex method) or 2 (interior point method)"); |
|
711 return retval; |
5237
|
712 } |
|
713 |
|
714 //-- Save option |
|
715 volatile int save_pb = 0; |
5240
|
716 OCTAVE_GLPK_GET_INT_PARAM ("save", save_pb); |
|
717 save_pb = save_pb != 0; |
5237
|
718 |
5234
|
719 //-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
720 //-- Real parameters |
|
721 //-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
|
722 |
|
723 //-- Ratio test option |
5240
|
724 OCTAVE_GLPK_GET_REAL_PARAM ("relax", 0); |
5232
|
725 |
5234
|
726 //-- Relative tolerance used to check if the current basic solution |
|
727 //-- is primal feasible |
5240
|
728 OCTAVE_GLPK_GET_REAL_PARAM ("tolbnd", 1); |
5234
|
729 |
|
730 //-- Absolute tolerance used to check if the current basic solution |
|
731 //-- is dual feasible |
5240
|
732 OCTAVE_GLPK_GET_REAL_PARAM ("toldj", 2); |
5232
|
733 |
5234
|
734 //-- Relative tolerance used to choose eligible pivotal elements of |
|
735 //-- the simplex table in the ratio test |
5240
|
736 OCTAVE_GLPK_GET_REAL_PARAM ("tolpiv", 3); |
5234
|
737 |
5240
|
738 OCTAVE_GLPK_GET_REAL_PARAM ("objll", 4); |
5234
|
739 |
5240
|
740 OCTAVE_GLPK_GET_REAL_PARAM ("objul", 5); |
5232
|
741 |
5240
|
742 OCTAVE_GLPK_GET_REAL_PARAM ("tmlim", 6); |
5234
|
743 |
5240
|
744 OCTAVE_GLPK_GET_REAL_PARAM ("outdly", 7); |
5234
|
745 |
5240
|
746 OCTAVE_GLPK_GET_REAL_PARAM ("tolint", 8); |
5234
|
747 |
5240
|
748 OCTAVE_GLPK_GET_REAL_PARAM ("tolobj", 9); |
5234
|
749 |
|
750 //-- Assign pointers to the output parameters |
|
751 ColumnVector xmin (mrowsc); |
|
752 ColumnVector fmin (1); |
|
753 ColumnVector status (1); |
|
754 ColumnVector lambda (mrowsA); |
|
755 ColumnVector redcosts (mrowsc); |
|
756 ColumnVector time (1); |
|
757 ColumnVector mem (1); |
5232
|
758 |
5234
|
759 int jmpret = setjmp (mark); |
5235
|
760 |
5234
|
761 if (jmpret == 0) |
5235
|
762 glpk (sense, mrowsc, mrowsA, c, nz, rn.fortran_vec (), |
|
763 cn.fortran_vec (), a.fortran_vec (), b, ctype, |
|
764 freeLB.fortran_vec (), lb, freeUB.fortran_vec (), |
|
765 ub, vartype.fortran_vec (), isMIP, lpsolver, |
|
766 save_pb, xmin.fortran_vec (), fmin.fortran_vec (), |
|
767 status.fortran_vec (), lambda.fortran_vec (), |
|
768 redcosts.fortran_vec (), time.fortran_vec (), |
|
769 mem.fortran_vec ()); |
5232
|
770 |
5234
|
771 Octave_map extra; |
|
772 |
5238
|
773 if (! isMIP) |
|
774 { |
|
775 extra.assign ("lambda", octave_value (lambda)); |
|
776 extra.assign ("redcosts", octave_value (redcosts)); |
|
777 } |
|
778 |
5234
|
779 extra.assign ("time", octave_value (time)); |
|
780 extra.assign ("mem", octave_value (mem)); |
5232
|
781 |
5234
|
782 retval(3) = extra; |
|
783 retval(2) = octave_value(status); |
|
784 retval(1) = octave_value(fmin); |
|
785 retval(0) = octave_value(xmin); |
|
786 |
5235
|
787 #else |
|
788 |
|
789 gripe_not_supported ("glpk"); |
|
790 |
|
791 #endif |
|
792 |
5234
|
793 return retval; |
5232
|
794 } |