Skip to content

Commit f33c37a

Browse files
committed
Fix format
1 parent 0dfda7f commit f33c37a

2 files changed

Lines changed: 99 additions & 74 deletions

File tree

src/matrix_input.jl

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ function MOI.get(
8080
end
8181

8282
# We cannot use MOI.Utilities.Hyperrectangle in case the vector type is not `Vector`
83-
struct Hyperrectangle{T,LT<:AbstractVector{T},UT<:AbstractVector{T}} <: MOI.Utilities.AbstractVectorBounds
83+
struct Hyperrectangle{T,LT<:AbstractVector{T},UT<:AbstractVector{T}} <:
84+
MOI.Utilities.AbstractVectorBounds
8485
lower::LT
8586
upper::UT
8687
end
@@ -90,12 +91,18 @@ MOI.Utilities.function_constants(::Hyperrectangle{T}, row) where {T} = zero(T)
9091
# TODO specialize for SparseVector
9192
function linear_function(c::AbstractVector{T}) where {T}
9293
return MOI.ScalarAffineFunction(
93-
[MOI.ScalarAffineTerm(c[i], MOI.VariableIndex(i)) for i in eachindex(c)],
94+
[
95+
MOI.ScalarAffineTerm(c[i], MOI.VariableIndex(i)) for
96+
i in eachindex(c)
97+
],
9498
zero(T),
9599
)
96100
end
97101

98-
function linear_objective(sense::MOI.OptimizationSense, c::AbstractVector{T}) where {T}
102+
function linear_objective(
103+
sense::MOI.OptimizationSense,
104+
c::AbstractVector{T},
105+
) where {T}
99106
model = MOI.Utilities.ObjectiveContainer{T}()
100107
MOI.set(model, MOI.ObjectiveSense(), sense)
101108
func = linear_function(c)
@@ -113,21 +120,14 @@ function MOI.Utilities.extract_function(
113120
for col in axes(A, 2)
114121
val = A[row, col]
115122
if !iszero(val)
116-
push!(
117-
func.terms,
118-
MOI.ScalarAffineTerm(val, MOI.VariableIndex(col)),
119-
)
123+
push!(func.terms, MOI.ScalarAffineTerm(val, MOI.VariableIndex(col)))
120124
end
121125
end
122126
return func
123127
end
124128

125129
# Copy-paste of MOI.Utilities.MutableSparseMatrixCSC
126-
function _first_in_column(
127-
A::SparseMatrixCSC,
128-
row::Integer,
129-
col::Integer,
130-
)
130+
function _first_in_column(A::SparseMatrixCSC, row::Integer, col::Integer)
131131
range = SparseArrays.nzrange(A, col)
132132
idx = searchsortedfirst(view(A.rowval, range), row)
133133
return get(range, idx, last(range) + 1)
@@ -173,7 +173,10 @@ function nonnegative_variables(n, ::Type{T}) where {T}
173173
return model
174174
end
175175

176-
function interval_variables(lower::AbstractVector{T}, upper::AbstractVector{T}) where {T}
176+
function interval_variables(
177+
lower::AbstractVector{T},
178+
upper::AbstractVector{T},
179+
) where {T}
177180
@assert eachindex(lower) == eachindex(upper)
178181
model = MOI.Utilities.VariablesContainer{T}()
179182
for i in eachindex(lower)
@@ -184,15 +187,18 @@ function interval_variables(lower::AbstractVector{T}, upper::AbstractVector{T})
184187
return model
185188
end
186189

187-
MOI.Utilities.@product_of_sets(
188-
EqualTos,
189-
MOI.EqualTo{T},
190-
)
190+
MOI.Utilities.@product_of_sets(EqualTos, MOI.EqualTo{T},)
191191

192-
function equality_constraints(A::AbstractMatrix{T}, b::AbstractVector{T}) where {T}
192+
function equality_constraints(
193+
A::AbstractMatrix{T},
194+
b::AbstractVector{T},
195+
) where {T}
193196
sets = EqualTos{T}()
194197
for _ in eachindex(b)
195-
MOI.Utilities.add_set(sets, MOI.Utilities.set_index(sets, MOI.EqualTo{T}))
198+
MOI.Utilities.add_set(
199+
sets,
200+
MOI.Utilities.set_index(sets, MOI.EqualTo{T}),
201+
)
196202
end
197203
MOI.Utilities.final_touch(sets)
198204
constants = Hyperrectangle(b, b)
@@ -201,15 +207,18 @@ function equality_constraints(A::AbstractMatrix{T}, b::AbstractVector{T}) where
201207
return model
202208
end
203209

204-
MOI.Utilities.@product_of_sets(
205-
LessThans,
206-
MOI.LessThan{T},
207-
)
210+
MOI.Utilities.@product_of_sets(LessThans, MOI.LessThan{T},)
208211

209-
function lessthan_constraints(A::AbstractMatrix{T}, b::AbstractVector{T}) where {T}
212+
function lessthan_constraints(
213+
A::AbstractMatrix{T},
214+
b::AbstractVector{T},
215+
) where {T}
210216
sets = LessThans{T}()
211217
for _ in eachindex(b)
212-
MOI.Utilities.add_set(sets, MOI.Utilities.set_index(sets, MOI.LessThan{T}))
218+
MOI.Utilities.add_set(
219+
sets,
220+
MOI.Utilities.set_index(sets, MOI.LessThan{T}),
221+
)
213222
end
214223
MOI.Utilities.final_touch(sets)
215224
constants = Hyperrectangle(FillArrays.Zeros{T}(length(b)), b)
@@ -218,15 +227,19 @@ function lessthan_constraints(A::AbstractMatrix{T}, b::AbstractVector{T}) where
218227
return model
219228
end
220229

221-
MOI.Utilities.@product_of_sets(
222-
Intervals,
223-
MOI.Interval{T},
224-
)
230+
MOI.Utilities.@product_of_sets(Intervals, MOI.Interval{T},)
225231

226-
function interval_constraints(A::AbstractMatrix{T}, lower::AbstractVector{T}, upper::AbstractVector{T}) where {T}
232+
function interval_constraints(
233+
A::AbstractMatrix{T},
234+
lower::AbstractVector{T},
235+
upper::AbstractVector{T},
236+
) where {T}
227237
sets = Intervals{T}()
228238
for _ in eachindex(lower)
229-
MOI.Utilities.add_set(sets, MOI.Utilities.set_index(sets, MOI.Interval{T}))
239+
MOI.Utilities.add_set(
240+
sets,
241+
MOI.Utilities.set_index(sets, MOI.Interval{T}),
242+
)
230243
end
231244
MOI.Utilities.final_touch(sets)
232245
constants = Hyperrectangle(lower, upper)
@@ -242,7 +255,11 @@ MOI.Utilities.@mix_of_scalar_sets(
242255
MOI.LessThan{T},
243256
)
244257

245-
function mix_of_constraints(A::AbstractMatrix{T}, b::AbstractVector{T}, senses::Vector{ConstraintSense}) where {T}
258+
function mix_of_constraints(
259+
A::AbstractMatrix{T},
260+
b::AbstractVector{T},
261+
senses::Vector{ConstraintSense},
262+
) where {T}
246263
@assert eachindex(b) == eachindex(senses)
247264
sets = MixedLinearSets{T}()
248265
for sense in senses
@@ -266,7 +283,12 @@ s.t. A x == b
266283
x ≥ 0
267284
```
268285
"""
269-
function lp_standard_form(sense::MOI.OptimizationSense, c::AbstractVector{T}, A::AbstractMatrix{T}, b::AbstractVector{T}) where {T}
286+
function lp_standard_form(
287+
sense::MOI.OptimizationSense,
288+
c::AbstractVector{T},
289+
A::AbstractMatrix{T},
290+
b::AbstractVector{T},
291+
) where {T}
270292
m, n = size(A)
271293
@assert length(c) == n
272294
@assert length(b) == m
@@ -286,7 +308,12 @@ sense ⟨c, x⟩
286308
s.t. Ax <= b
287309
```
288310
"""
289-
function lp_geometric_form(sense::MOI.OptimizationSense, c::AbstractVector{T}, A::AbstractMatrix{T}, b::AbstractVector{T}) where {T}
311+
function lp_geometric_form(
312+
sense::MOI.OptimizationSense,
313+
c::AbstractVector{T},
314+
A::AbstractMatrix{T},
315+
b::AbstractVector{T},
316+
) where {T}
290317
m, n = size(A)
291318
@assert length(c) == n
292319
@assert length(b) == m

test/linear.jl

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ end
1717

1818
function _test_expected(form, expected, var_names, con_names, S)
1919
model = MOI.Utilities.Model{Float64}()
20-
MOI.copy_to(
21-
MOI.Bridges.Constraint.Scalarize{Float64}(model),
22-
form,
23-
)
20+
MOI.copy_to(MOI.Bridges.Constraint.Scalarize{Float64}(model), form)
2421
MOI.set(
2522
model,
2623
MOI.VariableName(),
@@ -30,10 +27,9 @@ function _test_expected(form, expected, var_names, con_names, S)
3027
MOI.set(
3128
model,
3229
MOI.ConstraintName(),
33-
MOI.ConstraintIndex{
34-
MOI.ScalarAffineFunction{Float64},
35-
S,
36-
}.(eachindex(con_names)),
30+
MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},S}.(
31+
eachindex(con_names),
32+
),
3733
con_names,
3834
)
3935
return MOI.Test.util_test_models_equal(
@@ -44,7 +40,6 @@ function _test_expected(form, expected, var_names, con_names, S)
4440
)
4541
end
4642

47-
4843
function test_standard_form()
4944
dense_A = [
5045
1.0 2.0
@@ -76,24 +71,28 @@ function test_standard_form()
7671
[dense_c, sparsevec(dense_c)],
7772
)
7873
@testset "change $func" for (func, args) in [
79-
(MatOI.lp_standard_form, (
80-
sense,
81-
c,
82-
A,
83-
b,
84-
)),
85-
(MatOI.lp_solver_form, (
86-
sense,
87-
c,
88-
A,
89-
b,
90-
[MatOI.EQUAL_TO, MatOI.EQUAL_TO],
91-
v_lb,
92-
v_ub,
93-
)),
74+
(MatOI.lp_standard_form, (sense, c, A, b)),
75+
(
76+
MatOI.lp_solver_form,
77+
(
78+
sense,
79+
c,
80+
A,
81+
b,
82+
[MatOI.EQUAL_TO, MatOI.EQUAL_TO],
83+
v_lb,
84+
v_ub,
85+
),
86+
),
9487
]
9588
lp = func(args...)
96-
_test_expected(lp, expected, var_names, con_names, MOI.EqualTo{Float64})
89+
_test_expected(
90+
lp,
91+
expected,
92+
var_names,
93+
con_names,
94+
MOI.EqualTo{Float64},
95+
)
9796
end
9897
end
9998
end
@@ -127,13 +126,14 @@ function test_geometric_form()
127126
[dense_b, sparsevec(dense_b)],
128127
[dense_c, sparsevec(dense_c)],
129128
)
130-
lp = MatOI.lp_geometric_form(
131-
sense,
132-
c,
133-
A,
134-
b,
129+
lp = MatOI.lp_geometric_form(sense, c, A, b)
130+
_test_expected(
131+
lp,
132+
expected,
133+
var_names,
134+
con_names,
135+
MOI.LessThan{Float64},
135136
)
136-
_test_expected(lp, expected, var_names, con_names, MOI.LessThan{Float64})
137137
end
138138
end
139139
end
@@ -170,16 +170,14 @@ function test_form()
170170
[dense_ub, sparsevec(dense_ub)],
171171
[dense_c, sparsevec(dense_c)],
172172
)
173-
lp = MatOI.lp_form(
174-
sense,
175-
c,
176-
A,
177-
c_lb,
178-
c_ub,
179-
v_lb,
180-
v_ub,
173+
lp = MatOI.lp_form(sense, c, A, c_lb, c_ub, v_lb, v_ub)
174+
_test_expected(
175+
lp,
176+
expected,
177+
var_names,
178+
con_names,
179+
MOI.Interval{Float64},
181180
)
182-
_test_expected(lp, expected, var_names, con_names, MOI.Interval{Float64})
183181
end
184182
end
185183
end

0 commit comments

Comments
 (0)