I've been working at a rcpsp problem using docplex in python. I consider 10 tasks with indicative costs and a worker that has to complete these tasks in 10 timeframes (can be weeks, days etc.).
One of my constraints is that the worker can perform a specific set of tasks per timeframe (worker_availability list). If I consider the example on the link below, one can restrict a worker's availability to not exceed a specific point i.e. mdl.sum(resources) <= capacity, where capacity is a fixed number.
I want to use a dynamic constraint that obeys worker_availability, i.e. at point 0 my worker can process 2 tasks, at 1 0 tasks etc.
Does anyone know how to that with docplex in python?
link: http://ibmdecisionoptimization.github.io/docplex-doc/cp/visu.rcpsp.py.html?highlight=rcpsp
import numpy as np
import pandas as pd
import sys
import time
from docplex.cp.model import *
# Tasks to be planned
Tasks= ["task1",
"task2",
"task3",
"task4",
"task5",
"task6",
"task7",
"task8",
"task9",
"task10"]
# Duration of Tasks
Task_Duration = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
# Cost of tasks
Cost= {"task1": 700,
"task2": 1200,
"task3": 189,
"task4": 296,
"task5": 562,
"task6": 584,
"task7": -100,
"task8": -200,
"task9": - 189,
"task10": -296
}
# Define time interval, worker availability
time_interval = number_of_week - ReleaseDate
worker_availability = [2,0,2,0,2,0,2,1,1,1]
timespan = 10
# Create model
model = CpoModel()
# Create interval variables
itvs = {}
for i,t in enumerate(Tasks):
_name = '_'+str(t)
itvs[t] = model.interval_var(start=(0, INTERVAL_MAX), end = (INTERVAL_MIN,10), size = Duration[i], name = _name)
# case of static constraints - this is where we need to get it to dynamic
tasks = [model.pulse(itvs[t], 1) for t in Tasks]
model.add(model.sum(tasks) <= 2)
# Solve the model
tmp = 0
time_interval = number_of_week - ReleaseDate
for task in Tasks:
tmp += Cost[task] + Cost[task] * model.max([time_interval - model.start_of(itvs[task]),0])
model.add(model.minimize(tmp))
msol = model.solve(FailLimit=300000)
# Print output
for t in Tasks:
wt = msol.get_var_solution(itvs[t])
print(t,'start at:',wt.get_start())
print('Cost: ', msol.get_objective_values()[0])
print('worker availability: ',worker_availability)