@@ -165,8 +165,8 @@ function SciMLBase.reinit!(cache::NonlinearSolvePolyAlgorithmCache, args...; kwa
165
165
end
166
166
167
167
"""
168
- RobustMultiNewton(; concrete_jac = nothing, linsolve = nothing, precs = DEFAULT_PRECS ,
169
- autodiff = nothing)
168
+ RobustMultiNewton(::Type{T} = Float64 ; concrete_jac = nothing, linsolve = nothing,
169
+ precs = DEFAULT_PRECS, autodiff = nothing)
170
170
171
171
A polyalgorithm focused on robustness. It uses a mixture of Newton methods with different
172
172
globalizing techniques (trust region updates, line searches, etc.) in order to find a
@@ -176,6 +176,11 @@ Basically, if this algorithm fails, then "most" good ways of solving your proble
176
176
you may need to think about reformulating the model (either there is an issue with the model,
177
177
or more precision / more stable linear solver choice is required).
178
178
179
+ ### Arguments
180
+
181
+ - `T`: The eltype of the initial guess. It is only used to check if some of the algorithms
182
+ are compatible with the problem type. Defaults to `Float64`.
183
+
179
184
### Keyword Arguments
180
185
181
186
- `autodiff`: determines the backend used for the Jacobian. Note that this argument is
@@ -196,28 +201,38 @@ or more precision / more stable linear solver choice is required).
196
201
algorithms, consult the
197
202
[LinearSolve.jl documentation](https://docs.sciml.ai/LinearSolve/stable/).
198
203
"""
199
- function RobustMultiNewton (; concrete_jac = nothing , linsolve = nothing ,
200
- precs = DEFAULT_PRECS, autodiff = nothing )
201
- algs = (TrustRegion (; concrete_jac, linsolve, precs),
202
- TrustRegion (; concrete_jac, linsolve, precs, autodiff,
203
- radius_update_scheme = RadiusUpdateSchemes. Bastin),
204
- NewtonRaphson (; concrete_jac, linsolve, precs, linesearch = BackTracking (),
205
- autodiff),
206
- TrustRegion (; concrete_jac, linsolve, precs,
207
- radius_update_scheme = RadiusUpdateSchemes. NLsolve, autodiff),
208
- TrustRegion (; concrete_jac, linsolve, precs,
209
- radius_update_scheme = RadiusUpdateSchemes. Fan, autodiff))
204
+ function RobustMultiNewton (:: Type{T} = Float64; concrete_jac = nothing , linsolve = nothing ,
205
+ precs = DEFAULT_PRECS, autodiff = nothing ) where {T}
206
+ if __is_complex (T)
207
+ # Let's atleast have something here for complex numbers
208
+ algs = (NewtonRaphson (; concrete_jac, linsolve, precs, autodiff),)
209
+ else
210
+ algs = (TrustRegion (; concrete_jac, linsolve, precs),
211
+ TrustRegion (; concrete_jac, linsolve, precs, autodiff,
212
+ radius_update_scheme = RadiusUpdateSchemes. Bastin),
213
+ NewtonRaphson (; concrete_jac, linsolve, precs, linesearch = BackTracking (),
214
+ autodiff),
215
+ TrustRegion (; concrete_jac, linsolve, precs,
216
+ radius_update_scheme = RadiusUpdateSchemes. NLsolve, autodiff),
217
+ TrustRegion (; concrete_jac, linsolve, precs,
218
+ radius_update_scheme = RadiusUpdateSchemes. Fan, autodiff))
219
+ end
210
220
return NonlinearSolvePolyAlgorithm (algs, Val (:NLS ))
211
221
end
212
222
213
223
"""
214
- FastShortcutNonlinearPolyalg(; concrete_jac = nothing, linsolve = nothing,
215
- precs = DEFAULT_PRECS, must_use_jacobian::Val = Val(false),
216
- prefer_simplenonlinearsolve::Val{SA} = Val(false), autodiff = nothing)
224
+ FastShortcutNonlinearPolyalg(::Type{T} = Float64; concrete_jac = nothing,
225
+ linsolve = nothing, precs = DEFAULT_PRECS, must_use_jacobian::Val = Val(false),
226
+ prefer_simplenonlinearsolve::Val{SA} = Val(false), autodiff = nothing) where {T}
217
227
218
228
A polyalgorithm focused on balancing speed and robustness. It first tries less robust methods
219
229
for more performance and then tries more robust techniques if the faster ones fail.
220
230
231
+ ### Arguments
232
+
233
+ - `T`: The eltype of the initial guess. It is only used to check if some of the algorithms
234
+ are compatible with the problem type. Defaults to `Float64`.
235
+
221
236
### Keyword Arguments
222
237
223
238
- `autodiff`: determines the backend used for the Jacobian. Note that this argument is
@@ -238,53 +253,76 @@ for more performance and then tries more robust techniques if the faster ones fa
238
253
algorithms, consult the
239
254
[LinearSolve.jl documentation](https://docs.sciml.ai/LinearSolve/stable/).
240
255
"""
241
- function FastShortcutNonlinearPolyalg (; concrete_jac = nothing , linsolve = nothing ,
242
- precs = DEFAULT_PRECS, must_use_jacobian:: Val{JAC} = Val (false ),
256
+ function FastShortcutNonlinearPolyalg (:: Type{T} = Float64; concrete_jac = nothing ,
257
+ linsolve = nothing , precs = DEFAULT_PRECS, must_use_jacobian:: Val{JAC} = Val (false ),
243
258
prefer_simplenonlinearsolve:: Val{SA} = Val (false ),
244
- autodiff = nothing ) where {JAC, SA}
259
+ autodiff = nothing ) where {T, JAC, SA}
245
260
if JAC
246
- algs = (NewtonRaphson (; concrete_jac, linsolve, precs, autodiff),
247
- NewtonRaphson (; concrete_jac, linsolve, precs, linesearch = BackTracking (),
248
- autodiff),
249
- TrustRegion (; concrete_jac, linsolve, precs, autodiff),
250
- TrustRegion (; concrete_jac, linsolve, precs,
251
- radius_update_scheme = RadiusUpdateSchemes. Bastin, autodiff))
252
- else
253
- # SimpleNewtonRaphson and SimpleTrustRegion are not robust to singular Jacobians
254
- # and thus are not included in the polyalgorithm
255
- if SA
256
- algs = (SimpleBroyden (),
257
- Broyden (; init_jacobian = Val (:true_jacobian )),
258
- SimpleKlement (),
259
- NewtonRaphson (; concrete_jac, linsolve, precs, autodiff),
260
- NewtonRaphson (; concrete_jac, linsolve, precs, linesearch = BackTracking (),
261
- autodiff),
262
- NewtonRaphson (; concrete_jac, linsolve, precs, linesearch = BackTracking (),
263
- autodiff),
264
- TrustRegion (; concrete_jac, linsolve, precs,
265
- radius_update_scheme = RadiusUpdateSchemes. Bastin, autodiff))
261
+ if __is_complex (T)
262
+ algs = (NewtonRaphson (; concrete_jac, linsolve, precs, autodiff),)
266
263
else
267
- algs = (Broyden (),
268
- Broyden (; init_jacobian = Val (:true_jacobian )),
269
- Klement (; linsolve, precs),
270
- NewtonRaphson (; concrete_jac, linsolve, precs, autodiff),
264
+ algs = (NewtonRaphson (; concrete_jac, linsolve, precs, autodiff),
271
265
NewtonRaphson (; concrete_jac, linsolve, precs, linesearch = BackTracking (),
272
266
autodiff),
273
267
TrustRegion (; concrete_jac, linsolve, precs, autodiff),
274
268
TrustRegion (; concrete_jac, linsolve, precs,
275
269
radius_update_scheme = RadiusUpdateSchemes. Bastin, autodiff))
276
270
end
271
+ else
272
+ # SimpleNewtonRaphson and SimpleTrustRegion are not robust to singular Jacobians
273
+ # and thus are not included in the polyalgorithm
274
+ if SA
275
+ if __is_complex (T)
276
+ algs = (SimpleBroyden (),
277
+ Broyden (; init_jacobian = Val (:true_jacobian )),
278
+ SimpleKlement (),
279
+ NewtonRaphson (; concrete_jac, linsolve, precs, autodiff))
280
+ else
281
+ algs = (SimpleBroyden (),
282
+ Broyden (; init_jacobian = Val (:true_jacobian )),
283
+ SimpleKlement (),
284
+ NewtonRaphson (; concrete_jac, linsolve, precs, autodiff),
285
+ NewtonRaphson (; concrete_jac, linsolve, precs,
286
+ linesearch = BackTracking (), autodiff),
287
+ NewtonRaphson (; concrete_jac, linsolve, precs,
288
+ linesearch = BackTracking (), autodiff),
289
+ TrustRegion (; concrete_jac, linsolve, precs,
290
+ radius_update_scheme = RadiusUpdateSchemes. Bastin, autodiff))
291
+ end
292
+ else
293
+ if __is_complex (T)
294
+ algs = (Broyden (),
295
+ Broyden (; init_jacobian = Val (:true_jacobian )),
296
+ Klement (; linsolve, precs),
297
+ NewtonRaphson (; concrete_jac, linsolve, precs, autodiff))
298
+ else
299
+ algs = (Broyden (),
300
+ Broyden (; init_jacobian = Val (:true_jacobian )),
301
+ Klement (; linsolve, precs),
302
+ NewtonRaphson (; concrete_jac, linsolve, precs, autodiff),
303
+ NewtonRaphson (; concrete_jac, linsolve, precs,
304
+ linesearch = BackTracking (), autodiff),
305
+ TrustRegion (; concrete_jac, linsolve, precs, autodiff),
306
+ TrustRegion (; concrete_jac, linsolve, precs,
307
+ radius_update_scheme = RadiusUpdateSchemes. Bastin, autodiff))
308
+ end
309
+ end
277
310
end
278
311
return NonlinearSolvePolyAlgorithm (algs, Val (:NLS ))
279
312
end
280
313
281
314
"""
282
- FastShortcutNLLSPolyalg(; concrete_jac = nothing, linsolve = nothing,
283
- precs = DEFAULT_PRECS, kwargs...)
315
+ FastShortcutNLLSPolyalg(::Type{T} = Float64 ; concrete_jac = nothing, linsolve = nothing,
316
+ precs = DEFAULT_PRECS, kwargs...)
284
317
285
318
A polyalgorithm focused on balancing speed and robustness. It first tries less robust methods
286
319
for more performance and then tries more robust techniques if the faster ones fail.
287
320
321
+ ### Arguments
322
+
323
+ - `T`: The eltype of the initial guess. It is only used to check if some of the algorithms
324
+ are compatible with the problem type. Defaults to `Float64`.
325
+
288
326
### Keyword Arguments
289
327
290
328
- `autodiff`: determines the backend used for the Jacobian. Note that this argument is
@@ -305,42 +343,58 @@ for more performance and then tries more robust techniques if the faster ones fa
305
343
algorithms, consult the
306
344
[LinearSolve.jl documentation](https://docs.sciml.ai/LinearSolve/stable/).
307
345
"""
308
- function FastShortcutNLLSPolyalg (; concrete_jac = nothing , linsolve = nothing ,
309
- precs = DEFAULT_PRECS, kwargs... )
310
- algs = (GaussNewton (; concrete_jac, linsolve, precs, kwargs... ),
311
- GaussNewton (; concrete_jac, linsolve, precs, linesearch = BackTracking (),
312
- kwargs... ),
313
- LevenbergMarquardt (; concrete_jac, linsolve, precs, kwargs... ))
346
+ function FastShortcutNLLSPolyalg (:: Type{T} = Float64; concrete_jac = nothing ,
347
+ linsolve = nothing , precs = DEFAULT_PRECS, kwargs... ) where {T}
348
+ if __is_complex (T)
349
+ algs = (GaussNewton (; concrete_jac, linsolve, precs, kwargs... ),
350
+ LevenbergMarquardt (; concrete_jac, linsolve, precs, kwargs... ))
351
+ else
352
+ algs = (GaussNewton (; concrete_jac, linsolve, precs, kwargs... ),
353
+ TrustRegion (; concrete_jac, linsolve, precs, kwargs... ),
354
+ GaussNewton (; concrete_jac, linsolve, precs, linesearch = BackTracking (),
355
+ kwargs... ),
356
+ TrustRegion (; concrete_jac, linsolve, precs,
357
+ radius_update_scheme = RadiusUpdateSchemes. Bastin, kwargs... ),
358
+ LevenbergMarquardt (; concrete_jac, linsolve, precs, kwargs... ))
359
+ end
314
360
return NonlinearSolvePolyAlgorithm (algs, Val (:NLLS ))
315
361
end
316
362
317
363
# # Defaults
318
364
319
365
# # TODO : In the long run we want to use an `Assumptions` API like LinearSolve to specify
320
366
# # the conditioning of the Jacobian and such
367
+
368
+ # # TODO : Currently some of the algorithms like LineSearches / TrustRegion don't support
369
+ # # complex numbers. We should use the `DiffEqBase` trait for this once all of the
370
+ # # NonlinearSolve algorithms support it. For now we just do a check and remove the
371
+ # # unsupported ones from default
372
+
321
373
# # Defaults to a fast and robust poly algorithm in most cases. If the user went through
322
374
# # the trouble of specifying a custom jacobian function, we should use algorithms that
323
375
# # can use that!
324
-
325
376
function SciMLBase. __init (prob:: NonlinearProblem , :: Nothing , args... ; kwargs... )
326
377
must_use_jacobian = Val (prob. f. jac != = nothing )
327
- return SciMLBase. __init (prob, FastShortcutNonlinearPolyalg (; must_use_jacobian),
378
+ return SciMLBase. __init (prob,
379
+ FastShortcutNonlinearPolyalg (eltype (prob. u0); must_use_jacobian),
328
380
args... ; kwargs... )
329
381
end
330
382
331
383
function SciMLBase. __solve (prob:: NonlinearProblem , :: Nothing , args... ; kwargs... )
332
384
must_use_jacobian = Val (prob. f. jac != = nothing )
333
385
prefer_simplenonlinearsolve = Val (prob. u0 isa SArray)
334
386
return SciMLBase. __solve (prob,
335
- FastShortcutNonlinearPolyalg (; must_use_jacobian,
387
+ FastShortcutNonlinearPolyalg (eltype (prob . u0) ; must_use_jacobian,
336
388
prefer_simplenonlinearsolve), args... ; kwargs... )
337
389
end
338
390
339
391
function SciMLBase. __init (prob:: NonlinearLeastSquaresProblem , :: Nothing , args... ; kwargs... )
340
- return SciMLBase. __init (prob, FastShortcutNLLSPolyalg (), args... ; kwargs... )
392
+ return SciMLBase. __init (prob, FastShortcutNLLSPolyalg (eltype (prob. u0)), args... ;
393
+ kwargs... )
341
394
end
342
395
343
396
function SciMLBase. __solve (prob:: NonlinearLeastSquaresProblem , :: Nothing , args... ;
344
397
kwargs... )
345
- return SciMLBase. __solve (prob, FastShortcutNLLSPolyalg (), args... ; kwargs... )
398
+ return SciMLBase. __solve (prob, FastShortcutNLLSPolyalg (eltype (prob. u0)), args... ;
399
+ kwargs... )
346
400
end
0 commit comments