[英]Precedence Constraint Pyomo
I have some problems with my prcedence constraint code. 我的优先约束代码有一些问题。 Here an example:
这里是一个例子:
I would like to implement the following predecessor constraint: 我想实现以下先前约束:
where: 哪里:
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.
In order to satisfy the constraint, P_i represent a set with de predecessor tasks of i. 为了满足该约束,P_i代表一个具有i的前置任务的集合。
In order to standardize the code, I use a predecessor matrix to create sets depending on the task, saved in a dictionary. 为了使代码标准化,我使用前身矩阵根据任务创建了集合,并将其保存在字典中。 Here it is my code:
这是我的代码:
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]
Here it is the constraint: 这是约束:
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)
I am not sure how to implement it in my constraint, please any idea? 我不确定如何在自己的约束下实施它,请问有什么想法? Is there another form to do it?
还有另一种形式吗? Thanks in advance
提前致谢
@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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.