Polyhedra.jl icon indicating copy to clipboard operation
Polyhedra.jl copied to clipboard

[feature] mapping from variable to index in representations

Open matbesancon opened this issue 6 years ago • 4 comments

It would be nice to be able to keep track of which variables correspond to which index in the representations:

julia> m = Model()
julia> @variable(m, α[1:2,1:2] >= 0)
julia> @variable(m, β[1:2] >= 0)
julia> @constraint(m, -α[2,1] - 4* α[2,2] + β[2] >= 2)
julia> @constraint(m, -α[1,1] - 4* α[1,2] + β[1] >= 4)
julia> const poly = Polyhedra.polyhedron(m, CDDLib.Library(:exact))
Polyhedron CDDLib.Polyhedron{Rational{BigInt}}:
8-element iterator of HalfSpace{Rational{BigInt},Array{Rational{BigInt},1}}:
 HalfSpace(Rational{BigInt}[-1//1, 0//1, 0//1, 0//1, 0//1, 0//1], 0//1)
 HalfSpace(Rational{BigInt}[0//1, -1//1, 0//1, 0//1, 0//1, 0//1], 0//1)
 HalfSpace(Rational{BigInt}[0//1, 0//1, -1//1, 0//1, 0//1, 0//1], 0//1)
 HalfSpace(Rational{BigInt}[0//1, 0//1, 0//1, -1//1, 0//1, 0//1], 0//1)
 HalfSpace(Rational{BigInt}[0//1, 0//1, 0//1, 0//1, -1//1, 0//1], 0//1)
 HalfSpace(Rational{BigInt}[0//1, 0//1, 0//1, 0//1, 0//1, -1//1], 0//1)
 HalfSpace(Rational{BigInt}[0//1, -1//1, 0//1, 0//1, 1//1, 4//1], -2//1)

Here which column is which variable? Same thing for V-rep

V-representation CDDLib.CDDGeneratorMatrix{Rational{BigInt},CDDLib.GMPRational}:
1-element iterator of Array{Rational{BigInt},1}:
 Rational{BigInt}[4//1, 2//1, 0//1, 0//1, 0//1, 0//1],
6-element iterator of Ray{Rational{BigInt},Array{Rational{BigInt},1}}:
 Ray(Rational{BigInt}[4//1, 0//1, 0//1, 1//1, 0//1, 0//1])
 Ray(Rational{BigInt}[1//1, 0//1, 1//1, 0//1, 0//1, 0//1])
 Ray(Rational{BigInt}[1//1, 0//1, 0//1, 0//1, 0//1, 0//1])
 Ray(Rational{BigInt}[0//1, 1//1, 0//1, 0//1, 0//1, 0//1])
 Ray(Rational{BigInt}[0//1, 1//1, 0//1, 0//1, 1//1, 0//1])
 Ray(Rational{BigInt}[0//1, 4//1, 0//1, 0//1, 0//1, 1//1])

The tricky part is that variable declaration order change this

matbesancon avatar Apr 17 '19 15:04 matbesancon

Even trickier, it looks like reshape is reversed from variable definition:

julia> m = Model()
julia> @variable(m, 0 <= x[1:2,1:2] <= 2)
julia> @constraint(m, x[1,1] + x[1,2] <= 1)
julia> Polyhedra.polyhedron(m, CDDLib.Library(:exact)) |> Polyhedra.vrep
# first one is
julia> v = [0//1, 1//1, 2//1, 0//1]
julia> reshape(v, (2,2))
2×2 Array{Rational{Int64},2}:
 0//1  2//1
 1//1  0//1

matbesancon avatar Apr 17 '19 15:04 matbesancon

That's annoying indeed, we could provide a dictionary to help this. The order is the creation order which is the same as their order in all_variables(model)

blegat avatar Apr 17 '19 16:04 blegat

ok, then this issue should also be reported in JuMP for all_variables

matbesancon avatar Apr 17 '19 17:04 matbesancon

We should probably expose the MOIU.IndexMap returned here: https://github.com/JuliaPolyhedra/Polyhedra.jl/blob/8a0d39a1bc8610530e999b29d8acdfa0db17ead8/src/lphrep.jl#L21 We could have a

poly, ref_map = polyhedron_with_map(model, lib)

where ref_map maps JuMP.VariableRef to the index between 1 and fulldim(poly) corresponding to this variable. This ref_map would be a simple wrapper around the MOIU.IndexMap returned by the MOI.copy_to. What do you think ?

blegat avatar Apr 19 '19 10:04 blegat