@@ -8,6 +8,40 @@ function Base.showerror(io::IO, e::IncompatibleOptimizerError)
8
8
print (io, e. err)
9
9
end
10
10
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
+
11
45
"""
12
46
```julia
13
47
solve(prob::OptimizationProblem, alg::AbstractOptimizationAlgorithm, args...; kwargs...)
@@ -94,6 +128,9 @@ function solve(prob::OptimizationProblem, alg, args...;
94
128
if supports_opt_cache_interface (alg)
95
129
solve! (init (prob, alg, args... ; kwargs... ))
96
130
else
131
+ if prob. u0 != = nothing && ! isconcretetype (eltype (prob. u0))
132
+ throw (NonConcreteEltypeError (eltype (prob. u0)))
133
+ end
97
134
_check_opt_alg (prob, alg; kwargs... )
98
135
__solve (prob, alg, args... ; kwargs... )
99
136
end
@@ -169,6 +206,9 @@ These arguments can be passed as `kwargs...` to `init`.
169
206
See also [`solve(prob::OptimizationProblem, alg, args...; kwargs...)`](@ref)
170
207
"""
171
208
function init (prob:: OptimizationProblem , alg, args... ; kwargs... ):: AbstractOptimizationCache
209
+ if prob. u0 != = nothing && ! isconcretetype (eltype (prob. u0))
210
+ throw (NonConcreteEltypeError (eltype (prob. u0)))
211
+ end
172
212
_check_opt_alg (prob:: OptimizationProblem , alg; kwargs... )
173
213
cache = __init (prob, alg, args... ; prob. kwargs... , kwargs... )
174
214
return cache
0 commit comments