Skip to content

Commit 65631b3

Browse files
format
1 parent db91862 commit 65631b3

File tree

5 files changed

+25
-41
lines changed

5 files changed

+25
-41
lines changed

src/NonLinearProgram/NonLinearProgram.jl

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ function MOI.supports_constraint(
125125
return true
126126
end
127127

128-
function _add_leq_geq(form::Form, idx::MOI.ConstraintIndex, set::MOI.GreaterThan)
128+
function _add_leq_geq(
129+
form::Form,
130+
idx::MOI.ConstraintIndex,
131+
set::MOI.GreaterThan,
132+
)
129133
form.geq_values[idx] = set.lower
130134
return
131135
end
@@ -434,10 +438,7 @@ function _cache_evaluator!(model::Model)
434438
return model.cache
435439
end
436440

437-
function DiffOpt.forward_differentiate!(
438-
model::Model;
439-
tol = 1e-6,
440-
)
441+
function DiffOpt.forward_differentiate!(model::Model; tol = 1e-6)
441442
model.diff_time = @elapsed begin
442443
cache = _cache_evaluator!(model)
443444
form = model.model
@@ -451,10 +452,7 @@ function DiffOpt.forward_differentiate!(
451452
end
452453

453454
# Compute Jacobian
454-
Δs = _compute_sensitivity(
455-
model;
456-
tol = tol,
457-
)
455+
Δs = _compute_sensitivity(model; tol = tol)
458456

459457
# Extract primal and dual sensitivities
460458
primal_Δs = Δs[1:length(model.cache.primal_vars), :] * Δp # Exclude slacks
@@ -468,19 +466,13 @@ function DiffOpt.forward_differentiate!(
468466
return nothing
469467
end
470468

471-
function DiffOpt.reverse_differentiate!(
472-
model::Model;
473-
tol = 1e-6,
474-
)
469+
function DiffOpt.reverse_differentiate!(model::Model; tol = 1e-6)
475470
model.diff_time = @elapsed begin
476471
cache = _cache_evaluator!(model)
477472
form = model.model
478473

479474
# Compute Jacobian
480-
Δs = _compute_sensitivity(
481-
model;
482-
tol = tol,
483-
)
475+
Δs = _compute_sensitivity(model; tol = tol)
484476
num_primal = length(cache.primal_vars)
485477
# Fetch primal sensitivities
486478
Δx = zeros(num_primal)

src/NonLinearProgram/nlp_utilities.jl

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,7 @@ function _compute_derivatives_no_relax(
434434
num_vars = _get_num_primal_vars(model)
435435
num_cons = _get_num_constraints(model)
436436
num_ineq = length(ineq_locations)
437-
K = model.input_cache.factorization(
438-
M,
439-
num_vars + num_ineq,
440-
num_cons
441-
) # Factorization
437+
K = model.input_cache.factorization(M, num_vars + num_ineq, num_cons) # Factorization
442438
if isnothing(K)
443439
return zeros(size(M, 1), size(N, 2)), K, N
444440
end
@@ -457,10 +453,7 @@ _sense_mult(model::Model) = objective_sense(model) == MOI.MIN_SENSE ? 1.0 : -1.0
457453
458454
Compute the sensitivity of the solution given sensitivity of the parameters (Δp).
459455
"""
460-
function _compute_sensitivity(
461-
model::Model;
462-
tol = 1e-6,
463-
)
456+
function _compute_sensitivity(model::Model; tol = 1e-6)
464457
# Solution and bounds
465458
X,
466459
V_L,
@@ -487,7 +480,7 @@ function _compute_sensitivity(
487480
geq_locations,
488481
ineq_locations,
489482
has_up,
490-
has_low
483+
has_low,
491484
)
492485
## Adjust signs based on JuMP convention
493486
num_vars = _get_num_primal_vars(model)

src/diff_opt.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ Lu-factorization. If no inertia correction is needed, it only performs the LU
1919
factorization.
2020
"""
2121
struct LuFactorizationWithInertiaCorrection{T<:Real} <: Function
22-
st::T
22+
st::T
2323
max_corrections::Int
2424
end
25-
function LuFactorizationWithInertiaCorrection(; st::T = 1e-6, max_corrections::Int = 50) where T
25+
function LuFactorizationWithInertiaCorrection(;
26+
st::T = 1e-6,
27+
max_corrections::Int = 50,
28+
) where {T}
2629
return LuFactorizationWithInertiaCorrection{T}(st, max_corrections)
2730
end
2831

2932
function (lu_struct::LuFactorizationWithInertiaCorrection)(
3033
M::SparseArrays.SparseMatrixCSC,
3134
num_w,
32-
num_cons
35+
num_cons,
3336
)
3437
# Factorization
3538
K = SparseArrays.lu(M; check = false)

src/moi_wrapper.jl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -769,11 +769,7 @@ function MOI.set(
769769
return
770770
end
771771

772-
function MOI.set(
773-
model::Optimizer,
774-
::MFactorization,
775-
factorization,
776-
)
772+
function MOI.set(model::Optimizer, ::MFactorization, factorization)
777773
model.input_cache.factorization = factorization
778774
return
779775
end

test/nlp_program.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -720,25 +720,25 @@ function test_changing_factorization()
720720
@test_throws MethodError DiffOpt.forward_differentiate!(m)
721721

722722
# correct type and correct number of arguments
723-
MOI.set(m, DiffOpt.MFactorization(), (M, num_w, num_constraints) -> SparseArrays.lu(M))
723+
MOI.set(
724+
m,
725+
DiffOpt.MFactorization(),
726+
(M, num_w, num_constraints) -> SparseArrays.lu(M),
727+
)
724728

725729
# Compute derivatives
726730
DiffOpt.forward_differentiate!(m)
727731

728732
# Test sensitivities
729733
@test all(
730734
isapprox(
731-
[
732-
MOI.get(m, DiffOpt.ForwardVariablePrimal(), x[i]) for
733-
i in 1:P
734-
],
735+
[MOI.get(m, DiffOpt.ForwardVariablePrimal(), x[i]) for i in 1:P],
735736
[0.1 for _ in 1:P];
736737
atol = 1e-8,
737738
),
738739
)
739740
end
740741

741-
742742
end # module
743743

744744
TestNLPProgram.runtests()

0 commit comments

Comments
 (0)