纸浆自动加载约束

2024-10-01 15:41:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试纸浆优化库,以解决一个简单的问题。在

我有一个矩阵来定义问题的约束条件。 一旦我有了矩阵,我想自动建立约束函数。以上是代码示例:

from pulp import LpProblem, LpMinimize, LpVariable, LpStatus, value, LpInteger
import numpy as np

# Not important. It only generates the matrix A
def schedule_gen_special(N, Na):
    matrix = np.zeros((N,N))
    for i in range(Na):
        for j in range(N):
            if(i < N):
                matrix[i][j] = 1
                i = i + 1
    matrix = matrix[:, :N-Na+2]
    return matrix

N = 6
Na = 4
A = schedule_gen_special(N, Na)

# Create the 'prob' variable to contain the problem data
prob = LpProblem("Distribution of shifts", LpMinimize)

# Defines the variables under optimization
x = []
x = [LpVariable("turno"+str(i), 0, None, LpInteger) for i in range(1,5)]

# Defines the objective function
prob2 += sum(x),'number of workers'

在这里之前,一切都很好。在这一点上,我必须定义约束,标准的方法是:

^{pr2}$

然而,矩阵A具有约束信息:

array([[ 1.,  0.,  0.,  0.],
       [ 1.,  1.,  0.,  0.],
       [ 1.,  1.,  1.,  0.],
       [ 1.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.],
       [ 0.,  0.,  1.,  1.]]),

其中第一行对应于第一个约束。。。等等。在

是否可以只考虑矩阵A来自动定义约束?在


Tags: theinimportfor定义nprange矩阵

热门问题