据我所知,这并不容易实现。
问题存储在底层MathOptInterface
(MOI)特定数据结构。例如,约束总是存储为MOI.AbstractFunction
- in - MOI.AbstractSet
。对于MOI.ObjectiveFunction
。 (参见 MOI 文档:https://jump.dev/MathOptInterface.jl/dev/apimanual/#Functions-1)
但是,您可以尝试以矩阵向量形式重新计算目标函数项和约束。
例如,假设您仍然拥有JuMP.Model
Mod
,您可以检查目标函数通过输入更接近:
using MathOptInterface
const MOI = MathOptInterface
# this only works if you have a linear objective function (the model has a ScalarAffineFunction as its objective)
obj = MOI.get(Mod, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}())
# take a look at the terms
obj.terms
# from this you could extract your vector c
c = zeros(4)
for term in obj.terms
c[term.variable_index.value] = term.coefficient
end
@show(c)
这确实给出了:c = [1.;1.;2.;2.]
.
您可以对底层 MOI 执行类似的操作。限制条件.
# list all the constraints present in the model
cons = MOI.get(Mod, MOI.ListOfConstraints())
@show(cons)
在这种情况下,我们只有一种类型的约束,即(MOI.ScalarAffineFunction{Float64}
in MOI.LessThan{Float64})
# get the constraint indices for this combination of F(unction) in S(et)
F = cons[1][1]
S = cons[1][2]
ci = MOI.get(Mod, MOI.ListOfConstraintIndices{F,S}())
你得到两个约束索引(存储在数组中ci
),因为这个组合 F - in - S 有两个约束。
让我们仔细研究一下其中的第一个:
ci1 = ci[1]
# to get the function and set corresponding to this constraint (index):
moi_backend = backend(Mod)
f = MOI.get(moi_backend, MOI.ConstraintFunction(), ci1)
f
又是类型MOI.ScalarAffineFunction
对应一行a1
在你的A = [a1; ...; am]
矩阵。该行由下式给出:
a1 = zeros(4)
for term in f.terms
a1[term.variable_index.value] = term.coefficient
end
@show(a1) # gives [2.0 0 3.0 0] (the first row of your A matrix)
获取对应的第一个条目b1
你的b = [b1; ...; bm]
向量,你必须查看相同约束索引的约束集ci1
:
s = MOI.get(moi_backend, MOI.ConstraintSet(), ci1)
@show(s) # MathOptInterface.LessThan{Float64}(100.0)
b1 = s.upper
我希望这能让您对数据如何存储有一些直观的了解MathOptInterface
format.
您必须对所有约束和所有约束类型执行此操作,并将它们作为约束矩阵中的行堆叠A
和矢量b
.