coin-or / pulp

A python Linear Programming API
http://coin-or.github.io/pulp/
Other
2.04k stars 381 forks source link

how to use piecewise linear constraint in pulp? #173

Closed pancerZH closed 6 years ago

pancerZH commented 6 years ago

For example, I have one constraints goes: x + y >=2 or x + y ==2 there are many such constraints in my model, so how to express the or in pulp?

naveenashwa77 commented 6 years ago

import pulp

define whether you gonna minimize or maximize ur objective

lp_solve = pulp.LpProblem("My LP Problem", pulp.LpMaximize or pulp.LpMinimize)

define your variables

x = pulp.LpVariable('x', lowBound=0, cat='Continuous') y = pulp.LpVariable('y', lowBound=2, cat='Continuous')

define Objective function

lp_solve +=

set your Constraints

lp_solve +=

call solver

lp_solve.solve() for variable in lp_solve.variables(): print "{} = {}".format(variable.name, variable.varValue) print pulp.value(lp_solve.objective)

stumitchell commented 6 years ago

your example seems to be strange as x+y >=2 includes x+y == 2 so you only need the first.

however in general you will need to at binary integer decision variables to your model to use 'or' constraints

Stu

Stuart Mitchell PhD Engineering Science Extraordinary Freelance Programmer and Optimisation Guru www.stuartmitchell.com

On Mon, Apr 30, 2018 at 3:50 AM, naveenashwa77 notifications@github.com wrote:

import pulp

define whether you gonna minimize or maximize ur objective

lp_solve = pulp.LpProblem("My LP Problem", pulp.LpMaximize or pulp.LpMinimize)

define your variables

x = pulp.LpVariable('x', lowBound=0, cat='Continuous') y = pulp.LpVariable('y', lowBound=2, cat='Continuous') define Objective function

lp_solve += set your Constraints

lp_solve += (x+y) == 25 - x lp_solve += (x + y) >= 2

call solver

lp_solve.solve() for variable in lp_solve.variables(): print "{} = {}".format(variable.name, variable.varValue) print pulp.value(my_lp_problem.objective)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/coin-or/pulp/issues/173#issuecomment-385260774, or mute the thread https://github.com/notifications/unsubscribe-auth/AAtEWeokYDe9UKpd8Z8qJsBofRjuQJ8Xks5tteEogaJpZM4TrY7C .

pancerZH commented 6 years ago

Thank you for help but so sorry to find I made a mistake, the constraints should be: x + y >= 2 or x + y == 0 I have tried to write them like this: lp_solve += (x+y)>=2 lp_solve += (x+y)==0 It just did not work(obviously) and no result returned. And I can't use constraint as lp_solve += (x+y)>=0, because I don't want the (0, 2) part. Then how to deal with it?

stumitchell commented 6 years ago

have a look at this video

https://www.youtube.com/watch?v=-3my1TkyFiM

Stuart Mitchell PhD Engineering Science Extraordinary Freelance Programmer and Optimisation Guru www.stuartmitchell.com

On Mon, Apr 30, 2018 at 1:08 PM, 张文喆 notifications@github.com wrote:

Thank you for help but so sorry to find I made a mistake, the constraints should be: x + y >= 2 or x + y == 0 I have tried to write them like this: lp_solve += (x+y)>=2 lp_solve += (x+y)==0 It just did not work(obviously) and no result returned. And I can't use constraint as lp_solve += (x+y)>=0, because I don't want the (0, 2) part. Then how to deal with it?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/coin-or/pulp/issues/173#issuecomment-385297018, or mute the thread https://github.com/notifications/unsubscribe-auth/AAtEWcAHMNp4oMyP5EZhtFoF7xzj-Agkks5ttmPwgaJpZM4TrY7C .

pancerZH commented 6 years ago

Thank you for help! Finally I solved it with nonlinear programming : )