Skip to content

Commit 0679d05

Browse files
Remove OptimizationBase.jl pieces
1 parent 6f1a1d0 commit 0679d05

File tree

1 file changed

+0
-217
lines changed

1 file changed

+0
-217
lines changed

src/solve.jl

Lines changed: 0 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -31,223 +31,6 @@ function Base.showerror(io::IO, e::NonConcreteEltypeError)
3131
print(io, e.eltype)
3232
end
3333

34-
# Skip the DiffEqBase handling
35-
36-
struct IncompatibleOptimizerError <: Exception
37-
err::String
38-
end
39-
40-
function Base.showerror(io::IO, e::IncompatibleOptimizerError)
41-
print(io, e.err)
42-
end
43-
44-
"""
45-
```julia
46-
solve(prob::OptimizationProblem, alg::AbstractOptimizationAlgorithm,
47-
args...; kwargs...)::OptimizationSolution
48-
```
49-
50-
For information about the returned solution object, refer to the documentation for [`OptimizationSolution`](@ref)
51-
52-
## Keyword Arguments
53-
54-
The arguments to `solve` are common across all of the optimizers.
55-
These common arguments are:
56-
57-
- `maxiters`: the maximum number of iterations
58-
- `maxtime`: the maximum amount of time (typically in seconds) the optimization runs for
59-
- `abstol`: absolute tolerance in changes of the objective value
60-
- `reltol`: relative tolerance in changes of the objective value
61-
- `callback`: a callback function
62-
63-
Some optimizer algorithms have special keyword arguments documented in the
64-
solver portion of the documentation and their respective documentation.
65-
These arguments can be passed as `kwargs...` to `solve`. Similarly, the special
66-
keyword arguments for the `local_method` of a global optimizer are passed as a
67-
`NamedTuple` to `local_options`.
68-
69-
Over time, we hope to cover more of these keyword arguments under the common interface.
70-
71-
A warning will be shown if a common argument is not implemented for an optimizer.
72-
73-
## Callback Functions
74-
75-
The callback function `callback` is a function that is called after every optimizer
76-
step. Its signature is:
77-
78-
```julia
79-
callback = (state, loss_val) -> false
80-
```
81-
82-
where `state` is an `OptimizationState` and stores information for the current
83-
iteration of the solver and `loss_val` is loss/objective value. For more
84-
information about the fields of the `state` look at the `OptimizationState`
85-
documentation. The callback should return a Boolean value, and the default
86-
should be `false`, so the optimization stops if it returns `true`.
87-
88-
### Callback Example
89-
90-
Here we show an example of a callback function that plots the prediction at the current value of the optimization variables.
91-
For a visualization callback, we would need the prediction at the current parameters i.e. the solution of the `ODEProblem` `prob`.
92-
So we call the `predict` function within the callback again.
93-
94-
```julia
95-
function predict(u)
96-
Array(solve(prob, Tsit5(), p = u))
97-
end
98-
99-
function loss(u, p)
100-
pred = predict(u)
101-
sum(abs2, batch .- pred)
102-
end
103-
104-
callback = function (state, l; doplot = false) #callback function to observe training
105-
display(l)
106-
# plot current prediction against data
107-
if doplot
108-
pred = predict(state.u)
109-
pl = scatter(t, ode_data[1, :], label = "data")
110-
scatter!(pl, t, pred[1, :], label = "prediction")
111-
display(plot(pl))
112-
end
113-
return false
114-
end
115-
```
116-
117-
If the chosen method is a global optimizer that employs a local optimization
118-
method, a similar set of common local optimizer arguments exists. Look at `MLSL` or `AUGLAG`
119-
from NLopt for an example. The common local optimizer arguments are:
120-
121-
- `local_method`: optimizer used for local optimization in global method
122-
- `local_maxiters`: the maximum number of iterations
123-
- `local_maxtime`: the maximum amount of time (in seconds) the optimization runs for
124-
- `local_abstol`: absolute tolerance in changes of the objective value
125-
- `local_reltol`: relative tolerance in changes of the objective value
126-
- `local_options`: `NamedTuple` of keyword arguments for local optimizer
127-
"""
128-
function solve(prob::OptimizationProblem, alg, args...;
129-
kwargs...)::AbstractOptimizationSolution
130-
if supports_opt_cache_interface(alg)
131-
solve!(init(prob, alg, args...; kwargs...))
132-
else
133-
if prob.u0 !== nothing && !isconcretetype(eltype(prob.u0))
134-
throw(NonConcreteEltypeError(eltype(prob.u0)))
135-
end
136-
_check_opt_alg(prob, alg; kwargs...)
137-
__solve(prob, alg, args...; kwargs...)
138-
end
139-
end
140-
141-
function SciMLBase.solve(
142-
prob::EnsembleProblem{T}, args...; kwargs...) where {T <: OptimizationProblem}
143-
return SciMLBase.__solve(prob, args...; kwargs...)
144-
end
145-
146-
function _check_opt_alg(prob::OptimizationProblem, alg; kwargs...)
147-
!allowsbounds(alg) && (!isnothing(prob.lb) || !isnothing(prob.ub)) &&
148-
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) does not support box constraints. Either remove the `lb` or `ub` bounds passed to `OptimizationProblem` or use a different algorithm."))
149-
requiresbounds(alg) && isnothing(prob.lb) &&
150-
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) requires box constraints. Either pass `lb` and `ub` bounds to `OptimizationProblem` or use a different algorithm."))
151-
!allowsconstraints(alg) && !isnothing(prob.f.cons) &&
152-
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) does not support constraints. Either remove the `cons` function passed to `OptimizationFunction` or use a different algorithm."))
153-
requiresconstraints(alg) && isnothing(prob.f.cons) &&
154-
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) requires constraints, pass them with the `cons` kwarg in `OptimizationFunction`."))
155-
# Check that if constraints are present and the algorithm supports constraints, both lcons and ucons are provided
156-
allowsconstraints(alg) && !isnothing(prob.f.cons) &&
157-
(isnothing(prob.lcons) || isnothing(prob.ucons)) &&
158-
throw(ArgumentError("Constrained optimization problem requires both `lcons` and `ucons` to be provided to OptimizationProblem. " *
159-
"Example: OptimizationProblem(optf, u0, p; lcons=[-Inf], ucons=[0.0])"))
160-
!allowscallback(alg) && haskey(kwargs, :callback) &&
161-
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) does not support callbacks, remove the `callback` keyword argument from the `solve` call."))
162-
requiresgradient(alg) && !(prob.f isa AbstractOptimizationFunction) &&
163-
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) requires gradients, hence use `OptimizationFunction` to generate them with an automatic differentiation backend e.g. `OptimizationFunction(f, AutoForwardDiff())` or pass it in with `grad` kwarg."))
164-
requireshessian(alg) && !(prob.f isa AbstractOptimizationFunction) &&
165-
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) requires hessians, hence use `OptimizationFunction` to generate them with an automatic differentiation backend e.g. `OptimizationFunction(f, AutoFiniteDiff(); kwargs...)` or pass them in with `hess` kwarg."))
166-
requiresconsjac(alg) && !(prob.f isa AbstractOptimizationFunction) &&
167-
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) requires constraint jacobians, hence use `OptimizationFunction` to generate them with an automatic differentiation backend e.g. `OptimizationFunction(f, AutoFiniteDiff(); kwargs...)` or pass them in with `cons` kwarg."))
168-
requiresconshess(alg) && !(prob.f isa AbstractOptimizationFunction) &&
169-
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) requires constraint hessians, hence use `OptimizationFunction` to generate them with an automatic differentiation backend e.g. `OptimizationFunction(f, AutoFiniteDiff(), AutoFiniteDiff(hess=true); kwargs...)` or pass them in with `cons` kwarg."))
170-
return
171-
end
172-
173-
const OPTIMIZER_MISSING_ERROR_MESSAGE = """
174-
Optimization algorithm not found. Either the chosen algorithm is not a valid solver
175-
choice for the `OptimizationProblem`, or the Optimization solver library is not loaded.
176-
Make sure that you have loaded an appropriate Optimization.jl solver library, for example,
177-
`solve(prob,Optim.BFGS())` requires `using OptimizationOptimJL` and
178-
`solve(prob,Adam())` requires `using OptimizationOptimisers`.
179-
180-
For more information, see the Optimization.jl documentation: <https://docs.sciml.ai/Optimization/stable/>.
181-
"""
182-
183-
struct OptimizerMissingError <: Exception
184-
alg::Any
185-
end
186-
187-
function Base.showerror(io::IO, e::OptimizerMissingError)
188-
println(io, OPTIMIZER_MISSING_ERROR_MESSAGE)
189-
print(io, "Chosen Optimizer: ")
190-
print(e.alg)
191-
end
192-
193-
"""
194-
```julia
195-
init(prob::OptimizationProblem, alg::AbstractOptimizationAlgorithm, args...; kwargs...)
196-
```
197-
198-
## Keyword Arguments
199-
200-
The arguments to `init` are the same as to `solve` and common across all of the optimizers.
201-
These common arguments are:
202-
203-
- `maxiters` (the maximum number of iterations)
204-
- `maxtime` (the maximum of time the optimization runs for)
205-
- `abstol` (absolute tolerance in changes of the objective value)
206-
- `reltol` (relative tolerance in changes of the objective value)
207-
- `callback` (a callback function)
208-
209-
Some optimizer algorithms have special keyword arguments documented in the
210-
solver portion of the documentation and their respective documentation.
211-
These arguments can be passed as `kwargs...` to `init`.
212-
213-
See also [`solve(prob::OptimizationProblem, alg, args...; kwargs...)`](@ref)
214-
"""
215-
function init(prob::OptimizationProblem, alg, args...; kwargs...)::AbstractOptimizationCache
216-
if prob.u0 !== nothing && !isconcretetype(eltype(prob.u0))
217-
throw(NonConcreteEltypeError(eltype(prob.u0)))
218-
end
219-
_check_opt_alg(prob::OptimizationProblem, alg; kwargs...)
220-
cache = __init(prob, alg, args...; prob.kwargs..., kwargs...)
221-
return cache
222-
end
223-
224-
"""
225-
```julia
226-
solve!(cache::AbstractOptimizationCache)
227-
```
228-
229-
Solves the given optimization cache.
230-
231-
See also [`init(prob::OptimizationProblem, alg, args...; kwargs...)`](@ref)
232-
"""
233-
function solve!(cache::AbstractOptimizationCache)::AbstractOptimizationSolution
234-
__solve(cache)
235-
end
236-
237-
# needs to be defined for each cache
238-
supports_opt_cache_interface(alg) = false
239-
function __solve(cache::AbstractOptimizationCache)::AbstractOptimizationSolution end
240-
function __init(prob::OptimizationProblem, alg, args...;
241-
kwargs...)::AbstractOptimizationCache
242-
throw(OptimizerMissingError(alg))
243-
end
244-
245-
# if no cache interface is supported at least the following method has to be defined
246-
function __solve(prob::OptimizationProblem, alg, args...; kwargs...)
247-
throw(OptimizerMissingError(alg))
248-
end
249-
250-
25134
# Functions used in solve dispatches
25235

25336
eltypedual(x) = false

0 commit comments

Comments
 (0)