Mercurial > octave-nkf
comparison src/DLD-FUNCTIONS/__glpk__.cc @ 10154:40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 20 Jan 2010 17:33:41 -0500 |
parents | 0631d397fbe0 |
children | 12884915a8e4 |
comparison
equal
deleted
inserted
replaced
10153:2c28f9d0360f | 10154:40dfc0c99116 |
---|---|
201 lpx_add_cols (lp, n); | 201 lpx_add_cols (lp, n); |
202 for (int i = 0; i < n; i++) | 202 for (int i = 0; i < n; i++) |
203 { | 203 { |
204 //-- Define type of the structural variables | 204 //-- Define type of the structural variables |
205 if (! freeLB[i] && ! freeUB[i]) | 205 if (! freeLB[i] && ! freeUB[i]) |
206 { | 206 { |
207 if (lb[i] != ub[i]) | 207 if (lb[i] != ub[i]) |
208 lpx_set_col_bnds (lp, i+1, LPX_DB, lb[i], ub[i]); | 208 lpx_set_col_bnds (lp, i+1, LPX_DB, lb[i], ub[i]); |
209 else | 209 else |
210 lpx_set_col_bnds (lp, i+1, LPX_FX, lb[i], ub[i]); | 210 lpx_set_col_bnds (lp, i+1, LPX_FX, lb[i], ub[i]); |
211 } | 211 } |
212 else | 212 else |
213 { | 213 { |
214 if (! freeLB[i] && freeUB[i]) | 214 if (! freeLB[i] && freeUB[i]) |
215 lpx_set_col_bnds (lp, i+1, LPX_LO, lb[i], ub[i]); | 215 lpx_set_col_bnds (lp, i+1, LPX_LO, lb[i], ub[i]); |
216 else | 216 else |
217 { | 217 { |
218 if (freeLB[i] && ! freeUB[i]) | 218 if (freeLB[i] && ! freeUB[i]) |
219 lpx_set_col_bnds (lp, i+1, LPX_UP, lb[i], ub[i]); | 219 lpx_set_col_bnds (lp, i+1, LPX_UP, lb[i], ub[i]); |
220 else | 220 else |
221 lpx_set_col_bnds (lp, i+1, LPX_FR, lb[i], ub[i]); | 221 lpx_set_col_bnds (lp, i+1, LPX_FR, lb[i], ub[i]); |
222 } | 222 } |
223 } | 223 } |
224 | 224 |
225 // -- Set the objective coefficient of the corresponding | 225 // -- Set the objective coefficient of the corresponding |
226 // -- structural variable. No constant term is assumed. | 226 // -- structural variable. No constant term is assumed. |
227 lpx_set_obj_coef(lp,i+1,c[i]); | 227 lpx_set_obj_coef(lp,i+1,c[i]); |
228 | 228 |
229 if (isMIP) | 229 if (isMIP) |
230 lpx_set_col_kind (lp, i+1, vartype[i]); | 230 lpx_set_col_kind (lp, i+1, vartype[i]); |
231 } | 231 } |
232 | 232 |
233 lpx_add_rows (lp, m); | 233 lpx_add_rows (lp, m); |
234 | 234 |
235 for (int i = 0; i < m; i++) | 235 for (int i = 0; i < m; i++) |
241 If the i-th row is of S type, the i-th LB is used, but | 241 If the i-th row is of S type, the i-th LB is used, but |
242 the i-th UB is ignored. | 242 the i-th UB is ignored. |
243 */ | 243 */ |
244 | 244 |
245 switch (ctype[i]) | 245 switch (ctype[i]) |
246 { | 246 { |
247 case 'F': | 247 case 'F': |
248 typx = LPX_FR; | 248 typx = LPX_FR; |
249 break; | 249 break; |
250 | 250 |
251 case 'U': | 251 case 'U': |
252 typx = LPX_UP; | 252 typx = LPX_UP; |
253 break; | 253 break; |
254 | 254 |
255 case 'L': | 255 case 'L': |
256 typx = LPX_LO; | 256 typx = LPX_LO; |
257 break; | 257 break; |
258 | 258 |
259 case 'S': | 259 case 'S': |
260 typx = LPX_FX; | 260 typx = LPX_FX; |
261 break; | 261 break; |
262 | 262 |
263 case 'D': | 263 case 'D': |
264 typx = LPX_DB; | 264 typx = LPX_DB; |
265 break; | 265 break; |
266 } | 266 } |
267 | 267 |
268 lpx_set_row_bnds (lp, i+1, typx, b[i], b[i]); | 268 lpx_set_row_bnds (lp, i+1, typx, b[i], b[i]); |
269 | 269 |
270 } | 270 } |
271 | 271 |
273 | 273 |
274 if (save_pb) | 274 if (save_pb) |
275 { | 275 { |
276 static char tmp[] = "outpb.lp"; | 276 static char tmp[] = "outpb.lp"; |
277 if (lpx_write_cpxlp (lp, tmp) != 0) | 277 if (lpx_write_cpxlp (lp, tmp) != 0) |
278 { | 278 { |
279 error ("__glpk__: unable to write problem"); | 279 error ("__glpk__: unable to write problem"); |
280 longjmp (mark, -1); | 280 longjmp (mark, -1); |
281 } | 281 } |
282 } | 282 } |
283 | 283 |
284 //-- scale the problem data (if required) | 284 //-- scale the problem data (if required) |
285 //-- if (scale && (!presol || method == 1)) lpx_scale_prob(lp); | 285 //-- if (scale && (!presol || method == 1)) lpx_scale_prob(lp); |
286 //-- LPX_K_SCALE=IParam[1] LPX_K_PRESOL=IParam[16] | 286 //-- LPX_K_SCALE=IParam[1] LPX_K_PRESOL=IParam[16] |
304 | 304 |
305 switch (method) | 305 switch (method) |
306 { | 306 { |
307 case 'S': | 307 case 'S': |
308 { | 308 { |
309 if (isMIP) | 309 if (isMIP) |
310 { | 310 { |
311 method = 'I'; | 311 method = 'I'; |
312 errnum = lpx_simplex (lp); | 312 errnum = lpx_simplex (lp); |
313 errnum = lpx_integer (lp); | 313 errnum = lpx_integer (lp); |
314 } | 314 } |
315 else | 315 else |
316 errnum = lpx_simplex(lp); | 316 errnum = lpx_simplex(lp); |
317 } | 317 } |
318 break; | 318 break; |
319 | 319 |
320 case 'T': | 320 case 'T': |
321 errnum = lpx_interior(lp); | 321 errnum = lpx_interior(lp); |
339 errnum = 2 <=> Numerical problems with basis matrix. | 339 errnum = 2 <=> Numerical problems with basis matrix. |
340 */ | 340 */ |
341 if (errnum == LPX_E_OK) | 341 if (errnum == LPX_E_OK) |
342 { | 342 { |
343 if (isMIP) | 343 if (isMIP) |
344 { | 344 { |
345 *status = lpx_mip_status (lp); | 345 *status = lpx_mip_status (lp); |
346 *fmin = lpx_mip_obj_val (lp); | 346 *fmin = lpx_mip_obj_val (lp); |
347 } | 347 } |
348 else | 348 else |
349 { | 349 { |
350 if (lpsolver == 1) | 350 if (lpsolver == 1) |
351 { | 351 { |
352 *status = lpx_get_status (lp); | 352 *status = lpx_get_status (lp); |
353 *fmin = lpx_get_obj_val (lp); | 353 *fmin = lpx_get_obj_val (lp); |
354 } | 354 } |
355 else | 355 else |
356 { | 356 { |
357 *status = lpx_ipt_status (lp); | 357 *status = lpx_ipt_status (lp); |
358 *fmin = lpx_ipt_obj_val (lp); | 358 *fmin = lpx_ipt_obj_val (lp); |
359 } | 359 } |
360 } | 360 } |
361 | 361 |
362 if (isMIP) | 362 if (isMIP) |
363 { | 363 { |
364 for (int i = 0; i < n; i++) | 364 for (int i = 0; i < n; i++) |
365 xmin[i] = lpx_mip_col_val (lp, i+1); | 365 xmin[i] = lpx_mip_col_val (lp, i+1); |
366 } | 366 } |
367 else | 367 else |
368 { | 368 { |
369 /* Primal values */ | 369 /* Primal values */ |
370 for (int i = 0; i < n; i++) | 370 for (int i = 0; i < n; i++) |
371 { | 371 { |
372 if (lpsolver == 1) | 372 if (lpsolver == 1) |
373 xmin[i] = lpx_get_col_prim (lp, i+1); | 373 xmin[i] = lpx_get_col_prim (lp, i+1); |
374 else | 374 else |
375 xmin[i] = lpx_ipt_col_prim (lp, i+1); | 375 xmin[i] = lpx_ipt_col_prim (lp, i+1); |
376 } | 376 } |
377 | 377 |
378 /* Dual values */ | 378 /* Dual values */ |
379 for (int i = 0; i < m; i++) | 379 for (int i = 0; i < m; i++) |
380 { | 380 { |
381 if (lpsolver == 1) | 381 if (lpsolver == 1) |
382 lambda[i] = lpx_get_row_dual (lp, i+1); | 382 lambda[i] = lpx_get_row_dual (lp, i+1); |
383 else | 383 else |
384 lambda[i] = lpx_ipt_row_dual (lp, i+1); | 384 lambda[i] = lpx_ipt_row_dual (lp, i+1); |
385 } | 385 } |
386 | 386 |
387 /* Reduced costs */ | 387 /* Reduced costs */ |
388 for (int i = 0; i < lpx_get_num_cols (lp); i++) | 388 for (int i = 0; i < lpx_get_num_cols (lp); i++) |
389 { | 389 { |
390 if (lpsolver == 1) | 390 if (lpsolver == 1) |
391 redcosts[i] = lpx_get_col_dual (lp, i+1); | 391 redcosts[i] = lpx_get_col_dual (lp, i+1); |
392 else | 392 else |
393 redcosts[i] = lpx_ipt_col_dual (lp, i+1); | 393 redcosts[i] = lpx_ipt_col_dual (lp, i+1); |
394 } | 394 } |
395 } | 395 } |
396 | 396 |
397 *time = (clock () - t_start) / CLOCKS_PER_SEC; | 397 *time = (clock () - t_start) / CLOCKS_PER_SEC; |
398 | 398 |
399 #ifdef GLPK_PRE_4_14 | 399 #ifdef GLPK_PRE_4_14 |
400 *mem = (lib_env_ptr () -> mem_tpeak); | 400 *mem = (lib_env_ptr () -> mem_tpeak); |
417 | 417 |
418 #define OCTAVE_GLPK_GET_REAL_PARAM(NAME, IDX) \ | 418 #define OCTAVE_GLPK_GET_REAL_PARAM(NAME, IDX) \ |
419 do \ | 419 do \ |
420 { \ | 420 { \ |
421 if (PARAM.contains (NAME)) \ | 421 if (PARAM.contains (NAME)) \ |
422 { \ | 422 { \ |
423 Cell tmp = PARAM.contents (NAME); \ | 423 Cell tmp = PARAM.contents (NAME); \ |
424 \ | 424 \ |
425 if (! tmp.is_empty ()) \ | 425 if (! tmp.is_empty ()) \ |
426 { \ | 426 { \ |
427 lpxRealParam[IDX] = tmp(0).scalar_value (); \ | 427 lpxRealParam[IDX] = tmp(0).scalar_value (); \ |
428 \ | 428 \ |
429 if (error_state) \ | 429 if (error_state) \ |
430 { \ | 430 { \ |
431 error ("glpk: invalid value in param." NAME); \ | 431 error ("glpk: invalid value in param." NAME); \ |
432 return retval; \ | 432 return retval; \ |
433 } \ | 433 } \ |
434 } \ | 434 } \ |
435 else \ | 435 else \ |
436 { \ | 436 { \ |
437 error ("glpk: invalid value in param." NAME); \ | 437 error ("glpk: invalid value in param." NAME); \ |
438 return retval; \ | 438 return retval; \ |
439 } \ | 439 } \ |
440 } \ | 440 } \ |
441 } \ | 441 } \ |
442 while (0) | 442 while (0) |
443 | 443 |
444 #define OCTAVE_GLPK_GET_INT_PARAM(NAME, VAL) \ | 444 #define OCTAVE_GLPK_GET_INT_PARAM(NAME, VAL) \ |
445 do \ | 445 do \ |
446 { \ | 446 { \ |
447 if (PARAM.contains (NAME)) \ | 447 if (PARAM.contains (NAME)) \ |
448 { \ | 448 { \ |
449 Cell tmp = PARAM.contents (NAME); \ | 449 Cell tmp = PARAM.contents (NAME); \ |
450 \ | 450 \ |
451 if (! tmp.is_empty ()) \ | 451 if (! tmp.is_empty ()) \ |
452 { \ | 452 { \ |
453 VAL = tmp(0).int_value (); \ | 453 VAL = tmp(0).int_value (); \ |
454 \ | 454 \ |
455 if (error_state) \ | 455 if (error_state) \ |
456 { \ | 456 { \ |
457 error ("glpk: invalid value in param." NAME); \ | 457 error ("glpk: invalid value in param." NAME); \ |
458 return retval; \ | 458 return retval; \ |
459 } \ | 459 } \ |
460 } \ | 460 } \ |
461 else \ | 461 else \ |
462 { \ | 462 { \ |
463 error ("glpk: invalid value in param." NAME); \ | 463 error ("glpk: invalid value in param." NAME); \ |
464 return retval; \ | 464 return retval; \ |
465 } \ | 465 } \ |
466 } \ | 466 } \ |
467 } \ | 467 } \ |
468 while (0) | 468 while (0) |
469 | 469 |
470 DEFUN_DLD (__glpk__, args, , | 470 DEFUN_DLD (__glpk__, args, , |
471 "-*- texinfo -*-\n\ | 471 "-*- texinfo -*-\n\ |
510 if (args(1).is_sparse_type ()) | 510 if (args(1).is_sparse_type ()) |
511 { | 511 { |
512 SparseMatrix A = args(1).sparse_matrix_value (); // get the sparse matrix | 512 SparseMatrix A = args(1).sparse_matrix_value (); // get the sparse matrix |
513 | 513 |
514 if (error_state) | 514 if (error_state) |
515 { | 515 { |
516 error ("__glpk__: invalid value of A"); | 516 error ("__glpk__: invalid value of A"); |
517 return retval; | 517 return retval; |
518 } | 518 } |
519 | 519 |
520 mrowsA = A.rows (); | 520 mrowsA = A.rows (); |
521 octave_idx_type Anc = A.cols (); | 521 octave_idx_type Anc = A.cols (); |
522 octave_idx_type Anz = A.nzmax (); | 522 octave_idx_type Anz = A.nzmax (); |
523 rn.resize (Anz+1); | 523 rn.resize (Anz+1); |
524 cn.resize (Anz+1); | 524 cn.resize (Anz+1); |
525 a.resize (Anz+1, 0.0); | 525 a.resize (Anz+1, 0.0); |
526 | 526 |
527 if (Anc != mrowsc) | 527 if (Anc != mrowsc) |
528 { | 528 { |
529 error ("__glpk__: invalid value of A"); | 529 error ("__glpk__: invalid value of A"); |
530 return retval; | 530 return retval; |
531 } | 531 } |
532 | 532 |
533 for (octave_idx_type j = 0; j < Anc; j++) | 533 for (octave_idx_type j = 0; j < Anc; j++) |
534 for (octave_idx_type i = A.cidx(j); i < A.cidx(j+1); i++) | 534 for (octave_idx_type i = A.cidx(j); i < A.cidx(j+1); i++) |
535 { | 535 { |
536 nz++; | 536 nz++; |
537 rn(nz) = A.ridx(i) + 1; | 537 rn(nz) = A.ridx(i) + 1; |
538 cn(nz) = j + 1; | 538 cn(nz) = j + 1; |
539 a(nz) = A.data(i); | 539 a(nz) = A.data(i); |
540 } | 540 } |
541 } | 541 } |
542 else | 542 else |
543 { | 543 { |
544 Matrix A (args(1).matrix_value ()); // get the matrix | 544 Matrix A (args(1).matrix_value ()); // get the matrix |
545 | 545 |
546 if (error_state) | 546 if (error_state) |
547 { | 547 { |
548 error ("__glpk__: invalid value of A"); | 548 error ("__glpk__: invalid value of A"); |
549 return retval; | 549 return retval; |
550 } | 550 } |
551 | 551 |
552 mrowsA = A.rows (); | 552 mrowsA = A.rows (); |
553 rn.resize (mrowsA*mrowsc+1); | 553 rn.resize (mrowsA*mrowsc+1); |
554 cn.resize (mrowsA*mrowsc+1); | 554 cn.resize (mrowsA*mrowsc+1); |
555 a.resize (mrowsA*mrowsc+1, 0.0); | 555 a.resize (mrowsA*mrowsc+1, 0.0); |
556 | 556 |
557 for (int i = 0; i < mrowsA; i++) | 557 for (int i = 0; i < mrowsA; i++) |
558 { | 558 { |
559 for (int j = 0; j < mrowsc; j++) | 559 for (int j = 0; j < mrowsc; j++) |
560 { | 560 { |
561 if (A(i,j) != 0) | 561 if (A(i,j) != 0) |
562 { | 562 { |
563 nz++; | 563 nz++; |
564 rn(nz) = i + 1; | 564 rn(nz) = i + 1; |
565 cn(nz) = j + 1; | 565 cn(nz) = j + 1; |
566 a(nz) = A(i,j); | 566 a(nz) = A(i,j); |
567 } | 567 } |
568 } | 568 } |
569 } | 569 } |
570 | 570 |
571 } | 571 } |
572 | 572 |
573 //-- 3rd Input. A column array containing the right-hand side value | 573 //-- 3rd Input. A column array containing the right-hand side value |
574 // for each constraint in the constraint matrix. | 574 // for each constraint in the constraint matrix. |
575 Matrix B (args(2).matrix_value ()); | 575 Matrix B (args(2).matrix_value ()); |
576 | 576 |
577 if (error_state) | 577 if (error_state) |
578 { | 578 { |
579 error ("__glpk__: invalid value of b"); | 579 error ("__glpk__: invalid value of b"); |
597 //-- LB argument, default: Free | 597 //-- LB argument, default: Free |
598 Array<int> freeLB (mrowsc); | 598 Array<int> freeLB (mrowsc); |
599 for (int i = 0; i < mrowsc; i++) | 599 for (int i = 0; i < mrowsc; i++) |
600 { | 600 { |
601 if (xisinf (lb[i])) | 601 if (xisinf (lb[i])) |
602 { | 602 { |
603 freeLB(i) = 1; | 603 freeLB(i) = 1; |
604 lb[i] = -octave_Inf; | 604 lb[i] = -octave_Inf; |
605 } | 605 } |
606 else | 606 else |
607 freeLB(i) = 0; | 607 freeLB(i) = 0; |
608 } | 608 } |
609 | 609 |
610 //-- 5th Input. An array of at least length numcols containing the upper | 610 //-- 5th Input. An array of at least length numcols containing the upper |
611 //-- bound on each of the variables. | 611 //-- bound on each of the variables. |
612 Matrix UB (args(4).matrix_value ()); | 612 Matrix UB (args(4).matrix_value ()); |
621 | 621 |
622 Array<int> freeUB (mrowsc); | 622 Array<int> freeUB (mrowsc); |
623 for (int i = 0; i < mrowsc; i++) | 623 for (int i = 0; i < mrowsc; i++) |
624 { | 624 { |
625 if (xisinf (ub[i])) | 625 if (xisinf (ub[i])) |
626 { | 626 { |
627 freeUB(i) = 1; | 627 freeUB(i) = 1; |
628 ub[i] = octave_Inf; | 628 ub[i] = octave_Inf; |
629 } | 629 } |
630 else | 630 else |
631 freeUB(i) = 0; | 631 freeUB(i) = 0; |
632 } | 632 } |
633 | 633 |
634 //-- 6th Input. A column array containing the sense of each constraint | 634 //-- 6th Input. A column array containing the sense of each constraint |
635 //-- in the constraint matrix. | 635 //-- in the constraint matrix. |
636 charMatrix CTYPE (args(5).char_matrix_value ()); | 636 charMatrix CTYPE (args(5).char_matrix_value ()); |
655 Array<int> vartype (mrowsc); | 655 Array<int> vartype (mrowsc); |
656 volatile int isMIP = 0; | 656 volatile int isMIP = 0; |
657 for (int i = 0; i < mrowsc ; i++) | 657 for (int i = 0; i < mrowsc ; i++) |
658 { | 658 { |
659 if (VTYPE(i,0) == 'I') | 659 if (VTYPE(i,0) == 'I') |
660 { | 660 { |
661 isMIP = 1; | 661 isMIP = 1; |
662 vartype(i) = LPX_IV; | 662 vartype(i) = LPX_IV; |
663 } | 663 } |
664 else | 664 else |
665 vartype(i) = LPX_CV; | 665 vartype(i) = LPX_CV; |
666 } | 666 } |
667 | 667 |
668 //-- 8th Input. Sense of optimization. | 668 //-- 8th Input. Sense of optimization. |
669 volatile int sense; | 669 volatile int sense; |
670 double SENSE = args(7).scalar_value (); | 670 double SENSE = args(7).scalar_value (); |
794 //-- Absolute tolerance used to check if the current basic solution | 794 //-- Absolute tolerance used to check if the current basic solution |
795 //-- is dual feasible | 795 //-- is dual feasible |
796 OCTAVE_GLPK_GET_REAL_PARAM ("toldj", 2); | 796 OCTAVE_GLPK_GET_REAL_PARAM ("toldj", 2); |
797 | 797 |
798 //-- Relative tolerance used to choose eligible pivotal elements of | 798 //-- Relative tolerance used to choose eligible pivotal elements of |
799 //-- the simplex table in the ratio test | 799 //-- the simplex table in the ratio test |
800 OCTAVE_GLPK_GET_REAL_PARAM ("tolpiv", 3); | 800 OCTAVE_GLPK_GET_REAL_PARAM ("tolpiv", 3); |
801 | 801 |
802 OCTAVE_GLPK_GET_REAL_PARAM ("objll", 4); | 802 OCTAVE_GLPK_GET_REAL_PARAM ("objll", 4); |
803 | 803 |
804 OCTAVE_GLPK_GET_REAL_PARAM ("objul", 5); | 804 OCTAVE_GLPK_GET_REAL_PARAM ("objul", 5); |
822 | 822 |
823 int jmpret = setjmp (mark); | 823 int jmpret = setjmp (mark); |
824 | 824 |
825 if (jmpret == 0) | 825 if (jmpret == 0) |
826 glpk (sense, mrowsc, mrowsA, c, nz, rn.fortran_vec (), | 826 glpk (sense, mrowsc, mrowsA, c, nz, rn.fortran_vec (), |
827 cn.fortran_vec (), a.fortran_vec (), b, ctype, | 827 cn.fortran_vec (), a.fortran_vec (), b, ctype, |
828 freeLB.fortran_vec (), lb, freeUB.fortran_vec (), | 828 freeLB.fortran_vec (), lb, freeUB.fortran_vec (), |
829 ub, vartype.fortran_vec (), isMIP, lpsolver, | 829 ub, vartype.fortran_vec (), isMIP, lpsolver, |
830 save_pb, xmin.fortran_vec (), fmin.fortran_vec (), | 830 save_pb, xmin.fortran_vec (), fmin.fortran_vec (), |
831 status.fortran_vec (), lambda.fortran_vec (), | 831 status.fortran_vec (), lambda.fortran_vec (), |
832 redcosts.fortran_vec (), time.fortran_vec (), | 832 redcosts.fortran_vec (), time.fortran_vec (), |
833 mem.fortran_vec ()); | 833 mem.fortran_vec ()); |
834 | 834 |
835 Octave_map extra; | 835 Octave_map extra; |
836 | 836 |
837 if (! isMIP) | 837 if (! isMIP) |
838 { | 838 { |