-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add interface for custom scaling (#152)
- Loading branch information
Showing
5 changed files
with
82 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,71 @@ | ||
|
||
function _set_scaling!(con_scale::AbstractVector, jac::SparseMatrixCOO) | ||
@simd for i in 1:nnz(jac) | ||
row = @inbounds jac.I[i] | ||
@inbounds con_scale[row] = max(con_scale[row], abs(jac.V[i])) | ||
end | ||
end | ||
function _set_scaling!(con_scale::AbstractVector, jac::Matrix) | ||
for row in 1:size(jac, 1) | ||
for col in 1:size(jac, 2) | ||
@inbounds con_scale[row] = max(con_scale[row], abs(jac[row, col])) | ||
end | ||
end | ||
end | ||
|
||
""" | ||
scale_constraints!( | ||
nlp::AbstractNLPModel, | ||
con_scale::AbstractVector, | ||
jac::AbstractMatrix; | ||
max_gradient=1e-8, | ||
) | ||
Compute the scaling of the constraints associated | ||
to the nonlinear model `nlp`. By default, Ipopt's scaling | ||
is applied. The user can write its own function to scale | ||
appropriately any custom `AbstractNLPModel`. | ||
### Notes | ||
This function assumes that the Jacobian `jac` has been evaluated | ||
before calling this function. | ||
""" | ||
function scale_constraints!( | ||
nlp::AbstractNLPModel, | ||
con_scale::AbstractVector, | ||
jac::AbstractMatrix; | ||
max_gradient=1e-8, | ||
) | ||
fill!(con_scale, 0.0) | ||
_set_scaling!(con_scale, jac) | ||
con_scale .= min.(1.0, max_gradient ./ con_scale) | ||
end | ||
|
||
""" | ||
scale_objective( | ||
nlp::AbstractNLPModel, | ||
grad::AbstractVector; | ||
max_gradient=1e-8, | ||
) | ||
Compute the scaling of the objective associated to the | ||
nonlinear model `nlp`. By default, Ipopt's scaling | ||
is applied. The user can write its own function to scale | ||
appropriately the objective of any custom `AbstractNLPModel`. | ||
### Notes | ||
This function assumes that the gradient `gradient` has been evaluated | ||
before calling this function. | ||
""" | ||
function scale_objective( | ||
nlp::AbstractNLPModel, | ||
grad::AbstractVector; | ||
max_gradient=1e-8, | ||
) | ||
return min(1, max_gradient / norm(grad, Inf)) | ||
end | ||
|