SciPy.jl
SciPy is a mature Python library that offers a rich family of optimization, root–finding and linear‐programming algorithms. OptimizationSciPy.jl gives access to these routines through the unified Optimization.jl interface just like any native Julia optimizer.
OptimizationSciPy.jl relies on PythonCall. A minimal Python distribution containing SciPy will be installed automatically on first use, so no manual Python set-up is required.
Installation: OptimizationSciPy.jl
import Pkg
Pkg.add("OptimizationSciPy")Methods
Below is a catalogue of the solver families exposed by OptimizationSciPy.jl together with their convenience constructors. All of them accept the usual keyword arguments maxiters, maxtime, abstol, reltol, callback, progress in addition to any SciPy-specific options (passed verbatim via keyword arguments to solve).
Local Optimizer
Derivative-Free
ScipyNelderMead()– Simplex Nelder–Mead algorithmScipyPowell()– Powell search along conjugate directionsScipyCOBYLA()– Linear approximation of constraints (supports nonlinear constraints)
Gradient-Based
ScipyCG()– Non-linear conjugate gradientScipyBFGS()– Quasi-Newton BFGSScipyLBFGSB()– Limited-memory BFGS with simple boundsScipyNewtonCG()– Newton-conjugate gradient (requires Hessian-vector products)ScipyTNC()– Truncated Newton with boundsScipySLSQP()– Sequential least-squares programming (supports constraints)ScipyTrustConstr()– Trust-region method for non-linear constraints
Hessian–Based / Trust-Region
ScipyDogleg(),ScipyTrustNCG(),ScipyTrustKrylov(),ScipyTrustExact()– Trust-region algorithms that optionally use or build Hessian information
Global Optimizer
ScipyDifferentialEvolution()– Differential evolution (requires bounds)ScipyBasinhopping()– Basin-hopping with local searchScipyDualAnnealing()– Dual annealing simulated annealingScipyShgo()– Simplicial homology global optimisation (supports constraints)ScipyDirect()– DeterministicDIRECTalgorithm (requires bounds)ScipyBrute()– Brute-force grid search (requires bounds)
Linear & Mixed-Integer Programming
ScipyLinprog("highs")– LP solvers from the HiGHS project and legacy interior-point/simplex methodsScipyMilp()– Mixed-integer linear programming via HiGHS branch-and-bound
Root Finding & Non-Linear Least Squares (experimental)
Support for ScipyRoot, ScipyRootScalar and ScipyLeastSquares is available for experimental root-finding and non-linear least-squares workflows.
OptimizationSciPy.ScipyMinimize — Type
ScipyMinimize(method::String = "BFGS")Optimizer wrapper for scipy.optimize.minimize using the selected SciPy method.
OptimizationSciPy.ScipyNelderMead — Function
ScipyNelderMead()Convenience constructor for ScipyMinimize("Nelder-Mead").
OptimizationSciPy.ScipyPowell — Function
ScipyPowell()Convenience constructor for ScipyMinimize("Powell").
OptimizationSciPy.ScipyCG — Function
ScipyCG()Convenience constructor for ScipyMinimize("CG").
OptimizationSciPy.ScipyBFGS — Function
ScipyBFGS()Convenience constructor for ScipyMinimize("BFGS").
OptimizationSciPy.ScipyNewtonCG — Function
ScipyNewtonCG()Convenience constructor for ScipyMinimize("Newton-CG").
OptimizationSciPy.ScipyLBFGSB — Function
ScipyLBFGSB()Convenience constructor for ScipyMinimize("L-BFGS-B").
OptimizationSciPy.ScipyTNC — Function
ScipyTNC()Convenience constructor for ScipyMinimize("TNC").
OptimizationSciPy.ScipyCOBYLA — Function
ScipyCOBYLA()Convenience constructor for ScipyMinimize("COBYLA").
OptimizationSciPy.ScipyCOBYQA — Function
ScipyCOBYQA()Convenience constructor for ScipyMinimize("COBYQA").
OptimizationSciPy.ScipySLSQP — Function
ScipySLSQP()Convenience constructor for ScipyMinimize("SLSQP").
OptimizationSciPy.ScipyTrustConstr — Function
ScipyTrustConstr()Convenience constructor for ScipyMinimize("trust-constr").
OptimizationSciPy.ScipyDogleg — Function
ScipyDogleg()Convenience constructor for ScipyMinimize("dogleg").
OptimizationSciPy.ScipyTrustNCG — Function
ScipyTrustNCG()Convenience constructor for ScipyMinimize("trust-ncg").
OptimizationSciPy.ScipyTrustKrylov — Function
ScipyTrustKrylov()Convenience constructor for ScipyMinimize("trust-krylov").
OptimizationSciPy.ScipyTrustExact — Function
ScipyTrustExact()Convenience constructor for ScipyMinimize("trust-exact").
OptimizationSciPy.ScipyMinimizeScalar — Type
ScipyMinimizeScalar(method::String = "brent")Optimizer wrapper for scipy.optimize.minimize_scalar using the selected SciPy method.
OptimizationSciPy.ScipyBrent — Function
ScipyBrent()Convenience constructor for ScipyMinimizeScalar("brent").
OptimizationSciPy.ScipyBounded — Function
ScipyBounded()Convenience constructor for ScipyMinimizeScalar("bounded").
OptimizationSciPy.ScipyGolden — Function
ScipyGolden()Convenience constructor for ScipyMinimizeScalar("golden").
OptimizationSciPy.ScipyLeastSquares — Type
ScipyLeastSquares(; method::String = "trf", loss::String = "linear")Optimizer wrapper for scipy.optimize.least_squares.
OptimizationSciPy.ScipyLeastSquaresTRF — Function
ScipyLeastSquaresTRF()Convenience constructor for ScipyLeastSquares(method = "trf").
OptimizationSciPy.ScipyLeastSquaresDogbox — Function
ScipyLeastSquaresDogbox()Convenience constructor for ScipyLeastSquares(method = "dogbox").
OptimizationSciPy.ScipyLeastSquaresLM — Function
ScipyLeastSquaresLM()Convenience constructor for ScipyLeastSquares(method = "lm").
OptimizationSciPy.ScipyRootScalar — Type
ScipyRootScalar(method::String = "brentq")Optimizer wrapper for scipy.optimize.root_scalar.
OptimizationSciPy.ScipyRoot — Type
ScipyRoot(method::String = "hybr")Optimizer wrapper for scipy.optimize.root.
OptimizationSciPy.ScipyLinprog — Type
ScipyLinprog(method::String = "highs")Optimizer wrapper for scipy.optimize.linprog.
OptimizationSciPy.ScipyMilp — Type
ScipyMilp()Optimizer wrapper for scipy.optimize.milp.
OptimizationSciPy.ScipyDifferentialEvolution — Type
ScipyDifferentialEvolution()Optimizer wrapper for scipy.optimize.differential_evolution.
OptimizationSciPy.ScipyBasinhopping — Type
ScipyBasinhopping()Optimizer wrapper for scipy.optimize.basinhopping.
OptimizationSciPy.ScipyDualAnnealing — Type
ScipyDualAnnealing()Optimizer wrapper for scipy.optimize.dual_annealing.
OptimizationSciPy.ScipyShgo — Type
ScipyShgo()Optimizer wrapper for scipy.optimize.shgo.
OptimizationSciPy.ScipyDirect — Type
ScipyDirect()Optimizer wrapper for scipy.optimize.direct.
OptimizationSciPy.ScipyBrute — Type
ScipyBrute()Optimizer wrapper for scipy.optimize.brute.
Examples
Unconstrained minimisation
using OptimizationBase, OptimizationSciPy, ADTypes, Zygote
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
x0 = zeros(2)
p = [1.0, 100.0]
f = OptimizationFunction(rosenbrock, ADTypes.AutoZygote())
prob = OptimizationProblem(f, x0, p)
sol = solve(prob, ScipyBFGS())
@show sol.objective # ≈ 0 at optimum7.717288356613562e-13Constrained optimisation with COBYLA
using OptimizationBase, OptimizationSciPy
# Objective
obj(x, p) = (x[1] + x[2] - 1)^2
# Single non-linear constraint: x₁² + x₂² ≈ 1 (with small tolerance)
cons(res, x, p) = (res .= [x[1]^2 + x[2]^2 - 1.0])
x0 = [0.5, 0.5]
prob = OptimizationProblem(
OptimizationFunction(obj; cons = cons),
x0, nothing, lcons = [-1e-6], ucons = [1e-6]) # Small tolerance instead of exact equality
sol = solve(prob, ScipyCOBYLA())
@show sol.u, sol.objective([0.9999995099640485, 1.1653740143777096e-5], 1.2462829129061485e-10)Differential evolution (global) with custom options
using OptimizationBase, OptimizationSciPy, Random, Statistics
Random.seed!(123)
ackley(x, p) = -20exp(-0.2*sqrt(mean(x .^ 2))) - exp(mean(cos.(2π .* x))) + 20 + ℯ
x0 = zeros(2) # initial guess is ignored by DE
prob = OptimizationProblem(ackley, x0; lb = [-5.0, -5.0], ub = [5.0, 5.0])
sol = solve(prob, ScipyDifferentialEvolution(); popsize = 20, mutation = (0.5, 1))
@show sol.objective4.440892098500626e-16Passing solver-specific options
Any keyword that Optimization.jl does not interpret is forwarded directly to SciPy. Refer to the SciPy optimisation API for the exhaustive list of options.
sol = solve(prob, ScipyTrustConstr(); verbose = 3, maxiter = 10_000)Troubleshooting
The original Python result object is attached to the solution in the original field:
sol = solve(prob, ScipyBFGS())
println(sol.original) # SciPy OptimizeResultIf SciPy raises an error it is re-thrown as a Julia ErrorException carrying the Python message, so look there first.
Contributing
Bug reports and feature requests are welcome in the Optimization.jl issue tracker. Pull requests that improve either the Julia wrapper or the documentation are highly appreciated.