1 /* 2 Common definitions 3 4 Copyright (C) 2014 - 2016 AMPL Optimization Inc 5 6 Permission to use, copy, modify, and distribute this software and its 7 documentation for any purpose and without fee is hereby granted, 8 provided that the above copyright notice appear in all copies and that 9 both that the copyright notice and this permission notice and warranty 10 disclaimer appear in supporting documentation. 11 12 The author and AMPL Optimization Inc disclaim all warranties with 13 regard to this software, including all implied warranties of 14 merchantability and fitness. In no event shall the author be liable 15 for any special, indirect or consequential damages or any damages 16 whatsoever resulting from loss of use, data or profits, whether in an 17 action of contract, negligence or other tortious action, arising out 18 of or in connection with the use or performance of this software. 19 20 Author: Victor Zverovich 21 */ 22 23 #ifndef MP_COMMON_H_ 24 #define MP_COMMON_H_ 25 26 #include "mp/error.h" // for MP_ASSERT 27 #include <cstddef> // for std::size_t 28 29 /** The mp namespace. */ 30 namespace mp { 31 32 /** Variable information */ 33 namespace var { 34 /** Variable type. */ 35 enum Type { 36 CONTINUOUS, /**< A continuous variable. */ 37 INTEGER /**< An integer variable. */ 38 }; 39 } 40 41 /** Objective information. */ 42 namespace obj { 43 /** Objective type. */ 44 enum Type { 45 MIN = 0, /**< A minimization objective. */ 46 MAX = 1 /**< A maximization objective. */ 47 }; 48 } 49 50 /** Function information. */ 51 namespace func { 52 /** Function type. */ 53 enum Type { 54 /** A numeric function. */ 55 NUMERIC = 0, 56 /** A symbolic function accepting numeric and string arguments. */ 57 SYMBOLIC = 1 58 }; 59 } 60 61 /** Complementarity constraint information. */ 62 class ComplInfo { 63 private: 64 int flags_; 65 66 public: 67 // Flags for the constructor. 68 enum { 69 /** Constraint upper bound is infinity (finite variable lower bound). */ 70 INF_UB = 1, 71 /** Constraint lower bound is -infinity (finite variable upper bound). */ 72 INF_LB = 2 73 }; 74 75 /** 76 \rst 77 Constructs a `ComplInfo` object. 78 \endrst 79 */ 80 explicit ComplInfo(int flags) : flags_(flags) { 81 MP_ASSERT((flags & ~(INF_UB | INF_LB)) == 0, 82 "invalid complementarity flags"); 83 } 84 85 /** Constraint lower bound. */ 86 double con_lb() const { 87 return (flags_ & INF_LB) != 0 ? 88 -std::numeric_limits<double>::infinity() : 0; 89 } 90 91 /** Constraint upper bound. */ 92 double con_ub() const { 93 return (flags_ & INF_UB) != 0 ? std::numeric_limits<double>::infinity() : 0; 94 } 95 }; 96 97 /** Suffix information. */ 98 namespace suf { 99 /** Suffix kind. */ 100 enum Kind { 101 VAR = 0, /**< Applies to variables. */ 102 CON = 1, /**< Applies to constraints. */ 103 OBJ = 2, /**< Applies to objectives. */ 104 PROBLEM = 3 /**< Applies to problems. */ 105 }; 106 107 // SV disabled following line as not compilable with ancient MSVC 10.0 108 // constexpr int KIND_MASK = Kind::VAR | Kind::CON | Kind::OBJ | Kind ::PROBLEM; 109 // Suffix flags. 110 enum { 111 FLOAT = 4, /**< Suffix values are floating-point numbers. */ 112 IODECL = 8, /**< Declare an INOUT suffix. */ 113 OUTPUT = 0x10, /**< Output suffix: return values from a solver. */ 114 INPUT = 0x20, /**< Input suffix: values were passed to a solver. */ 115 OUTONLY = 0x40 /**< Output only: reject as an input value. */ 116 }; 117 } 118 119 namespace internal { 120 enum { 121 SUFFIX_KIND_MASK = 3, // Mask for suffix kinds. 122 NUM_SUFFIX_KINDS = 4 // The number of suffix kinds. 123 }; 124 } 125 126 namespace sol { 127 /** Solution status. */ 128 enum Status { 129 NOT_CHECKED = -200, 130 /** Unknown status. */ 131 UNKNOWN = -1, 132 133 /** 134 An optimal solution found for an optimization problem or a feasible 135 solution found for a satisfaction problem. 136 */ 137 SOLVED = 0, 138 139 /** Solution returned but it can be non-optimal or even infeasible. */ 140 UNCERTAIN = 100, 141 142 /** Problem is infeasible. */ 143 INFEASIBLE = 200, 144 145 /** Problem is unbounded. */ 146 UNBOUNDED = 300, 147 148 /** Stopped by a limit, e.g. on iterations or time. */ 149 LIMIT = 400, 150 151 /** A solver error. */ 152 FAILURE = 500, 153 154 /** Interrupted by the user. */ 155 INTERRUPTED = 600 156 }; 157 } 158 159 /** Expression information. */ 160 namespace expr { 161 162 /** 163 \rst 164 Expression kind. 165 Both AMPL-like and mathematical notation is given for each expression in the 166 descriptions below as in :math:`\mathrm{abs}(x) = |x|`, unless they are 167 identical such as :math:`\mathrm{sin}(x)` or there is no standard 168 mathematical notation. 169 \endrst 170 */ 171 enum Kind { 172 /** An unknown expression. */ 173 UNKNOWN = 0, 174 175 /** The first expression kind other than the unknown expression kind. */ 176 FIRST_EXPR, 177 178 /** 179 \rst 180 The first numeric expression kind. Numeric expression kinds are in 181 the range [`~mp::expr::FIRST_NUMERIC`, `~mp::expr::LAST_NUMERIC`]. 182 \endrst 183 */ 184 FIRST_NUMERIC = FIRST_EXPR, 185 186 /** 187 \rst 188 A number such as 42 or -1.23e-4. 189 \endrst 190 */ 191 NUMBER = FIRST_NUMERIC, 192 193 /** 194 \rst 195 The first reference expression kind. Reference expression kinds are in 196 the range [`~mp::expr::FIRST_REFERENCE`, `~mp::expr::LAST_REFERENCE`]. 197 \endrst 198 */ 199 FIRST_REFERENCE, 200 201 /** A reference to a variable. */ 202 VARIABLE = FIRST_REFERENCE, 203 204 /** A reference to a common expression. */ 205 COMMON_EXPR, 206 207 /** The last reference expression kind. */ 208 LAST_REFERENCE = COMMON_EXPR, 209 210 /** 211 \rst 212 The first unary numeric expression kind. Unary numeric expression kinds 213 are in the range [`~mp::expr::FIRST_UNARY`, `~mp::expr::LAST_UNARY`]. 214 \endrst 215 */ 216 FIRST_UNARY, 217 218 /** 219 \rst 220 A unary minus, :math:`-x`. 221 \endrst 222 */ 223 MINUS = FIRST_UNARY, 224 225 /** 226 \rst 227 The absolute value function, :math:`\mathrm{abs}(x) = |x|`. 228 \endrst 229 */ 230 ABS, 231 232 /** 233 \rst 234 The floor function, :math:`\mathrm{floor}(x) = \lfloor x \rfloor`. 235 \endrst 236 */ 237 FLOOR, 238 239 /** 240 \rst 241 The ceiling function, :math:`\mathrm{ceil}(x) = \lceil x \rceil`. 242 \endrst 243 */ 244 CEIL, 245 246 /** 247 \rst 248 The square root function, :math:`\mathrm{sqrt}(x) = \sqrt{x}`. 249 \endrst 250 */ 251 SQRT, 252 253 /** 254 \rst 255 Squaring: :math:`x \mathop{\verb!^!} 2 = x^2`. 256 \endrst 257 */ 258 POW2, 259 260 /** 261 \rst 262 The natural exponential function, :math:`\mathrm{exp}(x) = e^x`. 263 \endrst 264 */ 265 EXP, 266 267 /** 268 \rst 269 The natural logarithmic function, :math:`\mathrm{log}(x) = \mathrm{ln}(x)`. 270 \endrst 271 */ 272 LOG, 273 274 /** 275 \rst 276 The base 10 logarithmic function, 277 :math:`\mathrm{log10}(x) = \mathrm{log}_{10}(x)`. 278 \endrst 279 */ 280 LOG10, 281 282 /** 283 \rst 284 Sine, :math:`\mathrm{sin}(x)`. 285 \endrst 286 */ 287 SIN, 288 289 /** 290 \rst 291 Hyperbolic sine, :math:`\mathrm{sinh}(x)`. 292 \endrst 293 */ 294 SINH, 295 296 /** 297 \rst 298 Cosine, :math:`\mathrm{cos}(x)`. 299 \endrst 300 */ 301 COS, 302 303 /** 304 \rst 305 Hyperbolic cosine, :math:`\mathrm{cosh}(x)`. 306 \endrst 307 */ 308 COSH, 309 310 /** 311 \rst 312 Tangent, :math:`\mathrm{tan}(x)`. 313 \endrst 314 */ 315 TAN, 316 317 /** 318 \rst 319 Hyperbolic tangent, :math:`\mathrm{tan}(x)`. 320 \endrst 321 */ 322 TANH, 323 324 /** 325 \rst 326 Inverse sine, :math:`\mathrm{asin}(x) = \mathrm{sin}^{-1}(x)`. 327 \endrst 328 */ 329 ASIN, 330 331 /** 332 \rst 333 Inverse hyperbolic sine, :math:`\mathrm{asinh}(x) = \mathrm{sinh}^{-1}(x)`. 334 \endrst 335 */ 336 ASINH, 337 338 /** 339 \rst 340 Inverse cosine, :math:`\mathrm{acos}(x) = \mathrm{cos}^{-1}(x)`. 341 \endrst 342 */ 343 ACOS, 344 345 /** 346 \rst 347 Inverse hyperbolic cosine, 348 :math:`\mathrm{acosh}(x) = \mathrm{cosh}^{-1}(x)`. 349 \endrst 350 */ 351 ACOSH, 352 353 /** 354 \rst 355 Inverse tangent, :math:`\mathrm{atan}(x) = \mathrm{tan}^{-1}(x)`. 356 \endrst 357 */ 358 ATAN, 359 360 /** 361 \rst 362 Inverse hyperbolic tangent, 363 :math:`\mathrm{atanh}(x) = \mathrm{tanh}^{-1}(x)`. 364 \endrst 365 */ 366 ATANH, 367 368 369 /** The last unary numeric expression kind. */ 370 LAST_UNARY = ATANH, 371 372 /** 373 \rst 374 The first binary expression kind. Binary expression kinds are in 375 the range [`~mp::expr::FIRST_BINARY`, `~mp::expr::LAST_BINARY`]. 376 \endrst 377 */ 378 FIRST_BINARY, 379 380 /** 381 \rst 382 Addition, :math:`x + y`. 383 \endrst 384 */ 385 ADD = FIRST_BINARY, 386 387 /** 388 \rst 389 Subtraction, :math:`x - y`. 390 \endrst 391 */ 392 SUB, 393 394 /** 395 \rst 396 The :math:`\mathrm{less}` operation, 397 :math:`x \mathop{\rm less} y = \mathrm{max}(x - y, 0)`. 398 \endrst 399 */ 400 LESS, 401 402 /** 403 \rst 404 Multiplication, :math:`x * y = x y`. 405 \endrst 406 */ 407 MUL, 408 409 /** 410 \rst 411 Division, :math:`x / y`. 412 \endrst 413 */ 414 DIV, 415 416 /** 417 \rst 418 Truncated division, :math:`x \mathop{\rm div} y = \mathrm{trunc}(x / y)`. 419 \endrst 420 */ 421 TRUNC_DIV, 422 423 /** 424 \rst 425 The modulo operation, :math:`x \mathop{\rm mod} y`. 426 \endrst 427 */ 428 MOD, 429 430 /** 431 \rst 432 Exponentiation, :math:`x \mathop{\verb!^!} y = x^y`. 433 \endrst 434 */ 435 POW, 436 437 /** 438 \rst 439 Exponentiation with a constant base, :math:`a^x`. 440 \endrst 441 */ 442 POW_CONST_BASE, 443 444 /** 445 \rst 446 Exponentiation with a constant exponent :math:`x^a`. 447 \endrst 448 */ 449 POW_CONST_EXP, 450 451 /** 452 \rst 453 Inverse tangent, :math:`\mathrm{atan2}(y, x) = \mathrm{tan}^{-1}(y/x)`. 454 \endrst 455 */ 456 ATAN2, 457 458 /** 459 \rst 460 The function :math:`\mathrm{precision}(x, n)` which returns :math:`x` 461 rounded to :math:`n` significant decimal digits. 462 \endrst 463 */ 464 PRECISION, 465 466 /** 467 \rst 468 The function :math:`\mathrm{round}(x, n)` which returns :math:`x` 469 rounded to :math:`n` digits past decimal point. 470 \endrst 471 */ 472 ROUND, 473 474 /** 475 \rst 476 The function :math:`\mathrm{trunc}(x, n)` which returns :math:`x` 477 truncated to :math:`n` digits past decimal point. 478 \endrst 479 */ 480 TRUNC, 481 482 /** The last binary numeric expression kind. */ 483 LAST_BINARY = TRUNC, 484 485 /** 486 \rst 487 An if-then-else expression, 488 :math:`\mathrm{if}\;c\;\mathrm{then}\;e_1\;[\mathrm{else}\;e_2]`, 489 where :math:`c` is a logical expression representing condition, while 490 :math:`e_1` and :math:`e_2` are numeric expressions. The expression 491 evaluates to :math:`e_1` if :math:`c` is true and to :math:`e_2` otherwise. 492 If the else clause is omitted, :math:`e_2` is assumed to be zero. 493 \endrst 494 */ 495 IF, 496 497 /** 498 \rst 499 A piecewise-linear term, 500 :math:`\verb|<<|b_1, ..., b_n; s_1, ..., s_{n + 1}\verb|>> | r`, 501 where :math:`b_i` are breakpoints, :math:`s_i` are slopes and :math:`r` is 502 a `reference <mp::expr::FIRST_REFERENCE>`. 503 \endrst 504 */ 505 PLTERM, 506 507 /** 508 \rst 509 A function call, :math:`f(e_1, ..., e_n)`, where :math:`f` is a function 510 name and :math:`e_i` are numeric or string expressions. 511 \endrst 512 */ 513 CALL, 514 515 /** 516 \rst 517 The first iterated expression kind. Iterated expression kinds are in 518 the range [`~mp::expr::FIRST_ITERATED`, `~mp::expr::LAST_ITERATED`]. 519 520 The term "iterated" in the context of operators and expressions comes 521 from the article `AMPL: A Mathematical Programming Language 522 <http://www.ampl.com/REFS/amplmod.pdf>`_ and is used to denote operators 523 indexed over sets. 524 \endrst 525 */ 526 FIRST_ITERATED, 527 528 /** 529 \rst 530 A vararg expression, :math:`\mathrm{min}` or :math:`\mathrm{max}`. 531 Vararg expression kinds are in the range 532 [`~mp::expr::FIRST_VARARG`, `~mp::expr::LAST_VARARG`]. 533 \endrst 534 */ 535 FIRST_VARARG = FIRST_ITERATED, 536 537 /** 538 \rst 539 Minimum, :math:`\mathrm{min}(e_1, ..., e_n) = \min_{i=1,...,n} e_i`. 540 \endrst 541 */ 542 MIN = FIRST_VARARG, 543 544 /** 545 \rst 546 Maximum, :math:`\mathrm{max}(e_1, ..., e_n) = \max_{i=1,...,n} e_i`. 547 \endrst 548 */ 549 MAX, 550 551 /** The last vararg expression kind. */ 552 LAST_VARARG = MAX, 553 554 /** 555 \rst 556 Summation, :math:`\mathrm{sum}(e_1, ..., e_n) = \sum_{i=1}^n e_i`. 557 \endrst 558 */ 559 SUM, 560 561 /** 562 \rst 563 A :math:`\mathrm{numberof}` expression, 564 :math:`\mathrm{numberof}\;e_0\;\mathrm{in}\;(e_1, ..., e_n)`, which 565 evaluates to the number of times the value of :math:`e_0` appears among the 566 values of :math:`e_1, ..., e_n`. 567 \endrst 568 */ 569 NUMBEROF, 570 571 /** The last iterated expression kind. */ 572 LAST_ITERATED = NUMBEROF, 573 574 /** 575 \rst 576 A symbolic :math:`\mathrm{numberof}` expression. 577 :math:`\mathrm{numberof}\;s_0\;\mathrm{in}\;(s_1, ..., s_n)`, which 578 evaluates to the number of times the value of :math:`s_0` appears among the 579 values of :math:`s_1, ..., s_n`. 580 \endrst 581 */ 582 NUMBEROF_SYM, 583 584 /** 585 \rst 586 A :math:`\mathrm{count}` expression, :math:`\mathrm{count}(l_1, ..., l_n)`, 587 where :math:`l_i` are logical expressions. This expression evaluates to 588 the number of :math:`l_i` whose values are true. 589 \endrst 590 */ 591 COUNT, 592 593 /** The last numeric expression kind. */ 594 LAST_NUMERIC = COUNT, 595 596 /** 597 \rst 598 The first logical expression kind. Logical expression kinds are in 599 the range [`~mp::expr::FIRST_LOGICAL`, `~mp::expr::LAST_LOGICAL`]. 600 \endrst 601 */ 602 FIRST_LOGICAL, 603 604 /** 605 \rst 606 A Boolean (logical) constant, true or false. 607 \endrst 608 */ 609 BOOL = FIRST_LOGICAL, 610 611 /** 612 \rst 613 A logical not, :math:`!l`, where :math:`l` is a logical expression. 614 \endrst 615 */ 616 NOT, 617 618 /** 619 \rst 620 The first binary logical expression kind. 621 Binary logical expression kinds are in the range 622 [`~mp::expr::FIRST_BINARY_LOGICAL`, `~mp::expr::LAST_BINARY_LOGICAL`]. 623 \endrst 624 */ 625 FIRST_BINARY_LOGICAL, 626 627 /** 628 \rst 629 Logical or, :math:`l_1` || :math:`l_2`. 630 \endrst 631 */ 632 OR = FIRST_BINARY_LOGICAL, 633 634 /** 635 \rst 636 Logical and, :math:`l_1` && :math:`l_2`. 637 \endrst 638 */ 639 AND, 640 641 /** 642 \rst 643 If and only if, :math:`l_1` <==> :math:`l_2`. 644 \endrst 645 */ 646 IFF, 647 648 /** The last binary logical expression kind. */ 649 LAST_BINARY_LOGICAL = IFF, 650 651 /** 652 \rst 653 The first relational expression kind. Relational expression kinds are in 654 the range [`~mp::expr::FIRST_RELATIONAL`, `~mp::expr::LAST_RELATIONAL`]. 655 \endrst 656 */ 657 FIRST_RELATIONAL, 658 659 /** 660 \rst 661 Less than, :math:`e_1` < :math:`e_2`. 662 \endrst 663 */ 664 LT = FIRST_RELATIONAL, 665 666 /** 667 \rst 668 Less or equal to, :math:`e_1` <= :math:`e_2`. 669 \endrst 670 */ 671 LE, 672 673 /** 674 \rst 675 Equal to, :math:`e_1` = :math:`e_2`. 676 \endrst 677 */ 678 EQ, 679 680 /** 681 \rst 682 Greater or equal to, :math:`e_1` >= :math:`e_2`. 683 \endrst 684 */ 685 GE, 686 687 /** 688 \rst 689 Greater than, :math:`e_1` > :math:`e_2`. 690 \endrst 691 */ 692 GT, 693 694 /** 695 \rst 696 Not equal to, :math:`e_1` != :math:`e_2`. 697 \endrst 698 */ 699 NE, 700 701 /** The last relational expression kind. */ 702 LAST_RELATIONAL = NE, 703 704 /** 705 \rst 706 The first logical count expression kind. 707 Logical count expression kinds are in the range 708 [`~mp::expr::FIRST_LOGICAL_COUNT`, `~mp::expr::LAST_LOGICAL_COUNT`]. 709 \endrst 710 */ 711 FIRST_LOGICAL_COUNT, 712 713 /** 714 \rst 715 An :math:`\mathrm{atleast}` expression, 716 :math:`\mathrm{atleast}\;e\;(l_1, ..., l_n)`, where :math:`e` is a numeric 717 expression and :math:`l_i` are logical expressions. It evaluates to true if 718 at least :math:`e` expressions :math:`l_i` are true. 719 \endrst 720 */ 721 ATLEAST = FIRST_LOGICAL_COUNT, 722 723 /** 724 \rst 725 An :math:`\mathrm{atmost}` expression, 726 :math:`\mathrm{atmost}\;e\;(l_1, ..., l_n)`, where :math:`e` is a numeric 727 expression and :math:`l_i` are logical expressions. It evaluates to true if 728 at most :math:`e` expressions :math:`l_i` are true. 729 \endrst 730 */ 731 ATMOST, 732 733 /** 734 \rst 735 An :math:`\mathrm{exactly}` expression, 736 :math:`\mathrm{exactly}\;e\;(l_1, ..., l_n)`, where :math:`e` is a numeric 737 expression and :math:`l_i` are logical expressions. It evaluates to true if 738 exactly :math:`e` expressions :math:`l_i` are true. 739 \endrst 740 */ 741 EXACTLY, 742 743 /** 744 \rst 745 The negation of an :math:`\mathrm{atleast}` expression, 746 :math:`!\mathrm{atleast}\;e\;(l_1, ..., l_n)`. 747 \endrst 748 */ 749 NOT_ATLEAST, 750 751 /** 752 \rst 753 The negation of an :math:`\mathrm{atmost}` expression, 754 :math:`!\mathrm{atmost}\;e\;(l_1, ..., l_n)`. 755 \endrst 756 */ 757 NOT_ATMOST, 758 759 /** 760 \rst 761 The negation of an :math:`\mathrm{exactly}` expression, 762 :math:`!\mathrm{exactly}\;e\;(l_1, ..., l_n)`. 763 \endrst 764 */ 765 NOT_EXACTLY, 766 767 /** The last logical count expression kind. */ 768 LAST_LOGICAL_COUNT = NOT_EXACTLY, 769 770 /** 771 \rst 772 An implication expression, 773 :math:`c\;\verb|==>|\;l_1\;[\mathrm{else}\;l_2]`, 774 where :math:`c` is a logical expression representing condition, while 775 :math:`l_1` and :math:`l_2` are logical expressions. The expression 776 evaluates to :math:`l_1` if :math:`c` is true and to :math:`l_2` otherwise. 777 If the else clause is omitted, :math:`l_2` is assumed to be true. 778 \endrst 779 */ 780 IMPLICATION, 781 782 /** 783 \rst 784 The first iterated logical expression kind. 785 Iterated logical expression kinds are in the range 786 [`~mp::expr::FIRST_ITERATED_LOGICAL`, `~mp::expr::LAST_ITERATED_LOGICAL`]. 787 \endrst 788 */ 789 FIRST_ITERATED_LOGICAL, 790 791 /** 792 \rst 793 An :math:`\mathrm{exists}` expression, 794 :math:`\mathrm{exists}(l_1, ..., l_n)`, where :math:`l_i` are logical 795 expressions. It evaluates to true if at least one :math:`l_i` is true. 796 \endrst 797 */ 798 EXISTS = FIRST_ITERATED_LOGICAL, 799 800 /** 801 \rst 802 A :math:`\mathrm{forall}` expression, 803 :math:`\mathrm{forall}(l_1, ..., l_n)`, where :math:`l_i` are logical 804 expressions. It evaluates to true if all :math:`l_i` are true. 805 \endrst 806 */ 807 FORALL, 808 809 /** The last iterated logical expression kind. */ 810 LAST_ITERATED_LOGICAL = FORALL, 811 812 /** 813 \rst 814 The first pairwise expression kind. Pairwise expression kinds are in the 815 range [`~mp::expr::FIRST_PAIRWISE`, `~mp::expr::LAST_PAIRWISE`]. 816 \endrst 817 */ 818 FIRST_PAIRWISE, 819 820 /** 821 \rst 822 An alldifferent expression, :math:`\mathrm{alldiff}(e_1, ..., e_n)`, 823 where :math:`e_i` are numeric expressions. It evaluates to true if all 824 :math:`e_i` take different values. 825 \endrst 826 */ 827 ALLDIFF = FIRST_PAIRWISE, 828 829 /** 830 \rst 831 The negation of an alldifferent expression, 832 :math:`!\mathrm{alldiff}(e_1, ..., e_n)`. 833 \endrst 834 */ 835 NOT_ALLDIFF, 836 837 /** The last pairwise expression kind. */ 838 LAST_PAIRWISE = NOT_ALLDIFF, 839 840 /** The last logical expression kind. */ 841 LAST_LOGICAL = LAST_PAIRWISE, 842 843 /** A string such as "abc". */ 844 STRING, 845 846 /** 847 \rst 848 A symbolic if-then-else expression. 849 :math:`\mathrm{if}\;c\;\mathrm{then}\;e_1\;[\mathrm{else}\;e_2]`, 850 where :math:`c` is a logical expression representing condition, while 851 :math:`e_1` and :math:`e_2` are numeric or string expressions. 852 The expression evaluates to :math:`e_1` if :math:`c` is true and to 853 :math:`e_2` otherwise. If :math:`e_2` is omitted, it is assumed to be zero. 854 \endrst 855 */ 856 IFSYM, 857 858 /** The last expression kind. */ 859 LAST_EXPR = IFSYM 860 }; 861 862 /** 863 \rst 864 Returns the string representation of the given expression kind. 865 Expressions of different kinds can have identical strings. 866 For example, `~mp::expr::POW`, `~mp::expr::POW_CONST_BASE` and 867 `~mp::expr::POW_CONST_EXP` all have the same representation "^". 868 \endrst 869 */ 870 const char *str(expr::Kind kind); 871 872 /** Returns the NL opcode for the given expression kind. */ 873 int nl_opcode(expr::Kind kind); 874 } // namespace expr 875 876 #define MP_CONST_DISPATCH(call) static_cast<const Impl*>(this)->call 877 #define MP_DISPATCH(call) static_cast<Impl*>(this)->call 878 #define MP_DISPATCH_STATIC(call) Impl::call 879 880 namespace internal { 881 882 // Suppresses warnings about unused variables. 883 inline void Unused(...) {} 884 885 // Returns true if ExprType is of kind k. 886 template <typename ExprType> 887 inline bool Is(expr::Kind k) { 888 int kind = k; 889 // If FIRST_KIND == LAST_KIND, then a decent optimizing compiler simplifies 890 // this to kind == ExprType::FIRST_KIND (checked with GCC 4.8.2). 891 // No need to do it ourselves. 892 return ExprType::FIRST_KIND <= kind && kind <= ExprType::LAST_KIND; 893 } 894 895 inline bool IsValid(expr::Kind kind) { 896 return kind >= expr::UNKNOWN && kind <= expr::LAST_EXPR; 897 } 898 899 // Expression information. 900 class ExprInfo { 901 private: 902 static const ExprInfo INFO[]; 903 904 friend int expr::nl_opcode(expr::Kind kind); 905 friend const char *expr::str(expr::Kind kind); 906 907 public: 908 int opcode; 909 const char *str; 910 }; 911 912 // Maximum NL opcode. 913 enum { MAX_OPCODE = 82 }; 914 915 class OpCodeInfo { 916 private: 917 static const OpCodeInfo INFO[MAX_OPCODE + 1]; 918 919 public: 920 expr::Kind kind; 921 expr::Kind first_kind; // First member of a kind. 922 923 friend const OpCodeInfo &GetOpCodeInfo(int opcode); 924 }; 925 926 inline const OpCodeInfo &GetOpCodeInfo(int opcode) { 927 MP_ASSERT(opcode >= 0 && opcode <= MAX_OPCODE, "invalid opcode"); 928 return OpCodeInfo::INFO[opcode]; 929 } 930 } // namespace internal 931 932 inline int expr::nl_opcode(expr::Kind kind) { 933 MP_ASSERT(internal::IsValid(kind), "invalid expression kind"); 934 return internal::ExprInfo::INFO[kind].opcode; 935 } 936 937 inline const char *expr::str(expr::Kind kind) { 938 MP_ASSERT(internal::IsValid(kind), "invalid expression kind"); 939 return internal::ExprInfo::INFO[kind].str; 940 } 941 942 /** Information about an optimization problem. */ 943 struct ProblemInfo { 944 /** Total number of variables. */ 945 int num_vars; 946 947 /** 948 Number of algebraic constraints including ranges and equality constraints. 949 It doesn't include logical constraints. 950 */ 951 int num_algebraic_cons; 952 953 /** Total number of objectives. */ 954 int num_objs; 955 956 /** Number of ranges (constraints with -Infinity < LHS < RHS < Infinity). */ 957 int num_ranges; 958 959 /** 960 Number of equality constraints or -1 if unknown (AMPL prior to 19970627). 961 */ 962 int num_eqns; 963 964 /** Number of logical constraints. */ 965 int num_logical_cons; 966 967 /** Returns the number of integer variables (includes binary variable). */ 968 int num_integer_vars() const { 969 return num_linear_binary_vars + num_linear_integer_vars + 970 num_nl_integer_vars_in_both + num_nl_integer_vars_in_cons + 971 num_nl_integer_vars_in_objs; 972 } 973 974 /** Returns the number of continuous variables. */ 975 int num_continuous_vars() const { return num_vars - num_integer_vars(); } 976 977 // Nonlinear and complementarity information 978 // ----------------------------------------- 979 980 /** Total number of nonlinear constraints. */ 981 int num_nl_cons; 982 983 /** Total number of nonlinear objectives. */ 984 int num_nl_objs; 985 986 /** Total number of complementarity conditions. */ 987 int num_compl_conds; 988 989 /** Number of nonlinear complementarity conditions. */ 990 int num_nl_compl_conds; 991 992 /** Number of complementarities involving double inequalities. */ 993 int num_compl_dbl_ineqs; 994 995 /** Number of complemented variables with a nonzero lower bound. */ 996 int num_compl_vars_with_nz_lb; 997 998 // Information about network constraints 999 // ------------------------------------- 1000 1001 /** Number of nonlinear network constraints. */ 1002 int num_nl_net_cons; 1003 1004 /** Number of linear network constraints. */ 1005 int num_linear_net_cons; 1006 1007 // Information about nonlinear variables 1008 // ------------------------------------- 1009 1010 /** 1011 Number of nonlinear variables in constraints including nonlinear 1012 variables in both constraints and objectives. 1013 */ 1014 int num_nl_vars_in_cons; 1015 1016 /** 1017 Number of nonlinear variables in objectives including nonlinear 1018 variables in both constraints and objectives. 1019 */ 1020 int num_nl_vars_in_objs; 1021 1022 /** Number of nonlinear variables in both constraints and objectives. */ 1023 int num_nl_vars_in_both; 1024 1025 // Miscellaneous 1026 // ------------- 1027 1028 /** Number of linear network variables (arcs). */ 1029 int num_linear_net_vars; 1030 1031 /** Number of functions. */ 1032 int num_funcs; 1033 1034 // Information about discrete variables 1035 // ------------------------------------ 1036 1037 /** Number of linear binary variables. */ 1038 int num_linear_binary_vars; 1039 1040 /** Number of linear non-binary integer variables. */ 1041 int num_linear_integer_vars; 1042 1043 /** 1044 Number of integer nonlinear variables in both constraints and objectives. 1045 */ 1046 int num_nl_integer_vars_in_both; 1047 1048 /** Number of integer nonlinear variables just in constraints. */ 1049 int num_nl_integer_vars_in_cons; 1050 1051 /** Number of integer nonlinear variables just in objectives. */ 1052 int num_nl_integer_vars_in_objs; 1053 1054 // Information about nonzeros 1055 // -------------------------- 1056 1057 /** Number of nonzeros in constraints' Jacobian. */ 1058 std::size_t num_con_nonzeros; 1059 1060 /** Number of nonzeros in all objective gradients. */ 1061 std::size_t num_obj_nonzeros; 1062 1063 // Information about names 1064 // ----------------------- 1065 1066 /** Length of longest constraint name if names are available. */ 1067 int max_con_name_len; 1068 1069 /** Length of longest variable name if names are available. */ 1070 int max_var_name_len; 1071 1072 // Information about common expressions 1073 // ------------------------------------ 1074 1075 /** 1076 Number of common expressions that appear both in constraints 1077 and objectives. 1078 */ 1079 int num_common_exprs_in_both; 1080 1081 /** 1082 Number of common expressions that appear in multiple constraints 1083 and don't appear in objectives. 1084 */ 1085 int num_common_exprs_in_cons; 1086 1087 /** 1088 Number of common expressions that appear in multiple objective 1089 and don't appear in constraints. 1090 */ 1091 int num_common_exprs_in_objs; 1092 1093 /** 1094 Number of common expressions that only appear in a single constraint 1095 and don't appear in objectives. 1096 */ 1097 int num_common_exprs_in_single_cons; 1098 1099 /** 1100 Number of common expressions that only appear in a single objective 1101 and don't appear in constraints. 1102 */ 1103 int num_common_exprs_in_single_objs; 1104 1105 /** Returns the total number of common expressions. */ 1106 int num_common_exprs() const { 1107 return num_common_exprs_in_both + num_common_exprs_in_cons + 1108 num_common_exprs_in_objs + num_common_exprs_in_single_cons + 1109 num_common_exprs_in_single_objs; 1110 } 1111 }; 1112 /* SV disabled enum classes as not compilable with ancient MSVC 10.0 1113 enum class IISStatus { 1114 non = 0, 1115 low = 1, 1116 fix = 2, 1117 upp = 3, 1118 mem = 4, 1119 pmem = 5, 1120 plow = 6, 1121 pupp = 7, 1122 bug = 8 1123 }; 1124 1125 enum class BasicStatus { 1126 none= 0, 1127 bas = 1, 1128 sup = 2, 1129 low = 3, 1130 upp = 4, 1131 equ = 5, 1132 btw = 6 1133 }; 1134 */ 1135 } // namespace mp 1136 1137 #endif // MP_COMMON_H_ 1138