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
688 stars 75 forks source link

Is there currently a feasible way to use NamedTuple or ComponentArray as x0 for Optimization.jl #736

Closed chooron closed 2 weeks ago

chooron commented 2 months ago

Question❓

The parameters given in the problem I need to optimize are of type namedtuple, for example: (model_1=(a=1.0, b=2.0), model_2=(a=2.0,b=3.0),

The parameters given in the problem I need to optimize are of type namedtuple, for example. But from the tutorial, x0, lb and ub are all Vector types. When I use namedtuple as x0, lb, ub, the code does not work:

using Optimization

rosenbrock(x, p) = (p[1] - x[:a])^2 + p[2] * (x[:b] - x[:a]^2)^2
x0 = (a=0.0, b=0.0)
p = [1.0, 100.0]

using OptimizationBBO
prob = OptimizationProblem(rosenbrock, x0, p, lb=(a=-1.0, b=-1.0), ub=(a=1.0, b=1.0))
sol = solve(prob, BBO_adaptive_de_rand_1_bin_radiuslimited())

Also I tried ComponentArray but it still doesn't seem to work:

using Optimization
using ComponentArrays

rosenbrock(x, p) = (p[1] - x[:a])^2 + p[2] * (x[:b] - x[:a]^2)^2
x0 = ComponentVector(a=0.0, b=0.0)
p = [1.0, 100.0]

using OptimizationBBO
prob = OptimizationProblem(rosenbrock, x0, p, lb=ComponentVector(a=-1.0, b=-1.0), ub=ComponentVector(a=1.0, b=1.0))
sol = solve(prob, BBO_adaptive_de_rand_1_bin_radiuslimited())
ChrisRackauckas commented 2 months ago

Yes, it works depending on the solver. The issue is that the solver you chose only allows vectors.