优先约束Pyom

2024-03-29 06:49:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我的进程约束代码有一些问题。举个例子:

Picture which shows the four tasks with the precedence

我想实现以下前置约束:

Here is the mathematical algorithm

其中:

i = tasks;
t = period;
j = model of product

x = binary variable which returns 1
    if task i is done in period t for model j and 0 otherwise.

为了满足约束,p\u i表示一个集合,其中de precedure tasks为i。

为了使代码标准化,我使用前置矩阵根据保存在字典中的任务创建集合。这是我的密码:

import pyomo.environ
from pyomo.core import *
from pyomo.opt import SolverFactory
M_predecessor = [[0,0,0,0,],[0,0,0,0],[1,1,0,0,],[0,0,1,0,]]

predecessor = dict()
for i in range(4):
    b = i+1    
    predecessor[b] = []
    for j in range(4):
        if M_predecessor[i][j] == 1:
            predecessor[b].append(j+1)

model = ConcreteModel()

model.TASKS = RangeSet(1,len(M_predecessor))
model.PERIODS = RangeSet(1,10)
model.MODELS = [1]

这里是约束条件:

def rest1_rule(model, i, j):
   return sum(t * model.x[i,t,j] for t in model.PERIODS) >= (
       sum(t * model.x[p for p in predecessor[i],t,j] for t in model.PERIODS)) + model.tiempo[p for p in predecessor[i],j] 
model.rest1 = Constraint(model.TASKS, model.MODELS, rule=rest1_rule)

我不知道如何实现它在我的约束,请有什么想法?有没有别的办法? 提前谢谢


Tags: 代码infromimportformodelifrule
1条回答
网友
1楼 · 发布于 2024-03-29 06:49:58
@model.Constraint(model.TASKS, model.TASKS, model.MODELS)
def rest1(m, i, p, j):
    if p in predecessor[i]:
        return sum(t * m.x[i, t, j] for t in m.PERIODS) >= (
            sum(t * m.x[p, t, j] for t in m.PERIODS)
            + model.timepo[p])
    else:
        return Constraint.NoConstraint

相关问题 更多 >