Skip to content

Commit

Permalink
Fix adding variable and constraint names in copy_to (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Oct 26, 2022
1 parent a789aad commit adf310e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2186,7 +2186,9 @@ function _copy_to_columns(dest::Optimizer, src::MOI.ModelLike, mapping)
_VariableInfo(MOI.VariableIndex(0), HighsInt(0)),
)
info = _info(dest, index)
info.name = MOI.get(dest, MOI.VariableName(), x)
if MOI.supports(src, MOI.VariableName(), MOI.VariableIndex)
info.name = MOI.get(src, MOI.VariableName(), x)
end
info.index = index
info.column = HighsInt(i - 1)
mapping[x] = index
Expand All @@ -2207,13 +2209,14 @@ function _extract_row_data(
V::Vector{Float64},
::Type{S},
) where {S}
F = MOI.ScalarAffineFunction{Float64}
row = length(I) == 0 ? 1 : I[end] + 1
list = _constraints(src, MOI.ScalarAffineFunction{Float64}, S)
numrows = length(list)
_add_sizehint!(rowlower, numrows)
_add_sizehint!(rowupper, numrows)
n_terms = 0
fs = Array{MOI.ScalarAffineFunction{Float64}}(undef, numrows)
fs = Array{F}(undef, numrows)
for (i, c_index) in enumerate(list)
f = MOI.get(src, MOI.ConstraintFunction(), c_index)
fs[i] = f
Expand All @@ -2226,10 +2229,12 @@ function _extract_row_data(
dest.affine_constraint_info,
_ConstraintInfo(set),
)
dest.affine_constraint_info[key].row =
HighsInt(length(dest.affine_constraint_info) - 1)
mapping[c_index] =
MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},S}(key.value)
info = dest.affine_constraint_info[key]
info.row = HighsInt(length(dest.affine_constraint_info) - 1)
if MOI.supports(src, MOI.ConstraintName(), MOI.ConstraintIndex{F,S})
info.name = MOI.get(src, MOI.ConstraintName(), c_index)
end
mapping[c_index] = MOI.ConstraintIndex{F,S}(key.value)
end
_add_sizehint!(I, n_terms)
_add_sizehint!(J, n_terms)
Expand Down
11 changes: 11 additions & 0 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,17 @@ function test_option_nothing()
return
end

function test_copy_to_names()
dest = HiGHS.Optimizer()
src = MOI.Utilities.Model{Float64}()
MOI.Utilities.loadfromstring!(src, "variables: x\nc: 2.0 * x <= 1.0")
_ = MOI.copy_to(dest, src)
@test MOI.get(dest, MOI.VariableIndex, "x") isa MOI.VariableIndex
F, S = MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}
@test MOI.get(dest, MOI.ConstraintIndex, "c") isa MOI.ConstraintIndex{F,S}
return
end

end

TestMOIHighs.runtests()

2 comments on commit adf310e

@odow
Copy link
Member Author

@odow odow commented on adf310e Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/71156

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.2.0 -m "<description of version>" adf310ea6a404a7fa94ddc3fa80a1690b8292db1
git push origin v1.2.0

Please sign in to comment.