CPLEX 中的动态工作器约束 python
Dynamic worker constraint in CPLEX python
我一直在 python 中使用 docplex 解决 rcpsp 问题。
我考虑 10 个具有指示性成本的任务和一个必须在 10 个时间范围内(可以是几周、几天等)完成这些任务的工人。
我的一个限制是工作人员可以在每个时间范围内执行一组特定的任务(worker_availability 列表)。如果我考虑下面 link 的示例,可以限制工人的可用性不超过特定点,即 mdl.sum(resources) <= capacity,其中 capacity 是一个固定数字。
我想使用遵守 worker_availability 的动态约束,即在点 0 我的工作人员可以处理 2 个任务,在 1 0 任务等
有人知道如何在 python 中使用 docplex 吗?
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)
你可以依靠 always_in
让我举个例子OPL。
using CP;
range r=1..2;
dvar interval itvs[r] size 3;
cumulFunction simultaneousJobs=sum(i in r) pulse(itvs[i],1);
minimize max(i in r) endOf(itvs[i]);
subject to
{
simultaneousJobs<=2;
//alwaysIn(simultaneousJobs,0,1,0,1);
}
最好给出 3 objective,对于 simultaneousJob,我们看到
如果我们取消对 alwaysIn 的注释,我们会得到 4 作为 objective,对于累积,我们会看到
我一直在 python 中使用 docplex 解决 rcpsp 问题。 我考虑 10 个具有指示性成本的任务和一个必须在 10 个时间范围内(可以是几周、几天等)完成这些任务的工人。
我的一个限制是工作人员可以在每个时间范围内执行一组特定的任务(worker_availability 列表)。如果我考虑下面 link 的示例,可以限制工人的可用性不超过特定点,即 mdl.sum(resources) <= capacity,其中 capacity 是一个固定数字。
我想使用遵守 worker_availability 的动态约束,即在点 0 我的工作人员可以处理 2 个任务,在 1 0 任务等
有人知道如何在 python 中使用 docplex 吗?
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)
你可以依靠 always_in
让我举个例子OPL。
using CP;
range r=1..2;
dvar interval itvs[r] size 3;
cumulFunction simultaneousJobs=sum(i in r) pulse(itvs[i],1);
minimize max(i in r) endOf(itvs[i]);
subject to
{
simultaneousJobs<=2;
//alwaysIn(simultaneousJobs,0,1,0,1);
}
最好给出 3 objective,对于 simultaneousJob,我们看到
如果我们取消对 alwaysIn 的注释,我们会得到 4 作为 objective,对于累积,我们会看到