首页 > 解决方案 > 双变量的错误结果

问题描述

我有以下代码:

for v in model.component_objects(Var, active=True):
    varobject = getattr(model, str(v))
    for index in varobject:
        if isinstance(varobject[index].domain, IntegerSet) or isinstance(varobject[index].domain, BooleanSet):
            varobject[index].fixed = True

results = opt.solve(model, tee=True)         variables

整数变量在第一次运行后已固定,因此可以获得对偶变量。问题是我为其中一个约束获得的对偶变量的值与我对相同模型和数据使用游戏获得的值不同。即使我为不同的变量得到相同的值。有人知道我做错了什么吗?提前致谢!

生成输出的函数:

def generate_output(df, file, variable, indices, column_name):
    writer = pd.ExcelWriter(file, engine='xlsxwriter')
    df_copy = df[(df[list({'variable_name': variable})] == pd.Series({'variable_name': variable})).all(axis=1)].drop('variable_name', 1)
    mindex = pd.MultiIndex.from_tuples(df_copy['variable_index'], names=indices)
    df_copy = pd.DataFrame(df_copy[['value']].values, index=mindex, columns=[column_name]).unstack('hour').reset_index()
    df_copy.to_excel(writer, 'Tabelle1')
    writer.save()
    wb = op.reader.excel.load_workbook(file)
    sh = wb['Tabelle1']
    sh.delete_rows(3, 1)
    wb.save(file)

标签: pythonpyomo

解决方案


我在对偶变量的值上遇到了类似的问题。

我对比了使用 Xpress 求解器 (opt = SolverFactory("amplxpress")) 和 Gurobi 求解器 (opt = SolverFactory("gurobi")) 从 Fico Xpress 客户端/终端和 pyomo 模型获得的结果。

在 Fico Xpress 中,与等式约束关联的对偶是 >=0,与 >= 约束关联的对偶是 >=0,而与 <= 约束关联的对偶是 <=0。

在 Pyomo 中,我发现与等式约束关联的对偶仍然 >=0,但是与不等式约束(>=0 或 <=0)关联的对偶始终<=0。

我想要的等式约束的对偶并没有太大的差异(差异的大小可能取决于模型),而我想要的不等式约束的对偶差异很大(在大小和符号上都有很大的不同),但是,它可能不会表明来自 pyomo 的对偶必然是不正确的。

我使用与不平等约束相关的对偶作为价格(应该 >=0)来计算一些收入/利润,其中我在 Fico Xpress 中使用的等式基于非负对​​偶。由于 pyomo 给了我非正对偶,我操纵方程并得到相同的结果,即使 pyomo 的对偶值与 Fico Xpress 完全不同。

由于与您的模型相关联的对偶可能具有特定的物理含义,您是否在比较对偶值之前检查了通过操作对偶获得的结果的正确性?


推荐阅读