Skip to content

Commit fe9891d

Browse files
committed
Add tests for coin-or#815
1 parent ebbbfde commit fe9891d

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

pulp/tests/test_pulp.py

+65
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
import tempfile
99
import unittest
10+
from decimal import Decimal
1011

1112
from pulp import (
1213
LpAffineExpression,
@@ -1792,6 +1793,70 @@ def test_regression_805(self):
17921793
self.assertEqual(c.name, "Test2")
17931794
self.assertEqual(c.expr.name, "Test1")
17941795

1796+
def test_decimal_815(self):
1797+
# See: https://github.com/coin-or/pulp/issues/815
1798+
m1 = 3
1799+
m2 = Decimal("8.1")
1800+
extra = 5
1801+
1802+
x = LpVariable("x", lowBound=0, upBound=50, cat=LpContinuous)
1803+
y = LpVariable("y", lowBound=0, upBound=Decimal("32.24"), cat=LpContinuous)
1804+
include_extra = LpVariable("include_extra1", cat=LpBinary)
1805+
1806+
prob = LpProblem("graph", LpMaximize)
1807+
1808+
prob += y
1809+
1810+
# y = 3x + 5 | y = 3x
1811+
e1 = x * m1 + include_extra * extra - y
1812+
c1 = e1 == 0
1813+
prob += c1
1814+
1815+
# y = 8.1x - 6
1816+
e2 = x * m2 - 6 - y
1817+
c2 = e2 == 0
1818+
prob += c2
1819+
1820+
# This generates two possible systems of equations,
1821+
# y = 3x + 5
1822+
# y = 8.1x - 6
1823+
# this intersects at ~(11/5, 58/5)
1824+
1825+
# OR
1826+
# y = 3x
1827+
# y = 8.1x-6
1828+
# this intersects at ~(6/5, 18/5)
1829+
pulpTestCheck(
1830+
prob,
1831+
self.solver,
1832+
[const.LpStatusOptimal],
1833+
{x: 2.15686, y: 11.4706},
1834+
)
1835+
1836+
def test_decimal_815_addinplace(self):
1837+
# See: https://github.com/coin-or/pulp/issues/815
1838+
m1 = 3
1839+
m2 = Decimal("8.1")
1840+
extra = 5
1841+
1842+
x = LpVariable("x", lowBound=0, upBound=50, cat=LpContinuous)
1843+
y = LpVariable("y", lowBound=0, upBound=Decimal("32.24"), cat=LpContinuous)
1844+
include_extra = LpVariable("include_extra1", cat=LpBinary)
1845+
1846+
expression = LpAffineExpression()
1847+
expression += x * m1 + include_extra*extra - y
1848+
self.assertEqual(str(expression), "5*include_extra1 + 3*x - y")
1849+
1850+
with self.assertRaises(TypeError):
1851+
second_expression = LpAffineExpression()
1852+
second_expression += x * m2 - 6 - y
1853+
1854+
second_expression = LpAffineExpression(constant=Decimal("0"))
1855+
second_expression += x * m2 - 6 - y
1856+
self.assertEqual(str(second_expression), "8.1*x - y - 6.0")
1857+
1858+
second_expression_2 = x * m2 - 6 - y
1859+
self.assertEqual(str(second_expression_2), "8.1*x - y - 6.0")
17951860

17961861
class PULP_CBC_CMDTest(BaseSolverTest.PuLPTest):
17971862
solveInst = PULP_CBC_CMD

0 commit comments

Comments
 (0)