-
Notifications
You must be signed in to change notification settings - Fork 5
Add InfiniteOpt as an Extension #130
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
Open
dnguyen227
wants to merge
32
commits into
infiniteopt:master
Choose a base branch
from
dnguyen227:infiniteext
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
15803de
Add InfiniteOpt as an extension
pulsipher ebf603c
Update tests
pulsipher 06a0b4f
add pkg
pulsipher 3786845
fix extension versioning
pulsipher 1886c29
Merge branch 'master' into infiniteopt_ext
pulsipher 19f620d
bug fix
pulsipher c803d51
Merge remote-tracking branch 'upstream/infiniteopt_ext' into infiniteext
dnguyen227 68b1bb9
merged with main WIP
dnguyen227 b9fbc36
wip
dnguyen227 0caadbf
hull and bigm working version
dnguyen227 afec911
psplit working
dnguyen227 ad6423f
Tests added.
dnguyen227 c7f5b6a
julia version
dnguyen227 6e9b625
version change
dnguyen227 ef789bf
update CI
dnguyen227 36e3f77
added docstrings
dnguyen227 6f701ad
docstring for create_blank_variable
dnguyen227 5f2e4b7
version revert, dispatch on VariableProperties to create free variables
dnguyen227 beb5045
CI.yml change
dnguyen227 a0babfc
removed InfiniteOpt frolm targets (testing)
dnguyen227 b3c87af
CI to 1.9 and 1.6
dnguyen227 92e2407
docstring update, renamed all_variables to collect_all_vars (avoid Ju…
dnguyen227 bfcd3a7
additional tests, versoin change
dnguyen227 e301188
additional tests
dnguyen227 d62c7c6
Update runtests.jl
dnguyen227 9314bd7
Uncomment includes and update package management
dnguyen227 a99d183
additional tests
dnguyen227 950ee3d
.
dnguyen227 ecc322c
.
dnguyen227 9bb4249
simplify runtest.jl
dnguyen227 3df02ed
edited dependences, refactored InfiniteDisjunctiveProgramming.jl
dnguyen227 cbcb936
80 character edit
dnguyen227 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ jobs: | |
| strategy: | ||
| matrix: | ||
| version: | ||
| - '1.6' | ||
| - '1.10' | ||
| os: | ||
| - ubuntu-latest | ||
| - macOS-latest | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| module InfiniteDisjunctiveProgramming | ||
|
|
||
| import JuMP.MOI as _MOI | ||
| import InfiniteOpt, JuMP | ||
| import DisjunctiveProgramming as DP | ||
|
|
||
| ################################################################################ | ||
| # MODEL | ||
| ################################################################################ | ||
| function DP.InfiniteGDPModel(args...; kwargs...) | ||
| return DP.GDPModel{ | ||
| InfiniteOpt.InfiniteModel, | ||
| InfiniteOpt.GeneralVariableRef, | ||
| InfiniteOpt.InfOptConstraintRef | ||
| }(args...; kwargs...) | ||
| end | ||
|
|
||
| function DP.collect_all_vars(model::InfiniteOpt.InfiniteModel) | ||
| vars = JuMP.all_variables(model) | ||
| derivs = InfiniteOpt.all_derivatives(model) | ||
| return append!(vars, derivs) | ||
| end | ||
|
|
||
| ################################################################################ | ||
| # VARIABLES | ||
| ################################################################################ | ||
| DP.InfiniteLogical(prefs...) = DP.Logical(InfiniteOpt.Infinite(prefs...)) | ||
|
|
||
| _is_parameter(vref::InfiniteOpt.GeneralVariableRef) = | ||
| _is_parameter(InfiniteOpt.dispatch_variable_ref(vref)) | ||
| _is_parameter(::InfiniteOpt.DependentParameterRef) = true | ||
| _is_parameter(::InfiniteOpt.IndependentParameterRef) = true | ||
| _is_parameter(::InfiniteOpt.ParameterFunctionRef) = true | ||
| _is_parameter(::InfiniteOpt.FiniteParameterRef) = true | ||
| _is_parameter(::Any) = false | ||
|
|
||
| function DP.requires_disaggregation(vref::InfiniteOpt.GeneralVariableRef) | ||
| return !_is_parameter(vref) | ||
| end | ||
|
|
||
| function DP.VariableProperties(vref::InfiniteOpt.GeneralVariableRef) | ||
| info = DP.get_variable_info(vref) | ||
| name = JuMP.name(vref) | ||
| set = nothing | ||
| prefs = InfiniteOpt.parameter_refs(vref) | ||
| var_type = !isempty(prefs) ? InfiniteOpt.Infinite(prefs...) : nothing | ||
| return DP.VariableProperties(info, name, set, var_type) | ||
| end | ||
|
|
||
| # Extract parameter refs from expression and return VariableProperties with Infinite type | ||
| function DP.VariableProperties( | ||
| expr::JuMP.GenericAffExpr{C, InfiniteOpt.GeneralVariableRef} | ||
| ) where C | ||
| prefs = InfiniteOpt.parameter_refs(expr) | ||
| info = DP._free_variable_info() | ||
| var_type = !isempty(prefs) ? InfiniteOpt.Infinite(prefs...) : nothing | ||
| return DP.VariableProperties(info, "", nothing, var_type) | ||
| end | ||
|
|
||
| function DP.VariableProperties( | ||
| expr::JuMP.GenericQuadExpr{C, InfiniteOpt.GeneralVariableRef} | ||
| ) where C | ||
| prefs = InfiniteOpt.parameter_refs(expr) | ||
| info = DP._free_variable_info() | ||
| var_type = !isempty(prefs) ? InfiniteOpt.Infinite(prefs...) : nothing | ||
| return DP.VariableProperties(info, "", nothing, var_type) | ||
| end | ||
|
|
||
| function DP.VariableProperties( | ||
| exprs::Vector{<:Union{ | ||
| InfiniteOpt.GeneralVariableRef, | ||
| JuMP.GenericAffExpr{<:Any, InfiniteOpt.GeneralVariableRef}, | ||
| JuMP.GenericQuadExpr{<:Any, InfiniteOpt.GeneralVariableRef} | ||
| }} | ||
| ) | ||
| all_prefs = Set{InfiniteOpt.GeneralVariableRef}() | ||
| for expr in exprs | ||
| for pref in InfiniteOpt.parameter_refs(expr) | ||
| push!(all_prefs, pref) | ||
| end | ||
| end | ||
| prefs = Tuple(all_prefs) | ||
| info = DP._free_variable_info() | ||
| var_type = !isempty(prefs) ? InfiniteOpt.Infinite(prefs...) : nothing | ||
| return DP.VariableProperties(info, "", nothing, var_type) | ||
| end | ||
|
|
||
| function JuMP.value(vref::DP.LogicalVariableRef{InfiniteOpt.InfiniteModel}) | ||
| return JuMP.value(DP.binary_variable(vref)) .>= 0.5 | ||
| end | ||
|
|
||
| ################################################################################ | ||
| # CONSTRAINTS | ||
| ################################################################################ | ||
| function JuMP.add_constraint( | ||
| model::InfiniteOpt.InfiniteModel, | ||
| c::JuMP.VectorConstraint{F, S}, | ||
| name::String = "" | ||
| ) where {F, S <: DP.AbstractCardinalitySet} | ||
| return DP._add_cardinality_constraint(model, c, name) | ||
| end | ||
|
|
||
| function JuMP.add_constraint( | ||
| model::M, | ||
| c::JuMP.ScalarConstraint{DP._LogicalExpr{M}, S}, | ||
| name::String = "" | ||
| ) where {S, M <: InfiniteOpt.InfiniteModel} | ||
| return DP._add_logical_constraint(model, c, name) | ||
| end | ||
|
|
||
| function JuMP.add_constraint( | ||
| model::M, | ||
| c::JuMP.ScalarConstraint{DP.LogicalVariableRef{M}, S}, | ||
| name::String = "" | ||
| ) where {M <: InfiniteOpt.InfiniteModel, S} | ||
| error("Cannot define constraint on single logical variable, use `fix` instead.") | ||
| end | ||
|
|
||
| function JuMP.add_constraint( | ||
| model::M, | ||
| c::JuMP.ScalarConstraint{ | ||
| JuMP.GenericAffExpr{C, DP.LogicalVariableRef{M}}, S | ||
| }, | ||
| name::String = "" | ||
| ) where {M <: InfiniteOpt.InfiniteModel, S, C} | ||
| error("Cannot add, subtract, or multiply with logical variables.") | ||
| end | ||
|
|
||
| function JuMP.add_constraint( | ||
| model::M, | ||
| c::JuMP.ScalarConstraint{ | ||
| JuMP.GenericQuadExpr{C, DP.LogicalVariableRef{M}}, S | ||
| }, | ||
| name::String = "" | ||
| ) where {M <: InfiniteOpt.InfiniteModel, S, C} | ||
| error("Cannot add, subtract, or multiply with logical variables.") | ||
| end | ||
|
|
||
| ################################################################################ | ||
| # METHODS | ||
| ################################################################################ | ||
| function DP.get_constant( | ||
| expr::JuMP.GenericAffExpr{T, InfiniteOpt.GeneralVariableRef} | ||
| ) where {T} | ||
| constant = JuMP.constant(expr) | ||
| param_expr = zero(typeof(expr)) | ||
| for (var, coeff) in expr.terms | ||
| if _is_parameter(var) | ||
| JuMP.add_to_expression!(param_expr, coeff, var) | ||
| end | ||
| end | ||
| return constant + param_expr | ||
| end | ||
|
|
||
| function DP.disaggregate_expression( | ||
| model::M, | ||
| aff::JuMP.GenericAffExpr, | ||
| bvref::Union{JuMP.AbstractVariableRef, JuMP.GenericAffExpr}, | ||
| method::DP._Hull | ||
| ) where {M <: InfiniteOpt.InfiniteModel} | ||
| terms = Any[aff.constant * bvref] | ||
| for (vref, coeff) in aff.terms | ||
| if JuMP.is_binary(vref) | ||
| push!(terms, coeff * vref) | ||
| elseif vref isa InfiniteOpt.GeneralVariableRef && _is_parameter(vref) | ||
| push!(terms, coeff * vref * bvref) | ||
| elseif !haskey(method.disjunct_variables, (vref, bvref)) | ||
| push!(terms, coeff * vref) | ||
| else | ||
| dvref = method.disjunct_variables[vref, bvref] | ||
| push!(terms, coeff * dvref) | ||
| end | ||
| end | ||
| return JuMP.@expression(model, sum(terms)) | ||
| end | ||
|
|
||
| ################################################################################ | ||
| # ERROR MESSAGES | ||
| ################################################################################ | ||
| function DP.reformulate_model(::InfiniteOpt.InfiniteModel, ::DP.MBM) | ||
| error("The `MBM` method is not supported for `InfiniteModel`." * | ||
| "Please use `BigM`, `Hull`, `Indicator`, or `PSplit` instead.") | ||
| end | ||
|
|
||
| function DP.reformulate_model(::InfiniteOpt.InfiniteModel, ::DP.cutting_planes) | ||
| error("The `cutting_planes` method is not supported for `InfiniteModel`." * | ||
| "Please use `BigM`, `Hull`, `Indicator`, or `PSplit` instead.") | ||
| end | ||
|
|
||
| end | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| """ | ||
| InfiniteGDPModel(args...; kwargs...) | ||
|
|
||
| Creates an `InfiniteOpt.InfiniteModel` that is compatible with the | ||
| capabiltiies provided by DisjunctiveProgramming.jl. This requires | ||
| that InfiniteOpt be imported first. | ||
|
|
||
| **Example** | ||
| ```julia | ||
| julia> using DisjunctiveProgramming, InfiniteOpt | ||
|
|
||
| julia> InfiniteGDPModel() | ||
|
|
||
| ``` | ||
| """ | ||
| function InfiniteGDPModel end | ||
|
|
||
| """ | ||
| InfiniteLogical(prefs...) | ||
|
|
||
| Allows users to create infinite logical variables. This is a tag | ||
| for the `@variable` macro that is a combination of `InfiniteOpt.Infinite` | ||
| and `DisjunctiveProgramming.Logical`. This requires that InfiniteOpt be | ||
| first imported. | ||
|
|
||
| **Example** | ||
| ```julia | ||
| julia> using DisjunctiveProgramming, InfiniteOpt | ||
|
|
||
| julia> model = InfiniteGDPModel(); | ||
|
|
||
| julia> @infinite_parameter(model, t in [0, 1]); | ||
|
|
||
| julia> @infinite_parameter(model, x[1:2] in [-1, 1]); | ||
|
|
||
| julia> @variable(model, Y, InfiniteLogical(t, x)) # creates Y(t, x) in {True, False} | ||
| Y(t, x) | ||
| ``` | ||
| """ | ||
| function InfiniteLogical end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.