在 Pulp 中制定方程 Python

Formulating equations in Pulp Python

我正在尝试制定以下方程式:25、28、29、30 和 31,Pulp 在 Python 中使用包含 LpVariable 的字典,objective 最小化结尾结束从一个网络节点到节点的传输延迟。

方程式:

目前我正在弄清楚如何设置第 29 个约束,所以在我看来它应该是这样的:

model = LpProblem(name="ilp", sense=LpMinimize)
I = range(2)
K = range(4)

offsets = {i: LpVariable(name=f"offset_{i}", lowBound=lowBounds[i]) for i in I}

e2e_lat = {}
e2e_lat_lowBound = {}
for i in I:
    for k in K:
        e2e_lat[i] = offsets[i, k] + t_tx[i, k] - offsets[i, 1]
        e2e_lat_lowBound[i] = offsets[i, k].lowBound + t_tx[i, k]

for i in I:
    model += e2e_lat[i] == deadline[i]

#Objective
model += c2 * lpSum([e2e_lat[i] - e2e_lat_lowBound[i] for i in I])

由于我是线性规划的新手,所以我不知道应该如何编码。

如果有任何帮助,我将不胜感激。

考虑到之前发布的方程式,这是一个可能的解决方案:

from pulp import LpMinimize, LpProblem, LpStatus, lpSum, LpVariable, PULP_CBC_CMD

# Variables (ms)
# I = num of flows, K = num of frames
I = range(3)
K = range(4)
periods = [1.1, 1.1, 5]
deadline = [10, 30, 80]
t_tx = [0.012, 0.008, 0.0024]
e2e_lat = {}
e2e_lat_lowBound = {}

model = LpProblem(name="ilp", sense=LpMinimize)
offsets = {i: {k: LpVariable(name=f"f_{i}{k}", lowBound=k*periods[i]) for k in K} for i in I}

# Equation 25 & 28(e2e & e2e_lowBound definitions):
for i in range(len(offsets)):
    for k in range(len(offsets[i])):
        e2e_lat[i] = offsets[i][k] - t_tx[i] - offsets[i][0]
        e2e_lat_lowBound[i] = offsets[i][k].lowBound + t_tx[i]
        
        print("e2e: {} = {} - {} - {}".format(e2e_lat[i], offsets[i][k], t_tx[i], offsets[i][0]))
        print("e2e_lb: {} = {} + {}".format(e2e_lat_lowBound[i], offsets[i][k].lowBound, t_tx[i]))

# Equation 29 (Deadline):
for i in I:
    model += e2e_lat[i] <= deadline[i]
    
    print("{} <= {}".format(e2e_lat[i], deadline[i]))

# Equation 30 (offset upperBound): 
for i in range(len(offsets)):
    for k in range(len(offsets[i])):
        model += offsets[i][k] <= ((k+1)*periods[i] - t_tx[i])
        
        print("{} <= {}*{} - {}".format(offsets[i][k], k+1,periods[i], t_tx[i]))

# Equation 26 (Objective Function): 
model += lpSum([e2e_lat[i] - e2e_lat_lowBound[i] for i in range(len(e2e_lat))])

由于此等式涉及 TSN 调度问题的时间资源分配,我还需要添加以下等式以防止 2 个流重叠。

for i in range(len(offsets)):
    for k in range(len(offsets[i])):
        if i+1 in I: 
            model += (k+1)*periods[i] + offsets[i][k] + t_tx[i] <= (k+1)*periods[i+1] + offsets[i+1][k]