Skip to content

[FileFormats] add option for generic names #1808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/FileFormats/MPS/MPS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ end
struct Options
warn::Bool
objsense::Bool
generic_names::Bool
end

get_options(m::Model) = get(m.ext, :MPS_OPTIONS, Options(false, true))
get_options(m::Model) = get(m.ext, :MPS_OPTIONS, Options(false, false, false))

"""
Model(; kwargs...)
Expand All @@ -55,10 +56,17 @@ Keyword arguments are:

- `warn::Bool=false`: print a warning when variables or constraints are renamed.
- `print_objsense::Bool=false`: print the OBJSENSE section when writing
- `generic_names::Bool=false`: strip all names in the model and replace them
with the generic names `C\$i` and `R\$i` for the i'th column and row
respectively.
"""
function Model(; warn::Bool = false, print_objsense::Bool = false)
function Model(;
warn::Bool = false,
print_objsense::Bool = false,
generic_names::Bool = false,
)
model = Model{Float64}()
model.ext[:MPS_OPTIONS] = Options(warn, print_objsense)
model.ext[:MPS_OPTIONS] = Options(warn, print_objsense, generic_names)
return model
end

Expand Down Expand Up @@ -160,11 +168,15 @@ Write `model` to `io` in the MPS file format.
"""
function Base.write(io::IO, model::Model)
options = get_options(model)
FileFormats.create_unique_names(
model;
warn = options.warn,
replacements = Function[s->replace(s, ' ' => '_')],
)
if options.generic_names
FileFormats.create_generic_names(model)
else
FileFormats.create_unique_names(
model;
warn = options.warn,
replacements = Function[s->replace(s, ' ' => '_')],
)
end
ordered_names = String[]
names = Dict{MOI.VariableIndex,String}()
for x in MOI.get(model, MOI.ListOfVariableIndices())
Expand Down
25 changes: 24 additions & 1 deletion src/FileFormats/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
create_unique_names(
model::MOI.ModelLike;
warn::Bool = false,
replacements::Vector{Function} = Function[]
replacements::Vector{Function} = Function[],
)

Rename variables in `model` to ensure that all variables and constraints have
Expand All @@ -25,6 +25,29 @@ function create_unique_names(
return
end

"""
create_generic_names(model::MOI.ModelLike)

Rename all variables and constraints in `model` to have generic names.

This is helpful for users with proprietary models to avoid leaking information.
"""
function create_generic_names(model::MOI.ModelLike)
for (i, x) in enumerate(MOI.get(model, MOI.ListOfVariableIndices()))
MOI.set(model, MOI.VariableName(), x, "C$i")
end
i = 1
for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
if F == MOI.VariableIndex
continue # VariableIndex constraints do not need a name.
end
for c in MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
MOI.set(model, MOI.ConstraintName(), c, "R$i")
i += 1
end
end
end

function _replace(s::String, replacements::Vector{Function})
for f in replacements
s = f(s)
Expand Down
29 changes: 29 additions & 0 deletions test/FileFormats/MPS/MPS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,35 @@ function test_sos_constraints()
return
end

function test_generic_names()
model = MPS.Model(; generic_names = true)
x = MOI.add_variable(model)
y = MOI.add_variable(model)
c = MOI.add_constraint(
model,
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(1.0, [x, y]), 0.0),
MOI.EqualTo(1.0),
)
MOI.add_constraint(model, y, MOI.GreaterThan(2.0))
@test sprint(write, model) ==
"NAME \n" *
"OBJSENSE MIN\n" *
"ROWS\n" *
" N OBJ\n" *
" E R1\n" *
"COLUMNS\n" *
" C1 R1 1\n" *
" C2 R1 1\n" *
"RHS\n" *
" rhs R1 1\n" *
"RANGES\n" *
"BOUNDS\n" *
" FR bounds C1\n" *
" LO bounds C2 2\n" *
" PL bounds C2\n" *
"ENDATA\n"
end

function runtests()
for name in names(@__MODULE__, all = true)
if startswith("$(name)", "test_")
Expand Down