Skip to content
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

Add least-squares in objective for detected NLS problems #199

Merged
merged 1 commit into from
Jul 9, 2022
Merged

Conversation

tmigot
Copy link
Member

@tmigot tmigot commented Jul 8, 2022

@paraynaud Extending the script to detect the NLS problems described in JuliaSmoothOptimizers/ExpressionTreeForge.jl#14

In this PR, the objective entry is updated in the Meta for the concerned problems. In a following PR, we will replace the ADNLPModel constructor by an ADNLSModel.

using ExpressionTreeForge
using ADNLPModels, ModelingToolkit
using OptimizationProblems, OptimizationProblems.ADNLPProblems

function get_expr_tree(adnlp :: ADNLPModel; x0 :: Vector{T}=copy(adnlp.meta.x0), kwargs...) where T <: Number
  n = adnlp.meta.nvar
  ModelingToolkit.@variables x[1:n]
  fun = adnlp.f(x)
  ex = ExpressionTreeForge.transform_to_expr_tree(fun)
  return ex, n, x0
end

using ..ExpressionTreeForge.M_power_operator

myand(a::Bool,b::Bool) = a && b

function isnls(nlp)
  ex, n, x0 = get_expr_tree(nlp)
  F_expr = delete_imbricated_plus(ex)
  test_square(expr) = expr.field == Power_operator{Int}(2)
  is_nls = mapreduce(test_square, myand, F_expr) :: Bool
  return is_nls 
end 

problems_names = OptimizationProblems.meta[!,:name] #261 problems
problems_symbols = map(name -> Symbol(name), problems_names)
problems = map( symbol -> getfield(OptimizationProblems.ADNLPProblems, symbol)(), problems_symbols)

res_isnls = []
index_is_nls = []
name_is_nls = []
for (index,problem) in enumerate(problems)
  println(index)
  try
    bool = isnls(problem)
    push!(res_isnls, bool)
    if bool
      push!(index_is_nls, index)
      push!(name_is_nls, problems_names[index])
    end
  catch 
    println("the ", index, "-th $(problems_names[index]) problem is unsupported")
  end 
end

length(res_isnls) #243 supported problems

global cpt_isnls = 0
for (index, is_nls) in enumerate(res_isnls)
  global cpt_isnls += is_nls
end 

name_is_nls # 45 NLS problems

for name in name_is_nls
  lines = readlines("src/Meta/$(name).jl")
  lines = map(line -> replace(line, ":objtype => :other," => ":objtype => :least_squares,"), lines)
  open("src/Meta/$(name).jl", "w") do io
    for line in lines
      println(io, line)
    end
  end
end

@tmigot tmigot requested a review from paraynaud July 8, 2022 21:00
Copy link
Member

@paraynaud paraynaud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got the same 45 problems, so it should be ok!

@codecov
Copy link

codecov bot commented Jul 8, 2022

Codecov Report

Merging #199 (5d1f2bd) into main (66db451) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main     #199   +/-   ##
=======================================
  Coverage   99.89%   99.89%           
=======================================
  Files         745      745           
  Lines        6639     6639           
=======================================
  Hits         6632     6632           
  Misses          7        7           
Impacted Files Coverage Δ
src/Meta/arglina.jl 100.00% <ø> (ø)
src/Meta/arglinb.jl 100.00% <ø> (ø)
src/Meta/bdqrtic.jl 100.00% <ø> (ø)
src/Meta/beale.jl 100.00% <ø> (ø)
src/Meta/brownbs.jl 100.00% <ø> (ø)
src/Meta/brownden.jl 100.00% <ø> (ø)
src/Meta/brybnd.jl 100.00% <ø> (ø)
src/Meta/dixon3dq.jl 100.00% <ø> (ø)
src/Meta/errinros_mod.jl 100.00% <ø> (ø)
src/Meta/freuroth.jl 100.00% <ø> (ø)
... and 35 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 66db451...5d1f2bd. Read the comment docs.

@tmigot tmigot merged commit 090d95e into main Jul 9, 2022
@tmigot tmigot deleted the nls-in-meta branch July 9, 2022 19:50
@tmigot
Copy link
Member Author

tmigot commented Jul 25, 2022

#162

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants