lp_interior_point.cpp 36.2 KB
 André Anjos committed Dec 04, 2013 1 2 3 4 5 6 7 8 9 10 11 ``````/** * @file math/python/LPInteriorPoint.cc * @date Fri Jan 27 21:06:59 2012 +0100 * @author Laurent El Shafey * * @brief Binds the interior point methods which allow to solve a * Linear Programming problem (LP). * * Copyright (C) 2011-2013 Idiap Research Institute, Martigny, Switzerland */ `````` André Anjos committed Dec 09, 2013 12 13 ``````#include "lp_interior_point.h" #include `````` André Anjos committed Dec 04, 2013 14 ``````#include `````` André Anjos committed Dec 09, 2013 15 ``````#include `````` André Anjos committed Dec 04, 2013 16 `````` `````` André Anjos committed Dec 10, 2013 17 18 19 20 ``````/************************************************ * Implementation of LPInteriorPoint base class * ************************************************/ `````` André Anjos committed Dec 09, 2013 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 ``````PyDoc_STRVAR(s_lpinteriorpoint_str, XBOB_EXT_MODULE_PREFIX ".LPInteriorPoint"); PyDoc_STRVAR(s_lpinteriorpoint_doc, "Base class to solve a linear program using interior point methods.\n\ For more details about the algorithms,please refer to the following\n\ book: *\"Primal-Dual Interior-Point Methods\", Stephen J. Wright,\n\ ISBN: 978-0898713824, Chapter 5, \"Path-Following Algorithms\"*.\n\ \n\ .. warning::\n\ \n\ You cannot instantiate an object of this type directly, you must\n\ use it through one of the inherited types.\n\ \n\ The primal linear program (LP) is defined as follows:\n\ \n\ min transpose(c)*x, s.t. A*x=b, x>=0\n\ \n\ The dual formulation is:\n\ \n\ min transpose(b)*lambda, s.t. transpose(A)*lambda+mu=c\n\ \n\ "); /* Type definition for PyBobMathLpInteriorPointObject */ typedef struct { PyObject_HEAD /* Type-specific fields go here. */ `````` André Anjos committed Dec 10, 2013 49 `````` bob::math::LPInteriorPoint* base; `````` André Anjos committed Dec 09, 2013 50 51 52 53 54 55 56 57 58 `````` } PyBobMathLpInteriorPointObject; static int PyBobMathLpInteriorPoint_init(PyBobMathLpInteriorPointObject* self, PyObject*, PyObject*) { PyErr_SetString(PyExc_NotImplementedError, "cannot initialize object of base type `LPInteriorPoint' - use one of the inherited classes"); return -1; `````` André Anjos committed Dec 04, 2013 59 60 ``````} `````` André Anjos committed Dec 10, 2013 61 ``````PyDoc_STRVAR(s_M_str, "m"); `````` André Anjos committed Dec 09, 2013 62 63 64 65 ``````PyDoc_STRVAR(s_M_doc, "The first dimension of the problem/A matrix" ); `````` André Anjos committed Dec 10, 2013 66 67 ``````static PyObject* PyBobMathLpInteriorPoint_getM (PyBobMathLpInteriorPointObject* self, void* /*closure*/) { `````` André Anjos committed Dec 09, 2013 68 `````` return Py_BuildValue("n", self->base->getDimM()); `````` André Anjos committed Dec 04, 2013 69 70 ``````} `````` André Anjos committed Dec 10, 2013 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 ``````static int PyBobMathLpInteriorPoint_setM (PyBobMathLpInteriorPointObject* self, PyObject* o, void* /*closure*/) { Py_ssize_t M = PyNumber_AsSsize_t(o, PyExc_OverflowError); if (PyErr_Occurred()) return -1; try { self->base->setDimM(M); } catch (std::exception& e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return -1; } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot reset size M of LPInteriorPoint: unknown exception caught"); return -1; } return 0; } `````` André Anjos committed Dec 10, 2013 93 ``````PyDoc_STRVAR(s_N_str, "n"); `````` André Anjos committed Dec 09, 2013 94 95 96 97 ``````PyDoc_STRVAR(s_N_doc, "The second dimension of the problem/A matrix" ); `````` André Anjos committed Dec 10, 2013 98 99 ``````static PyObject* PyBobMathLpInteriorPoint_getN (PyBobMathLpInteriorPointObject* self, void* /*closure*/) { `````` André Anjos committed Dec 09, 2013 100 101 102 `````` return Py_BuildValue("n", self->base->getDimN()); } `````` André Anjos committed Dec 10, 2013 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 ``````static int PyBobMathLpInteriorPoint_setN (PyBobMathLpInteriorPointObject* self, PyObject* o, void* /*closure*/) { Py_ssize_t N = PyNumber_AsSsize_t(o, PyExc_OverflowError); if (PyErr_Occurred()) return -1; try { self->base->setDimN(N); } catch (std::exception& e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return -1; } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot reset size N of LPInteriorPoint: unknown exception caught"); return -1; } return 0; } `````` André Anjos committed Dec 09, 2013 125 126 127 128 129 ``````PyDoc_STRVAR(s_epsilon_str, "epsilon"); PyDoc_STRVAR(s_epsilon_doc, "The precision to determine whether an equality constraint is fulfilled or not" ); `````` André Anjos committed Dec 10, 2013 130 ``````static PyObject* PyBobMathLpInteriorPoint_getEpsilon (PyBobMathLpInteriorPointObject* self, void* /*closure*/) { `````` André Anjos committed Dec 09, 2013 131 132 133 `````` return Py_BuildValue("d", self->base->getEpsilon()); } `````` André Anjos committed Dec 10, 2013 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 ``````static int PyBobMathLpInteriorPoint_setEpsilon (PyBobMathLpInteriorPointObject* self, PyObject* o, void* /*closure*/) { double e = PyFloat_AsDouble(o); if (PyErr_Occurred()) return -1; try { self->base->setEpsilon(e); } catch (std::exception& ex) { PyErr_SetString(PyExc_RuntimeError, ex.what()); return -1; } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot reset `epsilon' of LPInteriorPoint: unknown exception caught"); return -1; } return 0; } `````` André Anjos committed Dec 10, 2013 156 ``````PyDoc_STRVAR(s_lambda_str, "lambda_"); `````` André Anjos committed Dec 09, 2013 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 ``````PyDoc_STRVAR(s_lambda_doc, "The value of the lambda dual variable (read-only)" ); static PyObject* PyBobMathLpInteriorPoint_lambda (PyBobMathLpInteriorPointObject* self) { Py_ssize_t length = self->base->getDimM(); PyObject* retval = PyBlitzArray_SimpleNew(NPY_FLOAT64, 1, &length); if (!retval) return 0; blitz::Array* wrapper = PyBlitzArrayCxx_AsBlitz (reinterpret_cast(retval)); (*wrapper) = self->base->getLambda(); return retval; } PyDoc_STRVAR(s_mu_str, "mu"); PyDoc_STRVAR(s_mu_doc, "The value of the mu dual variable (read-only)" ); static PyObject* PyBobMathLpInteriorPoint_mu (PyBobMathLpInteriorPointObject* self) { Py_ssize_t length = self->base->getDimN(); PyObject* retval = PyBlitzArray_SimpleNew(NPY_FLOAT64, 1, &length); if (!retval) return 0; blitz::Array* wrapper = PyBlitzArrayCxx_AsBlitz (reinterpret_cast(retval)); (*wrapper) = self->base->getMu(); return retval; } static PyGetSetDef PyBobMathLpInteriorPoint_getseters[] = { { s_M_str, `````` André Anjos committed Dec 10, 2013 193 194 `````` (getter)PyBobMathLpInteriorPoint_getM, (setter)PyBobMathLpInteriorPoint_setM, `````` André Anjos committed Dec 09, 2013 195 196 197 198 199 `````` s_M_doc, 0 }, { s_N_str, `````` André Anjos committed Dec 10, 2013 200 201 `````` (getter)PyBobMathLpInteriorPoint_getN, (setter)PyBobMathLpInteriorPoint_setN, `````` André Anjos committed Dec 09, 2013 202 203 204 205 206 `````` s_N_doc, 0 }, { s_epsilon_str, `````` André Anjos committed Dec 10, 2013 207 208 `````` (getter)PyBobMathLpInteriorPoint_getEpsilon, (setter)PyBobMathLpInteriorPoint_setEpsilon, `````` André Anjos committed Dec 09, 2013 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 `````` s_epsilon_doc, 0 }, { s_lambda_str, (getter)PyBobMathLpInteriorPoint_lambda, 0, s_lambda_doc, 0 }, { s_mu_str, (getter)PyBobMathLpInteriorPoint_mu, 0, s_mu_doc, 0 }, {0} /* Sentinel */ }; PyDoc_STRVAR(s_reset_str, "reset"); PyDoc_STRVAR(s_reset_doc, "o.reset(M, N) -> None\n\ \n\ Resets the size of the problem (M and N correspond to the dimensions of the\n\ A matrix"); static PyObject* PyBobMathLpInteriorPoint_reset (PyBobMathLpInteriorPointObject* self, PyObject *args, PyObject* kwds) { /* Parses input arguments in a single shot */ static const char* const_kwlist[] = {"M", "N", 0}; static char** kwlist = const_cast(const_kwlist); Py_ssize_t M = 0; Py_ssize_t N = 0; if (!PyArg_ParseTupleAndKeywords(args, kwds, "nn", kwlist, &M, &N)) return 0; try { self->base->reset(M, N); } catch (std::exception& e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return 0; } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot reset LPInteriorPoint: unknown exception caught"); return 0; } Py_RETURN_NONE; } PyDoc_STRVAR(s_solve_str, "solve"); PyDoc_STRVAR(s_solve_doc, "o.solve(A, b, c, x0, [lambda, mu]) -> x\n\ \n\ Solves an LP problem\n\ "); static PyObject* PyBobMathLpInteriorPoint_solve (PyBobMathLpInteriorPointObject* self, PyObject *args, PyObject* kwds) { /* Parses input arguments in a single shot */ static const char* const_kwlist[] = {"A", "b", "c", "x0", "lambda", "mu", 0}; static char** kwlist = const_cast(const_kwlist); PyBlitzArrayObject* A = 0; PyBlitzArrayObject* b = 0; PyBlitzArrayObject* c = 0; PyBlitzArrayObject* x0 = 0; PyBlitzArrayObject* lambda = 0; PyBlitzArrayObject* mu = 0; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O&O&O&|O&O&", kwlist, &PyBlitzArray_Converter, &A, &PyBlitzArray_Converter, &b, &PyBlitzArray_Converter, &c, &PyBlitzArray_Converter, &x0, &PyBlitzArray_Converter, &lambda, &PyBlitzArray_Converter, &mu )) return 0; if (A->type_num != NPY_FLOAT64 || A->ndim != 2) { PyErr_SetString(PyExc_TypeError, "Linear program solver only supports 64-bit floats 2D arrays for input vector `A'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x0); Py_XDECREF(lambda); Py_XDECREF(mu); return 0; } if (b->type_num != NPY_FLOAT64 || b->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program solver only supports 64-bit floats 1D arrays for input vector `b'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x0); Py_XDECREF(lambda); Py_XDECREF(mu); return 0; } if (c->type_num != NPY_FLOAT64 || c->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program solver only supports 64-bit floats 1D arrays for input vector `c'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x0); Py_XDECREF(lambda); Py_XDECREF(mu); return 0; } if (x0->type_num != NPY_FLOAT64 || x0->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program solver only supports 64-bit floats 1D arrays for input vector `x0'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x0); Py_XDECREF(lambda); Py_XDECREF(mu); return 0; } `````` André Anjos committed Dec 10, 2013 338 `````` if (lambda && (lambda->type_num != NPY_FLOAT64 || lambda->ndim != 1)) { `````` André Anjos committed Dec 09, 2013 339 340 341 342 343 344 345 346 347 348 `````` PyErr_SetString(PyExc_TypeError, "Linear program solver only supports 64-bit floats 1D arrays for input vector `lambda'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x0); Py_XDECREF(lambda); Py_XDECREF(mu); return 0; } `````` André Anjos committed Dec 10, 2013 349 `````` if (mu && (mu->type_num != NPY_FLOAT64 || mu->ndim != 1)) { `````` André Anjos committed Dec 09, 2013 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 `````` PyErr_SetString(PyExc_TypeError, "Linear program solver only supports 64-bit floats 1D arrays for input vector `mu'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x0); Py_XDECREF(lambda); Py_XDECREF(mu); return 0; } if ( (lambda && !mu) || (mu && !lambda) ) { PyErr_SetString(PyExc_RuntimeError, "Linear program solver requires none or both `mu' and `lambda' - you cannot just specify one of them"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x0); Py_XDECREF(lambda); Py_XDECREF(mu); return 0; } /* This is where the output is going to be stored */ PyObject* retval = PyBlitzArray_SimpleNew(NPY_FLOAT64, x0->ndim, x0->shape); if (!retval) { Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x0); Py_XDECREF(lambda); Py_XDECREF(mu); return 0; } blitz::Array* wrapper = PyBlitzArrayCxx_AsBlitz(reinterpret_cast(retval)); (*wrapper) = *PyBlitzArrayCxx_AsBlitz(x0); Py_DECREF(x0); try { if (lambda && mu) { self->base->solve( *PyBlitzArrayCxx_AsBlitz(A), *PyBlitzArrayCxx_AsBlitz(b), *PyBlitzArrayCxx_AsBlitz(c), *wrapper, *PyBlitzArrayCxx_AsBlitz(lambda), *PyBlitzArrayCxx_AsBlitz(mu) ); } else { self->base->solve( *PyBlitzArrayCxx_AsBlitz(A), *PyBlitzArrayCxx_AsBlitz(b), *PyBlitzArrayCxx_AsBlitz(c), *wrapper ); } } catch (std::exception& e) { PyErr_SetString(PyExc_RuntimeError, e.what()); } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot solve LPInteriorPoint: unknown exception caught"); } Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_XDECREF(lambda); Py_XDECREF(mu); if (PyErr_Occurred()) { Py_DECREF(retval); return 0; } /* We only "return" the first half of the `x' vector */ (reinterpret_cast(retval))->shape[0] /= 2; return retval; } PyDoc_STRVAR(s_is_feasible_str, "is_feasible"); PyDoc_STRVAR(s_is_feasible_doc, "o.is_feasible(A, b, c, x, lambda, mu) -> bool\n\ \n\ Checks if a primal-dual point (x,lambda,mu) belongs to the set of\n\ feasible point (i.e. fulfill the constraints)\n\ \n\ "); static PyObject* PyBobMathLpInteriorPoint_is_feasible (PyBobMathLpInteriorPointObject* self, PyObject *args, PyObject* kwds) { /* Parses input arguments in a single shot */ static const char* const_kwlist[] = {"A", "b", "c", "x", "lambda", "mu", 0}; static char** kwlist = const_cast(const_kwlist); PyBlitzArrayObject* A = 0; PyBlitzArrayObject* b = 0; PyBlitzArrayObject* c = 0; PyBlitzArrayObject* x = 0; PyBlitzArrayObject* lambda = 0; PyBlitzArrayObject* mu = 0; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O&O&O&O&O&", kwlist, &PyBlitzArray_Converter, &A, &PyBlitzArray_Converter, &b, &PyBlitzArray_Converter, &c, &PyBlitzArray_Converter, &x, &PyBlitzArray_Converter, &lambda, &PyBlitzArray_Converter, &mu )) return 0; if (A->type_num != NPY_FLOAT64 || A->ndim != 2) { PyErr_SetString(PyExc_TypeError, "Linear program is_feasible only supports 64-bit floats 2D arrays for input vector `A'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); return 0; } if (b->type_num != NPY_FLOAT64 || b->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program is_feasible only supports 64-bit floats 1D arrays for input vector `b'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); return 0; } if (c->type_num != NPY_FLOAT64 || c->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program is_feasible only supports 64-bit floats 1D arrays for input vector `c'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); return 0; } if (x->type_num != NPY_FLOAT64 || x->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program is_feasible only supports 64-bit floats 1D arrays for input vector `x0'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); return 0; } if (lambda->type_num != NPY_FLOAT64 || lambda->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program is_feasible only supports 64-bit floats 1D arrays for input vector `lambda'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); return 0; } if (mu->type_num != NPY_FLOAT64 || mu->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program is_feasible only supports 64-bit floats 1D arrays for input vector `mu'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); return 0; } bool feasible = false; try { feasible = self->base->isFeasible( *PyBlitzArrayCxx_AsBlitz(A), *PyBlitzArrayCxx_AsBlitz(b), *PyBlitzArrayCxx_AsBlitz(c), *PyBlitzArrayCxx_AsBlitz(x), *PyBlitzArrayCxx_AsBlitz(lambda), *PyBlitzArrayCxx_AsBlitz(mu) ); } catch (std::exception& e) { PyErr_SetString(PyExc_RuntimeError, e.what()); } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot check feasibility of LPInteriorPoint: unknown exception caught"); } Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); `````` André Anjos committed Dec 10, 2013 554 555 `````` if (PyErr_Occurred()) return 0; `````` André Anjos committed Dec 09, 2013 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 `````` if (feasible) Py_RETURN_TRUE; Py_RETURN_FALSE; } PyDoc_STRVAR(s_is_in_v_str, "is_in_v"); PyDoc_STRVAR(s_is_in_v_doc, "o.is_in_v(x, mu, theta) -> bool\n\ \n\ Check if a primal-dual point (x,lambda,mu) belongs to the V2\n\ neighborhood of the central path.\n\ \n\ "); static PyObject* PyBobMathLpInteriorPoint_is_in_v (PyBobMathLpInteriorPointObject* self, PyObject *args, PyObject* kwds) { /* Parses input arguments in a single shot */ static const char* const_kwlist[] = {"x", "mu", "theta", 0}; static char** kwlist = const_cast(const_kwlist); PyBlitzArrayObject* x = 0; PyBlitzArrayObject* mu = 0; double theta = 0.; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O&d", kwlist, &PyBlitzArray_Converter, &x, &PyBlitzArray_Converter, &mu, &theta )) return 0; if (x->type_num != NPY_FLOAT64 || x->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program is_in_v only supports 64-bit floats 1D arrays for input vector `x0'"); Py_DECREF(x); Py_DECREF(mu); return 0; } if (mu->type_num != NPY_FLOAT64 || mu->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program is_in_v only supports 64-bit floats 1D arrays for input vector `mu'"); Py_DECREF(x); Py_DECREF(mu); return 0; } bool in_v = false; try { in_v = self->base->isInV( *PyBlitzArrayCxx_AsBlitz(x), *PyBlitzArrayCxx_AsBlitz(mu), theta ); } catch (std::exception& e) { PyErr_SetString(PyExc_RuntimeError, e.what()); } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot check if point is in V at LPInteriorPoint: unknown exception caught"); } Py_DECREF(x); Py_DECREF(mu); `````` André Anjos committed Dec 10, 2013 620 621 `````` if (PyErr_Occurred()) return 0; `````` André Anjos committed Dec 09, 2013 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 `````` if (in_v) Py_RETURN_TRUE; Py_RETURN_FALSE; } PyDoc_STRVAR(s_is_in_v_s_str, "is_in_v_s"); PyDoc_STRVAR(s_is_in_v_s_doc, "o.is_in_v_s(A, b, c, x, lambda, mu) -> bool\n\ \n\ Checks if a primal-dual point (x,lambda,mu) belongs to the V\n\ neighborhood of the central path and the set of feasible points.\n\ \n\ "); static PyObject* PyBobMathLpInteriorPoint_is_in_v_s (PyBobMathLpInteriorPointObject* self, PyObject *args, PyObject* kwds) { /* Parses input arguments in a single shot */ static const char* const_kwlist[] = {"A", "b", "c", "x", "lambda", "mu", "theta", 0}; static char** kwlist = const_cast(const_kwlist); PyBlitzArrayObject* A = 0; PyBlitzArrayObject* b = 0; PyBlitzArrayObject* c = 0; PyBlitzArrayObject* x = 0; PyBlitzArrayObject* lambda = 0; PyBlitzArrayObject* mu = 0; double theta = 0.; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O&O&O&O&O&d", kwlist, &PyBlitzArray_Converter, &A, &PyBlitzArray_Converter, &b, &PyBlitzArray_Converter, &c, &PyBlitzArray_Converter, &x, &PyBlitzArray_Converter, &lambda, &PyBlitzArray_Converter, &mu, &theta )) return 0; if (A->type_num != NPY_FLOAT64 || A->ndim != 2) { PyErr_SetString(PyExc_TypeError, "Linear program is_in_v_s only supports 64-bit floats 2D arrays for input vector `A'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); return 0; } if (b->type_num != NPY_FLOAT64 || b->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program is_in_v_s only supports 64-bit floats 1D arrays for input vector `b'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); return 0; } if (c->type_num != NPY_FLOAT64 || c->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program is_in_v_s only supports 64-bit floats 1D arrays for input vector `c'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); return 0; } if (x->type_num != NPY_FLOAT64 || x->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program is_in_v_s only supports 64-bit floats 1D arrays for input vector `x0'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); return 0; } if (lambda->type_num != NPY_FLOAT64 || lambda->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program is_in_v_s only supports 64-bit floats 1D arrays for input vector `lambda'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); return 0; } if (mu->type_num != NPY_FLOAT64 || mu->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program is_in_v_s only supports 64-bit floats 1D arrays for input vector `mu'"); Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); return 0; } bool in_v_s = false; try { in_v_s = self->base->isInVS( *PyBlitzArrayCxx_AsBlitz(A), *PyBlitzArrayCxx_AsBlitz(b), *PyBlitzArrayCxx_AsBlitz(c), *PyBlitzArrayCxx_AsBlitz(x), *PyBlitzArrayCxx_AsBlitz(lambda), *PyBlitzArrayCxx_AsBlitz(mu), theta ); } catch (std::exception& e) { PyErr_SetString(PyExc_RuntimeError, e.what()); } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot check if point is in VS at LPInteriorPoint: unknown exception caught"); } Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); `````` André Anjos committed Dec 10, 2013 754 755 `````` if (PyErr_Occurred()) return 0; `````` André Anjos committed Dec 09, 2013 756 757 758 `````` if (in_v_s) Py_RETURN_TRUE; Py_RETURN_FALSE; `````` André Anjos committed Dec 04, 2013 759 760 ``````} `````` André Anjos committed Dec 10, 2013 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 ``````PyDoc_STRVAR(s_initialize_dual_lambda_mu_str, "initialize_dual_lambda_mu"); PyDoc_STRVAR(s_initialize_dual_lambda_mu_doc, "o.initialize_dual_lambda_mu(A, c) -> None\n\ \n\ Initializes the dual variables `lambda' and `mu' by minimizing the\n\ logarithmic barrier function.\n\ \n\ "); static PyObject* PyBobMathLpInteriorPoint_initialize_dual_lambda_mu (PyBobMathLpInteriorPointObject* self, PyObject *args, PyObject* kwds) { /* Parses input arguments in a single shot */ static const char* const_kwlist[] = {"A", "c", 0}; static char** kwlist = const_cast(const_kwlist); PyBlitzArrayObject* A = 0; PyBlitzArrayObject* c = 0; `````` André Anjos committed Dec 10, 2013 780 `````` if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O&", kwlist, `````` André Anjos committed Dec 10, 2013 781 `````` &PyBlitzArray_Converter, &A, `````` André Anjos committed Dec 10, 2013 782 `````` &PyBlitzArray_Converter, &c `````` André Anjos committed Dec 10, 2013 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 `````` )) return 0; if (A->type_num != NPY_FLOAT64 || A->ndim != 2) { PyErr_SetString(PyExc_TypeError, "Linear program initialize_dual_lambda_mu only supports 64-bit floats 2D arrays for input vector `A'"); Py_DECREF(A); Py_DECREF(c); return 0; } if (c->type_num != NPY_FLOAT64 || c->ndim != 1) { PyErr_SetString(PyExc_TypeError, "Linear program initialize_dual_lambda_mu only supports 64-bit floats 1D arrays for input vector `c'"); Py_DECREF(A); Py_DECREF(c); return 0; } try { self->base->initializeDualLambdaMu( *PyBlitzArrayCxx_AsBlitz(A), *PyBlitzArrayCxx_AsBlitz(c) ); } catch (std::exception& e) { PyErr_SetString(PyExc_RuntimeError, e.what()); } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot initialize dual lambda-mu at LPInteriorPoint: unknown exception caught"); } Py_DECREF(A); Py_DECREF(c); if (PyErr_Occurred()) return 0; Py_RETURN_NONE; } `````` André Anjos committed Dec 09, 2013 821 822 823 824 ``````static PyMethodDef PyBobMathLpInteriorPoint_methods[] = { { s_reset_str, (PyCFunction)PyBobMathLpInteriorPoint_reset, `````` André Anjos committed Dec 10, 2013 825 `````` METH_VARARGS|METH_KEYWORDS, `````` André Anjos committed Dec 09, 2013 826 827 828 829 830 `````` s_reset_doc }, { s_solve_str, (PyCFunction)PyBobMathLpInteriorPoint_solve, `````` André Anjos committed Dec 10, 2013 831 `````` METH_VARARGS|METH_KEYWORDS, `````` André Anjos committed Dec 09, 2013 832 833 834 835 836 `````` s_solve_doc }, { s_is_feasible_str, (PyCFunction)PyBobMathLpInteriorPoint_is_feasible, `````` André Anjos committed Dec 10, 2013 837 `````` METH_VARARGS|METH_KEYWORDS, `````` André Anjos committed Dec 09, 2013 838 839 840 841 842 `````` s_is_feasible_doc }, { s_is_in_v_str, (PyCFunction)PyBobMathLpInteriorPoint_is_in_v, `````` André Anjos committed Dec 10, 2013 843 `````` METH_VARARGS|METH_KEYWORDS, `````` André Anjos committed Dec 09, 2013 844 845 846 847 848 `````` s_is_in_v_doc }, { s_is_in_v_s_str, (PyCFunction)PyBobMathLpInteriorPoint_is_in_v_s, `````` André Anjos committed Dec 10, 2013 849 `````` METH_VARARGS|METH_KEYWORDS, `````` André Anjos committed Dec 09, 2013 850 851 `````` s_is_in_v_s_doc }, `````` André Anjos committed Dec 10, 2013 852 853 854 `````` { s_initialize_dual_lambda_mu_str, (PyCFunction)PyBobMathLpInteriorPoint_initialize_dual_lambda_mu, `````` André Anjos committed Dec 10, 2013 855 `````` METH_VARARGS|METH_KEYWORDS, `````` André Anjos committed Dec 10, 2013 856 857 `````` s_initialize_dual_lambda_mu_doc }, `````` André Anjos committed Dec 09, 2013 858 859 860 `````` {0} /* Sentinel */ }; `````` André Anjos committed Dec 10, 2013 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 ``````static PyObject* PyBobMathLpInteriorPoint_RichCompare (PyBobMathLpInteriorPointObject* self, PyBobMathLpInteriorPointObject* other, int op) { switch (op) { case Py_EQ: if (*(self->base) == *(other->base)) Py_RETURN_TRUE; Py_RETURN_FALSE; break; case Py_NE: if (*(self->base) != *(other->base)) Py_RETURN_TRUE; Py_RETURN_FALSE; break; default: Py_INCREF(Py_NotImplemented); return Py_NotImplemented; } } `````` André Anjos committed Dec 09, 2013 880 881 ``````PyTypeObject PyBobMathLpInteriorPoint_Type = { PyObject_HEAD_INIT(0) `````` André Anjos committed Dec 10, 2013 882 883 884 885 `````` 0, /*ob_size*/ s_lpinteriorpoint_str, /*tp_name*/ sizeof(PyBobMathLpInteriorPointObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ `````` André Anjos committed Dec 10, 2013 886 `````` 0, /*tp_dealloc*/ `````` André Anjos committed Dec 10, 2013 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 `````` 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ 0, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash */ 0, /*tp_call*/ 0, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ s_lpinteriorpoint_doc, /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ (richcmpfunc)PyBobMathLpInteriorPoint_RichCompare, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ PyBobMathLpInteriorPoint_methods, /* tp_methods */ 0, /* tp_members */ PyBobMathLpInteriorPoint_getseters, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ (initproc)PyBobMathLpInteriorPoint_init, /* tp_init */ 0, /* tp_alloc */ `````` André Anjos committed Dec 10, 2013 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 `````` 0, /* tp_new */ }; /**************************************************** * Implementation of LPInteriorPointShortstep class * ****************************************************/ PyDoc_STRVAR(s_lpinteriorpointshortstep_str, XBOB_EXT_MODULE_PREFIX ".LPInteriorPointShortstep"); PyDoc_STRVAR(s_lpinteriorpointshortstep_doc, "LPInteriorPointShortstep(M, N, theta, epsilon) -> new LPInteriorPointShortstep\n\ \n\ A Linear Program solver based on a short step interior point method.\n\ \n\ See :py:class:`LPInteriorPoint` for more details on the base class.\n\ \n\ Constructor parameters:\n\ \n\ M\n\ (int) first dimension of the A matrix\n\ \n\ N\n\ (int) second dimension of the A matrix\n\ \n\ theta\n\ (float) theta The value defining the size of the V2 neighborhood\n\ \n\ epsilon\n\ (float) The precision to determine whether an equality constraint\n\ is fulfilled or not.\n\ \n\ "); /* Type definition for PyBobMathLpInteriorPointObject */ typedef struct { PyBobMathLpInteriorPointObject parent; /* Type-specific fields go here. */ bob::math::LPInteriorPointShortstep* base; } PyBobMathLpInteriorPointShortstepObject; static int PyBobMathLpInteriorPointShortstep_init(PyBobMathLpInteriorPointShortstepObject* self, PyObject* args, PyObject* kwds) { /* Parses input arguments in a single shot */ static const char* const_kwlist[] = {"M", "N", "theta", "epsilon", 0}; static char** kwlist = const_cast(const_kwlist); Py_ssize_t M = 0; Py_ssize_t N = 0; double theta = 0.; double epsilon = 0.; if (!PyArg_ParseTupleAndKeywords(args, kwds, "nndd", kwlist, &M, &N, &theta, &epsilon)) return -1; self->base = new bob::math::LPInteriorPointShortstep(M, N, theta, epsilon); if (!self->base) { PyErr_SetString(PyExc_MemoryError, "could not allocate new LPInteriorPointShortstep object"); return -1; } self->parent.base = self->base; return 0; } static void PyBobMathLpInteriorPointShortstep_delete (PyBobMathLpInteriorPointShortstepObject* self) { delete self->base; self->parent.base = 0; self->base = 0; self->parent.ob_type->tp_free((PyObject*)self); } PyDoc_STRVAR(s_theta_str, "theta"); PyDoc_STRVAR(s_theta_doc, "The value theta used to define a V2 neighborhood" ); static PyObject* PyBobMathLpInteriorPointShortstep_getTheta (PyBobMathLpInteriorPointShortstepObject* self, void* /*closure*/) { return Py_BuildValue("d", self->base->getTheta()); } static int PyBobMathLpInteriorPointShortstep_setTheta (PyBobMathLpInteriorPointShortstepObject* self, PyObject* o, void* /*closure*/) { double e = PyFloat_AsDouble(o); if (PyErr_Occurred()) return -1; try { self->base->setTheta(e); } catch (std::exception& ex) { PyErr_SetString(PyExc_RuntimeError, ex.what()); return -1; } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot reset `theta' of LPInteriorPointShortstep: unknown exception caught"); return -1; } return 0; } static PyGetSetDef PyBobMathLpInteriorPointShortstep_getseters[] = { { s_theta_str, (getter)PyBobMathLpInteriorPointShortstep_getTheta, (setter)PyBobMathLpInteriorPointShortstep_setTheta, s_theta_doc, 0 }, {0} /* Sentinel */ }; static PyObject* PyBobMathLpInteriorPointShortstep_RichCompare (PyBobMathLpInteriorPointShortstepObject* self, PyBobMathLpInteriorPointShortstepObject* other, int op) { switch (op) { case Py_EQ: if (*(self->base) == *(other->base)) Py_RETURN_TRUE; Py_RETURN_FALSE; break; case Py_NE: if (*(self->base) != *(other->base)) Py_RETURN_TRUE; Py_RETURN_FALSE; break; default: Py_INCREF(Py_NotImplemented); return Py_NotImplemented; } } PyTypeObject PyBobMathLpInteriorPointShortstep_Type = { PyObject_HEAD_INIT(0) 0, /*ob_size*/ s_lpinteriorpointshortstep_str, /*tp_name*/ sizeof(PyBobMathLpInteriorPointShortstepObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ (destructor)PyBobMathLpInteriorPointShortstep_delete, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ 0, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash */ 0, /*tp_call*/ 0, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ 0, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ s_lpinteriorpointshortstep_doc, /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ (richcmpfunc)PyBobMathLpInteriorPointShortstep_RichCompare, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ 0, /* tp_methods */ 0, /* tp_members */ PyBobMathLpInteriorPointShortstep_getseters, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ (initproc)PyBobMathLpInteriorPointShortstep_init, /* tp_init */ 0, /* tp_alloc */ 0, /* tp_new */ `````` André Anjos committed Dec 09, 2013 1097 1098 1099 ``````}; /** `````` André Anjos committed Dec 04, 2013 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 ``````static bool is_in_vinf(bob::math::LPInteriorPointLongstep& op, bob::python::const_ndarray x, bob::python::const_ndarray mu, const double gamma) { return op.isInV(x.bz(), mu.bz(), gamma); } void bind_math_lp_interiorpoint() { class_, bases >("LPInteriorPointShortstep", "A Linear Program solver based on a short step interior point method", init((arg("self"), arg("M"), arg("N"), arg("theta"), arg("epsilon")), "Constructs a new LPInteriorPointShortstep solver")) .def(init((arg("self"), arg("solver")), "Copy constructs a solver")) .def(self == self) .def(self != self) .add_property("theta", &bob::math::LPInteriorPointShortstep::getTheta, &bob::math::LPInteriorPointShortstep::setTheta, "The value theta used to define a V2 neighborhood") ; class_, bases >("LPInteriorPointPredictorCorrector", "A Linear Program solver based on a predictor-corrector interior point method", init((arg("self"), arg("M"), arg("N"), arg("theta_pred"), arg("theta_corr"), arg("epsilon")), "Constructs a new LPInteriorPointPredictorCorrector solver")) .def(init((arg("self"), arg("solver")), "Copy constructs a solver")) .def(self == self) .def(self != self) .add_property("theta_pred", &bob::math::LPInteriorPointPredictorCorrector::getThetaPred, &bob::math::LPInteriorPointPredictorCorrector::setThetaPred, "The value theta_pred used to define a V2 neighborhood") .add_property("theta_corr", &bob::math::LPInteriorPointPredictorCorrector::getThetaCorr, &bob::math::LPInteriorPointPredictorCorrector::setThetaCorr, "The value theta_corr used to define a V2 neighborhood") ; class_, bases >("LPInteriorPointLongstep", "A Linear Program solver based on a ong step interior point method", init((arg("self"), arg("M"), arg("N"), arg("gamma"), arg("sigma"), arg("epsilon")), "Constructs a new LPInteriorPointLongstep solver")) .def(init((arg("self"), arg("solver")), "Copy constructs a solver")) .def(self == self) .def(self != self) .def("is_in_v", &is_in_vinf, (arg("self"), arg("x"), arg("mu"), arg("gamma")), "Check if a primal-dual point (x,lambda,mu) belongs to the V-inf neighborhood of the central path") .add_property("gamma", &bob::math::LPInteriorPointLongstep::getGamma, &bob::math::LPInteriorPointLongstep::setGamma, "The value gamma used to define a V-inf neighborhood") .add_property("sigma", &bob::math::LPInteriorPointLongstep::getSigma, &bob::math::LPInteriorPointLongstep::setSigma, "The value sigma used to define a V-inf neighborhood") ; } `````` André Anjos committed Dec 09, 2013 1134 ``**/``