用python纸浆创建一个优化问题以最小化两个求和

问题描述

我需要创建一个最小化问题来最小化这个函数

∑_i(disp i Ho) + Co∑_iWi

主要任务是用初始值为零的最优值填充下表。

# table to replicate ################################
#               set-21 ott-21 nov-21 dec-21 gen-21
# Qi              0       0     0      0      0
# Wi              0       0     0      0      0
# Qai             0       0     0      0      0
# disp_i          0       0     0      0      0
#########################################################

主要问题是对象函数中没有提到的另外两个变量,由于它们所涉及的一些限制,需要优化。

disp_i 可以从 -inf 到 +inf。 Wi 是二进制 (0,1)。 h0 和 c0 是共变量。 其他常量在附加代码中指定。

我曾尝试使用 scipy 解决问题,但我无法弄清楚。 然后我尝试用纸浆,但我仍然无法弄清楚。

这是我目前编写的代码。对我来说似乎写得很好,但我总是遇到一些错误

import numpy as np
from pulp import *

# table to replicate ################################
#               set-21 ott-21 nov-21 dec-21 gen-21
# Qi              0       0     0      0      0
# Wi              0       0     0      0      0
# Qai             0       0     0      0      0
# disp_i          0       0     0      0      0
#########################################################

#lists (table columns)
temp_int = ["set-21","ott-21","nov-21","dec-21","gen-21"]

#dictionaries
D_i = {"set-21": 3255,"ott-21": 3241,"nov-21": 2634,"dec-21": 3292,"gen-21": 3287}

# transp = {"Qi": {"set-21": 0,"ott-21": 0,"nov-21": 0,"dec-21": 0,"gen-21": 0},#           "Wi": {"set-21": 0,#           "Qai": {"set-21": 0,#           "disp_i": {"set-21": 0,"gen-21": 0}
#           }

# define
h0 = 0.005596951
c0 = 32.1
arrot = 275
M = 10000000
lotMin = 3300
disp_0 = 2286

# set problem variable
prob = LpProblem("Supply_Chain",LpMinimize)

# decision variables
Qi_var = LpVariable.dicts("Decision_Variables",temp_int,lowBound=0,cat=LpInteger)
Wi_var = LpVariable.dicts("Wi",upBound=1,cat=LpInteger)
Qai_var = LpVariable.dicts("Qai",cat=LpInteger)
disp_1_var = LpVariable.dicts("disp_i",lowBound=-np.inf,cat=LpInteger)

# objective function
prob += h0*lpSum(disp_1_var[i] for i in temp_int) + c0*lpSum(Wi_var[i] for i in temp_int)

# constraints
for i in temp_int:
    prob += Qi_var[i] >= lotMin * Wi_var[i]

for i in temp_int:
    prob += arrot*Qai_var[i] == Qi_var[i]

#for i in temp_int:
    prob += lpSum(Qi_var[i] for i in temp_int) >= lpSum(D_i[i] for i in temp_int)

for i in temp_int:
    prob += disp_1_var[i] >= 0

for i in temp_int:
    prob += Qi_var[i] <= M*Wi_var[i]

for i in temp_int:
    prob += Qi_var[i] >= Wi_var[i]

for i in temp_int:
    if i == 0:
        disp_1_var[i] = disp_0 + Qi_var[i] - D_i[i]
    else:
        disp_1_var[i] = disp_0 + Qi_var[i] - D_i[i]  # disp_1_var[i-1]

prob.solve()
print("Status: ",LpStatus[prob.status])

for i in temp_int:
     print(Qi_var[i].varValue)
     print(disp_1_var[i].varValue)

这是我得到的输出

C:\Python\python395\python.exe C:/Users/davide.tamagnini.con/AppData/Roaming/JetBrains/PyCharmCE2021.1/scratches/example.py
Welcome to the CBC MILP Solver 
Version: 2.9.0 
Build Date: Feb 12 2015 

command line - C:\Python\python395\lib\site-packages\pulp\apis\..\solverdir\cbc\win\64\cbc.exe C:\Users\DAVIDE~1.CON\AppData\Local\Temp\f262fcfe1f714303af02b97868f3a9a5-pulp.mps branch printingOptions all solution C:\Users\DAVIDE~1.CON\AppData\Local\Temp\f262fcfe1f714303af02b97868f3a9a5-pulp.sol (default strategy 1)
At line 2 NAME          MODEL
At line 3 ROWS
At line 35 COLUMNS
At line 156 RHS
At line 187 BOUNDS
Bad image at line 193 <  LO BND       X0000005  -inf >
Bad image at line 194 <  LO BND       X0000006  -inf >
Bad image at line 195 <  LO BND       X0000007  -inf >
Bad image at line 196 <  LO BND       X0000008  -inf >
Bad image at line 197 <  LO BND       X0000009  -inf >
At line 208 ENDATA
Problem MODEL has 30 rows,20 columns and 70 elements
Coin0008I MODEL read with 5 errors
There were 5 errors on input
** Current model not valid
Option for printingOptions changed from normal to all
** Current model not valid
No match for C:\Users\DAVIDE~1.CON\AppData\Local\Temp\f262fcfe1f714303af02b97868f3a9a5-pulp.sol - ? for list of commands
Total time (cpu seconds):       0.01   (Wallclock seconds):       0.01

Traceback (most recent call last):
  File "C:\Users\davide.tamagnini.con\AppData\Roaming\JetBrains\PyCharmCE2021.1\scratches\example.py",line 73,in <module>
    prob.solve()
  File "C:\Python\python395\lib\site-packages\pulp\pulp.py",line 1737,in solve
    status = solver.actualSolve(self,**kwargs)
  File "C:\Python\python395\lib\site-packages\pulp\apis\coin_api.py",line 101,in actualSolve
    return self.solve_CBC(lp,line 159,in solve_CBC
    raise PulpSolverError("Pulp: Error while executing "+self.path)
pulp.apis.core.PulpSolverError: Pulp: Error while executing C:\Python\python395\lib\site-packages\pulp\apis\..\solverdir\cbc\win\64\cbc.exe

Process finished with exit code 1

我们衷心接受任何建议。 谢谢

解决方法

好的。你已经为你完成了工作。 :) 无意冒犯,但这是一个完整的火车残骸。让我给你一些事情开始。我的总体建议是从很小的地方开始(没有限制),只得到一些可以构建和解决的东西,即使它是无稽之谈,然后慢慢构建,而不是尝试一次编写整个代码,这样更难排除故障。>

问题:

  1. 不要导入 numpy。保存以备后用。您的模型在这方面存在致命错误:

    Disp_1_var = LpVariable.dicts("Disp_i",temp_int,lowBound=-np.inf,cat=LpInteger)

numpy 负无穷大在纸浆中无效。它到底有什么价值?忘记这个下限

  1. 可能有效,我不确定,但没用:

    概率 += arrot*Qai_var[i] == Qi_var[i]

如果 x*5 = y,那么你只有一个变量,去掉另一个。

  1. 我不确定这是否有效:

    lpSum(Qi_var[i] for i in temp_int) >= lpSum(D_i[i] for i in temp_int)

重写它,以便您只有一个带有一点代数的 lpSum...

  1. temp_int 何时会为零?为什么要命名temp_int? :) 并且您不能分配这样的变量,您需要进行约束。

    for i in temp_int:

    if i == 0:
        Disp_1_var[i] = Disp_0 + Qi_var[i] - D_i[i]
    else:
        Disp_1_var[i] = Disp_0 + Qi_var[i] - D_i[i]  # Disp_1_var[i-1]
    

许多修复。我建议从头开始,一次引入一小块......