Skip to content

Commit adf310e

Browse files
authored
Fix adding variable and constraint names in copy_to (#134)
1 parent a789aad commit adf310e

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/MOI_wrapper.jl

+11-6
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,9 @@ function _copy_to_columns(dest::Optimizer, src::MOI.ModelLike, mapping)
21862186
_VariableInfo(MOI.VariableIndex(0), HighsInt(0)),
21872187
)
21882188
info = _info(dest, index)
2189-
info.name = MOI.get(dest, MOI.VariableName(), x)
2189+
if MOI.supports(src, MOI.VariableName(), MOI.VariableIndex)
2190+
info.name = MOI.get(src, MOI.VariableName(), x)
2191+
end
21902192
info.index = index
21912193
info.column = HighsInt(i - 1)
21922194
mapping[x] = index
@@ -2207,13 +2209,14 @@ function _extract_row_data(
22072209
V::Vector{Float64},
22082210
::Type{S},
22092211
) where {S}
2212+
F = MOI.ScalarAffineFunction{Float64}
22102213
row = length(I) == 0 ? 1 : I[end] + 1
22112214
list = _constraints(src, MOI.ScalarAffineFunction{Float64}, S)
22122215
numrows = length(list)
22132216
_add_sizehint!(rowlower, numrows)
22142217
_add_sizehint!(rowupper, numrows)
22152218
n_terms = 0
2216-
fs = Array{MOI.ScalarAffineFunction{Float64}}(undef, numrows)
2219+
fs = Array{F}(undef, numrows)
22172220
for (i, c_index) in enumerate(list)
22182221
f = MOI.get(src, MOI.ConstraintFunction(), c_index)
22192222
fs[i] = f
@@ -2226,10 +2229,12 @@ function _extract_row_data(
22262229
dest.affine_constraint_info,
22272230
_ConstraintInfo(set),
22282231
)
2229-
dest.affine_constraint_info[key].row =
2230-
HighsInt(length(dest.affine_constraint_info) - 1)
2231-
mapping[c_index] =
2232-
MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},S}(key.value)
2232+
info = dest.affine_constraint_info[key]
2233+
info.row = HighsInt(length(dest.affine_constraint_info) - 1)
2234+
if MOI.supports(src, MOI.ConstraintName(), MOI.ConstraintIndex{F,S})
2235+
info.name = MOI.get(src, MOI.ConstraintName(), c_index)
2236+
end
2237+
mapping[c_index] = MOI.ConstraintIndex{F,S}(key.value)
22332238
end
22342239
_add_sizehint!(I, n_terms)
22352240
_add_sizehint!(J, n_terms)

test/MOI_wrapper.jl

+11
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,17 @@ function test_option_nothing()
294294
return
295295
end
296296

297+
function test_copy_to_names()
298+
dest = HiGHS.Optimizer()
299+
src = MOI.Utilities.Model{Float64}()
300+
MOI.Utilities.loadfromstring!(src, "variables: x\nc: 2.0 * x <= 1.0")
301+
_ = MOI.copy_to(dest, src)
302+
@test MOI.get(dest, MOI.VariableIndex, "x") isa MOI.VariableIndex
303+
F, S = MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}
304+
@test MOI.get(dest, MOI.ConstraintIndex, "c") isa MOI.ConstraintIndex{F,S}
305+
return
306+
end
307+
297308
end
298309

299310
TestMOIHighs.runtests()

0 commit comments

Comments
 (0)