Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using modulus operator in Constraint #762

Open
rogeriobiondi opened this issue Jul 17, 2024 · 3 comments
Open

Using modulus operator in Constraint #762

rogeriobiondi opened this issue Jul 17, 2024 · 3 comments

Comments

@rogeriobiondi
Copy link

rogeriobiondi commented Jul 17, 2024

What is your question

Hello, is it possible using the modulus operator in a constraint?

For example

q1 = pulp.LpVariable(name = 'q1', lowBound=100, cat='Integer')
Z += (q1 % 100) == 0

I'm getting an invalid operator:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[39], line 25
---> 25 Z += q1 % 100 == 0

TypeError: unsupported operand type(s) for %: 'LpVariable' and 'int'

I would want that the variable q1 would be a multiple of 100.

Thank you very much.

@Alannikos
Copy link

hi, i also met the error, did you find the solution? @rogeriobiondi

@urbanophile
Copy link
Contributor

No, pulp doesn't allow the use of modulo operator % in constraint like that.

Modulo is not linear function, so in some ways this is the expected response for a linear programming model. Since x is an integer, though, you can represent a modulo constraint by introducing a new variable q so that x - q*100 == 0, like so:

>>> from pulp import *
>>> x = LpVariable("x", 100, 200, cat="Integer")
>>> y = LpVariable("y", 100, 200, cat="Integer")
>>> prob = LpProblem("moduloExample", LpMaximize)
>>> prob += x+y<=399
>>> modulus = 100
>>> q = LpVariable("q",0,modulus-1, "Integer") # helper variable
>>> prob += x - modulus * q == 0
>>> status = prob.solve(PULP_CBC_CMD(msg=0))
>>> LpStatus[status]
1

>>> value(x)
100.0

>>> value(y)
100.0

>>> value(q)
1.0

@rogeriobiondi
Copy link
Author

rogeriobiondi commented Sep 9, 2024

After studying a little in the OR books, I found this exact reason: the possible solution space will be bounded by the restrictions, and they must be all linear equations. So I had to rethink the problem and adapt it to the technique.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants