Python Pulp Solver: How to use compare signs (>= or <=) as variables in equation?

Question:

Hello I am trying to develop a Python Pulp Linear Programming solver which is not hard-coded but instead takes all input from the user.
I have successfully done the part of objective function but I am having problem with the constraint equation part.

here’s a part from my code:

sym = input("Enter constraint symbol '>=' or '<=' or '=': ")
op = operatorlookup.get(sym)
prob += lpSum([1*descVar[0] + 1*descVar[2]]) op 200, "Min boys models"

The operator lookup class returns the operator used:

operatorlookup = {
    '+': operator.add,
    '-': operator.sub,
    '>=': operator.gt,
    '<=': operator.lt,
    '=': operator.eq
}

My Problem is how can I assign the symbol >= or <= in the equation without getting the syntax error?
A hardcoded constraint would be written like this:

prob += lpSum([1*descVar[0] + 1*descVar[2]]) >= 800, "CaloriesMinimum"

I cannot use the + or "" because it outputs an error. Any help will be appreciated thank you

Asked By: ItsBuddy007

||

Answers:

assuming

import operator
op = operator.ge

Python doesn’t interpret variable as operator so 5 op 2 doesn’t work. But operators in operator module are functions and can be executed as such:

op(5, 2)  # equivalent to 5 >= 2

https://docs.python.org/3/library/operator.html

Answered By: mojeto