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

Support the JuMP macro operator #197

Merged
merged 4 commits into from
Sep 6, 2024

Conversation

amontoison
Copy link
Member

close #195

Copy link
Contributor

github-actions bot commented Sep 4, 2024

Package name latest stable
NLSProblems.jl
OptimizationProblems.jl

@odow
Copy link
Contributor

odow commented Sep 6, 2024

index a3f36e4..908c38c 100644
--- a/src/utils.jl
+++ b/src/utils.jl
@@ -426,41 +426,46 @@ function parser_MOI(moimodel, index_map, nvar)
 end
 
 # Affine or quadratic, nothing to do
-function _nlp_model(model::Union{Nothing, MOI.Nonlinear.Model}, ::MOI.ModelLike, ::Type, ::Type)
-  return model
-end
+_nlp_model(::MOI.Nonlinear.Model, ::MOI.ModelLike, ::Type, ::Type) = false
 
 function _nlp_model(
-  dest::Union{Nothing, MOI.Nonlinear.Model},
+  dest::MOI.Nonlinear.Model,
   src::MOI.ModelLike,
   F::Type{<:Union{MOI.ScalarNonlinearFunction, MOI.VectorNonlinearFunction}},
   S::Type,
 )
+  has_nonlinear = false
   for ci in MOI.get(src, MOI.ListOfConstraintIndices{F, S}())
-    if isnothing(dest)
-      dest = MOI.Nonlinear.Model()
-    end
     MOI.Nonlinear.add_constraint(
       dest,
       MOI.get(src, MOI.ConstraintFunction(), ci),
       MOI.get(src, MOI.ConstraintSet(), ci),
     )
+    has_nonlinear = true
   end
-  return dest
+  return has_nonlinear
 end
 
-function _nlp_model(model::MOI.ModelLike)
-  nlp_model = nothing
+function _nlp_model(model::MOI.ModelLike)::Union{Nothing,MOI.Nonlinear.Model}
+  nlp_model = MOI.Nonlinear.Model()
+  has_nonlinear = false
+  for attr in MOI.get(model, MOI.ListOfModelAttributesSet())
+    if attr isa MOI.UserDefinedFunction
+      has_nonlinear = true
+      args = MOI.get(model, attr)
+      MOI.Nonlinear.register_operator(nlp_model, attr.name, attr.arity, args...)
+    end
+  end
   for (F, S) in MOI.get(model, MOI.ListOfConstraintTypesPresent())
-    nlp_model = _nlp_model(nlp_model, model, F, S)
+    has_nonlinear |= _nlp_model(nlp_model, model, F, S)
   end
   F = MOI.get(model, MOI.ObjectiveFunctionType())
   if F <: MOI.ScalarNonlinearFunction
-    if isnothing(nlp_model)
-      nlp_model = MOI.Nonlinear.Model()
-    end
-    attr = MOI.ObjectiveFunction{F}()
-    MOI.Nonlinear.set_objective(nlp_model, MOI.get(model, attr))
+    MOI.Nonlinear.set_objective(nlp_model, MOI.get(model, MOI.ObjectiveFunction{F}()))
+    has_nonlinear = true
+  end
+  if !has_nonlinear
+    return nothing
   end
   return nlp_model
 end

Results in

julia> nlp_jump = MathOptNLPModel(hs87())
MathOptNLPModel
  Problem name: Generic
   All variables: ████████████████████ 6      All constraints: ████████████████████ 4     
            free: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                 free: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
           lower: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                lower: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
           upper: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                upper: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
         low/upp: ████████████████████ 6              low/upp: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
           fixed: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                fixed: ████████████████████ 4     
          infeas: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0               infeas: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
            nnzh: (-23.81% sparsity)   26              linear: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
                                                    nonlinear: ████████████████████ 4     
                                                         nnzj: ( 37.50% sparsity)   15    

  Counters:
             obj: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                 grad: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                 cons: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
        cons_lin: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0             cons_nln: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                 jcon: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
           jgrad: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                  jac: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0              jac_lin: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
         jac_nln: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                jprod: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0            jprod_lin: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
       jprod_nln: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0               jtprod: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0           jtprod_lin: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
      jtprod_nln: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                 hess: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0                hprod: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0     
           jhess: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0               jhprod: ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅ 0 

@amontoison amontoison marked this pull request as ready for review September 6, 2024 03:04
Copy link

codecov bot commented Sep 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.06%. Comparing base (c8110ed) to head (0c6f4fe).
Report is 13 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #197      +/-   ##
==========================================
+ Coverage   90.39%   92.06%   +1.66%     
==========================================
  Files           4        4              
  Lines         906      958      +52     
==========================================
+ Hits          819      882      +63     
+ Misses         87       76      -11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@amontoison amontoison merged commit dfa0941 into JuliaSmoothOptimizers:main Sep 6, 2024
19 checks passed
@amontoison amontoison deleted the operator branch September 6, 2024 03:53
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.

Issue with new JuMP operator for problem HS87
2 participants