python - Pyomo 卡住解算器

标签 python python-3.x solver pyomo

我的代码有问题,我用较少数量的变量尝试过它并且它有效,但是我想将它扩展为具有相同结构的更大的代码,但是当我开始运行时,它什么也不做,优化器“永远”。

这是我的代码:

import pyomo.environ
from pyomo.core import *
from pyomo.opt import SolverFactory

precedence_jobs = dict({1: [], 2: [], 3: [], 4: [1, 2], 5: [3], 6: [4], 7: [4, 5], 8: [7], 9: [], 10: [9], 11: [9], 12: [10], 13: [11], 14: [12, 13], 15: [], 16: [15], 17: [16], 18: [16], 19: [17], 20: [], 21: [], 22: [20], 23: [21], 24: [22, 23], 25: [24], 26: [25], 27: [], 28: [], 29: [], 30: [27], 31: [28, 29], 32: [30, 31], 33: [32], 34: [33], 35: [33], 36: [], 37: [36], 38: [37], 39: [38], 40: [38], 41: [38], 42: [39]})
precedence_stations = dict({1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [], 9: [6, 8], 10: [], 11: [], 12: [], 13: [], 14: [], 15: [], 16: [], 17: [], 18: [], 19: [], 20: [18, 19], 21: [18, 19], 22: [], 23: [], 24: [], 25: [], 26: [], 27: [14, 26], 28: [14, 26], 29: [14, 26], 30: [], 31: [], 32: [], 33: [], 34: [], 35: [], 36: [34, 35], 37: [], 38: [], 39: [], 40: [], 41: [], 42: []})
jobs_per_station = dict({1: [1, 2, 3, 4, 5, 6, 7, 8], 2: [9, 10, 11, 12, 13, 14], 3: [15, 16, 17, 18, 19], 4: [20, 21, 22, 23, 24, 25, 26], 5: [27, 28, 29, 30, 31, 32, 33, 34, 35], 6: [36, 37, 38, 39, 40, 41, 42]})


model = ConcreteModel()

model.JOBS = RangeSet(1,42)
model.PERIODS = RangeSet(1,50)
model.MODELS = [1]
model.STATIONS = RangeSet(1,6)

model.time = Param(model.JOBS, model.MODELS, initialize={(1, 1): 2, (2, 1): 1, (3, 1): 1, (4, 1): 1, (5, 1): 2, (6, 1): 1, (7, 1): 1, (8, 1): 2, (9, 1): 1, (10, 1): 2, (11, 1): 2, (12, 1): 2, (13, 1): 1, (14, 1): 1, (15, 1): 2, (16, 1): 1, (17, 1): 1, (18, 1): 2, (19, 1): 1, (20, 1): 1, (21, 1): 1, (22, 1): 1, (23, 1): 2, (24, 1): 1, (25, 1): 2, (26, 1): 2, (27, 1): 1, (28, 1): 2, (29, 1): 1, (30, 1): 1, (31, 1): 1, (32, 1): 2, (33, 1): 1, (34, 1): 1, (35, 1): 1, (36, 1): 2, (37, 1): 2, (38, 1): 1, (39, 1): 1, (40, 1): 1, (41, 1): 2, (42, 1): 1}) 
model.hreq = Param(model.JOBS, model.MODELS, initialize={(1, 1): 2, (2, 1): 2, (3, 1):3, (4, 1): 1, (5, 1): 2, (6, 1): 4, (7, 1): 1, (8, 1): 3, (9, 1): 2, (10, 1): 1, (11, 1): 3, (12, 1): 2, (13, 1): 2, (14, 1): 2, (15, 1): 3, (16, 1): 1, (17, 1): 2, (18, 1): 1, (19, 1): 2, (20, 1): 2, (21, 1): 2, (22, 1): 2, (23, 1): 3, (24, 1): 2, (25, 1): 2, (26, 1): 1, (27, 1): 2, (28, 1): 2, (29, 1): 1, (30, 1): 4, (31, 1): 1, (32, 1): 1, (33, 1): 2, (34, 1): 2, (35, 1): 3, (36, 1): 1, (37, 1): 2, (38, 1): 3, (39, 1): 2, (40, 1): 1, (41, 1): 2, (42, 1): 1}) 

model.htotal = Param(model.STATIONS, initialize={1: 4, 2: 3, 3: 3, 4: 3, 5: 4, 6: 3}) 


model.x = Var(model.JOBS, model.PERIODS, model.MODELS, within=Boolean) 
model.lt = Var(model.MODELS, within=PositiveIntegers) 

def obj_rule(model):
    return model.lt[1]
model.obj = Objective(rule=obj_rule)

def rest1_rule(model, i, j):
    return sum(model.x[i,t,j] for t in model.PERIODS) == 1
model.rest1 = Constraint(model.JOBS, model.MODELS, rule=rest1_rule)

@model.Constraint(model.JOBS, model.PERIODS, model.STATIONS, model.MODELS)
def rest2_rule(model, i, t, k, j):
        return sum(model.hreq[i,j] * (sum(model.x[i,k,j] for k in sequence(t)) - sum(model.x[i,s,j] for s in sequence(t - model.time[i,j]))) for i in jobs_per_station[k]) <= model.htotal[k]

@model.Constraint(model.JOBS, model.JOBS, model.MODELS)
def rest3(model, i, p, j):
    if p in precedence_jobs[i]:
        return sum(t * model.x[i, t, j] for t in model.PERIODS) >= (sum(t * model.x[p, t, j] for t in model.PERIODS)+ model.time[p,j])
    else:
        return Constraint.NoConstraint

def rest4_rule(model, i, t, j):
    return model.lt[j] >= model.x[i,t,j] * (t + model.time[i,j] -1)
model.rest4 = Constraint(model.JOBS, model.PERIODS, model.MODELS, rule=rest4_rule)

@model.Constraint(model.JOBS, model.JOBS, model.MODELS)
def rest7(model, i, p, j):
    if p in precedence_stations[i]:
        return sum(t * model.x[i, t, j] for t in model.PERIODS) >= (sum(t * model.x[p, t, j] for t in model.PERIODS)+ model.time[p,j])
    else:
        return Constraint.NoConstraint



opt = SolverFactory("glpk")    

print("\nOptimal solution found\n" + '-'*80)
results = opt.solve(model)
model.solutions.load_from(results)

print("\nSummary of the solution found\n"+ '-'*80)
for j in model.MODELS:
    for t in model.PERIODS:
        for i in model.JOBS:
            if model.x[i,t,j].value == 1:
                print("The job",i,"of the model",j,"starts on period:",t)
print("\n")
for j in model.MODELOS:
    print("Lead Time of the model",j,":",model.lt[j].value)
print("\n")

可能是什么问题?太多限制而找不到解决方案? 请问,有什么想法吗?我尝试了很多方法,仍然不行。 预先非常感谢。

最佳答案

要开始进行故障排除,使用 tee=True 求解器选项将求解器输出流式传输到控制台可能会很有效。即:

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

除此之外,我们进入建模理论,我会向您推荐建模引用,例如 the HP Williams book .

关于python - Pyomo 卡住解算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51763924/

相关文章:

Python requests-html 抛出多处理错误

python - 共享库中使用的稀疏 BLAS 求解器不起作用(返回 '-1' )

python - pandas 中的多个多列选择

Python - 将 Flask 应用程序的名称更改为 app.py 时出现 ModuleNotFoundError。可能的解决方案?

python - 基于 Python 列表从 yaml 文件中检索数据

mysql - Peewee 选择十进制字段?

python - 使用 .loc 和日期检索值

Python tkinter rowspan 无法正确调整元素大小

c++ - 针对某些脚本语法中描述的问题,在 C++ 中实现求解器

python - sympy 在尝试求解一个简单的代数方程时挂起