Skip to content

Commit 485c89e

Browse files
authored
Improve performance of MOI.copy_to (#203)
1 parent aaca5ba commit 485c89e

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

src/MOI_wrapper/MOI_copy.jl

+20-25
Original file line numberDiff line numberDiff line change
@@ -92,34 +92,28 @@ function _add_set_data(cache, i, s::MOI.Interval{Float64})
9292
return
9393
end
9494

95-
function _extract_bound_data(src, map, cache, s::Type{S}) where {S}
95+
_add_set_data(cache, i, ::MOI.Integer) = (cache.types[i] = _INTEGER)
96+
97+
_add_set_data(cache, i, ::MOI.ZeroOne) = (cache.types[i] = _BINARY)
98+
99+
function _extract_variable_data(src, map, cache, ::Type{S}) where {S}
100+
ci_map = map.con_map[MOI.VariableIndex, S]
96101
for ci in MOI.get(src, MOI.ListOfConstraintIndices{MOI.VariableIndex,S}())
97102
f = MOI.get(src, MOI.ConstraintFunction(), ci)
98103
s = MOI.get(src, MOI.ConstraintSet(), ci)
99104
column = map[f].value
100105
_add_set_data(cache, column, s)
101-
map[ci] = MOI.ConstraintIndex{MOI.VariableIndex,S}(column)
102-
end
103-
return
104-
end
105-
106-
function _extract_type_data(src, map, cache, ::Type{S}) where {S}
107-
for ci in MOI.get(src, MOI.ListOfConstraintIndices{MOI.VariableIndex,S}())
108-
f = MOI.get(src, MOI.ConstraintFunction(), ci)
109-
column = map[f].value
110-
cache.types[column] = S == MOI.Integer ? _INTEGER : _BINARY
111-
map[ci] = MOI.ConstraintIndex{MOI.VariableIndex,S}(column)
106+
ci_map[ci] = MOI.ConstraintIndex{MOI.VariableIndex,S}(column)
112107
end
113108
return
114109
end
115110

116111
function _extract_row_data(src, map, cache, ::Type{S}) where {S}
112+
F = MOI.ScalarAffineFunction{Float64}
113+
ci_map = map.con_map[F, S]
117114
nnz = length(cache.I)
118115
row = nnz == 0 ? 1 : cache.I[end] + 1
119-
for ci in MOI.get(
120-
src,
121-
MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{Float64},S}(),
122-
)::Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},S}}
116+
for ci in MOI.get(src, MOI.ListOfConstraintIndices{F,S}())
123117
f = MOI.get(src, MOI.ConstraintFunction(), ci)
124118
if !MOI.Utilities.is_canonical(f)
125119
f = MOI.Utilities.canonical(f)
@@ -136,7 +130,7 @@ function _extract_row_data(src, map, cache, ::Type{S}) where {S}
136130
cache.J[nnz] = Cint(map[term.variable].value::Int64)
137131
cache.V[nnz] = term.coefficient
138132
end
139-
map[ci] = MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},S}(row)
133+
ci_map[ci] = MOI.ConstraintIndex{F,S}(row)
140134
row += 1
141135
end
142136
return
@@ -160,7 +154,7 @@ function _add_all_variables(model::Optimizer, cache::_OptimizerCache)
160154
N = length(cache.cl)
161155
glp_add_cols(model, N)
162156
sizehint!(model.variable_info, N)
163-
for i in 1:N
157+
@inbounds for i in 1:N
164158
bound = _get_moi_bound_type(cache.cl[i], cache.cu[i], cache.bounds[i])
165159
CleverDicts.add_item(
166160
model.variable_info,
@@ -193,7 +187,8 @@ function _add_all_constraints(dest::Optimizer, cache::_OptimizerCache)
193187
offset(cache.V),
194188
)
195189
sizehint!(dest.affine_constraint_info, N)
196-
for (i, l, u) in zip(1:N, cache.rl, cache.ru)
190+
@inbounds for i in 1:N
191+
l, u = cache.rl[i], cache.ru[i]
197192
if l == -Inf
198193
glp_set_row_bnds(dest, i, GLP_UP, -GLP_DBL_MAX, u)
199194
CleverDicts.add_item(
@@ -226,13 +221,13 @@ function MOI.copy_to(dest::Optimizer, src::MOI.ModelLike)
226221
cache = _OptimizerCache(length(variables))
227222
# Extract the problem data
228223
# Variable bounds:
229-
_extract_bound_data(src, map, cache, MOI.GreaterThan{Float64})
230-
_extract_bound_data(src, map, cache, MOI.LessThan{Float64})
231-
_extract_bound_data(src, map, cache, MOI.EqualTo{Float64})
232-
_extract_bound_data(src, map, cache, MOI.Interval{Float64})
224+
_extract_variable_data(src, map, cache, MOI.GreaterThan{Float64})
225+
_extract_variable_data(src, map, cache, MOI.LessThan{Float64})
226+
_extract_variable_data(src, map, cache, MOI.EqualTo{Float64})
227+
_extract_variable_data(src, map, cache, MOI.Interval{Float64})
233228
# Variable types:
234-
_extract_type_data(src, map, cache, MOI.Integer)
235-
_extract_type_data(src, map, cache, MOI.ZeroOne)
229+
_extract_variable_data(src, map, cache, MOI.Integer)
230+
_extract_variable_data(src, map, cache, MOI.ZeroOne)
236231
# Affine constraints:
237232
_extract_row_data(src, map, cache, MOI.GreaterThan{Float64})
238233
_extract_row_data(src, map, cache, MOI.LessThan{Float64})

0 commit comments

Comments
 (0)