Skip to content

Commit 20a4d89

Browse files
authored
Simplify implementation and add test for MOI.VariableName (#170)
1 parent d1a4efa commit 20a4d89

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

src/variable.jl

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,6 @@ end
4848

4949
## Name #######################################################################
5050
###############################################################################
51-
function set_column_name(task::Mosek.MSKtask, col::ColumnIndex, name::String)
52-
return Mosek.putvarname(task, col.value, name)
53-
end
54-
55-
function set_column_name(task::Mosek.MSKtask, mat::MatrixIndex, name::String)
56-
# Names of matrix index is not supported by Mosek at the moment
57-
msg = "Mosek does not support names for positive semidefinite variables."
58-
return throw(MOI.UnsupportedAttribute(MOI.VariableName(), msg))
59-
end
60-
61-
function set_column_name(m::Optimizer, vi::MOI.VariableIndex, name::String)
62-
return set_column_name(m.task, mosek_index(m, vi), name)
63-
end
64-
65-
function column_name(task::Mosek.MSKtask, col::ColumnIndex)
66-
return Mosek.getvarname(task, col.value)
67-
end
68-
69-
function column_name(m::Optimizer, vi::MOI.VariableIndex)
70-
return column_name(m.task, mosek_index(m, vi))
71-
end
72-
73-
function column_with_name(task::Mosek.MSKtask, name::String)
74-
asgn, col = Mosek.getvarnameindex(task, name)
75-
if iszero(asgn)
76-
return nothing
77-
end
78-
return col
79-
end
80-
81-
column_with_name(m::Optimizer, name::String) = column_with_name(m.task, name)
8251

8352
"""
8453
function clear_columns(task::Mosek.MSKtask, cols::Vector{Int32})
@@ -334,7 +303,16 @@ end
334303

335304
# We leave `supports` to `false` because it's not supported by matrix indices
336305
# See https://github.com/jump-dev/MosekTools.jl/issues/80
337-
# MOI.supports(::Optimizer, ::MOI.VariableName, ::Type{MOI.VariableIndex}) = true
306+
# We still implement the methods for people that knowingly set names of scalar
307+
# variables though.
308+
MOI.supports(::Optimizer, ::MOI.VariableName, ::Type{MOI.VariableIndex}) = false
309+
310+
_throw_if_matrix(::ColumnIndex) = nothing
311+
312+
function _throw_if_matrix(::MatrixIndex)
313+
msg = "Mosek does not support names for positive semidefinite variables."
314+
return throw(MOI.UnsupportedAttribute(MOI.VariableName(), msg))
315+
end
338316

339317
function MOI.set(
340318
m::Optimizer,
@@ -343,18 +321,22 @@ function MOI.set(
343321
name::String,
344322
)
345323
m.has_variable_names = true
346-
set_column_name(m, vi, name)
324+
col = mosek_index(m, vi)
325+
_throw_if_matrix(col)
326+
Mosek.putvarname(m.task, col.value, name)
347327
return
348328
end
349329

350330
function MOI.get(m::Optimizer, ::MOI.VariableName, vi::MOI.VariableIndex)
351-
return column_name(m, vi)
331+
col = mosek_index(m, vi)
332+
_throw_if_matrix(col)
333+
return Mosek.getvarname(m.task, col.value)
352334
end
353335

354336
function MOI.get(m::Optimizer, ::Type{MOI.VariableIndex}, name::String)
355-
col = column_with_name(m, name)
356-
if col === nothing
337+
asgn, index = Mosek.getvarnameindex(m.task, name)
338+
if iszero(asgn)
357339
return nothing
358340
end
359-
return index_of_column(m, col)
341+
return index_of_column(m, index)
360342
end

test/runtests.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,27 @@ function test_more_SDP_tests_by_forced_bridging()
477477
return
478478
end
479479

480+
function test_variable_name()
481+
model = MosekOptimizerWithFallback()
482+
x = MOI.add_variable(model)
483+
set = MOI.PositiveSemidefiniteConeTriangle(2)
484+
y, _ = MOI.add_constrained_variables(model, set)
485+
@test !MOI.supports(model, MOI.VariableName(), MOI.VariableIndex)
486+
@test MOI.get(model, MOI.VariableIndex, "x") === nothing
487+
MOI.set(model, MOI.VariableName(), x, "x")
488+
@test MOI.get(model, MOI.VariableIndex, "x") == x
489+
@test MOI.get(model, MOI.VariableName(), x) == "x"
490+
@test_throws(
491+
MOI.UnsupportedAttribute,
492+
MOI.get(model, MOI.VariableName(), y[1]),
493+
)
494+
@test_throws(
495+
MOI.UnsupportedAttribute,
496+
MOI.set(model, MOI.VariableName(), y[1], "y"),
497+
)
498+
return
499+
end
500+
480501
end # module
481502

482503
TestMosekTools.runtests()

0 commit comments

Comments
 (0)