"For all i in I" Excel VBA 中的线性规划约束类型

"For all i in I" type of linear programming constraints in Excel VBA

我打算使用 Excel VBA 进行线性规划。在 VBA 中,我是处理此类问题的新手,因此我按照一个示例来深入了解这一点:

Dim c As Vector = Vector.Create(-1.0, -3.0, 0.0, 0.0, 0.0, 0.0)
Dim A As Matrix = Matrix.Create(4, 6, New Double() _
{ _
    1, 1, 1, 0, 0, 0, _
    1, 1, 0, -1, 0, 0, _
    1, 0, 0, 0, 1, 0, _
    0, 1, 0, 0, 0, 1 _
}, MatrixElementOrder.RowMajor)
Dim b As Vector = Vector.Create(1.5, 0.5, 1.0, 1.0)
Dim lp1 As LinearProgram = New LinearProgram(c, A, b, 4)

我偶然发现的问题是:

is there a way to construct constraints of the type: sum{i in I} x[i,j], for all j in J?

And are there other ways to construct constraints rather than "manually" creating the constraint matrix, A (in this example) for types when there are a vast amount of constraints and/or variables?

作为答案发布,因为评论太长了。

如果您打算使用 VBA 创建一个成熟的线性规划算法,opensolver 的源代码可能是一个很好的起点。 Open 求解器从电子表格中提取问题并将其发送到 LP 求解器,并使用中间 VBA 数据结构,例如您尝试创建的数据结构。因此,他们的代码可以让您了解如何创建有用的数据结构。

如果您的目的是在电子表格环境中求解线性程序,而不是从头开发 LP 求解器,那么 (i) excel 的常规求解器,(ii) 开放式求解器,甚至 ( iii) solverstudio are good tools. As for your questions, there is not much magic to do.. Looping is rather inevitable, but not necessarily too slow.