How to use a variable as a divisor in PuLP

后端 未结 2 1225
别那么骄傲
别那么骄傲 2021-01-13 13:36

I was trying to resolve a LP problem with a constraint that is calculated by dividing variable A by variable B.

The simple version of the problem is as below:

2条回答
  •  不思量自难忘°
    2021-01-13 14:38

    I felt like zero shouldn't be allowed in my solution — and I included a variable that was the sum of x and y (think you're referring to it as A).

    from pulp import LpProblem, LpStatus, LpVariable
    from pulp import LpMinimize, LpMaximize, lpSum, value
    
    # I feel like zero shouldn't be allowed for lower bound...
    x = LpVariable("x", lowBound=1, cat="Integer")
    y = LpVariable("y", lowBound=1, cat="Integer")
    z = LpVariable("z", lowBound=1, cat="Integer")
    
    model = LpProblem("Divison problem", LpMinimize)
    
    model += x
    model += z == x + y
    model += z == 100
    
    # Rather than division, we can use multiplication
    model += x >= z * 0.5
    model += y <= z * 0.4
    
    model.solve()
    
    print(LpStatus[model.status])
    
    print(f"""
    x = {int(x.varValue):>3}  #  60
    y = {int(y.varValue):>3}  #  40
    z = {int(z.varValue):>3}  # 100
    """)
    

提交回复
热议问题