-
Notifications
You must be signed in to change notification settings - Fork 14
Add JuMP-like API #281
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
joaquimg
wants to merge
4
commits into
master
Choose a base branch
from
jg/jumpapi
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
Add JuMP-like API #281
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,141 @@ | ||
""" | ||
diff_model(optimizer_constructor; with_parametric_opt_interface::Bool = true, with_bridge_type = Float64, with_cache::Bool = true) | ||
|
||
Create a JuMP model with a differentiable optimizer. The optimizer is created | ||
using `optimizer_constructor`. This model will try to select the proper | ||
differentiable optimization method based on the problem structure. | ||
|
||
See also: [`nonlinear_diff_model`](@ref), [`conic_diff_model`](@ref), [`quadratic_diff_model`](@ref). | ||
""" | ||
function diff_model( | ||
optimizer_constructor; | ||
with_parametric_opt_interface::Bool = true, | ||
with_bridge_type = Float64, | ||
with_cache::Bool = true, | ||
) | ||
inner = diff_optimizer( | ||
optimizer_constructor; | ||
with_parametric_opt_interface = with_parametric_opt_interface, | ||
with_bridge_type = with_bridge_type, | ||
with_cache = with_cache, | ||
) | ||
return JuMP.direct_model(inner) | ||
end | ||
|
||
""" | ||
nonlinear_diff_model(optimizer_constructor; with_bridge_type = Float64, with_cache::Bool = true) | ||
|
||
Create a JuMP model with a differentiable optimizer for nonlinear programs. | ||
The optimizer is created using `optimizer_constructor`. | ||
|
||
See also: [`conic_diff_model`](@ref), [`quadratic_diff_model`](@ref), [`diff_model`](@ref). | ||
""" | ||
function nonlinear_diff_model( | ||
optimizer_constructor; | ||
with_bridge_type = Float64, | ||
with_cache::Bool = true, | ||
) | ||
inner = diff_optimizer( | ||
optimizer_constructor; | ||
with_parametric_opt_interface = false, | ||
with_bridge_type = with_bridge_type, | ||
with_cache = with_cache, | ||
) | ||
MOI.set(inner, ModelConstructor(), NonLinearProgram.Model) | ||
return JuMP.direct_model(inner) | ||
end | ||
|
||
""" | ||
conic_diff_model(optimizer_constructor; with_bridge_type = Float64, with_cache::Bool = true) | ||
|
||
Create a JuMP model with a differentiable optimizer for conic programs. | ||
The optimizer is created using `optimizer_constructor`. | ||
|
||
See also: [`nonlinear_diff_model`](@ref), [`quadratic_diff_model`](@ref), [`diff_model`](@ref). | ||
""" | ||
function conic_diff_model( | ||
optimizer_constructor; | ||
with_bridge_type = Float64, | ||
with_cache::Bool = true, | ||
) | ||
inner = diff_optimizer( | ||
optimizer_constructor; | ||
with_parametric_opt_interface = true, | ||
with_bridge_type = with_bridge_type, | ||
with_cache = with_cache, | ||
) | ||
MOI.set(inner, ModelConstructor(), ConicProgram.Model) | ||
return JuMP.direct_model(inner) | ||
end | ||
|
||
""" | ||
quadratic_diff_model(optimizer_constructor; with_bridge_type = Float64, with_cache::Bool = true) | ||
|
||
Create a JuMP model with a differentiable optimizer for quadratic programs. | ||
The optimizer is created using `optimizer_constructor`. | ||
|
||
See also: [`nonlinear_diff_model`](@ref), [`conic_diff_model`](@ref), [`diff_model`](@ref). | ||
""" | ||
function quadratic_diff_model( | ||
optimizer_constructor; | ||
with_bridge_type = Float64, | ||
with_cache::Bool = true, | ||
) | ||
inner = diff_optimizer( | ||
optimizer_constructor; | ||
with_parametric_opt_interface = true, | ||
with_bridge_type = with_bridge_type, | ||
with_cache = with_cache, | ||
) | ||
MOI.set(inner, ModelConstructor(), QuadraticProgram.Model) | ||
return JuMP.direct_model(inner) | ||
end | ||
|
||
""" | ||
set_forward_parameter(model::JuMP.Model, variable::JuMP.VariableRef, value::Number) | ||
|
||
Set the value of a parameter input sensitivity for forward mode. | ||
""" | ||
function set_forward_parameter( | ||
model::JuMP.Model, | ||
variable::JuMP.VariableRef, | ||
value::Number, | ||
) | ||
return MOI.set( | ||
model, | ||
ForwardConstraintSet(), | ||
ParameterRef(variable), | ||
Parameter(value), | ||
) | ||
end | ||
|
||
""" | ||
get_reverse_parameter(model::JuMP.Model, variable::JuMP.VariableRef) | ||
|
||
Get the value of a parameter output sensitivity for reverse mode. | ||
""" | ||
function get_reverse_parameter(model::JuMP.Model, variable::JuMP.VariableRef) | ||
return MOI.get(model, ReverseConstraintSet(), ParameterRef(variable)).value | ||
end | ||
|
||
""" | ||
set_reverse_variable(model::JuMP.Model, variable::JuMP.VariableRef, value::Number) | ||
|
||
Set the value of a variable input sensitivity for reverse mode. | ||
""" | ||
function set_reverse_variable( | ||
model::JuMP.Model, | ||
variable::JuMP.VariableRef, | ||
value::Number, | ||
) | ||
return MOI.set(model, ReverseVariablePrimal(), variable, value) | ||
end | ||
|
||
""" | ||
get_forward_variable(model::JuMP.Model, variable::JuMP.VariableRef) | ||
|
||
Get the value of a variable output sensitivity for forward mode. | ||
""" | ||
function get_forward_variable(model::JuMP.Model, variable::JuMP.VariableRef) | ||
return MOI.get(model, ForwardVariablePrimal(), variable) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# Copyright (c) 2020: Akshay Sharma and contributors | ||
# | ||
# Use of this source code is governed by an MIT-style license that can be found | ||
# in the LICENSE.md file or at https://opensource.org/licenses/MIT. | ||
|
||
module TestJuMPWrapper | ||
|
||
using Test | ||
using JuMP | ||
import DiffOpt | ||
import HiGHS | ||
import Ipopt | ||
import SCS | ||
import MathOptInterface as MOI | ||
|
||
const ATOL = 1e-3 | ||
const RTOL = 1e-3 | ||
|
||
function runtests() | ||
for name in names(@__MODULE__; all = true) | ||
if startswith("$name", "test_") | ||
@testset "$(name)" begin | ||
getfield(@__MODULE__, name)() | ||
end | ||
end | ||
end | ||
return | ||
end | ||
|
||
function test_jump_api() | ||
for (MODEL, SOLVER) in [ | ||
(DiffOpt.diff_model, Ipopt.Optimizer), | ||
(DiffOpt.quadratic_diff_model, HiGHS.Optimizer), | ||
(DiffOpt.quadratic_diff_model, SCS.Optimizer), | ||
(DiffOpt.quadratic_diff_model, Ipopt.Optimizer), | ||
# (DiffOpt.conic_diff_model, HiGHS.Optimizer), | ||
# (DiffOpt.conic_diff_model, SCS.Optimizer), # conicmodel has a issue with sign | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need at least one of the three to be enabled otherwise it's not tested There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, |
||
# (DiffOpt.conic_diff_model, Ipopt.Optimizer), | ||
# (DiffOpt.nonlinear_diff_model, HiGHS.Optimizer), # SQF ctr not supported? | ||
# (DiffOpt.nonlinear_diff_model, SCS.Optimizer), # returns zero for sensitivity | ||
(DiffOpt.nonlinear_diff_model, Ipopt.Optimizer), | ||
], | ||
ineq in [true, false], | ||
min in [true, false], | ||
flip in [true, false] | ||
|
||
@testset "$(MODEL) with: $(SOLVER), $(ineq ? "ineqs" : "eqs"), $(min ? "Min" : "Max"), $(flip ? "geq" : "leq")" begin | ||
model = MODEL(SOLVER) | ||
set_silent(model) | ||
|
||
p_val = 4.0 | ||
pc_val = 2.0 | ||
@variable(model, x) | ||
@variable(model, p in Parameter(p_val)) | ||
@variable(model, pc in Parameter(pc_val)) | ||
if ineq | ||
if !flip | ||
cons = @constraint(model, pc * x >= 3 * p) | ||
else | ||
cons = @constraint(model, pc * x <= 3 * p) | ||
end | ||
else | ||
@constraint(model, cons, pc * x == 3 * p) | ||
end | ||
sign = flip ? -1 : 1 | ||
if min | ||
@objective(model, Min, 2x * sign) | ||
else | ||
@objective(model, Max, -2x * sign) | ||
end | ||
optimize!(model) | ||
@test value(x) ≈ 3 * p_val / pc_val atol = ATOL rtol = RTOL | ||
|
||
# the function is | ||
# x(p, pc) = 3p / pc | ||
# hence, | ||
# dx/dp = 3 / pc | ||
# dx/dpc = -3p / pc^2 | ||
|
||
# First, try forward mode AD | ||
|
||
# differentiate w.r.t. p | ||
direction_p = 3.0 | ||
DiffOpt.set_forward_parameter(model, p, direction_p) | ||
DiffOpt.forward_differentiate!(model) | ||
@test DiffOpt.get_forward_variable(model, x) ≈ | ||
direction_p * 3 / pc_val atol = ATOL rtol = RTOL | ||
|
||
# update p and pc | ||
p_val = 2.0 | ||
pc_val = 6.0 | ||
set_parameter_value(p, p_val) | ||
set_parameter_value(pc, pc_val) | ||
# re-optimize | ||
optimize!(model) | ||
# check solution | ||
@test value(x) ≈ 3 * p_val / pc_val atol = ATOL rtol = RTOL | ||
|
||
# stop differentiating with respect to p | ||
DiffOpt.empty_input_sensitivities!(model) | ||
# differentiate w.r.t. pc | ||
direction_pc = 10.0 | ||
DiffOpt.set_forward_parameter(model, pc, direction_pc) | ||
DiffOpt.forward_differentiate!(model) | ||
@test DiffOpt.get_forward_variable(model, x) ≈ | ||
-direction_pc * 3 * p_val / pc_val^2 atol = ATOL rtol = RTOL | ||
|
||
# always a good practice to clear previously set sensitivities | ||
DiffOpt.empty_input_sensitivities!(model) | ||
# Now, reverse model AD | ||
direction_x = 10.0 | ||
DiffOpt.set_reverse_variable(model, x, direction_x) | ||
DiffOpt.reverse_differentiate!(model) | ||
@test DiffOpt.get_reverse_parameter(model, p) ≈ | ||
direction_x * 3 / pc_val atol = ATOL rtol = RTOL | ||
@test DiffOpt.get_reverse_parameter(model, pc) ≈ | ||
-direction_x * 3 * p_val / pc_val^2 atol = ATOL rtol = RTOL | ||
end | ||
end | ||
end | ||
|
||
end # module | ||
|
||
TestJuMPWrapper.runtests() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add comment on why this is false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, I might re rewrite some of the internals to try to ger rid of this