|
| 1 | +module InfiniteDisjunctiveProgramming |
| 2 | + |
| 3 | +import JuMP.MOI as _MOI |
| 4 | +import InfiniteOpt, JuMP |
| 5 | +import DisjunctiveProgramming as DP |
| 6 | + |
| 7 | +################################################################################ |
| 8 | +# MODEL |
| 9 | +################################################################################ |
| 10 | +function DP.InfiniteGDPModel(args...; kwargs...) |
| 11 | + return DP.GDPModel{ |
| 12 | + InfiniteOpt.InfiniteModel, |
| 13 | + InfiniteOpt.GeneralVariableRef, |
| 14 | + InfiniteOpt.InfOptConstraintRef |
| 15 | + }(args...; kwargs...) |
| 16 | +end |
| 17 | + |
| 18 | +function DP.collect_all_vars(model::InfiniteOpt.InfiniteModel) |
| 19 | + vars = JuMP.all_variables(model) |
| 20 | + derivs = InfiniteOpt.all_derivatives(model) |
| 21 | + return append!(vars, derivs) |
| 22 | +end |
| 23 | + |
| 24 | +################################################################################ |
| 25 | +# VARIABLES |
| 26 | +################################################################################ |
| 27 | +DP.InfiniteLogical(prefs...) = DP.Logical(InfiniteOpt.Infinite(prefs...)) |
| 28 | + |
| 29 | +_is_parameter(vref::InfiniteOpt.GeneralVariableRef) = |
| 30 | + _is_parameter(InfiniteOpt.dispatch_variable_ref(vref)) |
| 31 | +_is_parameter(::InfiniteOpt.DependentParameterRef) = true |
| 32 | +_is_parameter(::InfiniteOpt.IndependentParameterRef) = true |
| 33 | +_is_parameter(::InfiniteOpt.ParameterFunctionRef) = true |
| 34 | +_is_parameter(::InfiniteOpt.FiniteParameterRef) = true |
| 35 | +_is_parameter(::Any) = false |
| 36 | + |
| 37 | +function DP.requires_disaggregation(vref::InfiniteOpt.GeneralVariableRef) |
| 38 | + return !_is_parameter(vref) |
| 39 | +end |
| 40 | + |
| 41 | +function DP.VariableProperties(vref::InfiniteOpt.GeneralVariableRef) |
| 42 | + info = DP.get_variable_info(vref) |
| 43 | + name = JuMP.name(vref) |
| 44 | + set = nothing |
| 45 | + prefs = InfiniteOpt.parameter_refs(vref) |
| 46 | + var_type = !isempty(prefs) ? InfiniteOpt.Infinite(prefs...) : nothing |
| 47 | + return DP.VariableProperties(info, name, set, var_type) |
| 48 | +end |
| 49 | + |
| 50 | +# Extract parameter refs from expression and return VariableProperties with Infinite type |
| 51 | +function DP.VariableProperties( |
| 52 | + expr::Union{ |
| 53 | + JuMP.GenericAffExpr{C, InfiniteOpt.GeneralVariableRef}, |
| 54 | + JuMP.GenericQuadExpr{C, InfiniteOpt.GeneralVariableRef}, |
| 55 | + JuMP.GenericNonlinearExpr{InfiniteOpt.GeneralVariableRef} |
| 56 | + } |
| 57 | +) where C |
| 58 | + prefs = InfiniteOpt.parameter_refs(expr) |
| 59 | + info = DP._free_variable_info() |
| 60 | + var_type = !isempty(prefs) ? InfiniteOpt.Infinite(prefs...) : nothing |
| 61 | + return DP.VariableProperties(info, "", nothing, var_type) |
| 62 | +end |
| 63 | + |
| 64 | +function DP.VariableProperties( |
| 65 | + exprs::Vector{<:Union{ |
| 66 | + InfiniteOpt.GeneralVariableRef, |
| 67 | + JuMP.GenericAffExpr{<:Any, InfiniteOpt.GeneralVariableRef}, |
| 68 | + JuMP.GenericQuadExpr{<:Any, InfiniteOpt.GeneralVariableRef}, |
| 69 | + JuMP.GenericNonlinearExpr{InfiniteOpt.GeneralVariableRef} |
| 70 | + }} |
| 71 | +) |
| 72 | + all_prefs = Set{InfiniteOpt.GeneralVariableRef}() |
| 73 | + for expr in exprs |
| 74 | + for pref in InfiniteOpt.parameter_refs(expr) |
| 75 | + push!(all_prefs, pref) |
| 76 | + end |
| 77 | + end |
| 78 | + prefs = Tuple(all_prefs) |
| 79 | + info = DP._free_variable_info() |
| 80 | + var_type = !isempty(prefs) ? InfiniteOpt.Infinite(prefs...) : nothing |
| 81 | + return DP.VariableProperties(info, "", nothing, var_type) |
| 82 | +end |
| 83 | + |
| 84 | +function JuMP.value(vref::DP.LogicalVariableRef{InfiniteOpt.InfiniteModel}) |
| 85 | + return JuMP.value(DP.binary_variable(vref)) .>= 0.5 |
| 86 | +end |
| 87 | + |
| 88 | +################################################################################ |
| 89 | +# CONSTRAINTS |
| 90 | +################################################################################ |
| 91 | +function JuMP.add_constraint( |
| 92 | + model::InfiniteOpt.InfiniteModel, |
| 93 | + c::JuMP.VectorConstraint{F, S}, |
| 94 | + name::String = "" |
| 95 | +) where {F, S <: DP.AbstractCardinalitySet} |
| 96 | + return DP._add_cardinality_constraint(model, c, name) |
| 97 | +end |
| 98 | + |
| 99 | +function JuMP.add_constraint( |
| 100 | + model::M, |
| 101 | + c::JuMP.ScalarConstraint{DP._LogicalExpr{M}, S}, |
| 102 | + name::String = "" |
| 103 | +) where {S, M <: InfiniteOpt.InfiniteModel} |
| 104 | + return DP._add_logical_constraint(model, c, name) |
| 105 | +end |
| 106 | + |
| 107 | +function JuMP.add_constraint( |
| 108 | + model::M, |
| 109 | + c::JuMP.ScalarConstraint{DP.LogicalVariableRef{M}, S}, |
| 110 | + name::String = "" |
| 111 | +) where {M <: InfiniteOpt.InfiniteModel, S} |
| 112 | + error("Cannot define constraint on single logical variable, use `fix` instead.") |
| 113 | +end |
| 114 | + |
| 115 | +function JuMP.add_constraint( |
| 116 | + model::M, |
| 117 | + c::JuMP.ScalarConstraint{ |
| 118 | + JuMP.GenericAffExpr{C, DP.LogicalVariableRef{M}}, S |
| 119 | + }, |
| 120 | + name::String = "" |
| 121 | +) where {M <: InfiniteOpt.InfiniteModel, S, C} |
| 122 | + error("Cannot add, subtract, or multiply with logical variables.") |
| 123 | +end |
| 124 | + |
| 125 | +function JuMP.add_constraint( |
| 126 | + model::M, |
| 127 | + c::JuMP.ScalarConstraint{ |
| 128 | + JuMP.GenericQuadExpr{C, DP.LogicalVariableRef{M}}, S |
| 129 | + }, |
| 130 | + name::String = "" |
| 131 | +) where {M <: InfiniteOpt.InfiniteModel, S, C} |
| 132 | + error("Cannot add, subtract, or multiply with logical variables.") |
| 133 | +end |
| 134 | + |
| 135 | +################################################################################ |
| 136 | +# METHODS |
| 137 | +################################################################################ |
| 138 | +function DP.get_constant( |
| 139 | + expr::JuMP.GenericAffExpr{T, InfiniteOpt.GeneralVariableRef} |
| 140 | +) where {T} |
| 141 | + constant = JuMP.constant(expr) |
| 142 | + param_expr = zero(typeof(expr)) |
| 143 | + for (var, coeff) in expr.terms |
| 144 | + if _is_parameter(var) |
| 145 | + JuMP.add_to_expression!(param_expr, coeff, var) |
| 146 | + end |
| 147 | + end |
| 148 | + return constant + param_expr |
| 149 | +end |
| 150 | + |
| 151 | +function DP.disaggregate_expression( |
| 152 | + model::M, |
| 153 | + aff::JuMP.GenericAffExpr, |
| 154 | + bvref::Union{JuMP.AbstractVariableRef, JuMP.GenericAffExpr}, |
| 155 | + method::DP._Hull |
| 156 | +) where {M <: InfiniteOpt.InfiniteModel} |
| 157 | + terms = Any[aff.constant * bvref] |
| 158 | + for (vref, coeff) in aff.terms |
| 159 | + if JuMP.is_binary(vref) |
| 160 | + push!(terms, coeff * vref) |
| 161 | + elseif vref isa InfiniteOpt.GeneralVariableRef && _is_parameter(vref) |
| 162 | + push!(terms, coeff * vref * bvref) |
| 163 | + elseif !haskey(method.disjunct_variables, (vref, bvref)) |
| 164 | + push!(terms, coeff * vref) |
| 165 | + else |
| 166 | + dvref = method.disjunct_variables[vref, bvref] |
| 167 | + push!(terms, coeff * dvref) |
| 168 | + end |
| 169 | + end |
| 170 | + return JuMP.@expression(model, sum(terms)) |
| 171 | +end |
| 172 | + |
| 173 | +################################################################################ |
| 174 | +# ERROR MESSAGES |
| 175 | +################################################################################ |
| 176 | +function DP.reformulate_model(::InfiniteOpt.InfiniteModel, ::DP.MBM) |
| 177 | + error("The `MBM` method is not supported for `InfiniteModel`." * |
| 178 | + "Please use `BigM`, `Hull`, `Indicator`, or `PSplit` instead.") |
| 179 | +end |
| 180 | + |
| 181 | +function DP.reformulate_model(::InfiniteOpt.InfiniteModel, ::DP.cutting_planes) |
| 182 | + error("The `cutting_planes` method is not supported for `InfiniteModel`." * |
| 183 | + "Please use `BigM`, `Hull`, `Indicator`, or `PSplit` instead.") |
| 184 | +end |
| 185 | + |
| 186 | +end |
| 187 | + |
0 commit comments