简体   繁体   中英

Linear programming (optimization) with Pulp

I would like to ask you something regarding a Linear program for optimization. I already successfully setup the model. However, I have problems in setting up a metaheuristic to reduce the computation time. The basic optimization model can be seen here:

In the metaheuristic algorithm there is an while loop with a condition as follows:
while $ \\sum_{i=1}^I b_i y_i \\leq \\sum_{k=1}^K q_k $ do

I tried to realize this condition with the following code:

while lpSum(b[i]*y[i] for i in I)<=lpSum(q[k] for k in K): 

If calculate the two sums separately I get the right results for both. However, when I put them into this condition, the code runs into an endless loop, even when the condition gets fulfilled and it should break the loop. I guess it has to do with the data type, and that the argument can't be an LpAffineExpression . However, I am really struggling to understand this problem.

I hope you understood my problem and I would really appreciate your ideas and explanations a lot! Please tell me, if you need more information on something specific - sorry for being a beginner.

Thanks a lot in advance and best regards, Bernhard

lpSum s do not have a value, like a regular sum has.

Any Python objects can have be compared to other objects using built-in equations like __eq__ . That is how I can say date(2000, 1, 1) < date(2000, 1, 2) . However, lpAffineExpressions s (which lpSum s are a type of) are meant to be used in constraints. Their contents are variables, which are solved by the LP solver, so they do not yet have any values.

Thus the return value for lpSum(x) <= lpSum(y) is not true or false, like with normal equations, but it's an equation. And an equation is not None , or False , or any other falsey value. What you are saying is equivalent to while <some object>: , which is always true. Hence your infinite loop.


I don't know what "using a metaheuristic to reduce computation time" implies in this context - maybe you run a few iterations of the LP solver and then employ your metaheuristic on the result.

If that is the case, use b[i].value() to get the value the variable b[i] was given in that solution, and be sure to compute the total in a regular sum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM