Skip to content

Detect NLS pattern #174

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

Merged
merged 5 commits into from
Sep 1, 2024
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
6 changes: 6 additions & 0 deletions src/JSOSuite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ using LinearOperators, NLPModelsModifiers, SolverCore, SolverParameters
# JSO solvers
using JSOSolvers, Percival

@init begin
@require ExpressionTreeForge = "93090adf-0e31-445f-8c8f-44d91f61d7ad" begin
include("parse-functions.jl")
end
end

include("optimizers.jl")
include("selection.jl")
include("solve-model.jl")
Expand Down
18 changes: 18 additions & 0 deletions src/parse-functions.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
isnls(nlp::ADNLPModels.AbstractADNLPModel)
isnls(nlp::MathOptInterface.Nonlinear.Model)
isnls(nlp::JuMP.Model)
isnls(nlp::MathOptNLPModel)

Check if the given model has a nonlinear least squares objective.

The function uses the package ExpressionTreeForge to get the expression tree of the objective function,
then try to detect the least squares pattern. There is no guarantee that this function detects it accurately.
"""
function isnls(nlp)
expr_tree = ExpressionTreeForge.get_expression_tree(nlp)
F_expr = ExpressionTreeForge.extract_element_functions(expr_tree)
test_square(expr) = expr.field == ExpressionTreeForge.M_power_operator.Power_operator{Int}(2)
is_nls = mapreduce(test_square, &, F_expr)::Bool
return is_nls
end
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ADNLPModels = "54578032-b7ea-4c30-94aa-7cbd1cce6c9a"
CaNNOLeS = "5a1c9e79-9c58-5ec0-afc4-3298fdea2875"
DCISolver = "bee2e536-65f6-11e9-3844-e5bb4c9c55c9"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
ExpressionTreeForge = "93090adf-0e31-445f-8c8f-44d91f61d7ad"
FletcherPenaltySolver = "e59f0261-166d-4fee-8bf3-5e50457de5db"
JSOSolvers = "10dff2fc-5484-5881-a0e0-c90441020f8a"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
Expand Down Expand Up @@ -32,6 +33,7 @@ ADNLPModels = "0.8.4"
CaNNOLeS = "0.7"
DCISolver = "0.4"
DataFrames = "1"
ExpressionTreeForge = "^0.1.7"
FletcherPenaltySolver = "0.2"
JSOSolvers = "0.10, 0.11"
JuMP = "1"
Expand Down
23 changes: 23 additions & 0 deletions test/parse-function-test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
problems_names = meta[!, :name][1:50] # we test only the 50 first problems
problems_symbols =
map(name -> OptimizationProblems.ADNLPProblems.eval(Meta.parse(name)), problems_names)

@testset "NLS from NLP" begin
result_list_isnls = Bool[]
for (index, problem) in enumerate(problems_symbols)
try
detect_nls = JSOSuite.isnls(problem())
is_nls = meta[meta.name .== problems_names[index], :objtype][1] == :least_squares
push!(result_list_isnls, detect_nls)
if detect_nls
@test detect_nls == is_nls
@debug "Least squares objective, $index : $problem"
# detect_nls != is_nls && (@debug "$index : $problem")
else
@debug "Not least squares objective, $index : $problem"
end
catch
@debug "the $index-th $(problems_names[index]) problem is unsupported."
end
end
end
8 changes: 6 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ using JuMP, NLPModelsJuMP
using ADNLPModels, NLPModels, NLSProblems, QuadraticModels, OptimizationProblems, SparseMatricesCOO
using JSOSolvers, Percival, SolverCore

meta = OptimizationProblems.meta

using ExpressionTreeForge

include("parse-function-test.jl")

@testset "Test not loaded solvers" begin
nlp = ADNLPModel(x -> sum(x), ones(2))

Expand All @@ -30,8 +36,6 @@ end
using CaNNOLeS, DCISolver, FletcherPenaltySolver, NLPModelsIpopt, RipQP
using SolverBenchmark

meta = OptimizationProblems.meta

function test_in_place_solve(nlp, solver_name)
pkg_name = JSOSuite.optimizers[JSOSuite.optimizers.name_solver .== solver_name, :name_pkg][1]
pkg_name = replace(pkg_name, ".jl" => "")
Expand Down
Loading