python - 为什么我的 CPLEX 问题变得不可行?
问题描述
我已经定义了一个问题,希望找到在 24 小时期间(分为 6 个成本期间)以最佳成本交付数量的最佳方式。
我已经允许我们需要遵守的多个约束条件。
- 水库必须保持在最低水位以上(2.2m)
- 水库必须保持在最高水位以下(2.9m)
- 输送的体积必须 >= 最小体积(1,400,000 升)
为了保持和检查水库水位限制,我计算了水库体积,以计算出每升水的量以及它将如何影响每个点的水库水位。
我在另一个项目中定义了同样的问题并且它工作成功,但是这个泵可以通过改变更多的速度来提供更多的体积。
我的问题功能概述如下
def optimiser(self, cost_, volume_,v_min,flow_,min_level,max_level,initial_level,period_lengths,out_flow_, errors, hours_) :
FACTOR = 0.001*1800*self.RESERVOIR_VOLUME
CHANGABLE_LENGTHS = len(period_lengths)
input_flow_matrix=np.zeros((max(period_lengths), CHANGABLE_LENGTHS))
for i,l in enumerate(period_lengths):
input_flow_matrix[:l,i]=1
selection = cp.Variable(shape=cost_.shape, boolean=False)
# Constraints
assignment_constraint = cp.sum(selection,axis=1) == 1
input_flow_= cp.sum(cp.multiply(flow_,selection),axis=1)
input_flow_vector=cp.vec(cp.multiply(input_flow_matrix,np.ones((max(period_lengths), 1)) @ cp.reshape(input_flow_,(1,len(period_lengths)))))
res_flow= (input_flow_vector-cp.vec(out_flow_))
res_level=cp.cumsum(res_flow) * FACTOR + initial_level
volume_= cp.sum(cp.multiply(volume_,selection))
volume_constraint = volume_ >= v_min
min_level_constraint = res_level >= min_level
max_level_constraint = res_level <= max_level
if errors is LevelTooLowError:
constraints = [assignment_constraint, max_level_constraint, volume_constraint]
elif errors is LevelTooHighError:
constraints = [assignment_constraint, min_level_constraint, volume_constraint]
elif errors is MaxVolumeExceededError:
constraints = [assignment_constraint, min_level_constraint, volume_constraint]
else:
constraints = [assignment_constraint, max_level_constraint, min_level_constraint, volume_constraint]
cost_ = cp.sum(cp.multiply(cost_,selection))
assign_prob = cp.Problem(cp.Minimize(cost_),constraints)
assign_prob.solve(solver=cp.CPLEX, verbose=True)
return selection, res_level
numpy 数组:cost_
[[ 0. 4.85981558 5.05193127 5.72077851 6.36116417 7.00866522
7.64905087 8.32501351 8.99386075 9.69116957 10.40270918 11.14982578
11.91117317 12.70809754 13.5334835 ]
[ 0. 7.63685305 7.93874915 8.98979481 9.99611512 11.01361677
12.01993709 13.08216408 14.13320974 15.22898076 16.34711443 17.5211548
18.71755784 19.96986757 21.26690265]
[ 0. 2.86381989 2.97703093 3.37117305 3.74854317 4.13010629
4.50747641 4.90581153 5.29995365 5.71086778 6.13016791 6.57043305
7.01908419 7.48870034 7.97508849]
[ 0. 1.43190995 1.48851546 1.68558653 1.87427159 2.06505314
2.2537382 2.45290577 2.64997683 2.85543389 3.06508396 3.28521653
3.5095421 3.74435017 3.98754425]
[ 0. 1.73564842 1.80426117 2.04313518 2.27184435 2.50309472
2.73180388 2.97321911 3.21209312 3.46113199 3.71525328 3.98208064
4.25399042 4.53860627 4.83338696]
[ 0. 0.91121542 0.94723711 1.07264597 1.19271828 1.31412473
1.43419704 1.56094003 1.68634889 1.8170943 1.95050797 2.09059233
2.23334497 2.38276829 2.53752816]]
体积_
[[ 0. 298080. 371520. 440352. 498240. 556992. 608256. 653760. 697248.
736128. 775584. 814752. 854208. 887328. 921600.]
[ 0. 298080. 371520. 440352. 498240. 556992. 608256. 653760. 697248.
736128. 775584. 814752. 854208. 887328. 921600.]
[ 0. 111780. 139320. 165132. 186840. 208872. 228096. 245160. 261468.
276048. 290844. 305532. 320328. 332748. 345600.]
[ 0. 55890. 69660. 82566. 93420. 104436. 114048. 122580. 130734.
138024. 145422. 152766. 160164. 166374. 172800.]
[ 0. 74520. 92880. 110088. 124560. 139248. 152064. 163440. 174312.
184032. 193896. 203688. 213552. 221832. 230400.]
[ 0. 55890. 69660. 82566. 93420. 104436. 114048. 122580. 130734.
138024. 145422. 152766. 160164. 166374. 172800.]]
flow_(升/秒)
[['0.00' '10.35' '12.90' '15.29' '17.30' '19.34' '21.12' '22.70' '24.21'
'25.56' '26.93' '28.29' '29.66' '30.81' '32.00']
['0.00' '10.35' '12.90' '15.29' '17.30' '19.34' '21.12' '22.70' '24.21'
'25.56' '26.93' '28.29' '29.66' '30.81' '32.00']
['0.00' '10.35' '12.90' '15.29' '17.30' '19.34' '21.12' '22.70' '24.21'
'25.56' '26.93' '28.29' '29.66' '30.81' '32.00']
['0.00' '10.35' '12.90' '15.29' '17.30' '19.34' '21.12' '22.70' '24.21'
'25.56' '26.93' '28.29' '29.66' '30.81' '32.00']
['0.00' '10.35' '12.90' '15.29' '17.30' '19.34' '21.12' '22.70' '24.21'
'25.56' '26.93' '28.29' '29.66' '30.81' '32.00']
['0.00' '10.35' '12.90' '15.29' '17.30' '19.34' '21.12' '22.70' '24.21'
'25.56' '26.93' '28.29' '29.66' '30.81' '32.00']]
周期长度
[16 16 6 3 4 3]
外流_
[[10.09500027 9.62833341 6.72666672 8.66999976
8.54166667 8.53166676]
[10.09500027 9.62833341 6.72666672 8.66999976 8.79166667 8.57166672]
[ 9.64666653 9.61999989 5.79833327 8.54166667 8.79166667 8.57166672]
[ 9.64666653 9.61999989 5.79833327 0. 8.53166676 0. ]
[ 9.65666676 6.78333328 8.54500008 0. 0. 0. ]
[ 9.65666676 6.78333328 8.54500008 0. 0. 0. ]
[ 9.55666669 6.75833329 0. 0. 0. 0. ]
[ 9.55666669 6.75833329 0. 0. 0. 0. ]
[10.30666669 6.7750001 0. 0. 0. 0. ]
[10.30666669 6.7750001 0. 0. 0. 0. ]
[ 9.72166697 6.81166673 0. 0. 0. 0. ]
[ 9.72166697 6.81166673 0. 0. 0. 0. ]
[ 9.64833323 6.66000009 0. 0. 0. 0. ]
[ 9.64833323 6.66000009 0. 0. 0. 0. ]
[ 9.61833318 6.81833339 0. 0. 0. 0. ]
[ 9.61833318 6.81833339 0. 0. 0. 0. ]]
错误
None
因素
0.003360759456710468
当我运行程序来建立最适合这种情况的程序时,我会遇到CPLEX Error 1217: No solution exists.
导致我的问题变成unbounded
.
我已经运行了详细的问题,并有以下堆栈跟踪可以解决。
===============================================================================
CVXPY
v1.1.12
===============================================================================
(CVXPY) Oct 12 04:29:42 PM: Your problem has 90 variables, 4 constraints, and 0 parameters.
(CVXPY) Oct 12 04:29:42 PM: It is compliant with the following grammars: DCP, DQCP
(CVXPY) Oct 12 04:29:42 PM: (If you need to solve this problem multiple times, but with different data, consider using parameters.)
(CVXPY) Oct 12 04:29:42 PM: CVXPY will first compile your problem; then, it will invoke a numerical solver to obtain a solution.
-------------------------------------------------------------------------------
Compilation
-------------------------------------------------------------------------------
(CVXPY) Oct 12 04:29:42 PM: Compiling problem (target solver=CPLEX).
(CVXPY) Oct 12 04:29:42 PM: Reduction chain: CvxAttr2Constr -> Qp2SymbolicQp -> QpMatrixStuffing -> CPLEX
(CVXPY) Oct 12 04:29:42 PM: Applying reduction CvxAttr2Constr
(CVXPY) Oct 12 04:29:42 PM: Applying reduction Qp2SymbolicQp
(CVXPY) Oct 12 04:29:42 PM: Applying reduction QpMatrixStuffing
(CVXPY) Oct 12 04:29:42 PM: Applying reduction CPLEX
(CVXPY) Oct 12 04:29:42 PM: Finished problem compilation (took 9.762e-03 seconds).
-------------------------------------------------------------------------------
Numerical solver
-------------------------------------------------------------------------------
(CVXPY) Oct 12 04:29:42 PM: Invoking solver CPLEX to obtain a solution.
Version identifier: 20.1.0.0 | 2020-11-11 | 9bedb6d68
CPXPARAM_Read_DataCheck 1
Duplicate columns x40 and x34 make problem unbounded.
Presolve time = 0.00 sec. (0.08 ticks)
CPLEX Error 1217: No solution exists.
-------------------------------------------------------------------------------
Summary
-------------------------------------------------------------------------------
(CVXPY) Oct 12 04:29:42 PM: Problem status: unbounded
(CVXPY) Oct 12 04:29:42 PM: Optimal value: -inf
(CVXPY) Oct 12 04:29:42 PM: Compilation took 9.762e-03 seconds
(CVXPY) Oct 12 04:29:42 PM: Solver (including time spent in interface) took 2.994e-03 seconds
Traceback (most recent call last):
File ".\AECRun.py", line 9, in <module>
AEC(current_level, site_id, pump_combo, True)
File "C:\Users\Aidan\Documents\Belleek AEC\AEC.py", line 62, in __init__
print(json.dumps(self.get_regime()))
File "C:\Users\Aidan\Documents\Belleek AEC\AEC.py", line 575, in get_regime
assignments = [np.where(r==1.0)[0][0] for r in sol.value]
TypeError: 'NoneType' object is not iterable
我不明白为什么它会失败,因为当我删除一些约束时它仍然找不到解决方案,这让我觉得我忽略了一个简单的解决方案来解决我的混合整数 cplex 问题。
解决方案
推荐阅读
- php - 如何确定自己有多少 PHP CLI 进程正在运行,跨操作系统?
- ios - 保存到相册后,相机图像的 SHA256 哈希值不同
- google-cloud-platform - 云构建触发器看不到来自另一个项目的云源存储库
- javascript - 如何测试相对 url 在 JavaScript 中是否有效?
- django - Django 错误:CSRF 验证仅使用 Apache 失败(不使用开发服务器)
- angular7 - angular 7如何通过子组件模板中定义的父组件中的id访问元素
- docker - 詹金斯管道错误中的docker.image.inside:无法运行程序“docker”:错误= 2,没有这样的文件或目录
- html - 如何在引导程序 4 中增加自定义复选框的目标大小?
- javascript - 有没有更好的方法来调用和放置这个点击处理函数?
- android - 使 Android 应用隐藏的数字可见...仅适用于专业应用