1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 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 769 770 771 772 773 774 775 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 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 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 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 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 1097 1098 1099 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 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
|
#! /usr/bin/env python
# PuLP : Python LP Modeler
# Copyright (c) 2002-2005, Jean-Sebastien Roy (js@jeannot.org)
# Modifications Copyright (c) 2007- Stuart Anthony Mitchell (s.mitchell@auckland.ac.nz)
# $Id: pulp.py 1791 2008-04-23 22:54:34Z smit023 $
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
PuLP is an LP modeler written in python. PuLP can generate MPS or LP files
and call GLPK[1], COIN CLP/CBC[2], CPLEX[3], GUROBI[4] and MOSEK[5] to solve linear
problems.
See the examples directory for examples.
The examples require at least a solver in your PATH or a shared library file.
Documentation is found on https://www.coin-or.org/PuLP/.
A comprehensive wiki can be found at https://www.coin-or.org/PuLP/
Use LpVariable() to create new variables. To create a variable 0 <= x <= 3
>>> x = LpVariable("x", 0, 3)
To create a variable 0 <= y <= 1
>>> y = LpVariable("y", 0, 1)
Use LpProblem() to create new problems. Create "myProblem"
>>> prob = LpProblem("myProblem", const.LpMinimize)
Combine variables to create expressions and constraints and add them to the
problem.
>>> prob += x + y <= 2
If you add an expression (not a constraint), it will
become the objective.
>>> prob += -4 * x + y
Choose a solver and solve the problem. ex:
>>> status = prob.solve(PULP_CBC_CMD(msg=0))
Display the status of the solution
>>> const.LpStatus[status]
'Optimal'
You can get the value of the variables using value(). ex:
>>> value(x)
2.0
Exported Classes:
- LpProblem -- Container class for a Linear programming problem
- LpVariable -- Variables that are added to constraints in the LP
- LpConstraint -- A constraint of the general form
a1x1+a2x2 ...anxn (<=, =, >=) b
- LpConstraintVar -- Used to construct a column of the model in column-wise
modelling
Exported Functions:
- value() -- Finds the value of a variable or expression
- lpSum() -- given a list of the form [a1*x1, a2x2, ..., anxn] will construct
a linear expression to be used as a constraint or variable
- lpDot() --given two lists of the form [a1, a2, ..., an] and
[ x1, x2, ..., xn] will construct a linear epression to be used
as a constraint or variable
Comments, bug reports, patches and suggestions are welcome.
https://github.com/coin-or/pulp
References:
[1] http://www.gnu.org/software/glpk/glpk.html
[2] http://www.coin-or.org/
[3] http://www.cplex.com/
[4] http://www.gurobi.com/
[5] http://www.mosek.com/
"""
import sys
import warnings
from time import time
from .apis import LpSolverDefault, PULP_CBC_CMD
from .apis.core import clock
from .utilities import value
from . import constants as const
from . import mps_lp as mpslp
try:
from collections.abc import Iterable
except ImportError:
# python 2.7 compatible
from collections import Iterable
import logging
log = logging.getLogger(__name__)
try: # allow Python 2/3 compatibility
maketrans = str.maketrans
except AttributeError:
from string import maketrans
_DICT_TYPE = dict
if sys.platform not in ["cli"]:
# iron python does not like an OrderedDict
try:
from odict import OrderedDict
_DICT_TYPE = OrderedDict
except ImportError:
pass
try:
# python 2.7 or 3.1
from collections import OrderedDict
_DICT_TYPE = OrderedDict
except ImportError:
pass
try:
import ujson as json
except ImportError:
import json
import re
class LpElement(object):
"""Base class for LpVariable and LpConstraintVar"""
# To remove illegal characters from the names
illegal_chars = "-+[] ->/"
expression = re.compile("[{}]".format(re.escape(illegal_chars)))
trans = maketrans(illegal_chars, "________")
def setName(self, name):
if name:
if self.expression.match(name):
warnings.warn(
"The name {} has illegal characters that will be replaced by _".format(
name
)
)
self.__name = str(name).translate(self.trans)
else:
self.__name = None
def getName(self):
return self.__name
name = property(fget=getName, fset=setName)
def __init__(self, name):
self.name = name
# self.hash MUST be different for each variable
# else dict() will call the comparison operators that are overloaded
self.hash = id(self)
self.modified = True
def __hash__(self):
return self.hash
def __str__(self):
return self.name
def __repr__(self):
return self.name
def __neg__(self):
return -LpAffineExpression(self)
def __pos__(self):
return self
def __bool__(self):
return 1
def __add__(self, other):
return LpAffineExpression(self) + other
def __radd__(self, other):
return LpAffineExpression(self) + other
def __sub__(self, other):
return LpAffineExpression(self) - other
def __rsub__(self, other):
return other - LpAffineExpression(self)
def __mul__(self, other):
return LpAffineExpression(self) * other
def __rmul__(self, other):
return LpAffineExpression(self) * other
def __div__(self, other):
return LpAffineExpression(self) / other
def __rdiv__(self, other):
raise TypeError("Expressions cannot be divided by a variable")
def __le__(self, other):
return LpAffineExpression(self) <= other
def __ge__(self, other):
return LpAffineExpression(self) >= other
def __eq__(self, other):
return LpAffineExpression(self) == other
def __ne__(self, other):
if isinstance(other, LpVariable):
return self.name is not other.name
elif isinstance(other, LpAffineExpression):
if other.isAtomic():
return self is not other.atom()
else:
return 1
else:
return 1
class LpVariable(LpElement):
"""
This class models an LP Variable with the specified associated parameters
:param name: The name of the variable used in the output .lp file
:param lowBound: The lower bound on this variable's range.
Default is negative infinity
:param upBound: The upper bound on this variable's range.
Default is positive infinity
:param cat: The category this variable is in, Integer, Binary or
Continuous(default)
:param e: Used for column based modelling: relates to the variable's
existence in the objective function and constraints
"""
def __init__(
self, name, lowBound=None, upBound=None, cat=const.LpContinuous, e=None
):
LpElement.__init__(self, name)
self._lowbound_original = self.lowBound = lowBound
self._upbound_original = self.upBound = upBound
self.cat = cat
self.varValue = None
self.dj = None
if cat == const.LpBinary:
self.lowBound = 0
self.upBound = 1
self.cat = const.LpInteger
# Code to add a variable to constraints for column based
# modelling.
if e:
self.add_expression(e)
def toDict(self):
"""
Exports a variable into a dictionary with its relevant information
:return: a dictionary with the variable information
:rtype: dict
"""
return dict(
lowBound=self.lowBound,
upBound=self.upBound,
cat=self.cat,
varValue=self.varValue,
dj=self.dj,
name=self.name,
)
to_dict = toDict
@classmethod
def fromDict(cls, dj=None, varValue=None, **kwargs):
"""
Initializes a variable object from information that comes from a dictionary (kwargs)
:param dj: shadow price of the variable
:param float varValue: the value to set the variable
:param kwargs: arguments to initialize the variable
:return: a :py:class:`LpVariable`
:rtype: :LpVariable
"""
var = cls(**kwargs)
var.dj = dj
var.varValue = varValue
return var
from_dict = fromDict
def add_expression(self, e):
self.expression = e
self.addVariableToConstraints(e)
@classmethod
def matrix(
cls,
name,
indices=None, # required param. enforced within function for backwards compatibility
lowBound=None,
upBound=None,
cat=const.LpContinuous,
indexStart=[],
indexs=None,
):
# Backwards Compatiblity with Deprecation Warning for indexs
if indices is not None and indexs is not None:
raise TypeError(
"Both 'indices' and 'indexs' provided to LpVariable.matrix. Use one only, preferably 'indices'."
)
elif indices is not None:
pass
elif indexs is not None:
warnings.warn(
"'indexs' is deprecated; use 'indices'.", DeprecationWarning, 2
)
indices = indexs
else:
raise TypeError(
"LpVariable.matrix missing both 'indices' and deprecated 'indexs' arguments."
)
if not isinstance(indices, tuple):
indices = (indices,)
if "%" not in name:
name += "_%s" * len(indices)
index = indices[0]
indices = indices[1:]
if len(indices) == 0:
return [
LpVariable(name % tuple(indexStart + [i]), lowBound, upBound, cat)
for i in index
]
else:
return [
LpVariable.matrix(
name, indices, lowBound, upBound, cat, indexStart + [i]
)
for i in index
]
@classmethod
def dicts(
cls,
name,
indices=None, # required param. enforced within function for backwards compatibility
lowBound=None,
upBound=None,
cat=const.LpContinuous,
indexStart=[],
indexs=None,
):
"""
This function creates a dictionary of :py:class:`LpVariable` with the specified associated parameters.
:param name: The prefix to the name of each LP variable created
:param indices: A list of strings of the keys to the dictionary of LP
variables, and the main part of the variable name itself
:param lowBound: The lower bound on these variables' range. Default is
negative infinity
:param upBound: The upper bound on these variables' range. Default is
positive infinity
:param cat: The category these variables are in, Integer or
Continuous(default)
:param indexs: (deprecated) Replaced with `indices` parameter
:return: A dictionary of :py:class:`LpVariable`
"""
# Backwards Compatiblity with Deprecation Warning for indexs
if indices is not None and indexs is not None:
raise TypeError(
"Both 'indices' and 'indexs' provided to LpVariable.dicts. Use one only, preferably 'indices'."
)
elif indices is not None:
pass
elif indexs is not None:
warnings.warn(
"'indexs' is deprecated; use 'indices'.", DeprecationWarning, 2
)
indices = indexs
else:
raise TypeError(
"LpVariable.dicts missing both 'indices' and deprecated 'indexs' arguments."
)
if not isinstance(indices, tuple):
indices = (indices,)
if "%" not in name:
name += "_%s" * len(indices)
index = indices[0]
indices = indices[1:]
d = {}
if len(indices) == 0:
for i in index:
d[i] = LpVariable(
name % tuple(indexStart + [str(i)]), lowBound, upBound, cat
)
else:
for i in index:
d[i] = LpVariable.dicts(
name, indices, lowBound, upBound, cat, indexStart + [i]
)
return d
@classmethod
def dict(cls, name, indices, lowBound=None, upBound=None, cat=const.LpContinuous):
if not isinstance(indices, tuple):
indices = (indices,)
if "%" not in name:
name += "_%s" * len(indices)
lists = indices
if len(indices) > 1:
# Cartesian product
res = []
while len(lists):
first = lists[-1]
nres = []
if res:
if first:
for f in first:
nres.extend([[f] + r for r in res])
else:
nres = res
res = nres
else:
res = [[f] for f in first]
lists = lists[:-1]
index = [tuple(r) for r in res]
elif len(indices) == 1:
index = indices[0]
else:
return {}
d = {}
for i in index:
d[i] = cls(name % i, lowBound, upBound, cat)
return d
def getLb(self):
return self.lowBound
def getUb(self):
return self.upBound
def bounds(self, low, up):
self.lowBound = low
self.upBound = up
self.modified = True
def positive(self):
self.bounds(0, None)
def value(self):
return self.varValue
def round(self, epsInt=1e-5, eps=1e-7):
if self.varValue is not None:
if (
self.upBound != None
and self.varValue > self.upBound
and self.varValue <= self.upBound + eps
):
self.varValue = self.upBound
elif (
self.lowBound != None
and self.varValue < self.lowBound
and self.varValue >= self.lowBound - eps
):
self.varValue = self.lowBound
if (
self.cat == const.LpInteger
and abs(round(self.varValue) - self.varValue) <= epsInt
):
self.varValue = round(self.varValue)
def roundedValue(self, eps=1e-5):
if (
self.cat == const.LpInteger
and self.varValue != None
and abs(self.varValue - round(self.varValue)) <= eps
):
return round(self.varValue)
else:
return self.varValue
def valueOrDefault(self):
if self.varValue != None:
return self.varValue
elif self.lowBound != None:
if self.upBound != None:
if 0 >= self.lowBound and 0 <= self.upBound:
return 0
else:
if self.lowBound >= 0:
return self.lowBound
else:
return self.upBound
else:
if 0 >= self.lowBound:
return 0
else:
return self.lowBound
elif self.upBound != None:
if 0 <= self.upBound:
return 0
else:
return self.upBound
else:
return 0
def valid(self, eps):
if self.name == "__dummy" and self.varValue is None:
return True
if self.varValue is None:
return False
if self.upBound is not None and self.varValue > self.upBound + eps:
return False
if self.lowBound is not None and self.varValue < self.lowBound - eps:
return False
if (
self.cat == const.LpInteger
and abs(round(self.varValue) - self.varValue) > eps
):
return False
return True
def infeasibilityGap(self, mip=1):
if self.varValue == None:
raise ValueError("variable value is None")
if self.upBound != None and self.varValue > self.upBound:
return self.varValue - self.upBound
if self.lowBound != None and self.varValue < self.lowBound:
return self.varValue - self.lowBound
if (
mip
and self.cat == const.LpInteger
and round(self.varValue) - self.varValue != 0
):
return round(self.varValue) - self.varValue
return 0
def isBinary(self):
return self.cat == const.LpInteger and self.lowBound == 0 and self.upBound == 1
def isInteger(self):
return self.cat == const.LpInteger
def isFree(self):
return self.lowBound is None and self.upBound is None
def isConstant(self):
return self.lowBound is not None and self.upBound == self.lowBound
def isPositive(self):
return self.lowBound == 0 and self.upBound is None
def asCplexLpVariable(self):
if self.isFree():
return self.name + " free"
if self.isConstant():
return self.name + " = %.12g" % self.lowBound
if self.lowBound == None:
s = "-inf <= "
# Note: XPRESS and CPLEX do not interpret integer variables without
# explicit bounds
elif self.lowBound == 0 and self.cat == const.LpContinuous:
s = ""
else:
s = "%.12g <= " % self.lowBound
s += self.name
if self.upBound is not None:
s += " <= %.12g" % self.upBound
return s
def asCplexLpAffineExpression(self, name, constant=1):
return LpAffineExpression(self).asCplexLpAffineExpression(name, constant)
def __ne__(self, other):
if isinstance(other, LpElement):
return self.name is not other.name
elif isinstance(other, LpAffineExpression):
if other.isAtomic():
return self is not other.atom()
else:
return 1
else:
return 1
def addVariableToConstraints(self, e):
"""adds a variable to the constraints indicated by
the LpConstraintVars in e
"""
for constraint, coeff in e.items():
constraint.addVariable(self, coeff)
def setInitialValue(self, val, check=True):
"""
sets the initial value of the variable to `val`
May be used for warmStart a solver, if supported by the solver
:param float val: value to set to variable
:param bool check: if True, we check if the value fits inside the variable bounds
:return: True if the value was set
:raises ValueError: if check=True and the value does not fit inside the bounds
"""
lb = self.lowBound
ub = self.upBound
config = [
("smaller", "lowBound", lb, lambda: val < lb),
("greater", "upBound", ub, lambda: val > ub),
]
for rel, bound_name, bound_value, condition in config:
if bound_value is not None and condition():
if not check:
return False
raise ValueError(
"In variable {}, initial value {} is {} than {} {}".format(
self.name, val, rel, bound_name, bound_value
)
)
self.varValue = val
return True
def fixValue(self):
"""
changes lower bound and upper bound to the initial value if exists.
:return: None
"""
self._lowbound_unfix = self.lowBound
self._upbound_unfix = self.upBound
val = self.varValue
if val is not None:
self.bounds(val, val)
def isFixed(self):
"""
:return: True if upBound and lowBound are the same
:rtype: bool
"""
return self.isConstant()
def unfixValue(self):
self.bounds(self._lowbound_original, self._upbound_original)
class LpAffineExpression(_DICT_TYPE):
"""
A linear combination of :class:`LpVariables<LpVariable>`.
Can be initialised with the following:
#. e = None: an empty Expression
#. e = dict: gives an expression with the values being the coefficients of the keys (order of terms is undetermined)
#. e = list or generator of 2-tuples: equivalent to dict.items()
#. e = LpElement: an expression of length 1 with the coefficient 1
#. e = other: the constant is initialised as e
Examples:
>>> f=LpAffineExpression(LpElement('x'))
>>> f
1*x + 0
>>> x_name = ['x_0', 'x_1', 'x_2']
>>> x = [LpVariable(x_name[i], lowBound = 0, upBound = 10) for i in range(3) ]
>>> c = LpAffineExpression([ (x[0],1), (x[1],-3), (x[2],4)])
>>> c
1*x_0 + -3*x_1 + 4*x_2 + 0
"""
# to remove illegal characters from the names
trans = maketrans("-+[] ", "_____")
def setName(self, name):
if name:
self.__name = str(name).translate(self.trans)
else:
self.__name = None
def getName(self):
return self.__name
name = property(fget=getName, fset=setName)
def __init__(self, e=None, constant=0, name=None):
self.name = name
# TODO remove isinstance usage
if e is None:
e = {}
if isinstance(e, LpAffineExpression):
# Will not copy the name
self.constant = e.constant
super(LpAffineExpression, self).__init__(list(e.items()))
elif isinstance(e, dict):
self.constant = constant
super(LpAffineExpression, self).__init__(list(e.items()))
elif isinstance(e, Iterable):
self.constant = constant
super(LpAffineExpression, self).__init__(e)
elif isinstance(e, LpElement):
self.constant = 0
super(LpAffineExpression, self).__init__([(e, 1)])
else:
self.constant = e
super(LpAffineExpression, self).__init__()
# Proxy functions for variables
def isAtomic(self):
return len(self) == 1 and self.constant == 0 and list(self.values())[0] == 1
def isNumericalConstant(self):
return len(self) == 0
def atom(self):
return list(self.keys())[0]
# Functions on expressions
def __bool__(self):
return (float(self.constant) != 0.0) or (len(self) > 0)
def value(self):
s = self.constant
for v, x in self.items():
if v.varValue is None:
return None
s += v.varValue * x
return s
def valueOrDefault(self):
s = self.constant
for v, x in self.items():
s += v.valueOrDefault() * x
return s
def addterm(self, key, value):
y = self.get(key, 0)
if y:
y += value
self[key] = y
else:
self[key] = value
def emptyCopy(self):
return LpAffineExpression()
def copy(self):
"""Make a copy of self except the name which is reset"""
# Will not copy the name
return LpAffineExpression(self)
def __str__(self, constant=1):
s = ""
for v in self.sorted_keys():
val = self[v]
if val < 0:
if s != "":
s += " - "
else:
s += "-"
val = -val
elif s != "":
s += " + "
if val == 1:
s += str(v)
else:
s += str(val) + "*" + str(v)
if constant:
if s == "":
s = str(self.constant)
else:
if self.constant < 0:
s += " - " + str(-self.constant)
elif self.constant > 0:
s += " + " + str(self.constant)
elif s == "":
s = "0"
return s
def sorted_keys(self):
"""
returns the list of keys sorted by name
"""
result = [(v.name, v) for v in self.keys()]
result.sort()
result = [v for _, v in result]
return result
def __repr__(self):
l = [str(self[v]) + "*" + str(v) for v in self.sorted_keys()]
l.append(str(self.constant))
s = " + ".join(l)
return s
@staticmethod
def _count_characters(line):
# counts the characters in a list of strings
return sum(len(t) for t in line)
def asCplexVariablesOnly(self, name):
"""
helper for asCplexLpAffineExpression
"""
result = []
line = ["%s:" % name]
notFirst = 0
variables = self.sorted_keys()
for v in variables:
val = self[v]
if val < 0:
sign = " -"
val = -val
elif notFirst:
sign = " +"
else:
sign = ""
notFirst = 1
if val == 1:
term = "%s %s" % (sign, v.name)
else:
# adding zero to val to remove instances of negative zero
term = "%s %.12g %s" % (sign, val + 0, v.name)
if self._count_characters(line) + len(term) > const.LpCplexLPLineSize:
result += ["".join(line)]
line = [term]
else:
line += [term]
return result, line
def asCplexLpAffineExpression(self, name, constant=1):
"""
returns a string that represents the Affine Expression in lp format
"""
# refactored to use a list for speed in iron python
result, line = self.asCplexVariablesOnly(name)
if not self:
term = " %s" % self.constant
else:
term = ""
if constant:
if self.constant < 0:
term = " - %s" % (-self.constant)
elif self.constant > 0:
term = " + %s" % self.constant
if self._count_characters(line) + len(term) > const.LpCplexLPLineSize:
result += ["".join(line)]
line = [term]
else:
line += [term]
result += ["".join(line)]
result = "%s\n" % "\n".join(result)
return result
def addInPlace(self, other):
if isinstance(other, int) and (other == 0):
return self
if other is None:
return self
if isinstance(other, LpElement):
self.addterm(other, 1)
elif isinstance(other, LpAffineExpression):
self.constant += other.constant
for v, x in other.items():
self.addterm(v, x)
elif isinstance(other, dict):
for e in other.values():
self.addInPlace(e)
elif isinstance(other, list) or isinstance(other, Iterable):
for e in other:
self.addInPlace(e)
else:
self.constant += other
return self
def subInPlace(self, other):
if isinstance(other, int) and (other == 0):
return self
if other is None:
return self
if isinstance(other, LpElement):
self.addterm(other, -1)
elif isinstance(other, LpAffineExpression):
self.constant -= other.constant
for v, x in other.items():
self.addterm(v, -x)
elif isinstance(other, dict):
for e in other.values():
self.subInPlace(e)
elif isinstance(other, list) or isinstance(other, Iterable):
for e in other:
self.subInPlace(e)
else:
self.constant -= other
return self
def __neg__(self):
e = self.emptyCopy()
e.constant = -self.constant
for v, x in self.items():
e[v] = -x
return e
def __pos__(self):
return self
def __add__(self, other):
return self.copy().addInPlace(other)
def __radd__(self, other):
return self.copy().addInPlace(other)
def __iadd__(self, other):
return self.addInPlace(other)
def __sub__(self, other):
return self.copy().subInPlace(other)
def __rsub__(self, other):
return (-self).addInPlace(other)
def __isub__(self, other):
return (self).subInPlace(other)
def __mul__(self, other):
e = self.emptyCopy()
if isinstance(other, LpAffineExpression):
e.constant = self.constant * other.constant
if len(other):
if len(self):
raise TypeError("Non-constant expressions cannot be multiplied")
else:
c = self.constant
if c != 0:
for v, x in other.items():
e[v] = c * x
else:
c = other.constant
if c != 0:
for v, x in self.items():
e[v] = c * x
elif isinstance(other, LpVariable):
return self * LpAffineExpression(other)
else:
if other != 0:
e.constant = self.constant * other
for v, x in self.items():
e[v] = other * x
return e
def __rmul__(self, other):
return self * other
def __div__(self, other):
if isinstance(other, LpAffineExpression) or isinstance(other, LpVariable):
if len(other):
raise TypeError(
"Expressions cannot be divided by a non-constant expression"
)
other = other.constant
e = self.emptyCopy()
e.constant = self.constant / other
for v, x in self.items():
e[v] = x / other
return e
def __truediv__(self, other):
if isinstance(other, LpAffineExpression) or isinstance(other, LpVariable):
if len(other):
raise TypeError(
"Expressions cannot be divided by a non-constant expression"
)
other = other.constant
e = self.emptyCopy()
e.constant = self.constant / other
for v, x in self.items():
e[v] = x / other
return e
def __rdiv__(self, other):
e = self.emptyCopy()
if len(self):
raise TypeError(
"Expressions cannot be divided by a non-constant expression"
)
c = self.constant
if isinstance(other, LpAffineExpression):
e.constant = other.constant / c
for v, x in other.items():
e[v] = x / c
else:
e.constant = other / c
return e
def __le__(self, other):
return LpConstraint(self - other, const.LpConstraintLE)
def __ge__(self, other):
return LpConstraint(self - other, const.LpConstraintGE)
def __eq__(self, other):
return LpConstraint(self - other, const.LpConstraintEQ)
def toDict(self):
"""
exports the :py:class:`LpAffineExpression` into a list of dictionaries with the coefficients
it does not export the constant
:return: list of dictionaries with the coefficients
:rtype: list
"""
return [dict(name=k.name, value=v) for k, v in self.items()]
to_dict = toDict
class LpConstraint(LpAffineExpression):
"""An LP constraint"""
def __init__(self, e=None, sense=const.LpConstraintEQ, name=None, rhs=None):
"""
:param e: an instance of :class:`LpAffineExpression`
:param sense: one of :data:`~pulp.const.LpConstraintEQ`, :data:`~pulp.const.LpConstraintGE`, :data:`~pulp.const.LpConstraintLE` (0, 1, -1 respectively)
:param name: identifying string
:param rhs: numerical value of constraint target
"""
LpAffineExpression.__init__(self, e, name=name)
if rhs is not None:
self.constant -= rhs
self.sense = sense
self.pi = None
self.slack = None
self.modified = True
def getLb(self):
if (self.sense == const.LpConstraintGE) or (self.sense == const.LpConstraintEQ):
return -self.constant
else:
return None
def getUb(self):
if (self.sense == const.LpConstraintLE) or (self.sense == const.LpConstraintEQ):
return -self.constant
else:
return None
def __str__(self):
s = LpAffineExpression.__str__(self, 0)
if self.sense is not None:
s += " " + const.LpConstraintSenses[self.sense] + " " + str(-self.constant)
return s
def asCplexLpConstraint(self, name):
"""
Returns a constraint as a string
"""
result, line = self.asCplexVariablesOnly(name)
if not list(self.keys()):
line += ["0"]
c = -self.constant
if c == 0:
c = 0 # Supress sign
term = " %s %.12g" % (const.LpConstraintSenses[self.sense], c)
if self._count_characters(line) + len(term) > const.LpCplexLPLineSize:
result += ["".join(line)]
line = [term]
else:
line += [term]
result += ["".join(line)]
result = "%s\n" % "\n".join(result)
return result
def changeRHS(self, RHS):
"""
alters the RHS of a constraint so that it can be modified in a resolve
"""
self.constant = -RHS
self.modified = True
def __repr__(self):
s = LpAffineExpression.__repr__(self)
if self.sense is not None:
s += " " + const.LpConstraintSenses[self.sense] + " 0"
return s
def copy(self):
"""Make a copy of self"""
return LpConstraint(self, self.sense)
def emptyCopy(self):
return LpConstraint(sense=self.sense)
def addInPlace(self, other):
if isinstance(other, LpConstraint):
if self.sense * other.sense >= 0:
LpAffineExpression.addInPlace(self, other)
self.sense |= other.sense
else:
LpAffineExpression.subInPlace(self, other)
self.sense |= -other.sense
elif isinstance(other, list):
for e in other:
self.addInPlace(e)
else:
LpAffineExpression.addInPlace(self, other)
# raise TypeError, "Constraints and Expressions cannot be added"
return self
def subInPlace(self, other):
if isinstance(other, LpConstraint):
if self.sense * other.sense <= 0:
LpAffineExpression.subInPlace(self, other)
self.sense |= -other.sense
else:
LpAffineExpression.addInPlace(self, other)
self.sense |= other.sense
elif isinstance(other, list):
for e in other:
self.subInPlace(e)
else:
LpAffineExpression.subInPlace(self, other)
# raise TypeError, "Constraints and Expressions cannot be added"
return self
def __neg__(self):
c = LpAffineExpression.__neg__(self)
c.sense = -c.sense
return c
def __add__(self, other):
return self.copy().addInPlace(other)
def __radd__(self, other):
return self.copy().addInPlace(other)
def __sub__(self, other):
return self.copy().subInPlace(other)
def __rsub__(self, other):
return (-self).addInPlace(other)
def __mul__(self, other):
if isinstance(other, LpConstraint):
c = LpAffineExpression.__mul__(self, other)
if c.sense == 0:
c.sense = other.sense
elif other.sense != 0:
c.sense *= other.sense
return c
else:
return LpAffineExpression.__mul__(self, other)
def __rmul__(self, other):
return self * other
def __div__(self, other):
if isinstance(other, LpConstraint):
c = LpAffineExpression.__div__(self, other)
if c.sense == 0:
c.sense = other.sense
elif other.sense != 0:
c.sense *= other.sense
return c
else:
return LpAffineExpression.__mul__(self, other)
def __rdiv__(self, other):
if isinstance(other, LpConstraint):
c = LpAffineExpression.__rdiv__(self, other)
if c.sense == 0:
c.sense = other.sense
elif other.sense != 0:
c.sense *= other.sense
return c
else:
return LpAffineExpression.__mul__(self, other)
def valid(self, eps=0):
val = self.value()
if self.sense == const.LpConstraintEQ:
return abs(val) <= eps
else:
return val * self.sense >= -eps
def makeElasticSubProblem(self, *args, **kwargs):
"""
Builds an elastic subproblem by adding variables to a hard constraint
uses FixedElasticSubProblem
"""
return FixedElasticSubProblem(self, *args, **kwargs)
def toDict(self):
"""
exports constraint information into a dictionary
:return: dictionary with all the constraint information
"""
return dict(
sense=self.sense,
pi=self.pi,
constant=self.constant,
name=self.name,
coefficients=LpAffineExpression.toDict(self),
)
@classmethod
def fromDict(cls, _dict):
"""
Initializes a constraint object from a dictionary with necessary information
:param dict _dict: dictionary with data
:return: a new :py:class:`LpConstraint`
"""
const = cls(
e=_dict["coefficients"],
rhs=-_dict["constant"],
name=_dict["name"],
sense=_dict["sense"],
)
const.pi = _dict["pi"]
return const
from_dict = fromDict
class LpFractionConstraint(LpConstraint):
"""
Creates a constraint that enforces a fraction requirement a/b = c
"""
def __init__(
self,
numerator,
denominator=None,
sense=const.LpConstraintEQ,
RHS=1.0,
name=None,
complement=None,
):
"""
creates a fraction Constraint to model constraints of
the nature
numerator/denominator {==, >=, <=} RHS
numerator/(numerator + complement) {==, >=, <=} RHS
:param numerator: the top of the fraction
:param denominator: as described above
:param sense: the sense of the relation of the constraint
:param RHS: the target fraction value
:param complement: as described above
"""
self.numerator = numerator
if denominator is None and complement is not None:
self.complement = complement
self.denominator = numerator + complement
elif denominator is not None and complement is None:
self.denominator = denominator
self.complement = denominator - numerator
else:
self.denominator = denominator
self.complement = complement
lhs = self.numerator - RHS * self.denominator
LpConstraint.__init__(self, lhs, sense=sense, rhs=0, name=name)
self.RHS = RHS
def findLHSValue(self):
"""
Determines the value of the fraction in the constraint after solution
"""
if abs(value(self.denominator)) >= const.EPS:
return value(self.numerator) / value(self.denominator)
else:
if abs(value(self.numerator)) <= const.EPS:
# zero divided by zero will return 1
return 1.0
else:
raise ZeroDivisionError
def makeElasticSubProblem(self, *args, **kwargs):
"""
Builds an elastic subproblem by adding variables and splitting the
hard constraint
uses FractionElasticSubProblem
"""
return FractionElasticSubProblem(self, *args, **kwargs)
class LpConstraintVar(LpElement):
"""A Constraint that can be treated as a variable when constructing
a LpProblem by columns
"""
def __init__(self, name=None, sense=None, rhs=None, e=None):
LpElement.__init__(self, name)
self.constraint = LpConstraint(name=self.name, sense=sense, rhs=rhs, e=e)
def addVariable(self, var, coeff):
"""
Adds a variable to the constraint with the
activity coeff
"""
self.constraint.addterm(var, coeff)
def value(self):
return self.constraint.value()
class LpProblem(object):
"""An LP Problem"""
def __init__(self, name="NoName", sense=const.LpMinimize):
"""
Creates an LP Problem
This function creates a new LP Problem with the specified associated parameters
:param name: name of the problem used in the output .lp file
:param sense: of the LP problem objective. \
Either :data:`~pulp.const.LpMinimize` (default) \
or :data:`~pulp.const.LpMaximize`.
:return: An LP Problem
"""
if " " in name:
warnings.warn("Spaces are not permitted in the name. Converted to '_'")
name = name.replace(" ", "_")
self.objective = None
self.constraints = _DICT_TYPE()
self.name = name
self.sense = sense
self.sos1 = {}
self.sos2 = {}
self.status = const.LpStatusNotSolved
self.sol_status = const.LpSolutionNoSolutionFound
self.noOverlap = 1
self.solver = None
self.modifiedVariables = []
self.modifiedConstraints = []
self.resolveOK = False
self._variables = []
self._variable_ids = {} # old school using dict.keys() for a set
self.dummyVar = None
self.solutionTime = 0
self.solutionCpuTime = 0
# locals
self.lastUnused = 0
def __repr__(self):
s = self.name + ":\n"
if self.sense == 1:
s += "MINIMIZE\n"
else:
s += "MAXIMIZE\n"
s += repr(self.objective) + "\n"
if self.constraints:
s += "SUBJECT TO\n"
for n, c in self.constraints.items():
s += c.asCplexLpConstraint(n) + "\n"
s += "VARIABLES\n"
for v in self.variables():
s += v.asCplexLpVariable() + " " + const.LpCategories[v.cat] + "\n"
return s
def __getstate__(self):
# Remove transient data prior to pickling.
state = self.__dict__.copy()
del state["_variable_ids"]
return state
def __setstate__(self, state):
# Update transient data prior to unpickling.
self.__dict__.update(state)
self._variable_ids = {}
for v in self._variables:
self._variable_ids[v.hash] = v
def copy(self):
"""Make a copy of self. Expressions are copied by reference"""
lpcopy = LpProblem(name=self.name, sense=self.sense)
lpcopy.objective = self.objective
lpcopy.constraints = self.constraints.copy()
lpcopy.sos1 = self.sos1.copy()
lpcopy.sos2 = self.sos2.copy()
return lpcopy
def deepcopy(self):
"""Make a copy of self. Expressions are copied by value"""
lpcopy = LpProblem(name=self.name, sense=self.sense)
if self.objective is not None:
lpcopy.objective = self.objective.copy()
lpcopy.constraints = {}
for k, v in self.constraints.items():
lpcopy.constraints[k] = v.copy()
lpcopy.sos1 = self.sos1.copy()
lpcopy.sos2 = self.sos2.copy()
return lpcopy
def toDict(self):
"""
creates a dictionary from the model with as much data as possible.
It replaces variables by variable names.
So it requires to have unique names for variables.
:return: dictionary with model data
:rtype: dict
"""
try:
self.checkDuplicateVars()
except const.PulpError:
raise const.PulpError(
"Duplicated names found in variables:\nto export the model, variable names need to be unique"
)
self.fixObjective()
variables = self.variables()
return dict(
objective=dict(
name=self.objective.name, coefficients=self.objective.toDict()
),
constraints=[v.toDict() for v in self.constraints.values()],
variables=[v.toDict() for v in variables],
parameters=dict(
name=self.name,
sense=self.sense,
status=self.status,
sol_status=self.sol_status,
),
sos1=list(self.sos1.values()),
sos2=list(self.sos2.values()),
)
to_dict = toDict
@classmethod
def fromDict(cls, _dict):
"""
Takes a dictionary with all necessary information to build a model.
And returns a dictionary of variables and a problem object
:param _dict: dictionary with the model stored
:return: a tuple with a dictionary of variables and a :py:class:`LpProblem`
"""
# we instantiate the problem
params = _dict["parameters"]
pb_params = {"name", "sense"}
args = {k: params[k] for k in pb_params}
pb = cls(**args)
pb.status = params["status"]
pb.sol_status = params["sol_status"]
# recreate the variables.
var = {v["name"]: LpVariable.fromDict(**v) for v in _dict["variables"]}
# objective function.
# we change the names for the objects:
obj_e = {var[v["name"]]: v["value"] for v in _dict["objective"]["coefficients"]}
pb += LpAffineExpression(e=obj_e, name=_dict["objective"]["name"])
# constraints
# we change the names for the objects:
def edit_const(const):
const = dict(const)
const["coefficients"] = {
var[v["name"]]: v["value"] for v in const["coefficients"]
}
return const
constraints = [edit_const(v) for v in _dict["constraints"]]
for c in constraints:
pb += LpConstraint.fromDict(c)
# last, parameters, other options
list_to_dict = lambda v: {k: v for k, v in enumerate(v)}
pb.sos1 = list_to_dict(_dict["sos1"])
pb.sos2 = list_to_dict(_dict["sos2"])
return var, pb
from_dict = fromDict
def toJson(self, filename, *args, **kwargs):
"""
Creates a json file from the LpProblem information
:param str filename: filename to write json
:param args: additional arguments for json function
:param kwargs: additional keyword arguments for json function
:return: None
"""
with open(filename, "w") as f:
json.dump(self.toDict(), f, *args, **kwargs)
to_json = toJson
@classmethod
def fromJson(cls, filename):
"""
Creates a new Lp Problem from a json file with information
:param str filename: json file name
:return: a tuple with a dictionary of variables and an LpProblem
:rtype: (dict, :py:class:`LpProblem`)
"""
with open(filename, "r") as f:
data = json.load(f)
return cls.fromDict(data)
from_json = fromJson
@classmethod
def fromMPS(cls, filename, sense=const.LpMinimize, **kwargs):
data = mpslp.readMPS(filename, sense=sense, **kwargs)
return cls.fromDict(data)
def normalisedNames(self):
constraintsNames = {k: "C%07d" % i for i, k in enumerate(self.constraints)}
_variables = self.variables()
variablesNames = {k.name: "X%07d" % i for i, k in enumerate(_variables)}
return constraintsNames, variablesNames, "OBJ"
def isMIP(self):
for v in self.variables():
if v.cat == const.LpInteger:
return 1
return 0
def roundSolution(self, epsInt=1e-5, eps=1e-7):
"""
Rounds the lp variables
Inputs:
- none
Side Effects:
- The lp variables are rounded
"""
for v in self.variables():
v.round(epsInt, eps)
def unusedConstraintName(self):
self.lastUnused += 1
while 1:
s = "_C%d" % self.lastUnused
if s not in self.constraints:
break
self.lastUnused += 1
return s
def valid(self, eps=0):
for v in self.variables():
if not v.valid(eps):
return False
for c in self.constraints.values():
if not c.valid(eps):
return False
else:
return True
def infeasibilityGap(self, mip=1):
gap = 0
for v in self.variables():
gap = max(abs(v.infeasibilityGap(mip)), gap)
for c in self.constraints.values():
if not c.valid(0):
gap = max(abs(c.value()), gap)
return gap
def addVariable(self, variable):
"""
Adds a variable to the problem before a constraint is added
@param variable: the variable to be added
"""
if variable.hash not in self._variable_ids:
self._variables.append(variable)
self._variable_ids[variable.hash] = variable
def addVariables(self, variables):
"""
Adds variables to the problem before a constraint is added
@param variables: the variables to be added
"""
for v in variables:
self.addVariable(v)
def variables(self):
"""
Returns the problem variables
:return: A list containing the problem variables
:rtype: (list, :py:class:`LpVariable`)
"""
if self.objective:
self.addVariables(list(self.objective.keys()))
for c in self.constraints.values():
self.addVariables(list(c.keys()))
self._variables.sort(key=lambda v: v.name)
return self._variables
def variablesDict(self):
variables = {}
if self.objective:
for v in self.objective:
variables[v.name] = v
for c in list(self.constraints.values()):
for v in c:
variables[v.name] = v
return variables
def add(self, constraint, name=None):
self.addConstraint(constraint, name)
def addConstraint(self, constraint, name=None):
if not isinstance(constraint, LpConstraint):
raise TypeError("Can only add LpConstraint objects")
if name:
constraint.name = name
try:
if constraint.name:
name = constraint.name
else:
name = self.unusedConstraintName()
except AttributeError:
raise TypeError("Can only add LpConstraint objects")
# removed as this test fails for empty constraints
# if len(constraint) == 0:
# if not constraint.valid():
# raise ValueError, "Cannot add false constraints"
if name in self.constraints:
if self.noOverlap:
raise const.PulpError("overlapping constraint names: " + name)
else:
print("Warning: overlapping constraint names:", name)
self.constraints[name] = constraint
self.modifiedConstraints.append(constraint)
self.addVariables(list(constraint.keys()))
def setObjective(self, obj):
"""
Sets the input variable as the objective function. Used in Columnwise Modelling
:param obj: the objective function of type :class:`LpConstraintVar`
Side Effects:
- The objective function is set
"""
if isinstance(obj, LpVariable):
# allows the user to add a LpVariable as an objective
obj = obj + 0.0
try:
obj = obj.constraint
name = obj.name
except AttributeError:
name = None
self.objective = obj
self.objective.name = name
self.resolveOK = False
def __iadd__(self, other):
if isinstance(other, tuple):
other, name = other
else:
name = None
if other is True:
return self
elif other is False:
raise TypeError("A False object cannot be passed as a constraint")
elif isinstance(other, LpConstraintVar):
self.addConstraint(other.constraint)
elif isinstance(other, LpConstraint):
self.addConstraint(other, name)
elif isinstance(other, LpAffineExpression):
if self.objective is not None:
warnings.warn("Overwriting previously set objective.")
self.objective = other
if name is not None:
# we may keep the LpAffineExpression name
self.objective.name = name
elif isinstance(other, LpVariable) or isinstance(other, (int, float)):
if self.objective is not None:
warnings.warn("Overwriting previously set objective.")
self.objective = LpAffineExpression(other)
self.objective.name = name
else:
raise TypeError(
"Can only add LpConstraintVar, LpConstraint, LpAffineExpression or True objects"
)
return self
def extend(self, other, use_objective=True):
"""
extends an LpProblem by adding constraints either from a dictionary
a tuple or another LpProblem object.
@param use_objective: determines whether the objective is imported from
the other problem
For dictionaries the constraints will be named with the keys
For tuples an unique name will be generated
For LpProblems the name of the problem will be added to the constraints
name
"""
if isinstance(other, dict):
for name in other:
self.constraints[name] = other[name]
elif isinstance(other, LpProblem):
for v in set(other.variables()).difference(self.variables()):
v.name = other.name + v.name
for name, c in other.constraints.items():
c.name = other.name + name
self.addConstraint(c)
if use_objective:
self.objective += other.objective
else:
for c in other:
if isinstance(c, tuple):
name = c[0]
c = c[1]
else:
name = None
if not name:
name = c.name
if not name:
name = self.unusedConstraintName()
self.constraints[name] = c
def coefficients(self, translation=None):
coefs = []
if translation == None:
for c in self.constraints:
cst = self.constraints[c]
coefs.extend([(v.name, c, cst[v]) for v in cst])
else:
for c in self.constraints:
ctr = translation[c]
cst = self.constraints[c]
coefs.extend([(translation[v.name], ctr, cst[v]) for v in cst])
return coefs
def writeMPS(self, filename, mpsSense=0, rename=0, mip=1):
"""
Writes an mps files from the problem information
:param str filename: name of the file to write
:param int mpsSense:
:param bool rename: if True, normalized names are used for variables and constraints
:param mip: variables and variable renames
:return:
Side Effects:
- The file is created
"""
return mpslp.writeMPS(self, filename, mpsSense=mpsSense, rename=rename, mip=mip)
def writeLP(self, filename, writeSOS=1, mip=1, max_length=100):
"""
Write the given Lp problem to a .lp file.
This function writes the specifications (objective function,
constraints, variables) of the defined Lp problem to a file.
:param str filename: the name of the file to be created.
:return: variables
Side Effects:
- The file is created
"""
return mpslp.writeLP(
self, filename=filename, writeSOS=writeSOS, mip=mip, max_length=max_length
)
def checkDuplicateVars(self):
"""
Checks if there are at least two variables with the same name
:return: 1
:raises `const.PulpError`: if there ar duplicates
"""
vs = self.variables()
repeated_names = {}
for v in vs:
repeated_names[v.name] = repeated_names.get(v.name, 0) + 1
repeated_names = [
(key, value) for key, value in list(repeated_names.items()) if value >= 2
]
if repeated_names:
raise const.PulpError("Repeated variable names:\n" + str(repeated_names))
return 1
def checkLengthVars(self, max_length):
"""
Checks if variables have names smaller than `max_length`
:param int max_length: max size for variable name
:return:
:raises const.PulpError: if there is at least one variable that has a long name
"""
vs = self.variables()
long_names = [v.name for v in vs if len(v.name) > max_length]
if long_names:
raise const.PulpError(
"Variable names too long for Lp format\n" + str(long_names)
)
return 1
def assignVarsVals(self, values):
variables = self.variablesDict()
for name in values:
if name != "__dummy":
variables[name].varValue = values[name]
def assignVarsDj(self, values):
variables = self.variablesDict()
for name in values:
if name != "__dummy":
variables[name].dj = values[name]
def assignConsPi(self, values):
for name in values:
try:
self.constraints[name].pi = values[name]
except KeyError:
pass
def assignConsSlack(self, values, activity=False):
for name in values:
try:
if activity:
# reports the activity not the slack
self.constraints[name].slack = -1 * (
self.constraints[name].constant + float(values[name])
)
else:
self.constraints[name].slack = float(values[name])
except KeyError:
pass
def get_dummyVar(self):
if self.dummyVar is None:
self.dummyVar = LpVariable("__dummy", 0, 0)
return self.dummyVar
def fixObjective(self):
if self.objective is None:
self.objective = 0
wasNone = 1
else:
wasNone = 0
if not isinstance(self.objective, LpAffineExpression):
self.objective = LpAffineExpression(self.objective)
if self.objective.isNumericalConstant():
dummyVar = self.get_dummyVar()
self.objective += dummyVar
else:
dummyVar = None
return wasNone, dummyVar
def restoreObjective(self, wasNone, dummyVar):
if wasNone:
self.objective = None
elif not dummyVar is None:
self.objective -= dummyVar
def solve(self, solver=None, **kwargs):
"""
Solve the given Lp problem.
This function changes the problem to make it suitable for solving
then calls the solver.actualSolve() method to find the solution
:param solver: Optional: the specific solver to be used, defaults to the
default solver.
Side Effects:
- The attributes of the problem object are changed in
:meth:`~pulp.solver.LpSolver.actualSolve()` to reflect the Lp solution
"""
if not (solver):
solver = self.solver
if not (solver):
solver = LpSolverDefault
wasNone, dummyVar = self.fixObjective()
# time it
self.startClock()
status = solver.actualSolve(self, **kwargs)
self.stopClock()
self.restoreObjective(wasNone, dummyVar)
self.solver = solver
return status
def startClock(self):
"initializes properties with the current time"
self.solutionCpuTime = -clock()
self.solutionTime = -time()
def stopClock(self):
"updates time wall time and cpu time"
self.solutionTime += time()
self.solutionCpuTime += clock()
def sequentialSolve(
self, objectives, absoluteTols=None, relativeTols=None, solver=None, debug=False
):
"""
Solve the given Lp problem with several objective functions.
This function sequentially changes the objective of the problem
and then adds the objective function as a constraint
:param objectives: the list of objectives to be used to solve the problem
:param absoluteTols: the list of absolute tolerances to be applied to
the constraints should be +ve for a minimise objective
:param relativeTols: the list of relative tolerances applied to the constraints
:param solver: the specific solver to be used, defaults to the default solver.
"""
# TODO Add a penalty variable to make problems elastic
# TODO add the ability to accept different status values i.e. infeasible etc
if not (solver):
solver = self.solver
if not (solver):
solver = LpSolverDefault
if not (absoluteTols):
absoluteTols = [0] * len(objectives)
if not (relativeTols):
relativeTols = [1] * len(objectives)
# time it
self.startClock()
statuses = []
for i, (obj, absol, rel) in enumerate(
zip(objectives, absoluteTols, relativeTols)
):
self.setObjective(obj)
status = solver.actualSolve(self)
statuses.append(status)
if debug:
self.writeLP("%sSequence.lp" % i)
if self.sense == const.LpMinimize:
self += obj <= value(obj) * rel + absol, "%s_Sequence_Objective" % i
elif self.sense == const.LpMaximize:
self += obj >= value(obj) * rel + absol, "%s_Sequence_Objective" % i
self.stopClock()
self.solver = solver
return statuses
def resolve(self, solver=None, **kwargs):
"""
resolves an Problem using the same solver as previously
"""
if not (solver):
solver = self.solver
if self.resolveOK:
return self.solver.actualResolve(self, **kwargs)
else:
return self.solve(solver=solver, **kwargs)
def setSolver(self, solver=LpSolverDefault):
"""Sets the Solver for this problem useful if you are using
resolve
"""
self.solver = solver
def numVariables(self):
"""
:return: number of variables in model
"""
return len(self._variable_ids)
def numConstraints(self):
"""
:return: number of constraints in model
"""
return len(self.constraints)
def getSense(self):
return self.sense
def assignStatus(self, status, sol_status=None):
"""
Sets the status of the model after solving.
:param status: code for the status of the model
:param sol_status: code for the status of the solution
:return:
"""
if status not in const.LpStatus:
raise const.PulpError("Invalid status code: " + str(status))
if sol_status is not None and sol_status not in const.LpSolution:
raise const.PulpError("Invalid solution status code: " + str(sol_status))
self.status = status
if sol_status is None:
sol_status = const.LpStatusToSolution.get(
status, const.LpSolutionNoSolutionFound
)
self.sol_status = sol_status
return True
class FixedElasticSubProblem(LpProblem):
"""
Contains the subproblem generated by converting a fixed constraint
:math:`\\sum_{i}a_i x_i = b` into an elastic constraint.
:param constraint: The LpConstraint that the elastic constraint is based on
:param penalty: penalty applied for violation (+ve or -ve) of the constraints
:param proportionFreeBound:
the proportional bound (+ve and -ve) on
constraint violation that is free from penalty
:param proportionFreeBoundList: the proportional bound on \
constraint violation that is free from penalty, expressed as a list\
where [-ve, +ve]
"""
def __init__(
self,
constraint,
penalty=None,
proportionFreeBound=None,
proportionFreeBoundList=None,
):
subProblemName = "%s_elastic_SubProblem" % constraint.name
LpProblem.__init__(self, subProblemName, const.LpMinimize)
self.objective = LpAffineExpression()
self.constraint = constraint
self.constant = constraint.constant
self.RHS = -constraint.constant
self.objective = LpAffineExpression()
self += constraint, "_Constraint"
# create and add these variables but disabled
self.freeVar = LpVariable("_free_bound", upBound=0, lowBound=0)
self.upVar = LpVariable("_pos_penalty_var", upBound=0, lowBound=0)
self.lowVar = LpVariable("_neg_penalty_var", upBound=0, lowBound=0)
constraint.addInPlace(self.freeVar + self.lowVar + self.upVar)
if proportionFreeBound:
proportionFreeBoundList = [proportionFreeBound, proportionFreeBound]
if proportionFreeBoundList:
# add a costless variable
self.freeVar.upBound = abs(constraint.constant * proportionFreeBoundList[0])
self.freeVar.lowBound = -abs(
constraint.constant * proportionFreeBoundList[1]
)
# Note the reversal of the upbound and lowbound due to the nature of the
# variable
if penalty is not None:
# activate these variables
self.upVar.upBound = None
self.lowVar.lowBound = None
self.objective = penalty * self.upVar - penalty * self.lowVar
def _findValue(self, attrib):
"""
safe way to get the value of a variable that may not exist
"""
var = getattr(self, attrib, 0)
if var:
if value(var) is not None:
return value(var)
else:
return 0.0
else:
return 0.0
def isViolated(self):
"""
returns true if the penalty variables are non-zero
"""
upVar = self._findValue("upVar")
lowVar = self._findValue("lowVar")
freeVar = self._findValue("freeVar")
result = abs(upVar + lowVar) >= const.EPS
if result:
log.debug(
"isViolated %s, upVar %s, lowVar %s, freeVar %s result %s"
% (self.name, upVar, lowVar, freeVar, result)
)
log.debug(
"isViolated value lhs %s constant %s" % (self.findLHSValue(), self.RHS)
)
return result
def findDifferenceFromRHS(self):
"""
The amount the actual value varies from the RHS (sense: LHS - RHS)
"""
return self.findLHSValue() - self.RHS
def findLHSValue(self):
"""
for elastic constraints finds the LHS value of the constraint without
the free variable and or penalty variable assumes the constant is on the
rhs
"""
upVar = self._findValue("upVar")
lowVar = self._findValue("lowVar")
freeVar = self._findValue("freeVar")
return self.constraint.value() - self.constant - upVar - lowVar - freeVar
def deElasticize(self):
"""de-elasticize constraint"""
self.upVar.upBound = 0
self.lowVar.lowBound = 0
def reElasticize(self):
"""
Make the Subproblem elastic again after deElasticize
"""
self.upVar.lowBound = 0
self.upVar.upBound = None
self.lowVar.upBound = 0
self.lowVar.lowBound = None
def alterName(self, name):
"""
Alters the name of anonymous parts of the problem
"""
self.name = "%s_elastic_SubProblem" % name
if hasattr(self, "freeVar"):
self.freeVar.name = self.name + "_free_bound"
if hasattr(self, "upVar"):
self.upVar.name = self.name + "_pos_penalty_var"
if hasattr(self, "lowVar"):
self.lowVar.name = self.name + "_neg_penalty_var"
class FractionElasticSubProblem(FixedElasticSubProblem):
"""
Contains the subproblem generated by converting a Fraction constraint
numerator/(numerator+complement) = b
into an elastic constraint
:param name: The name of the elastic subproblem
:param penalty: penalty applied for violation (+ve or -ve) of the constraints
:param proportionFreeBound: the proportional bound (+ve and -ve) on
constraint violation that is free from penalty
:param proportionFreeBoundList: the proportional bound on
constraint violation that is free from penalty, expressed as a list
where [-ve, +ve]
"""
def __init__(
self,
name,
numerator,
RHS,
sense,
complement=None,
denominator=None,
penalty=None,
proportionFreeBound=None,
proportionFreeBoundList=None,
):
subProblemName = "%s_elastic_SubProblem" % name
self.numerator = numerator
if denominator is None and complement is not None:
self.complement = complement
self.denominator = numerator + complement
elif denominator is not None and complement is None:
self.denominator = denominator
self.complement = denominator - numerator
else:
raise const.PulpError(
"only one of denominator and complement must be specified"
)
self.RHS = RHS
self.lowTarget = self.upTarget = None
LpProblem.__init__(self, subProblemName, const.LpMinimize)
self.freeVar = LpVariable("_free_bound", upBound=0, lowBound=0)
self.upVar = LpVariable("_pos_penalty_var", upBound=0, lowBound=0)
self.lowVar = LpVariable("_neg_penalty_var", upBound=0, lowBound=0)
if proportionFreeBound:
proportionFreeBoundList = [proportionFreeBound, proportionFreeBound]
if proportionFreeBoundList:
upProportionFreeBound, lowProportionFreeBound = proportionFreeBoundList
else:
upProportionFreeBound, lowProportionFreeBound = (0, 0)
# create an objective
self += LpAffineExpression()
# There are three cases if the constraint.sense is ==, <=, >=
if sense in [const.LpConstraintEQ, const.LpConstraintLE]:
# create a constraint the sets the upper bound of target
self.upTarget = RHS + upProportionFreeBound
self.upConstraint = LpFractionConstraint(
self.numerator,
self.complement,
const.LpConstraintLE,
self.upTarget,
denominator=self.denominator,
)
if penalty is not None:
self.lowVar.lowBound = None
self.objective += -1 * penalty * self.lowVar
self.upConstraint += self.lowVar
self += self.upConstraint, "_upper_constraint"
if sense in [const.LpConstraintEQ, const.LpConstraintGE]:
# create a constraint the sets the lower bound of target
self.lowTarget = RHS - lowProportionFreeBound
self.lowConstraint = LpFractionConstraint(
self.numerator,
self.complement,
const.LpConstraintGE,
self.lowTarget,
denominator=self.denominator,
)
if penalty is not None:
self.upVar.upBound = None
self.objective += penalty * self.upVar
self.lowConstraint += self.upVar
self += self.lowConstraint, "_lower_constraint"
def findLHSValue(self):
"""
for elastic constraints finds the LHS value of the constraint without
the free variable and or penalty variable assumes the constant is on the
rhs
"""
# uses code from LpFractionConstraint
if abs(value(self.denominator)) >= const.EPS:
return value(self.numerator) / value(self.denominator)
else:
if abs(value(self.numerator)) <= const.EPS:
# zero divided by zero will return 1
return 1.0
else:
raise ZeroDivisionError
def isViolated(self):
"""
returns true if the penalty variables are non-zero
"""
if abs(value(self.denominator)) >= const.EPS:
if self.lowTarget is not None:
if self.lowTarget > self.findLHSValue():
return True
if self.upTarget is not None:
if self.findLHSValue() > self.upTarget:
return True
else:
# if the denominator is zero the constraint is satisfied
return False
def lpSum(vector):
"""
Calculate the sum of a list of linear expressions
:param vector: A list of linear expressions
"""
return LpAffineExpression().addInPlace(vector)
def lpDot(v1, v2):
"""Calculate the dot product of two lists of linear expressions"""
if not const.isiterable(v1) and not const.isiterable(v2):
return v1 * v2
elif not const.isiterable(v1):
return lpDot([v1] * len(v2), v2)
elif not const.isiterable(v2):
return lpDot(v1, [v2] * len(v1))
else:
return lpSum([lpDot(e1, e2) for e1, e2 in zip(v1, v2)])
|