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 all commits
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
11 changes: 11 additions & 0 deletions docs/src/submodules/FileFormats/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_NL)
An AMPL (.nl) model
```

**The REW file format**

```jldoctest
julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_REW)
A Mathematical Programming System (MPS) model
```

Note that the [REW format](https://www.gurobi.com/documentation/9.5/refman/rew_format.html)
is identical to the MPS file format, except that all names are replaced with
generic identifiers.

**The SDPA file format**

```jldoctest
Expand Down
8 changes: 8 additions & 0 deletions src/FileFormats/FileFormats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ List of accepted export formats.
- `FORMAT_MOF`: the MathOptFormat file format
- `FORMAT_MPS`: the MPS file format
- `FORMAT_NL`: the AMPL .nl file format
- `FORMAT_REW`: the .rew file format, which is MPS with generic names
- `FORMAT_SDPA`: the SemiDefinite Programming Algorithm format
"""
@enum(
Expand All @@ -36,6 +37,7 @@ List of accepted export formats.
FORMAT_MOF,
FORMAT_MPS,
FORMAT_NL,
FORMAT_REW,
FORMAT_SDPA,
)

Expand Down Expand Up @@ -69,6 +71,8 @@ function Model(;
return MPS.Model(; kwargs...)
elseif format == FORMAT_NL
return NL.Model(; kwargs...)
elseif format == FORMAT_REW
return MPS.Model(; generic_names = true, kwargs...)
elseif format == FORMAT_SDPA
return SDPA.Model(; kwargs...)
else
Expand All @@ -81,6 +85,10 @@ function Model(;
(".lp", LP.Model),
(".mof.json", MOF.Model),
(".mps", MPS.Model),
(
".rew",
(; kwargs...) -> MPS.Model(; generic_names = true, kwargs...),
),
(".nl", NL.Model),
(".dat-s", SDPA.Model),
(".sdpa", SDPA.Model),
Expand Down
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
84 changes: 84 additions & 0 deletions test/FileFormats/MPS/MPS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,90 @@ 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" *
"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 test_rew_filename()
model = MOI.FileFormats.Model(; filename = "test.rew")
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" *
"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 test_rew_format()
model = MOI.FileFormats.Model(; format = MOI.FileFormats.FORMAT_REW)
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" *
"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