使用IBM CPLEX python API的分段函数,但无法解决该问题 [英] Using the piecewise function of the IBM CPLEX python API, but the problem cannot be solved

查看:442
本文介绍了使用IBM CPLEX python API的分段函数,但无法解决该问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用MILP(混合整数线性规划)来计算单位承诺问题. (单位承诺:试图找到最佳发电机调度的优化问题) 由于发电机功率与成本之间的关系是二次函数,因此我使用分段函数将功率转换为成本.

I try to use MILP (Mixed Integer Linear Programming) to calculate the unit commitment problem. (unit commitment: An optimization problem trying to find the best scheduling of generator) Because the relationship between generator power and cost is a quadratic function, so I use piecewise function to convert power to cost.

在此处输入图片描述

我修改此页面上的答案:输入链接描述这里

I modify the answer on this page :enter link description here

简单的程序结构是这样的:

The simple program structure is like this::

from docplex.mp.model import Model

mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')

#after 4 buses, additional buses of a given size are cheaper
f1=mdl.piecewise(0, [(0,0),(4,2000),(10,4400)], 0.8) 
f2=mdl.piecewise(0, [(0,0),(4,1600),(10,3520)], 0.8) 
cost1= f1(nbbus40)
cost2 = f2(nbbus30)

mdl.minimize(cost1+ cost1)
mdl.solve()
mdl.report()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value) 

给出

* model buses solved with objective = 3520.000
nbBus40  =  0
nbBus30  =  10.0 

答案是完美的,但是没有办法应用我的例子. 我使用分段函数来表示功率和成本之间的分段线性关系,并获得了一个新对象(cost1),然后计算了该对象的最小值. 以下是我的实际代码(简单): 在此处输入图片描述(min1,miny1),(pw1_1,pw1_1y),(pw1_2 ,pw1_2y),(max1,maxy1)是功率成本曲线上的断点

The answer is perfect but there is no way to apply my example. I used a piecewise function to formulate a piecewise linear relationship between power and cost, and got a new object (cost1), and then calculated the minimum value of this object. The following is my actual code(simply): enter image description here (min1,miny1), (pw1_1,pw1_1y),(pw1_2,pw1_2y),(max1,maxy1)Are the breakpoints on the power-cost curve

pwl_func_1phase = ucpm.piecewise(0, [(0,0),(min1,miny1), (pw1_1,pw1_1y),(pw1_2,pw1_2y),(max1,maxy1)], 0)
#df_decision_vars_spinning is a dataframe store Optimization variables
df_decision_vars_spinning.at[(units,period),'variable_cost'] = pwl_func_1phase(df_decision_vars_spinning.at[(units,period),'production'] )

total_variable_cost = ucpm.sum((df_decision_vars_spinning.variable_cost))
ucpm.minimize(total_variable_cost )

我不知道是什么原因导致无法解决此优化问题. 这是我的完整代码: https://colab.research.google.com/drive/1JSKfOf0Vzo3E3FywsxcDdOz4sAwCgOHd?usp = sharing

I don’t know what causes this optimization problem can't be solve. here is my complete code :https://colab.research.google.com/drive/1JSKfOf0Vzo3E3FywsxcDdOz4sAwCgOHd?usp=sharing

推荐答案

使用不受限制的CPLEX版本,您的模型可以解决(尽管速度很慢).这里有两个想法可以更好地控制solve()中发生的事情

With an unlimited edition of CPLEX, your model solves (though very slowly). Here are two ideas to better control what happens in solve()

  1. 使用Solve(log_output = True)打印日志:您会发现差距在缩小
  2. 设置Mip间隙:将Mip间隙设置为5%将在36s时停止求解

  1. use solve(log_output=True) to print the log: you'll see the gap going down
  2. set a mip gap: setting mip gap to 5% stops the solve at 36s

ucpm.parameters.mip.tolerances.mipgap = 0.05

ucpm.parameters.mip.tolerances.mipgap = 0.05

ucpm.solve(log_output = True)

ucpm.solve(log_output=True)

这篇关于使用IBM CPLEX python API的分段函数,但无法解决该问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆