【问题标题】:OR Tools Constraint that requires equility of variables [ortools]或工具要求变量相等的约束 [ortools]
【发布时间】:2018-11-29 11:12:49
【问题描述】:

我想使用 OR-Tools 创建一个线性求解器模型。我有两个 numVar,我的约束之一是这两个变量的相等性。但是我找不到一种方法来设置带有变量的约束。我可以用 gurobi 库做到这一点。以下是这两个库的代码。有没有办法将变量传递到约束的两侧,例如 xa==xb? 提前致谢。

古罗比密码:

from gurobipy import Model, GRB
m = Model('rafinery')
xa = m.addVar(vtype=GRB.CONTINUOUS)
xb = m.addVar(vtype=GRB.CONTINUOUS)

# production constraint
m.addConstr(xa*400 + xb*300 >= 25000)
m.addConstr(xa*300 + xb*400 >= 27000)
m.addConstr(xa*200 + xb*500 >= 30000)
m.addConstr(xa>=0)
m.addConstr(xb>=0)
m.addConstr(xa==xb)

m.setObjective(xa * 20000 + xb * 25000, GRB.MINIMIZE)
m.update()
m.setObjective(xa + xb, GRB.MINIMIZE)
m.update()
m.optimize()

for v in m.getVars():
    print(v.varName, v.x)
print("Cost:", m.objVal)  

OR-工具代码:

from ortools.linear_solver import pywraplp
solver = pywraplp.Solver('SolveSimpleSystem', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)

# Create Variables
xa = solver.NumVar(0, solver.infinity(),'xa')
xb = solver.NumVar(0, solver.infinity(),'xb')

# Create Constraints
# xa*400 + xb*300 >= 25000
high = solver.Constraint(25000, solver.infinity())
high.SetCoefficient(xa, 400)
high.SetCoefficient(xb, 300)

# xa*300 + xb*400 >= 27000
middle = solver.Constraint(27000, solver.infinity())
middle.SetCoefficient(xa,300)
middle.SetCoefficient(xb,400)

# xa*200 + xb*500 >= 30000
high = solver.Constraint(30000, solver.infinity())
high.SetCoefficient(xa, 200)
high.SetCoefficient(xb, 500)

# another constraint that factories work for same days
# ******************** get error here ****************
noidle = solver.Constraint(xa,xa) 
noidle.SetCoefficient(xb,1)

# obj = minimize production cost which is
# xa * 20000 + xb * 25000
obj = solver.Objective()
obj.SetCoefficient(xa, 20000)
obj.SetCoefficient(xb, 25000)
obj.SetMinimization()

solver.Solve()
print("Rafinery Solution:")
print("Rafinery A work days:", xa.solution_value())
print("Rafinery B work days:", xb.solution_value())
print("Cost : ", solver.Objective().Value())

【问题讨论】:

    标签: or-tools


    【解决方案1】:
    from ortools.linear_solver import pywraplp
    solver = pywraplp.Solver('SolveSimpleSystem', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
    
    # Create Variables
    xa = solver.NumVar(0, solver.infinity(),'xa')
    xb = solver.NumVar(0, solver.infinity(),'xb')
    
    # production constraint
    solver.Add(xa*400 + xb*300 >= 25000)
    solver.Add(xa*300 + xb*400 >= 27000)
    solver.Add(xa*200 + xb*500 >= 30000)
    solver.Add(xa>=0)
    solver.Add(xb>=0)
    solver.Add(xa==xb)
    
    solver.Minimize(xa * 20000 + xb * 25000)
    
    solver.Solve()
    print("Rafinery Solution:")
    print("Rafinery A work days:", xa.solution_value())
    print("Rafinery B work days:", xb.solution_value())
    print("Cost : ", solver.Objective().Value())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 2017-02-10
      • 1970-01-01
      • 2017-11-14
      • 2021-11-30
      • 1970-01-01
      相关资源
      最近更新 更多