Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Pyomo混合整数线性规划中建立正确的连接约束?_Python_Optimization_Linear Programming_Pyomo - Fatal编程技术网

Python 如何在Pyomo混合整数线性规划中建立正确的连接约束?

Python 如何在Pyomo混合整数线性规划中建立正确的连接约束?,python,optimization,linear-programming,pyomo,Python,Optimization,Linear Programming,Pyomo,背景:为了学习这个框架,我正在做一项作业,我正在使用Pyomo。 以下是作业: 从这个问题中,我了解到我应该引入一个二进制变量来触发固定成本约束 以下是我的实现: from pyomo.environ import * VERY_LARGE_NUMBER = 1e8 def part_4_april(data, limit_ballons, limit_labor): model = ConcreteModel() PACKAGES = data.keys()

背景:为了学习这个框架,我正在做一项作业,我正在使用Pyomo。 以下是作业:

从这个问题中,我了解到我应该引入一个二进制变量来触发固定成本约束

以下是我的实现:

from pyomo.environ import *

VERY_LARGE_NUMBER = 1e8


def part_4_april(data, limit_ballons, limit_labor):

    model = ConcreteModel()

    PACKAGES = data.keys()

    model.x = Var(PACKAGES, within=NonNegativeReals)
    model.y = Var(PACKAGES, within=Binary)

    model.profit = Objective(
        expr=sum(
            (
                (data[p]["price"] - data[p]["cost"]) * model.x[p]
                - data[p]["fixed_cost"] * model.y[p]
            )
            for p in PACKAGES
        ),
        sense=maximize,
    )

    model.ballon_cst = Constraint(
        expr=sum(data[p]["ballons"] * model.x[p] for p in PACKAGES) <= limit_ballons
    )

    model.labor_cst = Constraint(
        expr=sum(data[p]["labor"] * model.x[p] for p in PACKAGES) <= limit_labor
    )

    model.fixed_cost_cst = Constraint(
        expr=sum(model.x[p] - VERY_LARGE_NUMBER * model.y[p] for p in PACKAGES) <= 0
    )

    opt = SolverFactory("cbc")
    results = opt.solve(model, tee=True)
    model.pprint()
    return model


if __name__ == "__main__":
    # part 4
    data = {
        "Standard": {"labor": 3, "ballons": 2, "cost": 2, "price": 3, "fixed_cost": 10},
        "Joyful": {"labor": 5, "ballons": 5, "cost": 3, "price": 5, "fixed_cost": 5},
        "Fabulous": {"labor": 8, "ballons": 8, "cost": 4, "price": 7, "fixed_cost": 1},
    }
    model = part_4_april(data, limit_ballons=360, limit_labor=500)
    print("----- PART 4: April -----")
    print("Profit:", model.profit())
    for c in data.keys():
        print("  ", c, ":", model.x[c]())

我的代码或我使用二进制变量的方式有问题吗?

我最终通过Bethany Nicholson和Erwin Kalvelangen的评论找到了解决方案-我的代码有两个问题。首先,固定成本不得包含总和,应单独应用于所有包

# NOT...
# model.fixed_cost_cst = Constraint(
#     expr=sum(model.x[p] - VERY_LARGE_NUMBER * model.y[p] for p in PACKAGES) <= 0
# )

# must be applied to all packages individually 
def fixed_cost_rule(model, p):
    return model.x[p] - VERY_LARGE_NUMBER * model.y[p] <= 0

model.fixed_cost_cst = Constraint(PACKAGES, rule=fixed_cost_rule)
#不是。。。
#model.fixed_cost_cst=约束条件(

#expr=sum(model.x[p]-VERY_NUMBER*model.y[p]表示包中的p)作为调试工具,您可以
model.pprint()
你的模型,并确保它看起来像它应该的样子。我做了
pprint
模型,问题并不明显在哪里。作为一名经济学家,我想说的是,在这样的需求下,利润最大化,提高了包的价格;-)说真的,你在应用模型的类型上有限制吗(MIP,没有,…)以及解算器引擎(glpk、ipopt等)?您确定所期望的解决方案是正确的解决方案吗?当我将这些数字插入模型并评估你的目标函数时,我得到了159个利润,而不是188个。你的大M太大了。此外,固定成本约束是错误的(不应该有总和)。
# NOT...
# model.fixed_cost_cst = Constraint(
#     expr=sum(model.x[p] - VERY_LARGE_NUMBER * model.y[p] for p in PACKAGES) <= 0
# )

# must be applied to all packages individually 
def fixed_cost_rule(model, p):
    return model.x[p] - VERY_LARGE_NUMBER * model.y[p] <= 0

model.fixed_cost_cst = Constraint(PACKAGES, rule=fixed_cost_rule)