获取使用资源的价值

Get the value of resource used

给定以下代码(示例来自:https://www.ibm.com/docs/en/icos/20.1.0?topic=f-pulse

from docplex.cp.model import CpoModel, INTERVAL_MAX
import docplex.cp.utils_visu as visu

mdl = CpoModel()

a1=mdl.integer_var(name="a1")
a=mdl.interval_var(size=(1,10),end=(0,14),name="a")
b=mdl.interval_var(size=(1,10),end=(0,14),name="b")
c=mdl.interval_var(size=(1,10),end=(0,14),name="c")
d=mdl.interval_var(size=(1,10),end=(0,14),name="d")
// Horizon of the schedule is 14
// Cumul function resourceUse represents the level of a discrete resource.
// Activities a1,a2,a3,a4 require between 1 and 10 units of resource
resourceUse = mdl.pulse(a, 1,10) + mdl.pulse(b,1,10) + mdl.pulse(c, 1,10) + mdl.pulse(d, 1,10)

mdl.add(mdl.size_of(a)*mdl.height_at_start(a,resourceUse) >= 22)
mdl.add(mdl.size_of(b)*mdl.height_at_start(b,resourceUse) >= 22)
mdl.add(mdl.size_of(c)*mdl.height_at_start(c,resourceUse) >= 22)
mdl.add(mdl.size_of(d)*mdl.height_at_start(d,resourceUse) >= 22)
mdl.add(resourceUse<=7)


# Solve model

print("Solving model....")
msol = mdl.solve(FailLimit=100000, TimeLimit=10,execfile='/opt/ibm/ILOG/CPLEX_Studio201/cpoptimizer/bin/x86-64_linux/cpoptimizer')
print("Solution: ")
msol.print_solution()

输出

a: (start=6, end=14, size=8, length=8)
b: (start=0, end=8, size=8, length=8)
c: (start=0, end=6, size=6, length=6)
d: (start=8, end=14, size=6, length=6)

由于使用的资源在 1 到 10 之间,有没有办法知道给定解决方案所使用的资源的确切数量?

您可以使用一些间接决策变量:

from docplex.cp.model import CpoModel, INTERVAL_MAX
import docplex.cp.utils_visu as visu

mdl = CpoModel()

a1=mdl.integer_var(name="a1")
a=mdl.interval_var(size=(1,10),end=(0,14),name="a")
b=mdl.interval_var(size=(1,10),end=(0,14),name="b")
c=mdl.interval_var(size=(1,10),end=(0,14),name="c")
d=mdl.interval_var(size=(1,10),end=(0,14),name="d")
#Horizon of the schedule is 14
# Cumul function resourceUse represents the level of a discrete resource.
# Activities a1,a2,a3,a4 require between 1 and 10 units of resource
resourceUse = mdl.pulse(a, 1,10) + mdl.pulse(b,1,10) + mdl.pulse(c, 1,10) + mdl.pulse(d, 1,10)

mdl.add(mdl.size_of(a)*mdl.height_at_start(a,resourceUse) >= 22)
mdl.add(mdl.size_of(b)*mdl.height_at_start(b,resourceUse) >= 22)
mdl.add(mdl.size_of(c)*mdl.height_at_start(c,resourceUse) >= 22)
mdl.add(mdl.size_of(d)*mdl.height_at_start(d,resourceUse) >= 22)
mdl.add(resourceUse<=7)

height_a = mdl.integer_var(0,10)
mdl.add(height_a == mdl.height_at_start(a,resourceUse))

height_b = mdl.integer_var(0,10)
mdl.add(height_b == mdl.height_at_start(b,resourceUse))

height_c = mdl.integer_var(0,10)
mdl.add(height_c == mdl.height_at_start(c,resourceUse))

height_d = mdl.integer_var(0,10)
mdl.add(height_d == mdl.height_at_start(d,resourceUse))


# Solve model

print("Solving model....")
msol = mdl.solve(FailLimit=100000, TimeLimit=10)
print("Solution: ")
msol.print_solution()



ha=msol[height_a]
hb=msol[height_b]
hc=msol[height_c]
hd=msol[height_d]

print(ha," ",hb," ",hc," ",hd)

给予

a: (start=0, end=6, size=6, length=6)
b: (start=8, end=14, size=6, length=6)
c: (start=0, end=8, size=8, length=8)
d: (start=6, end=14, size=8, length=8)
4   4   3   3