lp_interior_point.cpp 29.8 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 09, 2013 17 18 19 20 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 49 50 51 52 53 54 55 56 57 ``````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. */ std::shared_ptr base; } PyBobMathLpInteriorPointObject; static PyObject* PyBobMathLpInteriorPoint_new(PyTypeObject* type, PyObject*, PyObject*) { /* Allocates the python object itself */ PyBobMathLpInteriorPointObject* self = (PyBobMathLpInteriorPointObject*)type->tp_alloc(type, 0); self->base.reset(); return reinterpret_cast(self); `````` André Anjos committed Dec 04, 2013 58 59 60 `````` } `````` André Anjos committed Dec 09, 2013 61 62 63 64 65 66 ``````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 67 68 ``````} `````` André Anjos committed Dec 09, 2013 69 70 71 72 73 ``````static void PyBobMathLpInteriorPoint_delete (PyBobMathLpInteriorPointObject* self) { self->base.reset(); self->ob_type->tp_free((PyObject*)self); `````` André Anjos committed Dec 04, 2013 74 75 ``````} `````` André Anjos committed Dec 09, 2013 76 77 78 79 80 ``````PyDoc_STRVAR(s_M_str, "M"); PyDoc_STRVAR(s_M_doc, "The first dimension of the problem/A matrix" ); `````` André Anjos committed Dec 10, 2013 81 82 ``````static PyObject* PyBobMathLpInteriorPoint_getM (PyBobMathLpInteriorPointObject* self, void* /*closure*/) { `````` André Anjos committed Dec 09, 2013 83 `````` return Py_BuildValue("n", self->base->getDimM()); `````` André Anjos committed Dec 04, 2013 84 85 ``````} `````` André Anjos committed Dec 10, 2013 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 ``````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 09, 2013 108 109 110 111 112 ``````PyDoc_STRVAR(s_N_str, "N"); PyDoc_STRVAR(s_N_doc, "The second dimension of the problem/A matrix" ); `````` André Anjos committed Dec 10, 2013 113 114 ``````static PyObject* PyBobMathLpInteriorPoint_getN (PyBobMathLpInteriorPointObject* self, void* /*closure*/) { `````` André Anjos committed Dec 09, 2013 115 116 117 `````` return Py_BuildValue("n", self->base->getDimN()); } `````` André Anjos committed Dec 10, 2013 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 ``````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 140 141 142 143 144 ``````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 145 ``````static PyObject* PyBobMathLpInteriorPoint_getEpsilon (PyBobMathLpInteriorPointObject* self, void* /*closure*/) { `````` André Anjos committed Dec 09, 2013 146 147 148 `````` return Py_BuildValue("d", self->base->getEpsilon()); } `````` André Anjos committed Dec 10, 2013 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 ``````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 09, 2013 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 ``````PyDoc_STRVAR(s_lambda_str, "lambda"); 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 208 209 `````` (getter)PyBobMathLpInteriorPoint_getM, (setter)PyBobMathLpInteriorPoint_setM, `````` André Anjos committed Dec 09, 2013 210 211 212 213 214 `````` s_M_doc, 0 }, { s_N_str, `````` André Anjos committed Dec 10, 2013 215 216 `````` (getter)PyBobMathLpInteriorPoint_getN, (setter)PyBobMathLpInteriorPoint_setN, `````` André Anjos committed Dec 09, 2013 217 218 219 220 221 `````` s_N_doc, 0 }, { s_epsilon_str, `````` André Anjos committed Dec 10, 2013 222 223 `````` (getter)PyBobMathLpInteriorPoint_getEpsilon, (setter)PyBobMathLpInteriorPoint_setEpsilon, `````` André Anjos committed Dec 09, 2013 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 338 339 340 341 342 343 344 345 346 347 348 349 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 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 `````` 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; } if (lambda->type_num != NPY_FLOAT64 || lambda->ndim != 1) { 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; } if (mu->type_num != NPY_FLOAT64 || mu->ndim != 1) { 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 569 570 `````` if (PyErr_Occurred()) return 0; `````` André Anjos committed Dec 09, 2013 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 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 `````` 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 635 636 `````` if (PyErr_Occurred()) return 0; `````` André Anjos committed Dec 09, 2013 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 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 `````` 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 769 770 `````` if (PyErr_Occurred()) return 0; `````` André Anjos committed Dec 09, 2013 771 772 773 `````` if (in_v_s) Py_RETURN_TRUE; Py_RETURN_FALSE; `````` André Anjos committed Dec 04, 2013 774 775 ``````} `````` André Anjos committed Dec 10, 2013 776 777 778 779 780 781 782 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 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 ``````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; double theta = 0.; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O&d", kwlist, &PyBlitzArray_Converter, &A, &PyBlitzArray_Converter, &c, &theta )) 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 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 ``````static PyMethodDef PyBobMathLpInteriorPoint_methods[] = { { s_reset_str, (PyCFunction)PyBobMathLpInteriorPoint_reset, METH_NOARGS, s_reset_doc }, { s_solve_str, (PyCFunction)PyBobMathLpInteriorPoint_solve, METH_NOARGS, s_solve_doc }, { s_is_feasible_str, (PyCFunction)PyBobMathLpInteriorPoint_is_feasible, METH_NOARGS, s_is_feasible_doc }, { s_is_in_v_str, (PyCFunction)PyBobMathLpInteriorPoint_is_in_v, METH_NOARGS, s_is_in_v_doc }, { s_is_in_v_s_str, (PyCFunction)PyBobMathLpInteriorPoint_is_in_v_s, METH_NOARGS, s_is_in_v_s_doc }, `````` André Anjos committed Dec 10, 2013 869 870 871 872 873 874 `````` { s_initialize_dual_lambda_mu_str, (PyCFunction)PyBobMathLpInteriorPoint_initialize_dual_lambda_mu, METH_NOARGS, s_initialize_dual_lambda_mu_doc }, `````` André Anjos committed Dec 09, 2013 875 876 877 `````` {0} /* Sentinel */ }; `````` André Anjos committed Dec 10, 2013 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 ``````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 897 898 ``````PyTypeObject PyBobMathLpInteriorPoint_Type = { PyObject_HEAD_INIT(0) `````` André Anjos committed Dec 10, 2013 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 `````` 0, /*ob_size*/ s_lpinteriorpoint_str, /*tp_name*/ sizeof(PyBobMathLpInteriorPointObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ (destructor)PyBobMathLpInteriorPoint_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_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 */ PyBobMathLpInteriorPoint_new, /* tp_new */ `````` André Anjos committed Dec 09, 2013 937 938 939 ``````}; /** `````` André Anjos committed Dec 04, 2013 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 ``````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() { .def(self == self) .def(self != self) 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 977 ``**/``