-
Notifications
You must be signed in to change notification settings - Fork 48
Add chebyquad #414
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
arnavk23
wants to merge
8
commits into
JuliaSmoothOptimizers:main
Choose a base branch
from
arnavk23:add-chebyquad-mainonly
base: main
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
Add chebyquad #414
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ab1dc3f
Fix chebyquad: AD compatibility, multi-precision, and test suite inte…
arnavk23 3e87752
fixing failing tests in chebyquad.jl
arnavk23 40fe6f1
correcting chebyquad.jl to remove type annotations and simplify the code
arnavk23 cf5dee5
Updated chebyquad.jl to avoid T(1)-style casts. The fix now uses one(…
arnavk23 a999c17
Apply suggestions from code review
arnavk23 bdfdc7f
passing checks
arnavk23 4c847c5
removing extra variables from the chebyquad problem
arnavk23 2c3a49e
adding changes to chebyquad.jl
arnavk23 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
Some comments aren't visible on the classic Files Changed page.
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,76 @@ | ||
| export chebyquad | ||
|
|
||
| function chebyquad(; use_nls::Bool = false, kwargs...) | ||
| model = use_nls ? :nls : :nlp | ||
| return chebyquad(Val(model); kwargs...) | ||
| end | ||
|
|
||
| function Cheby(xj, i::Integer) | ||
| # Evaluate T_i(x) via recurrence to avoid domain-branching and AD/tracer issues. | ||
| if i == 0 | ||
| return one(xj) | ||
| elseif i == 1 | ||
| return xj | ||
| end | ||
|
|
||
| tk_minus_1 = one(xj) | ||
| tk = xj | ||
| for _ = 2:i | ||
| tk_plus_1 = 2 * xj * tk - tk_minus_1 | ||
| tk_minus_1 = tk | ||
| tk = tk_plus_1 | ||
| end | ||
| return tk | ||
| end | ||
|
|
||
| function chebyquad( | ||
| ::Val{:nlp}; | ||
| n::Int = default_nvar, | ||
| m::Int = n, | ||
| type::Type{T} = Float64, | ||
| chebyshev = Cheby, | ||
| kwargs..., | ||
| ) where {T} | ||
| m = max(m, n) | ||
| function f(x; n = length(x), m = m, chebyshev = chebyshev) | ||
| return (one(T) / 2) * sum( | ||
| ((one(T) / n) * sum(chebyshev(x[j], 2i) for j = 1:n) + one(T) / ((2i)^2 - 1))^2 for | ||
| i = 1:div(m, 2) | ||
| ) + | ||
| (one(T) / 2) * sum( | ||
| ((one(T) / n) * sum(chebyshev(x[j], 2i - 1) for j = 1:n))^2 for i = 1:div(m + 1, 2) | ||
| ) | ||
| end | ||
| x0 = Vector{T}(undef, n) | ||
| for j = 1:n | ||
| x0[j] = j * one(T) / (n + one(T)) | ||
| end | ||
| return ADNLPModels.ADNLPModel(f, x0, name = "chebyquad"; kwargs...) | ||
| end | ||
|
|
||
| function chebyquad( | ||
| ::Val{:nls}; | ||
| n::Int = default_nvar, | ||
| m::Int = n, | ||
| type::Type{T} = Float64, | ||
| chebyshev = Cheby, | ||
| kwargs..., | ||
| ) where {T} | ||
| m = max(m, n) | ||
| function F!(r, x; n = length(x), m = length(r), chebyshev = chebyshev) | ||
| for i = 1:div(m, 2) | ||
| r[2i] = (one(T) / n) * sum(chebyshev(x[j], 2i) for j = 1:n) + one(T) / ((2i)^2 - 1) | ||
| r[2i - 1] = (one(T) / n) * sum(chebyshev(x[j], 2i - 1) for j = 1:n) | ||
| end | ||
| if mod(m, 2) == 1 | ||
| r[m] = (one(T) / n) * sum(chebyshev(x[j], m) for j = 1:n) | ||
| end | ||
| return r | ||
| end | ||
| x0 = Vector{T}(undef, n) | ||
| for j = 1:n | ||
| x0[j] = j * one(T) / (n + one(T)) | ||
| end | ||
| return ADNLPModels.ADNLSModel!(F!, x0, m, name = "chebyquad-nls"; kwargs...) | ||
| 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,26 @@ | ||
| chebyquad_meta = Dict( | ||
| :nvar => 100, | ||
| :variable_nvar => true, | ||
| :ncon => 0, | ||
| :variable_ncon => false, | ||
| :minimize => true, | ||
| :name => "chebyquad", | ||
| :has_equalities_only => false, | ||
| :has_inequalities_only => false, | ||
| :has_bounds => false, | ||
| :has_fixed_variables => false, | ||
| :objtype => :least_squares, | ||
| :contype => :unconstrained, | ||
| :best_known_lower_bound => -Inf, | ||
| :best_known_upper_bound => 500.0, | ||
| :is_feasible => true, | ||
| :defined_everywhere => missing, | ||
| :origin => :unknown, | ||
| ) | ||
| get_chebyquad_nvar(; n::Integer = default_nvar, kwargs...) = n | ||
| get_chebyquad_ncon(; n::Integer = default_nvar, kwargs...) = 0 | ||
| get_chebyquad_nlin(; n::Integer = default_nvar, kwargs...) = 0 | ||
| get_chebyquad_nnln(; n::Integer = default_nvar, kwargs...) = 0 | ||
| get_chebyquad_nequ(; n::Integer = default_nvar, kwargs...) = 0 | ||
| get_chebyquad_nineq(; n::Integer = default_nvar, kwargs...) = 0 | ||
| get_chebyquad_nls_nequ(; n::Integer = default_nvar, m::Int = n, kwargs...) = max(m, n) |
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,59 @@ | ||
| # | ||
| # The Chebyshev quadrature problem in variable dimension, using the | ||
| # exact formula for the shifted Chebyshev polynomials. This is a | ||
| # nonlinear least-squares problem with n groups. The Hessian is full. | ||
| # | ||
| # Source: Problem 35 in | ||
| # J.J. More', B.S. Garbow and K.E. Hillstrom, | ||
| # "Testing Unconstrained Optimization Software", | ||
| # ACM Transactions on Mathematical Software, vol. 7(1), pp. 17-41, 1981. | ||
| # Also problem 58 in | ||
| # A.R. Buckley, | ||
| # "Test functions for unconstrained minimization", | ||
| # TR 1989CS-3, Mathematics, statistics and computing centre, | ||
| # Dalhousie University, Halifax (CDN), 1989. | ||
| # | ||
| # classification SBR2-AN-V-0 | ||
| export chebyquad | ||
| function chebyquad(args...; n::Int = default_nvar, m::Int = n, kwargs...) | ||
| m = max(m, n) | ||
| nlp = Model() | ||
| x0 = [j/(n + 1) for j = 1:n] | ||
| @variable(nlp, x[j = 1:n], start = x0[j]) | ||
| # Chebyshev polynomial of the first kind, using explicit expression | ||
| @NLobjective( | ||
| nlp, | ||
| Min, | ||
| 0.5 * sum( | ||
| ( | ||
| 1 / n * sum( | ||
| ifelse( | ||
| 2 * x[j] - 1 ≥ 1, | ||
| cosh(2i * acosh(2 * x[j] - 1)), | ||
| ifelse( | ||
| 2 * x[j] - 1 ≤ -1, | ||
| (-1)^(2i) * cosh(2i * acosh(1 - 2 * x[j])), | ||
| cos(2i * acos(2 * x[j] - 1)), | ||
| ), | ||
| ) for j = 1:n | ||
| ) + 1 / ((2i)^2 - 1) | ||
| )^2 for i = 1:div(m, 2) | ||
| ) + | ||
| 0.5 * sum( | ||
| ( | ||
| 1 / n * sum( | ||
| ifelse( | ||
| 2 * x[j] - 1 ≥ 1, | ||
| cosh((2i - 1) * acosh(2 * x[j] - 1)), | ||
| ifelse( | ||
| 2 * x[j] - 1 ≤ -1, | ||
| (-1)^(2i - 1) * cosh((2i - 1) * acosh(1 - 2 * x[j])), | ||
| cos((2i - 1) * acos(2 * x[j] - 1)), | ||
| ), | ||
| ) for j = 1:n | ||
| ) | ||
| )^2 for i = 1:div(m + 1, 2) | ||
| ) | ||
| ) | ||
| return nlp | ||
| end |
Oops, something went wrong.
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.