lp_interior_point.cpp 25.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 81 82 ``````PyDoc_STRVAR(s_M_str, "M"); PyDoc_STRVAR(s_M_doc, "The first dimension of the problem/A matrix" ); static PyObject* PyBobMathLpInteriorPoint_M (PyBobMathLpInteriorPointObject* self) { return Py_BuildValue("n", self->base->getDimM()); `````` André Anjos committed Dec 04, 2013 83 84 ``````} `````` André Anjos committed Dec 09, 2013 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 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 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 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 620 621 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 ``````PyDoc_STRVAR(s_N_str, "N"); PyDoc_STRVAR(s_N_doc, "The second dimension of the problem/A matrix" ); static PyObject* PyBobMathLpInteriorPoint_N (PyBobMathLpInteriorPointObject* self) { return Py_BuildValue("n", self->base->getDimN()); } PyDoc_STRVAR(s_epsilon_str, "epsilon"); PyDoc_STRVAR(s_epsilon_doc, "The precision to determine whether an equality constraint is fulfilled or not" ); static PyObject* PyBobMathLpInteriorPoint_epsilon (PyBobMathLpInteriorPointObject* self) { return Py_BuildValue("d", self->base->getEpsilon()); } 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, (getter)PyBobMathLpInteriorPoint_M, 0, s_M_doc, 0 }, { s_N_str, (getter)PyBobMathLpInteriorPoint_N, 0, s_N_doc, 0 }, { s_epsilon_str, (getter)PyBobMathLpInteriorPoint_epsilon, 0, 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()); return 0; } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot solve LPInteriorPoint: unknown exception caught"); return 0; } 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()); return 0; } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot check feasibility of LPInteriorPoint: unknown exception caught"); return 0; } Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); 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()); return 0; } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot check if point is in V at LPInteriorPoint: unknown exception caught"); return 0; } Py_DECREF(x); Py_DECREF(mu); 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()); return 0; } catch (...) { PyErr_Format(PyExc_RuntimeError, "cannot check if point is in VS at LPInteriorPoint: unknown exception caught"); return 0; } Py_DECREF(A); Py_DECREF(b); Py_DECREF(c); Py_DECREF(x); Py_DECREF(lambda); Py_DECREF(mu); if (in_v_s) Py_RETURN_TRUE; Py_RETURN_FALSE; `````` André Anjos committed Dec 04, 2013 708 709 ``````} `````` André Anjos committed Dec 09, 2013 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 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 ``````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 }, {0} /* Sentinel */ }; PyTypeObject PyBobMathLpInteriorPoint_Type = { PyObject_HEAD_INIT(0) 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 */ 0, /* 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 04, 2013 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 ``````static void initialize_dual_lambda_mu(bob::math::LPInteriorPoint& op, bob::python::const_ndarray A, bob::python::const_ndarray c) { op.initializeDualLambdaMu(A.bz(), c.bz()); } 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) .def("initialize_dual_lambda_mu", &initialize_dual_lambda_mu, (arg("self"), arg("A"), arg("c")), "Initialize the dual variables lambda and mu by minimizing the logarithmic barrier function") ; 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 832 ``**/``