Skip to content
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

Order of columns during copy_to #2493

Closed
odow opened this issue May 3, 2024 · 1 comment · Fixed by #2495
Closed

Order of columns during copy_to #2493

odow opened this issue May 3, 2024 · 1 comment · Fixed by #2495

Comments

@odow
Copy link
Member

odow commented May 3, 2024

@mlubin and I have been discussing the following example:

julia> using JuMP

julia> function main()
           model = Model()
           @variable(model, x)
           @variable(model, y >= 0)
           @objective(model, Min, x + y)
           write_to_file(model, "file.mps")
           print(read("file.mps", String))
       end
main (generic function with 1 method)

julia> main()
NAME
ROWS
 N  OBJ
COLUMNS
    y         OBJ       1
    x         OBJ       1
RHS
RANGES
BOUNDS
 LO bounds    y         0
 PL bounds    y
 FR bounds    x
ENDATA

In JuMP, the columns are ordered x, y, but the MPS file, they are ordered as y, x.

Since the behavior of MIP solvers can depend on the column (and row) ordering, this can be undesirable.

The problem is:

"""
_try_constrain_variables_on_creation(
dest::MOI.ModelLike,
src::MOI.ModelLike,
index_map::IndexMap,
::Type{S},
) where {S<:MOI.AbstractScalarSet}
Copy the constraints of type `MOI.VariableIndex`-in-`S` from the model `src` to
the model `dest` and fill `index_map` accordingly. The copy is only done when the
variables to be copied are not already keys of `index_map`.
It returns a list of the constraints that were not added.
"""
function _try_constrain_variables_on_creation(
dest::MOI.ModelLike,
src::MOI.ModelLike,
index_map::IndexMap,
::Type{S},
) where {S<:MOI.AbstractScalarSet}
not_added = MOI.ConstraintIndex{MOI.VariableIndex,S}[]
for ci_src in
MOI.get(src, MOI.ListOfConstraintIndices{MOI.VariableIndex,S}())
f_src = MOI.get(src, MOI.ConstraintFunction(), ci_src)
if haskey(index_map, f_src)
# Can't add it because it contains a variable previously added
push!(not_added, ci_src)
else
set = MOI.get(src, MOI.ConstraintSet(), ci_src)::S
vi_dest, ci_dest = MOI.add_constrained_variable(dest, set)
index_map[ci_src] = ci_dest
index_map[f_src] = vi_dest
end
end
return not_added
end

else
Any[
_try_constrain_variables_on_creation(dest, src, index_map, S)
for S in sorted_variable_sets_by_cost(dest, src)
]

which eagerly tries to constrain variables on creation based on their set type.

I wonder if we should:

  1. not do this for scalar variables; or
  2. use a better heuristic, that adds variables in the correct order, but puts them in a set iff they are consecutive in the VectorOfVariables function.

The original motivation for doing this is so that variable bridges can be preferentially used.

I don't really have an answer yet. I'll explore some stuff and report here.

@odow
Copy link
Member Author

odow commented May 3, 2024

Okay. It's a bit more effort, but I think we can make this work out-of-the-box, just like we do for writing variable cones in the CBF writer.

See: #2478

x-ref #2494

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant