优先约束pyomo

时间:2018-08-03 15:23:35

标签: python python-3.x pyomo

我的优先约束代码有一些问题。这里是一个例子:

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_i代表一个具有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)

我不确定如何在约束条件下实施它,请问有什么想法?还有另一种形式吗? 预先感谢

1 个答案:

答案 0 :(得分:0)

@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