问题
I've initialised my LpVariable like so:
x = LpVariable('x', None, None)
At this point, my variable has upper and lower bounds as float('inf')
and float('-inf')
. Now based on some parameters of my logic, I want to bound the upper limit of this variable to, say, any x < 20
.
Can I only do this by adding in an LpProblem and modifying the Variable using the problem parameters?
y = LpProblem('Minimizing Problem', LpMinimize)
y += x < 20
Or is there another way to manipulate the variable? Changing x.upBound
doesn't seem to work. I can still set invalid integers/floats as the solution (ie. values > 20
) and it accepts them.
回答1:
Turns out both ways work. So for the example:
y = LpProblem("min", LpMinimize)
y += x + 10 # Objective Function
x = LpVariable('x', None, None) # set to bounds=[float("-inf"),float("inf")]
We can change the lower bound on x
from the default float("-inf")
to 20
in one of the following ways:
Option 1:
Modifying the constraints on the LpProblem. So, for instance, if you wanted to change the lowBound of x to 20
, you would need to use:
y += x > 20, "changing lower bound of x"
Option 2: Modifying the lowBound attribute on the LpVariable object:
x.lowBound = 20
Both these changes should give us the solution of y = 30
来源:https://stackoverflow.com/questions/40749833/how-do-i-change-the-bounds-of-a-lpvariable-in-pulp-dynamically