-
Notifications
You must be signed in to change notification settings - Fork 30
Refactor code base #163
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
Refactor code base #163
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,31 @@ | ||
| name = "NLSolversBase" | ||
| uuid = "d41bc354-129a-5804-8e4c-c37616107c6c" | ||
| version = "7.10.0" | ||
| version = "7.11.0" | ||
|
|
||
| [deps] | ||
| ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" | ||
| DifferentiationInterface = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63" | ||
| Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" | ||
| FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41" | ||
| ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" | ||
| LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
|
|
||
| [compat] | ||
| ADTypes = "1.11.0" | ||
| DifferentiationInterface = "0.6.43, 0.7" | ||
| FiniteDiff = "2.0" | ||
| ForwardDiff = "0.10, 1.0" | ||
| LinearAlgebra = "<0.0.1, 1" | ||
| julia = "1.10" | ||
|
|
||
| [extras] | ||
| ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" | ||
| ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66" | ||
| LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
| OptimTestProblems = "cec144fc-5a64-5bc6-99fb-dde8f63e154c" | ||
| Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
| RecursiveArrayTools = "731186ca-8d62-57ce-b412-fbd966d074cd" | ||
| SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
| Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
|
||
| [targets] | ||
| test = ["ADTypes", "ComponentArrays", "LinearAlgebra", "OptimTestProblems", "Random", "RecursiveArrayTools", "SparseArrays", "Test"] | ||
| test = ["ADTypes", "ComponentArrays", "OptimTestProblems", "Random", "RecursiveArrayTools", "SparseArrays", "Test"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,10 +40,10 @@ function _cb(lx::AbstractArray{Tx}, ux::AbstractArray{Tx}, lc::AbstractVector{Tc | |
| end | ||
|
|
||
| Base.eltype(::Type{ConstraintBounds{T}}) where T = T | ||
| Base.eltype(cb::ConstraintBounds) = eltype(typeof(cb)) | ||
|
|
||
| Base.convert(::Type{ConstraintBounds{T}}, cb::ConstraintBounds{T}) where {T} = cb | ||
| Base.convert(::Type{ConstraintBounds{T}}, cb::ConstraintBounds{S}) where {T,S} = | ||
| ConstraintBounds(cb.nc, cb.eqx, convert(Vector{T}, cb.valx), | ||
| ConstraintBounds{T}(cb.nc, cb.eqx, convert(Vector{T}, cb.valx), | ||
| cb.ineqx, cb.σx, convert(Vector{T}, cb.bx), | ||
| cb.eqc, convert(Vector{T}, cb.valc), cb.ineqc, | ||
| cb.σc, convert(Vector{T}, cb.bc)) | ||
|
|
@@ -80,11 +80,11 @@ function Base.show(io::IO, cb::ConstraintBounds) | |
| indent = " " | ||
| print(io, "ConstraintBounds:") | ||
| print(io, "\n Variables:") | ||
| showeq(io, indent, cb.eqx, cb.valx, 'x', :bracket) | ||
| showineq(io, indent, cb.ineqx, cb.σx, cb.bx, 'x', :bracket) | ||
| showeq(io, indent, cb.eqx, cb.valx, 'x', true) # bracket | ||
| showineq(io, indent, cb.ineqx, cb.σx, cb.bx, 'x', true) # bracket | ||
| print(io, "\n Linear/nonlinear constraints:") | ||
| showeq(io, indent, cb.eqc, cb.valc, 'c', :subscript) | ||
| showineq(io, indent, cb.ineqc, cb.σc, cb.bc, 'c', :subscript) | ||
| showeq(io, indent, cb.eqc, cb.valc, 'c', false) # subscript | ||
| showineq(io, indent, cb.ineqc, cb.σc, cb.bc, 'c', false) # subscript | ||
| nothing | ||
| end | ||
|
|
||
|
|
@@ -177,7 +177,7 @@ function TwiceDifferentiableConstraints(c!, lx::AbstractVector, ux::AbstractVect | |
| # TODO: get rid of this allocation with DI.Cache | ||
| ccache_righttype = zeros(promote_type(T, eltype(_x)), nc) | ||
| c!(ccache_righttype, _x) | ||
| return sum(_λ[i] * ccache[i] for i in eachindex(_λ, ccache)) | ||
| return LinearAlgebra.dot(_λ, ccache_righttype) | ||
| end | ||
|
|
||
| backend = get_adtype(autodiff, chunk) | ||
|
|
@@ -281,17 +281,17 @@ corresponding entry in `ineq`/`σ`/`b`. | |
| T is the element-type of the non-Int outputs | ||
| """ | ||
| function parse_constraints(::Type{T}, l, u) where T | ||
| size(l) == size(u) || throw(DimensionMismatch("l and u must be the same size, got $(size(l)) and $(size(u))")) | ||
| if size(l) != size(u) | ||
| throw(DimensionMismatch(LazyString("Size of lower bounds (", size(l), ") and number of upper bounds (", size(u), ") must be equal."))) | ||
|
Member
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. I see you typically use
Contributor
Author
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. I looked at the implememtation of the macro in base Julia 😅 And noticed all the parsing and string indexing going on there. This caused me to reconsider my (and our) use of the macro - it seems a bit unnecessary to hide all this information by using the macro if you can directly provide it with the constructor, even though it only affects the compilation times.
Member
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. I mean, it's meant as an optimization so why settle :) |
||
| end | ||
| eq, ineq = Int[], Int[] | ||
| σ = Int8[] | ||
| val, b = T[], T[] | ||
| σ = Array{Int8}(undef, 0) | ||
| for i = 1:length(l) | ||
| li, ui = l[i], u[i] | ||
| li <= ui || throw(ArgumentError("l must be smaller than u, got $li, $ui")) | ||
| for (i, (li, ui)) in enumerate(zip(l, u)) | ||
| if li == ui | ||
| push!(eq, i) | ||
| push!(val, ui) | ||
| else | ||
| elseif li < ui | ||
| if isfinite(li) | ||
| push!(ineq, i) | ||
| push!(σ, 1) | ||
|
|
@@ -302,36 +302,55 @@ function parse_constraints(::Type{T}, l, u) where T | |
| push!(σ, -1) | ||
| push!(b, ui) | ||
| end | ||
| else | ||
| throw(ArgumentError(LazyString("Lower bound (", li, ") must not be greater than upper bound (", ui, ")."))) | ||
| end | ||
| end | ||
| eq, val, ineq, σ, b | ||
| end | ||
|
|
||
| ### Compact printing of constraints | ||
|
|
||
| function showeq(io, indent, eq, val, chr, style) | ||
| if !isempty(eq) | ||
| function showeq(io::IO, indent::String, eqs::Vector{Int}, vals::Vector, chr::Char, bracket::Bool) | ||
| if !isempty(eqs) | ||
| print(io, '\n', indent) | ||
| if style == :bracket | ||
| eqstrs = map((i, v)->"$chr[$i]=$v", eq, val) | ||
| if bracket | ||
| for (i, (eq, val)) in enumerate(zip(eqs, vals)) | ||
| if i != 1 | ||
| print(io, ", ") | ||
| end | ||
| print(io, chr, "[", eq, "]=", val) | ||
| end | ||
| else | ||
| eqstrs = map((i, v)->"$(chr)_$i=$v", eq, val) | ||
| for (i, (eq, val)) in enumerate(zip(eqs, vals)) | ||
| if i != 1 | ||
| print(io, ", ") | ||
| end | ||
| print(io, chr, "_", eq, "=", val) | ||
| end | ||
| end | ||
| foreach(s->print(io, s * ", "), eqstrs[1:end - 1]) | ||
| print(io, eqstrs[end]) | ||
| end | ||
| return nothing | ||
| end | ||
|
|
||
| function showineq(io, indent, ineqs, σs, bs, chr, style) | ||
| function showineq(io::IO, indent::String, ineqs::Vector{Int}, σs::Vector{Int8}, bs::Vector, chr::Char, bracket::Bool) | ||
| if !isempty(ineqs) | ||
| print(io, '\n', indent) | ||
| if style == :bracket | ||
| ineqstrs = map((i, σ, b)->"$chr[$i]" * ineqstr(σ, b), ineqs, σs, bs) | ||
| if bracket | ||
| for (i, (ineq, σ, b)) in enumerate(zip(ineqs, σs, bs)) | ||
| if i != 1 | ||
| print(io, ", ") | ||
| end | ||
| print(io, chr, "[", ineq, "]", σ > 0 ? '≥' : '≤', b) | ||
| end | ||
| else | ||
| ineqstrs = map((i, σ, b)->"$(chr)_$i" * ineqstr(σ, b), ineqs, σs, bs) | ||
| for (i, (ineq, σ, b)) in enumerate(zip(ineqs, σs, bs)) | ||
| if i != 1 | ||
| print(io, ", ") | ||
| end | ||
| print(io, chr, "_", ineq, σ > 0 ? '≥' : '≤', b) | ||
| end | ||
| end | ||
| foreach(s->print(io, s * ", "), ineqstrs[1:end - 1]) | ||
| print(io, ineqstrs[end]) | ||
| end | ||
| return nothing | ||
| end | ||
| ineqstr(σ, b) = σ > 0 ? "≥$b" : "≤$b" | ||
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.
@pkofod this seems to be a bug that could explain spurious
IPNewtontest failures.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.
good catch