Skip to content

TypeError when LpAffineExpressions are Specified Using Decimals #815

Open
@mark92223

Description

@mark92223

When a LpAffineExpression is extended using Decimals, as of pulp 2.9.0 (and before), it would work without raising any exception and returning accurate results. Beginning in 3.0.0, it now raises a TypeError in the addInPlace method:

self.constant += other.constant * sign

A reproducing pytest file which can be run, passing in 2.9.0, and failing in 3.0.2(or 3.0.0): https://gist.github.com/mark92223/e590c21df931eda1927ae4230fec118d .

Is using Decimals in this context unsupported in Pulp?

Thank you!

Details for the issue

What did you do?

What did you expect to see?

The test to pass

What did you see instead?

    def test_it(self):
        from pulp import LpProblem, LpVariable, LpMaximize, LpAffineExpression
        from pulp.apis import GLPK_CMD

        m1, m2, extra = 3, Decimal("8.1"), 5

        problem = LpProblem("graph", LpMaximize)
        y = LpVariable("y", lowBound=0, upBound=Decimal("32.24"), cat="Continuous")
        problem += y

        expression = LpAffineExpression()
        include_extra = LpVariable("include_extra1", cat="Binary")
        x = LpVariable("x", lowBound=0,
                       upBound=random.randint(3, 50), cat="Continuous")
        # y = 3x + 5 | y = 3x
        expression += x * m1 + include_extra*extra - y
        problem += expression == 0

        second_expression = LpAffineExpression()
        # y = 8.1x - 6
>       second_expression += x * m2 - 6 - y

test_pypi_packages_for_wms.py:125:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/home/mark/ve3.13/lib/python3.13/site-packages/pulp/pulp.py:928: in __iadd__
    return self.addInPlace(other)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = 0.0, other = 8.1*x + -1*y + -6.0, sign = 1

    def addInPlace(self, other, sign=1):
        """
        :param int sign: the sign of the operation to do other.
            if we add other => 1
            if we subtract other => -1
        """
        if isinstance(other, int) and (other == 0):
            return self
        if other is None:
            return self
        if isinstance(other, LpElement):
            # if a variable, we add it to the dictionary
            self.addterm(other, sign)
        elif isinstance(other, (LpAffineExpression, LpConstraint)):
            # if an expression, we add each variable and the constant
>           self.constant += other.constant * sign
E           TypeError: unsupported operand type(s) for +=: 'float' and 'decimal.Decimal'

/home/mark/ve3.13/lib/python3.13/site-packages/pulp/pulp.py:888: TypeError

Useful extra information

The info below often helps, please fill it out if you're able to. :)

What operating system are you using?

  • Windows: ( version: ___ )
  • Linux: (Arch Linux)
  • Mac OS: ( version: ___ )
  • Other: ___

I'm using python version:

  • 3.7
  • 3.8
  • 3.9
  • 3.10
  • 3.11
  • Other: 3.13

I installed PuLP via:

Did you also

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions