@@ -8,6 +8,40 @@ function Base.showerror(io::IO, e::IncompatibleOptimizerError)
88 print (io, e. err)
99end
1010
11+ const NONCONCRETE_ELTYPE_MESSAGE = """
12+ Non-concrete element type inside of an `Array` detected.
13+ Arrays with non-concrete element types, such as
14+ `Array{Union{Float32,Float64}}`, are not supported by the
15+ optimizers. Anyways, this is bad for
16+ performance so you don't want to be doing this!
17+
18+ If this was a mistake, promote the element types to be
19+ all the same. If this was intentional, for example,
20+ using Unitful.jl with different unit values, then use
21+ an array type which has fast broadcast support for
22+ heterogeneous values such as the ArrayPartition
23+ from RecursiveArrayTools.jl. For example:
24+
25+ ```julia
26+ using RecursiveArrayTools
27+ x = ArrayPartition([1.0,2.0],[1f0,2f0])
28+ y = ArrayPartition([3.0,4.0],[3f0,4f0])
29+ x .+ y # fast, stable, and usable as u0 in some optimizers
30+ ```
31+
32+ Element type:
33+ """
34+
35+ struct NonConcreteEltypeError <: Exception
36+ eltype:: Any
37+ end
38+
39+ function Base. showerror (io:: IO , e:: NonConcreteEltypeError )
40+ print (io, NONCONCRETE_ELTYPE_MESSAGE)
41+ print (io, e. eltype)
42+ end
43+
44+
1145"""
1246```julia
1347solve(prob::OptimizationProblem, alg::AbstractOptimizationAlgorithm, args...; kwargs...)
@@ -94,6 +128,9 @@ function solve(prob::OptimizationProblem, alg, args...;
94128 if supports_opt_cache_interface (alg)
95129 solve! (init (prob, alg, args... ; kwargs... ))
96130 else
131+ if prob. u0 != = nothing && ! isconcretetype (eltype (prob. u0))
132+ throw (NonConcreteEltypeError (eltype (prob. u0)))
133+ end
97134 _check_opt_alg (prob, alg; kwargs... )
98135 __solve (prob, alg, args... ; kwargs... )
99136 end
@@ -169,6 +206,9 @@ These arguments can be passed as `kwargs...` to `init`.
169206See also [`solve(prob::OptimizationProblem, alg, args...; kwargs...)`](@ref)
170207"""
171208function init (prob:: OptimizationProblem , alg, args... ; kwargs... ):: AbstractOptimizationCache
209+ if prob. u0 != = nothing && ! isconcretetype (eltype (prob. u0))
210+ throw (NonConcreteEltypeError (eltype (prob. u0)))
211+ end
172212 _check_opt_alg (prob:: OptimizationProblem , alg; kwargs... )
173213 cache = __init (prob, alg, args... ; prob. kwargs... , kwargs... )
174214 return cache
0 commit comments