|
| 1 | +module PolyhedraJuMPExt |
| 2 | + |
| 3 | +import JuMP |
| 4 | +import Polyhedra: hrep, LPHRep, polyhedron, _optimize! |
| 5 | +using Polyhedra: Rep, Projection, _moi_set, fulldim, dimension_names, PolyhedraToLPBridge, ProjectionBridge |
| 6 | + |
| 7 | +""" |
| 8 | + hrep(model::JuMP.Model) |
| 9 | +
|
| 10 | +Builds an H-representation from the feasibility set of the JuMP model `model`. |
| 11 | +Note that if non-linear constraint are present in the model, they are ignored. |
| 12 | +""" |
| 13 | +hrep(model::JuMP.Model) = LPHRep(model) |
| 14 | +LPHRep(model::JuMP.Model) = LPHRep(JuMP.backend(model)) |
| 15 | +polyhedron(model::JuMP.Model, args...) = polyhedron(hrep(model), args...) |
| 16 | +_optimize!(model::JuMP.Model) = JuMP.optimize!(model) |
| 17 | + |
| 18 | +struct VariableInSet{V <: JuMP.ScalarVariable, S <: Union{Rep, Projection}} <: JuMP.AbstractVariable |
| 19 | + variables::Vector{V} |
| 20 | + set::S |
| 21 | +end |
| 22 | +function JuMP.build_variable(error_fun::Function, variables::Vector{<:JuMP.ScalarVariable}, set::Union{Rep, Projection}) |
| 23 | + if length(variables) != fulldim(set) |
| 24 | + error("Number of variables ($(length(variables))) does not match the full dimension of the polyhedron ($(fulldim(set))).") |
| 25 | + end |
| 26 | + return VariableInSet(variables, set) |
| 27 | +end |
| 28 | +function JuMP.add_variable(model::JuMP.AbstractModel, v::VariableInSet, names) |
| 29 | + dim_names = dimension_names(v.set) |
| 30 | + if dim_names !== nothing |
| 31 | + names = copy(names) |
| 32 | + for i in eachindex(names) |
| 33 | + if isempty(names[i]) && !isempty(dim_names[i]) |
| 34 | + names[i] = dim_names[i] |
| 35 | + end |
| 36 | + end |
| 37 | + end |
| 38 | + JuMP.add_bridge(model, PolyhedraToLPBridge) |
| 39 | + JuMP.add_bridge(model, ProjectionBridge) |
| 40 | + return JuMP.add_variable(model, JuMP.VariablesConstrainedOnCreation(v.variables, _moi_set(v.set)), names) |
| 41 | +end |
| 42 | +function JuMP.build_constraint(error_fun::Function, func, set::Rep) |
| 43 | + return JuMP.BridgeableConstraint( |
| 44 | + JuMP.build_constraint(error_fun, func, _moi_set(set)), |
| 45 | + PolyhedraToLPBridge) |
| 46 | +end |
| 47 | +function JuMP.build_constraint(error_fun::Function, func, set::Projection) |
| 48 | + return JuMP.BridgeableConstraint(JuMP.BridgeableConstraint( |
| 49 | + JuMP.build_constraint(error_fun, func, _moi_set(set)), |
| 50 | + ProjectionBridge), PolyhedraToLPBridge) |
| 51 | +end |
| 52 | + |
| 53 | +end |
0 commit comments