Skip to content

Commit 0b1f98b

Browse files
committed
Simplify with MatrixOfConstraints
1 parent 166502a commit 0b1f98b

1 file changed

Lines changed: 46 additions & 49 deletions

File tree

src/matrix_input.jl

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -79,66 +79,63 @@ function MOI.get(
7979
return _dot(model.A[ci.value, :])
8080
end
8181

82-
"""
83-
LPStandardForm{T, AT<:AbstractMatrix{T}, VT <: AbstractVector{T}} <: AbstractLPForm{T}
82+
MOI.Utilities.@product_of_sets(
83+
Equalities,
84+
MOI.EqualTo{T},
85+
)
8486

85-
Represents a problem of the form:
86-
```
87-
sense ⟨c, x⟩
88-
s.t. A x == b
89-
x ≥ 0
90-
```
91-
"""
92-
struct LPStandardForm{T,AT<:AbstractMatrix{T},VT<:AbstractVector{T}} <:
93-
AbstractLPForm{T}
94-
sense::MOI.OptimizationSense
95-
c::VT
96-
A::AT
97-
b::VT
98-
end
9987

100-
function MOI.get(
101-
::LPStandardForm{T},
102-
::MOI.ListOfConstraintTypesPresent,
103-
) where {T}
104-
return [
105-
(MOI.ScalarAffineFunction{T}, MOI.EqualTo{T}),
106-
(MOI.VectorOfVariables, MOI.Nonnegatives),
107-
]
88+
# TODO specialize for SparseVector
89+
function linear_function(c::AbstractVector{T}) where {T}
90+
return MOI.ScalarAffineFunction(
91+
[MOI.ScalarAffineTerm(c[i], MOI.VariableIndex(i)) for i in eachindex(c)],
92+
zero(T),
93+
)
10894
end
10995

110-
const EQ{T} = MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}}
111-
112-
function MOI.get(
113-
model::LPStandardForm{T},
114-
::MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{T},MOI.EqualTo{T}},
115-
) where {T}
116-
# TODO return `collect` with MOI v0.9.15 (see https://github.com/jump-dev/MathOptInterface.jl/pull/1110)
117-
return collect(MOIU.LazyMap{EQ{T}}(
118-
i -> EQ{T}(i), # FIXME `LazyMap` needs a `Function` so cannot just give `EQ{T}`
119-
1:size(model.A, 1),
120-
))
96+
function linear_objective(sense::MOI.OptimizationSense, c::AbstractVector{T})
97+
model = MOI.Utilities.ObjectiveContainer{T}()
98+
MOI.set(model, MOI.ObjectiveSense(), sense)
99+
func = linear_function(c)
100+
MOI.set(model, MOI.ObjectiveFunction{typeof(func)}(), func)
101+
return model
121102
end
122103

123-
function MOI.get(model::LPStandardForm, ::MOI.ConstraintSet, ci::EQ)
124-
return MOI.EqualTo(model.b[ci.value])
104+
function nonnegative_variables(n)
105+
model = MOI.Utilities.VariablesContainer{T}()
106+
for _ in 1:n
107+
MOI.add_constrained_variable(model, MOI.GreaterThan(zero(T)))
108+
end
109+
return model
125110
end
126111

127-
const NONNEG = MOI.ConstraintIndex{MOI.VectorOfVariables,MOI.Nonnegatives}
128-
129-
function MOI.get(
130-
::LPStandardForm,
131-
::MOI.ListOfConstraintIndices{MOI.VectorOfVariables,MOI.Nonnegatives},
132-
)
133-
return [NONNEG(1)]
112+
function equality_constraints(A::AbstractMatrix{T}, b::AbstractVector{T}) where {T}
113+
return MOI.Utilities.MatrixOfConstraint{T}(
114+
A,
115+
b,
116+
Equalities{T}(), # TODO
117+
)
134118
end
135119

136-
function MOI.get(model::LPStandardForm, ::MOI.ConstraintFunction, ci::NONNEG)
137-
return MOI.VectorOfVariables(MOI.get(model, MOI.ListOfVariableIndices()))
138-
end
120+
"""
121+
lp_standard_form(sense::MOI.OptimizationSense, c::AbtractVector, A::AbstractMatrix, b::AbstractVector)
139122
140-
function MOI.get(model::LPStandardForm, ::MOI.ConstraintSet, ci::NONNEG)
141-
return MOI.Nonnegatives(MOI.get(model, MOI.NumberOfVariables()))
123+
Represents a problem of the form:
124+
```
125+
sense ⟨c, x⟩
126+
s.t. A x == b
127+
x ≥ 0
128+
```
129+
"""
130+
function lp_standard_form(sense::MOI.OptimizationSense, c::AbtractVector{T}, A::AbstractMatrix{T}, b::AbstractVector{T}) where {T}
131+
m, n = size(A)
132+
@assert length(c) == n
133+
@assert length(b) == m
134+
return MOI.Utilities.GenericModel{T}(
135+
linear_objective(sense, c),
136+
nonnegative_variables(n),
137+
equality_constraints(A, b),
138+
)
142139
end
143140

144141
"""

0 commit comments

Comments
 (0)