Search code examples
pythonpython-3.xpyomo

Precedence Constraint Pyomo


I have some problems with my prcedence constraint code. Here an example:

Picture which shows the four tasks with the precedence

I would like to implement the following predecessor constraint:

Here is the mathematical algorithm

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.

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


Solution

  • @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