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

Convenient support for conjunctions and disjunctions #760

Open
lverweijen opened this issue Jul 15, 2024 · 1 comment
Open

Convenient support for conjunctions and disjunctions #760

lverweijen opened this issue Jul 15, 2024 · 1 comment

Comments

@lverweijen
Copy link

Occasionally, real life constraints may get the form of:

if is_married then age >= 18

These can be expressed in pulp by writing something like this:

BIG_CONSTANT = 99999
age = LpVar('age', 'Integer')
is_married = LpVar('is_married', 'Boolean')

constraint = age + (1 - is_married) * BIG_CONSTANT >= 18

but it isn't very readable.

Describe the new feature

I was thinking of adding conjunction and disjunction helpers lpOr and lpAnd that automatically convert constraints.
These operators should become nestable.

Then we can write something like this:

constraint = lpOr([is_married < 1, age >= 18])

Even more convenient would be to extend & and | operators of LpConstraint, so this can be written as:

constraint = (is_married < 1) | (age >= 18)

However, there is a conflict, because LpConstraint is inheriting from OrderedDict which already implements |. Maybe consider not inheriting from MutableMapping or accept an exception to this protocol.

More consequences of this:

  • Often new discrete helper variable and new constraints needs to be added to the model.
  • As a result the problem is no longer LP, but becomes MILP as soon as a boolean operator is used.

Additional info

Please answer these questions before submitting your feature request.

Does this feature exist in another product or project? Please provide a link.

Not sure, but Gurobi seems to support some mathematical operators as general constraints: https://www.gurobi.com/documentation/current/refman/general_constraints.html

@lverweijen
Copy link
Author

In https://github.com/lverweijen/FellegiHolt/blob/main/fellegiholt/rewrite_linear.py I managed to convert a few rules to linear ones like 'and', 'or', 'not', 'implication'. I took a different approach from above and started directly convert ast.Nodes.
There might be better ways of doing this, but it works for me. I would still be interested in having something like this.

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

1 participant