Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/format_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
shell: julia --color=yes {0}
run: |
using Pkg
Pkg.add(PackageSpec(name="JuliaFormatter", version="1"))
Pkg.add(PackageSpec(name="JuliaFormatter", version="2"))
using JuliaFormatter
format(".", verbose=true)
out = String(read(Cmd(`git diff`)))
Expand Down
10 changes: 5 additions & 5 deletions docs/src/examples/Thermal_Generation_Dispatch_Example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function diff_reverse(model::Model, ϵ::Float64 = 1.0)
perturbation = zeros(I + 1)

## Loop for each primal variable
for i in 1:I+1
for i in 1:(I+1)
## Set the perturbation in the Primal Variables and set the context to Backward
perturbation[i] = ϵ
MOI.set.(model, DiffOpt.ReverseVariablePrimal(), vect_ref, perturbation)
Expand Down Expand Up @@ -156,7 +156,7 @@ end
# Result Primal Values:
Plots.plot(
d,
data_results[1, :, 1:I+1];
data_results[1, :, 1:(I+1)];
title = "Generation by Demand",
label = ["Thermal Generation 1" "Thermal Generation 2" "Thermal Generation 3" "Generation Deficit"],
xlabel = "Demand [unit]",
Expand All @@ -166,7 +166,7 @@ Plots.plot(
# Result Sensitivity Analysis:
Plots.plot(
d,
data_results[1, :, I+2:2*(I+1)];
data_results[1, :, (I+2):(2*(I+1))];
title = "Sensitivity of Generation by Demand",
label = ["T. Gen. 1 Sensitivity" "T. Gen. 2 Sensitivity" "T. Gen. 3 Sensitivity" "Gen. Deficit Sensitivity"],
xlabel = "Demand [unit]",
Expand All @@ -177,7 +177,7 @@ Plots.plot(
# Result Primal Values:
Plots.plot(
d,
data_results[2, :, 1:I+1];
data_results[2, :, 1:(I+1)];
title = "Generation by Demand",
label = ["Thermal Generation 1" "Thermal Generation 2" "Thermal Generation 3" "Generation Deficit"],
xlabel = "Demand [unit]",
Expand All @@ -187,7 +187,7 @@ Plots.plot(
# Result Sensitivity Analysis:
Plots.plot(
d,
data_results[2, :, I+2:2*(I+1)];
data_results[2, :, (I+2):(2*(I+1))];
title = "Sensitivity of Generation by Demand",
label = ["T. Gen. 1 Sensitivity" "T. Gen. 2 Sensitivity" "T. Gen. 3 Sensitivity" "Gen. Deficit Sensitivity"],
xlabel = "Demand [unit]",
Expand Down
10 changes: 5 additions & 5 deletions docs/src/examples/Thermal_Generation_Dispatch_Example_new.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function diff_reverse(model::Model, ϵ::Float64 = 1.0)
dvect = Array{Float64,1}(undef, length(vect_ref))

## Loop for each primal variable
for i in 1:I+1
for i in 1:(I+1)
## Reset the sensitivities of the model
DiffOpt.empty_input_sensitivities!(model)

Expand Down Expand Up @@ -152,7 +152,7 @@ end
# Result Primal Values:
Plots.plot(
d,
data_results[1, :, 1:I+1];
data_results[1, :, 1:(I+1)];
title = "Generation by Demand",
label = ["Thermal Generation 1" "Thermal Generation 2" "Thermal Generation 3" "Generation Deficit"],
xlabel = "Demand [unit]",
Expand All @@ -162,7 +162,7 @@ Plots.plot(
# Result Sensitivity Analysis:
Plots.plot(
d,
data_results[1, :, I+2:2*(I+1)];
data_results[1, :, (I+2):(2*(I+1))];
title = "Sensitivity of Generation by Demand",
label = ["T. Gen. 1 Sensitivity" "T. Gen. 2 Sensitivity" "T. Gen. 3 Sensitivity" "Gen. Deficit Sensitivity"],
xlabel = "Demand [unit]",
Expand All @@ -173,7 +173,7 @@ Plots.plot(
# Result Primal Values:
Plots.plot(
d,
data_results[2, :, 1:I+1];
data_results[2, :, 1:(I+1)];
title = "Generation by Demand",
label = ["Thermal Generation 1" "Thermal Generation 2" "Thermal Generation 3" "Generation Deficit"],
xlabel = "Demand [unit]",
Expand All @@ -183,7 +183,7 @@ Plots.plot(
# Result Sensitivity Analysis:
Plots.plot(
d,
data_results[2, :, I+2:2*(I+1)];
data_results[2, :, (I+2):(2*(I+1))];
title = "Sensitivity of Generation by Demand",
label = ["T. Gen. 1 Sensitivity" "T. Gen. 2 Sensitivity" "T. Gen. 3 Sensitivity" "Gen. Deficit Sensitivity"],
xlabel = "Demand [unit]",
Expand Down
2 changes: 1 addition & 1 deletion docs/src/examples/Thermal_Generation_Dispatch_sweep.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ for (k, d_val) in enumerate(d_range)

@variable(model, d in Parameter(d_val)) # parameter
@variables(model, begin # decisions
0 <= g[i = 1:2] <= G[i]
0 <= g[i=1:2] <= G[i]
φ >= 0
end)

Expand Down
4 changes: 2 additions & 2 deletions docs/src/examples/autotuning-ridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ y = X * w_real + noise * randn(N)
l = N ÷ 2 # test train split

X_train = X[1:l, :]
X_test = X[l+1:N, :]
X_test = X[(l+1):N, :]
y_train = y[1:l]
y_test = y[l+1:N];
y_test = y[(l+1):N];

# ## Defining the regression problem

Expand Down
4 changes: 2 additions & 2 deletions docs/src/examples/autotuning-ridge_new.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ y = X * w_real + noise * randn(N)
l = N ÷ 2 # test train split

X_train = X[1:l, :]
X_test = X[l+1:N, :]
X_test = X[(l+1):N, :]
y_train = y[1:l]
y_test = y[l+1:N];
y_test = y[(l+1):N];

# ## Defining the regression problem

Expand Down
19 changes: 9 additions & 10 deletions docs/src/examples/chainrules_unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ function unit_commitment(
model,
Min,
sum(
(Cp[g] * p[g, t]) + (Cnl[g] * u[g, t]) for g in units,
t in 1:n_periods
(Cp[g] * p[g, t]) + (Cnl[g] * u[g, t]) for
g in units, t in 1:n_periods
),
)

Expand Down Expand Up @@ -282,14 +282,13 @@ function ChainRulesCore.rrule(
dnoload_costs[2] = sum(JuMP.coefficient.(obj, u[2, :]))

## computing derivative wrt constraint constant
dload1_demand =
JuMP.constant.(
MOI.get.(
model,
DiffOpt.ReverseConstraintFunction(),
energy_balance_cons,
)
)
dload1_demand = JuMP.constant.(
MOI.get.(
model,
DiffOpt.ReverseConstraintFunction(),
energy_balance_cons,
),
)
dload2_demand = copy(dload1_demand)
return (dload1_demand, dload2_demand, dgen_costs, dnoload_costs)
end
Expand Down
4 changes: 2 additions & 2 deletions docs/src/examples/chainrules_unit_new.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ function unit_commitment(
model,
Min,
sum(
(Cp[g] * p[g, t]) + (Cnl[g] * u[g, t]) for g in units,
t in 1:n_periods
(Cp[g] * p[g, t]) + (Cnl[g] * u[g, t]) for
g in units, t in 1:n_periods
),
)

Expand Down
4 changes: 2 additions & 2 deletions docs/src/examples/polyhedral_project_new.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ function (polytope::Polytope{N})(
set_silent(model)
@variable(model, x[1:layer_size, 1:batch_size])
@variable(model, y[1:layer_size, 1:batch_size] in Parameter.(y_data))
@variable(model, b[idx = 1:N] in Parameter.(polytope.b[idx]))
@variable(model, b[idx=1:N] in Parameter.(polytope.b[idx]))
@variable(
model,
w[idx = 1:N, i = 1:layer_size] in Parameter(polytope.w[idx][i])
w[idx=1:N, i=1:layer_size] in Parameter(polytope.w[idx][i])
)
@constraint(
model,
Expand Down
2 changes: 1 addition & 1 deletion docs/src/examples/sensitivity-analysis-ridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function fit_ridge(X, Y, alpha = 0.1)
@variable(model, w) # angular coefficient
@variable(model, b) # linear coefficient
## expression defining approximation error
@expression(model, e[i = 1:N], Y[i] - w * X[i] - b)
@expression(model, e[i=1:N], Y[i] - w * X[i] - b)
## objective minimizing squared error and ridge penalty
@objective(model, Min, 1 / N * dot(e, e) + alpha * (w^2))
optimize!(model)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/examples/sensitivity-analysis-ridge_new.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function build_fit_ridge(X_data, Y_data, alpha = 0.1)
@variable(model, X[1:N] in Parameter.(X_data))
@variable(model, Y[1:N] in Parameter.(Y_data))
## expression defining approximation error
@expression(model, e[i = 1:N], Y[i] - w * X[i] - b)
@expression(model, e[i=1:N], Y[i] - w * X[i] - b)
## objective minimizing squared error and ridge penalty
@objective(model, Min, 1 / N * dot(e, e) + α * (w^2))
optimize!(model)
Expand Down
4 changes: 2 additions & 2 deletions src/ConicProgram/ConicProgram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ function DiffOpt.forward_differentiate!(model::Model)
IterativeSolvers.lsqr(M, RHS)
end

du, dv, dw = dz[1:n], dz[n+1:n+m], dz[n+m+1]
du, dv, dw = dz[1:n], dz[(n+1):(n+m)], dz[n+m+1]
model.forw_grad_cache = ForwCache(du, dv, [dw])
end
return nothing
Expand Down Expand Up @@ -439,7 +439,7 @@ function DiffOpt._get_dA(
g = model.back_grad_cache.g
πz = model.back_grad_cache.πz
#return DiffOpt.lazy_combination(-, g, πz, n .+ i, 1:n)
return g[n.+i] * πz[1:n]' - πz[n.+i] * g[1:n]'
return g[n .+ i] * πz[1:n]' - πz[n .+ i] * g[1:n]'
end

function MOI.get(
Expand Down
8 changes: 4 additions & 4 deletions src/NonLinearProgram/NonLinearProgram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ function _inertia_correction(
max_corrections::Int = 50,
) where {T<:Real}
diag_mat = ones(size(M, 1))
diag_mat[num_w+1:num_w+num_cons] .= -1
diag_mat[(num_w+1):(num_w+num_cons)] .= -1
diag_mat = SparseArrays.spdiagm(diag_mat)
J = M + st * diag_mat
K = lu(J; check = false)
Expand Down Expand Up @@ -490,9 +490,9 @@ function _cache_evaluator!(model::Model)
num_w = num_primal + num_slacks
# Create index for dual variables
index_duals = [
num_w+1:num_w+num_constraints
num_w+num_constraints+1:num_w+num_constraints+num_low
num_w+num_constraints+num_low+num_geq+1:num_w+num_constraints+num_low+num_geq+num_up
(num_w+1):(num_w+num_constraints)
(num_w+num_constraints+1):(num_w+num_constraints+num_low)
(num_w+num_constraints+num_low+num_geq+1):(num_w+num_constraints+num_low+num_geq+num_up)
]
cons = sort(collect(keys(form.nlp_index_2_constraint)); by = x -> x.value)

Expand Down
32 changes: 16 additions & 16 deletions src/NonLinearProgram/nlp_utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ function _compute_solution_and_bounds(model::Model; tol = 1e-6)
leq_locations, # Indices of the leq constraints wrt the nlp constraints
geq_locations, # Indices of the geq constraints wrt the nlp constraints
ineq_locations, # Indices of the ineq constraints wrt the nlp constraints
vcat(has_up, collect(num_vars+num_geq+1:num_vars+num_geq+num_leq)), # Indices of variables with upper bounds (both primal and slack)
vcat(has_low, collect(num_vars+1:num_vars+num_geq)), # Indices of variables with lower bounds (both primal and slack)
vcat(has_up, collect((num_vars+num_geq+1):(num_vars+num_geq+num_leq))), # Indices of variables with upper bounds (both primal and slack)
vcat(has_low, collect((num_vars+1):(num_vars+num_geq))), # Indices of variables with lower bounds (both primal and slack)
cons # Vector of the nlp constraints
end

Expand Down Expand Up @@ -371,26 +371,26 @@ function _build_sensitivity_matrices(
)

M[1:len_w, 1:len_w] = W
M[1:len_w, len_w+1:len_w+num_cons] = A'
M[len_w+1:len_w+num_cons, 1:len_w] = A
M[1:len_w, len_w+num_cons+1:len_w+num_cons+num_low] = I_L
M[len_w+num_cons+1:len_w+num_cons+num_low, 1:len_w] = V_L
M[1:len_w, (len_w+1):(len_w+num_cons)] = A'
M[(len_w+1):(len_w+num_cons), 1:len_w] = A
M[1:len_w, (len_w+num_cons+1):(len_w+num_cons+num_low)] = I_L
M[(len_w+num_cons+1):(len_w+num_cons+num_low), 1:len_w] = V_L
M[
len_w+num_cons+1:len_w+num_cons+num_low,
len_w+num_cons+1:len_w+num_cons+num_low,
(len_w+num_cons+1):(len_w+num_cons+num_low),
(len_w+num_cons+1):(len_w+num_cons+num_low),
] = X_lb
M[len_w+num_cons+num_low+1:len_w+num_cons+num_low+num_up, 1:len_w] = V_U
M[(len_w+num_cons+num_low+1):(len_w+num_cons+num_low+num_up), 1:len_w] = V_U
M[
len_w+num_cons+num_low+1:len_w+num_cons+num_low+num_up,
len_w+num_cons+num_low+1:len_w+num_cons+num_low+num_up,
(len_w+num_cons+num_low+1):(len_w+num_cons+num_low+num_up),
(len_w+num_cons+num_low+1):(len_w+num_cons+num_low+num_up),
] = X_ub
M[1:len_w, len_w+num_cons+num_low+1:end] = I_U
M[1:len_w, (len_w+num_cons+num_low+1):end] = I_U

# N matrix
# N = [∇ₓₚL ; ∇ₚC; zeros(num_low + num_up, num_parms)]
N = spzeros(len_w + num_cons + num_low + num_up, num_parms)
N[1:len_w, :] = ∇ₓₚL
N[len_w+1:len_w+num_cons, :] = ∇ₚC
N[(len_w+1):(len_w+num_cons), :] = ∇ₚC

return M, N
end
Expand Down Expand Up @@ -491,10 +491,10 @@ function _compute_sensitivity(model::Model; tol = 1e-6)
num_lower = length(has_low)
_sense_multiplier = _sense_mult(model)
# Duals
∂s[num_w+1:num_w+num_cons, :] *= -_sense_multiplier
∂s[(num_w+1):(num_w+num_cons), :] *= -_sense_multiplier
# Dual bounds lower
∂s[num_w+num_cons+1:num_w+num_cons+num_lower, :] *= _sense_multiplier
∂s[(num_w+num_cons+1):(num_w+num_cons+num_lower), :] *= _sense_multiplier
# Dual bounds upper
∂s[num_w+num_cons+num_lower+1:end, :] *= -_sense_multiplier
∂s[(num_w+num_cons+num_lower+1):end, :] *= -_sense_multiplier
return ∂s
end
8 changes: 4 additions & 4 deletions src/QuadraticProgram/QuadraticProgram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ function DiffOpt.reverse_differentiate!(model::Model)
partial_grads = -solve_system(solver, LHS, RHS, iterative)

dz = partial_grads[1:nv]
dλ = partial_grads[nv+1:nv+nineq]
dν = partial_grads[nv+nineq+1:end]
dλ = partial_grads[(nv+1):(nv+nineq)]
dν = partial_grads[(nv+nineq+1):end]

model.back_grad_cache = ForwardReverseCache(dz, dλ, dν)
end
Expand Down Expand Up @@ -437,8 +437,8 @@ function DiffOpt.forward_differentiate!(model::Model)
solver = model.linear_solver
partial_grads = -solve_system(solver, LHS', RHS, iterative)
dz = partial_grads[1:nv]
dλ = partial_grads[nv+1:nv+length(λ)]
dν = partial_grads[nv+length(λ)+1:end]
dλ = partial_grads[(nv+1):(nv+length(λ))]
dν = partial_grads[(nv+length(λ)+1):end]

model.forw_grad_cache = ForwardReverseCache(dz, dλ, dν)
end
Expand Down
2 changes: 1 addition & 1 deletion test/conic_program.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ function _build_simple_sdp()

@constraint(model, c1, sum(x[i] for i in 1:3) == 4)

@constraint(model, c2[i = 1:3], x[i] ≥ 0)
@constraint(model, c2[i=1:3], x[i] ≥ 0)

@constraint(model, x[1] == 2)

Expand Down
Loading
Loading