forked from jump-dev/DiffOpt.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
(WIP) Test script sIPOPT #1
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
andrewrosemberg
wants to merge
109
commits into
master
Choose a base branch
from
ar/sipopt
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 6 commits
Commits
Show all changes
109 commits
Select commit
Hold shift + click to select a range
4b397a6
working script sipopt
andrewrosemberg 8098c3e
add todo Np
andrewrosemberg 1ac01c4
test dense hessian
andrewrosemberg ac8a239
add dense jacobian
andrewrosemberg 188ab6f
add parameters to the calculations
andrewrosemberg 2b30d5d
add overload script (wip)
andrewrosemberg 540b792
start nlp change
andrewrosemberg a9450fa
add full hessian and jacobian
andrewrosemberg 71879ba
first try working code
andrewrosemberg c175d52
comment
andrewrosemberg c026c99
update code
andrewrosemberg d163dfa
add comments
andrewrosemberg 223b4b6
add TODO tests
andrewrosemberg 7f05795
add todo slack variables
andrewrosemberg 7caec85
add slack variables
andrewrosemberg 983c182
add comments
andrewrosemberg cd6d518
adding tests
andrewrosemberg 97e5379
add hessian and jacobian test
andrewrosemberg 41672ce
start adding tests derivatives
andrewrosemberg a8bae3a
working derivatives
andrewrosemberg 17462cc
add nominal derivatives test
andrewrosemberg 0aadfe1
intermdetate solution
andrewrosemberg 25dc222
add upperbound
andrewrosemberg 0ef56d8
all tests passing
andrewrosemberg f00931f
fix type
andrewrosemberg f593afa
start adding approximation tests
andrewrosemberg 2a97799
fix sign
andrewrosemberg 52f075b
update signs
andrewrosemberg 6b567b5
update solution structure for test
andrewrosemberg 55f9b64
debug tests
andrewrosemberg 9b00e39
update debug
andrewrosemberg c695c52
fix signs
andrewrosemberg 9b0f72f
fix signs
andrewrosemberg 2f9bc7f
fix typos
andrewrosemberg ebe789c
adapt signs to obj sense
andrewrosemberg 4762fb0
add comments
andrewrosemberg 18e8bf8
debug fix and relax
andrewrosemberg 02592a9
update tests
andrewrosemberg 763d0c0
use sparse matrix
andrewrosemberg 41b88d9
add tests with variable constrained by equalto
andrewrosemberg 7d91082
allow variable fix
andrewrosemberg 2538e9c
add more tests
andrewrosemberg a3f8e1b
start test bilevel
andrewrosemberg eabce33
pass all to sparse
andrewrosemberg c2d73cd
add bilevel test
andrewrosemberg dda6469
update fix tolerance test
andrewrosemberg bb753ff
increase tests
andrewrosemberg 93d3981
more unit-tests
andrewrosemberg e20f4a8
update slack approximation
andrewrosemberg 7ddb3d2
fix order sign change
andrewrosemberg 7b3e591
update tests
andrewrosemberg 839158d
add singular example and create bilevel file
andrewrosemberg 3fdfc0f
add singular correction
andrewrosemberg c474dda
add non-linear bilevel test
andrewrosemberg 276b6ef
add strategic bidding
andrewrosemberg 873e080
update test
andrewrosemberg 5032c45
add opf
andrewrosemberg 66dfe60
add ac stregic bidding
andrewrosemberg 3cdc51c
update test
andrewrosemberg a32fd70
improve opf example
andrewrosemberg e3d88ab
update restoration
andrewrosemberg 9b27e3b
update script example sb ac
andrewrosemberg 6346d33
update code working
andrewrosemberg 1920286
update example
andrewrosemberg c74e930
update
andrewrosemberg f446840
add example
andrewrosemberg 544aa0b
fix help
andrewrosemberg c3a1e97
update
andrewrosemberg cff5ad0
add moonlander example
andrewrosemberg b3f4d94
add moonlander test
andrewrosemberg 9abb194
update tests
andrewrosemberg 533d3a4
start fix sign
andrewrosemberg 28c40b4
update code
andrewrosemberg 478e7db
separate tests
andrewrosemberg 4eebfda
update code
andrewrosemberg 0c0f63b
update debug
andrewrosemberg a043847
fix bug
andrewrosemberg 95d4c67
Merge pull request #3 from andrewrosemberg/ar/sipop2
andrewrosemberg a9c3c8d
update bidding
andrewrosemberg ed20b3c
update comparison
a5deb41
update results
a748520
udapte
1803fbb
fix typo
7000113
update
3203209
update
425cf56
update
bc87a86
update
68e7d49
update
0eba400
try distributed
5d98548
update
1661f37
update
cd90ce4
update
ccbba5b
update
11a5055
update
3309208
update
84585a5
update
56655d3
update
7fd09fd
update
7ffd2f8
update
b0aca2f
update
145f8a9
update new solvers
a83712b
update final
fc0a3ab
update
0cf8892
update
d082f73
update
cf07259
update
68cdf17
update
e022547
update
7a343dd
time tables
andrewrosemberg 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 |
---|---|---|
@@ -0,0 +1,282 @@ | ||
using MathOptInterface | ||
import MathOptInterface: Evaluator, _forward_eval_ϵ | ||
const MOI = MathOptInterface | ||
using MathOptInterface.Nonlinear | ||
|
||
mutable struct MOI.Evaluator{B} <: MOI.AbstractMathOptInterface.NLPEvaluator | ||
# The internal datastructure. | ||
model::Model | ||
# The abstract-differentiation backend | ||
backend::B | ||
# ordered_constraints is needed because `OrderedDict` doesn't support | ||
# looking up a key by the linear index. | ||
ordered_constraints::Vector{ConstraintIndex} | ||
# Storage for the NLPBlockDual, so that we can query the dual of individual | ||
# constraints without needing to query the full vector each time. | ||
constraint_dual::Vector{Float64} | ||
# Timers | ||
initialize_timer::Float64 | ||
eval_objective_timer::Float64 | ||
eval_constraint_timer::Float64 | ||
eval_objective_gradient_timer::Float64 | ||
eval_constraint_gradient_timer::Float64 | ||
eval_constraint_jacobian_timer::Float64 | ||
eval_hessian_objective_timer::Float64 | ||
eval_hessian_constraint_timer::Float64 | ||
eval_hessian_lagrangian_timer::Float64 | ||
parameters_as_variables::Bool | ||
|
||
function MOI.Evaluator( | ||
model::Model, | ||
backend::B = nothing, | ||
) where {B<:Union{Nothing,MOI.AbstractMathOptInterface.NLPEvaluator}} | ||
return new{B}( | ||
model, | ||
backend, | ||
ConstraintIndex[], | ||
Float64[], | ||
0.0, | ||
0.0, | ||
0.0, | ||
0.0, | ||
0.0, | ||
0.0, | ||
0.0, | ||
0.0, | ||
0.0, | ||
false | ||
) | ||
end | ||
end | ||
|
||
function Evaluator{B}( | ||
# The internal datastructure. | ||
model::Model, | ||
# The abstract-differentiation backend | ||
backend::B, | ||
# ordered_constraints is needed because `OrderedDict` doesn't support | ||
# looking up a key by the linear index. | ||
ordered_constraints::Vector{ConstraintIndex}, | ||
# Storage for the NLPBlockDual, so that we can query the dual of individual | ||
# constraints without needing to query the full vector each time. | ||
constraint_dual::Vector{Float64}, | ||
# Timers | ||
initialize_timer::Float64, | ||
eval_objective_timer::Float64, | ||
eval_constraint_timer::Float64, | ||
eval_objective_gradient_timer::Float64, | ||
eval_constraint_gradient_timer::Float64, | ||
eval_constraint_jacobian_timer::Float64, | ||
eval_hessian_objective_timer::Float64, | ||
eval_hessian_constraint_timer::Float64, | ||
eval_hessian_lagrangian_timer::Float64, | ||
) where {B<:Union{Nothing,MOI.AbstractMathOptInterface.NLPEvaluator}} | ||
return Evaluator{B}( | ||
# The internal datastructure. | ||
model::Model, | ||
# The abstract-differentiation backend | ||
backend::B, | ||
# ordered_constraints is needed because `OrderedDict` doesn't support | ||
# looking up a key by the linear index. | ||
ordered_constraints::Vector{ConstraintIndex}, | ||
# Storage for the NLPBlockDual, so that we can query the dual of individual | ||
# constraints without needing to query the full vector each time. | ||
constraint_dual::Vector{Float64}, | ||
# Timers | ||
initialize_timer::Float64, | ||
eval_objective_timer::Float64, | ||
eval_constraint_timer::Float64, | ||
eval_objective_gradient_timer::Float64, | ||
eval_constraint_gradient_timer::Float64, | ||
eval_constraint_jacobian_timer::Float64, | ||
eval_hessian_objective_timer::Float64, | ||
eval_hessian_constraint_timer::Float64, | ||
eval_hessian_lagrangian_timer::Float64, | ||
false | ||
) | ||
end | ||
|
||
|
||
function MOI.Nonlinear.ReverseAD._forward_eval_ϵ( | ||
d::MathOptInterface.Nonlinear.NLPEvaluator, | ||
ex::Union{_FunctionStorage,_SubexpressionStorage}, | ||
storage_ϵ::AbstractVector{ForwardDiff.Partials{N,T}}, | ||
partials_storage_ϵ::AbstractVector{ForwardDiff.Partials{N,T}}, | ||
x_values_ϵ, | ||
subexpression_values_ϵ, | ||
user_operators::Nonlinear.OperatorRegistry, | ||
) where {N,T} | ||
@assert length(storage_ϵ) >= length(ex.nodes) | ||
@assert length(partials_storage_ϵ) >= length(ex.nodes) | ||
zero_ϵ = zero(ForwardDiff.Partials{N,T}) | ||
# ex.nodes is already in order such that parents always appear before children | ||
# so a backwards pass through ex.nodes is a forward pass through the tree | ||
children_arr = SparseArrays.rowvals(ex.adj) | ||
for k in length(ex.nodes):-1:1 | ||
node = ex.nodes[k] | ||
partials_storage_ϵ[k] = zero_ϵ | ||
if node.type == Nonlinear.NODE_VARIABLE | ||
storage_ϵ[k] = x_values_ϵ[node.index] | ||
elseif node.type == Nonlinear.NODE_VALUE | ||
storage_ϵ[k] = zero_ϵ | ||
elseif node.type == Nonlinear.NODE_SUBEXPRESSION | ||
storage_ϵ[k] = subexpression_values_ϵ[node.index] | ||
elseif node.type == Nonlinear.NODE_PARAMETER | ||
storage_ϵ[k] = x_values_ϵ[node.index] | ||
# elseif !(d.parameters_as_variables) && node.type == Nonlinear.NODE_PARAMETER | ||
# storage_ϵ[k] = zero_ϵ | ||
else | ||
@assert node.type != Nonlinear.NODE_MOI_VARIABLE | ||
ϵtmp = zero_ϵ | ||
@inbounds children_idx = SparseArrays.nzrange(ex.adj, k) | ||
for c_idx in children_idx | ||
@inbounds ix = children_arr[c_idx] | ||
@inbounds partial = ex.partials_storage[ix] | ||
@inbounds storage_val = storage_ϵ[ix] | ||
# TODO: This "if" statement can take 8% of the hessian | ||
# evaluation time. Find a more efficient way. | ||
if !isfinite(partial) && storage_val == zero_ϵ | ||
continue | ||
end | ||
ϵtmp += storage_val * ex.partials_storage[ix] | ||
end | ||
storage_ϵ[k] = ϵtmp | ||
if node.type == Nonlinear.NODE_CALL_MULTIVARIATE | ||
# TODO(odow): consider how to refactor this into Nonlinear. | ||
op = node.index | ||
n_children = length(children_idx) | ||
if op == 3 # :* | ||
# Lazy approach for now. | ||
anyzero = false | ||
tmp_prod = one(ForwardDiff.Dual{TAG,T,N}) | ||
for c_idx in children_idx | ||
ix = children_arr[c_idx] | ||
sval = ex.forward_storage[ix] | ||
gnum = ForwardDiff.Dual{TAG}(sval, storage_ϵ[ix]) | ||
tmp_prod *= gnum | ||
anyzero = ifelse(sval * sval == zero(T), true, anyzero) | ||
end | ||
# By a quirk of floating-point numbers, we can have | ||
# anyzero == true && ForwardDiff.value(tmp_prod) != zero(T) | ||
if anyzero || n_children <= 2 | ||
for c_idx in children_idx | ||
prod_others = one(ForwardDiff.Dual{TAG,T,N}) | ||
for c_idx2 in children_idx | ||
(c_idx == c_idx2) && continue | ||
ix = children_arr[c_idx2] | ||
gnum = ForwardDiff.Dual{TAG}( | ||
ex.forward_storage[ix], | ||
storage_ϵ[ix], | ||
) | ||
prod_others *= gnum | ||
end | ||
partials_storage_ϵ[children_arr[c_idx]] = | ||
ForwardDiff.partials(prod_others) | ||
end | ||
else | ||
for c_idx in children_idx | ||
ix = children_arr[c_idx] | ||
prod_others = | ||
tmp_prod / ForwardDiff.Dual{TAG}( | ||
ex.forward_storage[ix], | ||
storage_ϵ[ix], | ||
) | ||
partials_storage_ϵ[ix] = | ||
ForwardDiff.partials(prod_others) | ||
end | ||
end | ||
elseif op == 4 # :^ | ||
@assert n_children == 2 | ||
idx1 = first(children_idx) | ||
idx2 = last(children_idx) | ||
@inbounds ix1 = children_arr[idx1] | ||
@inbounds ix2 = children_arr[idx2] | ||
@inbounds base = ex.forward_storage[ix1] | ||
@inbounds base_ϵ = storage_ϵ[ix1] | ||
@inbounds exponent = ex.forward_storage[ix2] | ||
@inbounds exponent_ϵ = storage_ϵ[ix2] | ||
base_gnum = ForwardDiff.Dual{TAG}(base, base_ϵ) | ||
exponent_gnum = ForwardDiff.Dual{TAG}(exponent, exponent_ϵ) | ||
if exponent == 2 | ||
partials_storage_ϵ[ix1] = 2 * base_ϵ | ||
elseif exponent == 1 | ||
partials_storage_ϵ[ix1] = zero_ϵ | ||
else | ||
partials_storage_ϵ[ix1] = ForwardDiff.partials( | ||
exponent_gnum * pow(base_gnum, exponent_gnum - 1), | ||
) | ||
end | ||
result_gnum = ForwardDiff.Dual{TAG}( | ||
ex.forward_storage[k], | ||
storage_ϵ[k], | ||
) | ||
# TODO(odow): fix me to use NaNMath.jl instead | ||
log_base_gnum = base_gnum < 0 ? NaN : log(base_gnum) | ||
partials_storage_ϵ[ix2] = | ||
ForwardDiff.partials(result_gnum * log_base_gnum) | ||
elseif op == 5 # :/ | ||
@assert n_children == 2 | ||
idx1 = first(children_idx) | ||
idx2 = last(children_idx) | ||
@inbounds ix1 = children_arr[idx1] | ||
@inbounds ix2 = children_arr[idx2] | ||
@inbounds numerator = ex.forward_storage[ix1] | ||
@inbounds numerator_ϵ = storage_ϵ[ix1] | ||
@inbounds denominator = ex.forward_storage[ix2] | ||
@inbounds denominator_ϵ = storage_ϵ[ix2] | ||
recip_denominator = | ||
1 / ForwardDiff.Dual{TAG}(denominator, denominator_ϵ) | ||
partials_storage_ϵ[ix1] = | ||
ForwardDiff.partials(recip_denominator) | ||
partials_storage_ϵ[ix2] = ForwardDiff.partials( | ||
-ForwardDiff.Dual{TAG}(numerator, numerator_ϵ) * | ||
recip_denominator * | ||
recip_denominator, | ||
) | ||
elseif op > 6 | ||
f_input = _UnsafeVectorView(d.jac_storage, n_children) | ||
for (i, c) in enumerate(children_idx) | ||
f_input[i] = ex.forward_storage[children_arr[c]] | ||
end | ||
H = _UnsafeLowerTriangularMatrixView( | ||
d.user_output_buffer, | ||
n_children, | ||
) | ||
has_hessian = Nonlinear.eval_multivariate_hessian( | ||
user_operators, | ||
user_operators.multivariate_operators[node.index], | ||
H, | ||
f_input, | ||
) | ||
# This might be `false` if we extend this code to all | ||
# multivariate functions. | ||
@assert has_hessian | ||
for col in 1:n_children | ||
dual = zero(ForwardDiff.Partials{N,T}) | ||
for row in 1:n_children | ||
# Make sure we get the lower-triangular component. | ||
h = row >= col ? H[row, col] : H[col, row] | ||
# Performance optimization: hessians can be quite | ||
# sparse | ||
if !iszero(h) | ||
i = children_arr[children_idx[row]] | ||
dual += h * storage_ϵ[i] | ||
end | ||
end | ||
i = children_arr[children_idx[col]] | ||
partials_storage_ϵ[i] = dual | ||
end | ||
end | ||
elseif node.type == Nonlinear.NODE_CALL_UNIVARIATE | ||
@inbounds child_idx = children_arr[ex.adj.colptr[k]] | ||
f′′ = Nonlinear.eval_univariate_hessian( | ||
user_operators, | ||
user_operators.univariate_operators[node.index], | ||
ex.forward_storage[child_idx], | ||
) | ||
partials_storage_ϵ[child_idx] = f′′ * storage_ϵ[child_idx] | ||
end | ||
end | ||
end | ||
return storage_ϵ[1] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
################################################ | ||
# sIPOPT test | ||
################################################ | ||
|
||
using JuMP | ||
using SparseArrays | ||
using LinearAlgebra | ||
using Ipopt | ||
# using MadNLP | ||
# using KNITRO | ||
|
||
############ | ||
# Test Case | ||
############ | ||
|
||
# Define the problem | ||
model = Model(Ipopt.Optimizer) | ||
|
||
# Parameters | ||
@variable(model, p ∈ MOI.Parameter(1.0)) | ||
@variable(model, p2 ∈ MOI.Parameter(2.0)) | ||
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. Why not |
||
|
||
# Variables | ||
@variable(model, x) | ||
@variable(model, y) | ||
|
||
# Constraints | ||
@constraint(model, con1, y >= p*sin(x)) # NLP Constraint | ||
@constraint(model, con2, x + y == p) | ||
@constraint(model, con3, p2 * x >= 0.1) | ||
@objective(model, Min, (1 - x)^2 + 100 * (y - x^2)^2) # NLP Objective | ||
optimize!(model) | ||
|
||
# Check local optimality | ||
termination_status(model) | ||
|
||
############ | ||
# Retrieve important quantities | ||
############ | ||
|
||
function _dense_hessian(hessian_sparsity, V, n) | ||
I = [i for (i, _) in hessian_sparsity] | ||
J = [j for (_, j) in hessian_sparsity] | ||
raw = SparseArrays.sparse(I, J, V, n, n) | ||
return Matrix( | ||
raw + raw' - | ||
SparseArrays.sparse(LinearAlgebra.diagm(0 => LinearAlgebra.diag(raw))), | ||
) | ||
end | ||
|
||
function _dense_jacobian(jacobian_sparsity, V, m, n) | ||
I = [i for (i, j) in jacobian_sparsity] | ||
J = [j for (i, j) in jacobian_sparsity] | ||
raw = SparseArrays.sparse(I, J, V, m, n) | ||
return Matrix(raw) | ||
end | ||
|
||
# Primal Solution | ||
primal_values = value.([x, y, p, p2]) | ||
dual_values = dual.([con1; con2; con3]) | ||
num_vars = length(primal_values) | ||
num_cons = length(dual_values) | ||
|
||
# `Evaluator`: Object that helps evaluating functions and calculating related values (Hessian, Jacobian, ...) | ||
evaluator = JuMP.MOI.Nonlinear.Evaluator(model.moi_backend.optimizer.model.nlp_model, JuMP.MOI.Nonlinear.SparseReverseMode(), | ||
[ model.moi_backend.model_to_optimizer_map[index(x)], | ||
model.moi_backend.model_to_optimizer_map[index(y)], | ||
model.moi_backend.model_to_optimizer_map[index(p)], | ||
model.moi_backend.model_to_optimizer_map[index(p2)], | ||
] | ||
) | ||
|
||
# Define what we will need to evaluate | ||
MOI.initialize(evaluator, [:Grad, :Jac, :Hess, :JacVec]) | ||
|
||
# Hessian "Symetric" structure values - Placeholder that will be modified to during the evaluation of the hessian | ||
hessian_sparsity = MOI.hessian_lagrangian_structure(evaluator) | ||
W = fill(NaN, length(hessian_sparsity)) | ||
|
||
# Modify H with the values for the hessian of the lagrangian | ||
MOI.eval_hessian_lagrangian(evaluator, W, primal_values, 1.0, dual_values) | ||
W = _dense_hessian(hessian_sparsity, W, num_vars) | ||
|
||
# Jacobian of the constraints Placeholder | ||
jacobian_sparsity = MOI.jacobian_structure(evaluator) | ||
A = zeros(length(jacobian_sparsity)) | ||
|
||
# Evaluate Jacobian | ||
MOI.eval_constraint_jacobian(evaluator, A, primal_values) | ||
A = _dense_jacobian(jacobian_sparsity, A, num_cons, num_vars) | ||
|
||
# TODO: ∇ₓₚL (Partial second derivative of the lagrangian wrt primal solution and parameters) ; | ||
# TODO: ∇ₚC (partial derivative of the equality constraintswith wrt parameters). | ||
|
||
############ | ||
# (WORK IN PROGRESS) - Non working code | ||
|
||
# Calculate Sensitivity | ||
############ | ||
V = diagm(dual_values) | ||
X = diagm(primal_values) | ||
|
||
M = [ | ||
[W A -I]; | ||
[A' 0 0]; | ||
[V 0 X] | ||
] | ||
|
||
N = [∇ₓₚL ; ∇ₚC] | ||
|
||
# sesitivity of the solution (primal-dual) with respect to the parameter | ||
∂s = inv(M) * N |
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.