SciML / Optimization.jl

Mathematical Optimization in Julia. Local, global, gradient-based and derivative-free. Linear, Quadratic, Convex, Mixed-Integer, and Nonlinear Optimization in one simple, fast, and differentiable interface.
https://docs.sciml.ai/Optimization/stable/
MIT License
716 stars 79 forks source link

Constraints being ignored #195

Closed I-Fons closed 2 years ago

I-Fons commented 2 years ago

I am trying to run this code:

using GalacticOptim , Optim , NLopt
myobjfcn(x, p) = (x[1]-p)^2 + p
constraint = (x,p) -> [18 - 5*x[1]]
x0 = ones(1)
p  = 2
optprob = OptimizationFunction(myobjfcn, GalacticOptim.AutoForwardDiff(); cons = constraint)
prob = GalacticOptim.OptimizationProblem(optprob, x0, p,
                                        lb = [0.0] , ub = [4.0],
                                        lcons = [-Inf] , ucons = [0.0])
sol = solve(prob, NLopt.LD_LBFGS())

It executes without throwing any error, but the solution provided is the one corresponding to the unconstrained problem. In other words, it lies outside the feasible region defined by including the constraint. The output I get when I execute the function is:

u: 1-element Vector{Float64}:
 2.0

Finally, the packages I am using, and their versions are listed below:

What am I doing wrong? I have emulated the syntax used in https://galacticoptim.sciml.ai/stable/optimization_packages/optim/#Local-Constraint, but this example seems to have the same problem when I run it, that is, the solution is the same when I use constraints and when I remove them.

Thanks for the attention!

Vaibhavdixit02 commented 2 years ago

The NLopt wrapper doesn't support constraints, you can use the MOI interface with NLopt to make it work. Your code would then be

using GalacticOptim , Optim , NLopt
myobjfcn(x, p) = (x[1]-p)^2 + p
constraint = (x,p) -> [18 - 5*x[1]]
x0 = ones(1)
p  = 2
optprob = OptimizationFunction(myobjfcn, GalacticOptim.AutoForwardDiff(); cons = constraint)
prob = GalacticOptim.OptimizationProblem(optprob, x0, p,
                                        lb = [0.0] , ub = [4.0],
                                        lcons = [-Inf] , ucons = [0.0])
sol = solve(prob,  GalacticOptim.MOI.OptimizerWithAttributes(NLopt.Optimizer, "algorithm" => :LN_BOBYQA))
ChrisRackauckas commented 2 years ago

We should error in this case, for now.

I-Fons commented 2 years ago

Thank you for the correction! However, as you already mention, the execution of those lines keep throwing the following error: ERROR: ArgumentError: invalid NLopt arguments: invalid algorithm for constraints

Vaibhavdixit02 commented 2 years ago

The algorithm doesn't support constraints in that case, try a different one from the docs https://nlopt.readthedocs.io/en/latest/

I-Fons commented 2 years ago

That was useful, thanks a lot!