-
Notifications
You must be signed in to change notification settings - Fork 24
/
test_optimisation.py
485 lines (423 loc) · 16.5 KB
/
test_optimisation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import io
import sys
import warnings
import numpy as np
import pytest
import pybop
class TestOptimisation:
"""
A class to test the optimisation class.
"""
@pytest.fixture
def dataset(self):
return pybop.Dataset(
{
"Time [s]": np.linspace(0, 360, 10),
"Current function [A]": np.zeros(10),
"Voltage [V]": np.ones(10),
}
)
@pytest.fixture
def one_parameter(self):
return pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(0.6, 0.2),
bounds=[0.58, 0.62],
)
@pytest.fixture
def two_parameters(self):
return pybop.Parameters(
pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(0.6, 0.2),
bounds=[0.58, 0.62],
),
pybop.Parameter(
"Positive electrode active material volume fraction",
prior=pybop.Gaussian(0.55, 0.05),
bounds=[0.53, 0.57],
),
)
@pytest.fixture
def model(self):
return pybop.lithium_ion.SPM()
@pytest.fixture
def cost(self, model, one_parameter, dataset):
problem = pybop.FittingProblem(
model,
one_parameter,
dataset,
)
return pybop.SumSquaredError(problem)
@pytest.fixture
def two_param_cost(self, model, two_parameters, dataset):
problem = pybop.FittingProblem(
model,
two_parameters,
dataset,
)
return pybop.SumSquaredError(problem)
@pytest.mark.parametrize(
"optimiser, expected_name",
[
(pybop.SciPyMinimize, "SciPyMinimize"),
(pybop.SciPyDifferentialEvolution, "SciPyDifferentialEvolution"),
(pybop.GradientDescent, "Gradient descent"),
(pybop.Adam, "Adam"),
(pybop.AdamW, "AdamW"),
(pybop.CMAES, "Covariance Matrix Adaptation Evolution Strategy (CMA-ES)"),
(pybop.CuckooSearch, "Cuckoo Search"),
(pybop.SNES, "Seperable Natural Evolution Strategy (SNES)"),
(pybop.XNES, "Exponential Natural Evolution Strategy (xNES)"),
(pybop.PSO, "Particle Swarm Optimisation (PSO)"),
(pybop.IRPropMin, "iRprop-"),
(pybop.NelderMead, "Nelder-Mead"),
],
)
@pytest.mark.unit
def test_optimiser_classes(self, two_param_cost, optimiser, expected_name):
# Test class construction
cost = two_param_cost
optim = optimiser(cost=cost)
assert optim.cost is not None
assert optim.name() == expected_name
# Test pybop.Optimisation construction
optim = pybop.Optimisation(cost=cost, optimiser=optimiser)
assert optim.cost is not None
assert optim.name() == expected_name
if optimiser not in [pybop.SciPyDifferentialEvolution]:
# Test construction without bounds
optim = optimiser(cost=cost, bounds=None)
assert optim.bounds is None
if issubclass(optimiser, pybop.BasePintsOptimiser):
assert optim._boundaries is None
@pytest.mark.unit
def test_no_optimisation_parameters(self, model, dataset):
problem = pybop.FittingProblem(
model=model, parameters=pybop.Parameters(), dataset=dataset
)
cost = pybop.RootMeanSquaredError(problem)
with pytest.raises(ValueError, match="There are no parameters to optimise."):
pybop.Optimisation(cost=cost)
@pytest.mark.parametrize(
"optimiser",
[
pybop.SciPyMinimize,
pybop.SciPyDifferentialEvolution,
pybop.GradientDescent,
pybop.Adam,
pybop.AdamW,
pybop.SNES,
pybop.XNES,
pybop.PSO,
pybop.IRPropMin,
pybop.NelderMead,
pybop.CuckooSearch,
],
)
@pytest.mark.unit
def test_optimiser_kwargs(self, cost, optimiser):
optim = optimiser(cost=cost, maxiter=1)
cost_bounds = cost.parameters.get_bounds()
# Check maximum iterations
optim.run()
assert optim.result.n_iterations == 1
if optimiser in [pybop.GradientDescent, pybop.Adam, pybop.NelderMead]:
# Ignored bounds
optim = optimiser(cost=cost, bounds=cost_bounds)
assert optim.bounds is None
elif optimiser in [pybop.PSO]:
assert optim.bounds == cost_bounds
# Cannot accept infinite bounds
bounds = {"upper": [np.inf], "lower": [0.57]}
with pytest.raises(
ValueError,
match="Either all bounds or no bounds must be set",
):
optim = optimiser(cost=cost, bounds=bounds)
else:
# Check and update bounds
assert optim.bounds == cost_bounds
bounds = {"upper": [0.63], "lower": [0.57]}
optim = optimiser(cost=cost, bounds=bounds)
assert optim.bounds == bounds
if issubclass(optimiser, pybop.BasePintsOptimiser):
optim = optimiser(
cost=cost,
use_f_guessed=True,
parallel=False,
min_iterations=3,
max_unchanged_iterations=5,
absolute_tolerance=1e-2,
relative_tolerance=1e-4,
max_evaluations=20,
threshold=1e-4,
)
with pytest.warns(
UserWarning,
match="Unrecognised keyword arguments: {'unrecognised': 10} will not be used.",
):
warnings.simplefilter("always")
optim = optimiser(cost=cost, unrecognised=10)
assert not optim.pints_optimiser.running()
else:
# Check bounds in list format and update tol
bounds = [
(lower, upper) for lower, upper in zip(bounds["lower"], bounds["upper"])
]
optim = optimiser(cost=cost, bounds=bounds, tol=1e-2)
assert optim.bounds == bounds
if optimiser in [
pybop.SciPyMinimize,
pybop.SciPyDifferentialEvolution,
pybop.XNES,
]:
# Pass nested options
optim = optimiser(cost=cost, options=dict(maxiter=10))
with pytest.raises(
Exception,
match="A duplicate maxiter option was found in the options dictionary.",
):
optimiser(cost=cost, maxiter=5, options=dict(maxiter=10))
# Pass similar keywords
with pytest.raises(
Exception,
match="option was passed in addition to the expected",
):
optimiser(cost=cost, maxiter=5, max_iterations=10)
if optimiser in [pybop.SciPyDifferentialEvolution]:
# Update population size
optimiser(cost=cost, popsize=5)
# Test invalid bounds
with pytest.raises(
ValueError, match="Bounds must be specified for differential_evolution."
):
optimiser(cost=cost, bounds=None)
with pytest.raises(
ValueError, match="Bounds must be specified for differential_evolution."
):
optimiser(cost=cost, bounds=[(0, np.inf)])
with pytest.raises(
ValueError, match="Bounds must be specified for differential_evolution."
):
optimiser(cost=cost, bounds={"upper": [np.inf], "lower": [0.57]})
# Test AdamW hyperparameters
if optimiser in [pybop.AdamW]:
optim = optimiser(cost=cost, b1=0.9, b2=0.999, lambda_=0.1)
optim.pints_optimiser.set_b1(0.9)
optim.pints_optimiser.set_b2(0.9)
optim.pints_optimiser.set_lambda(0.1)
assert optim.pints_optimiser._b1 == 0.9
assert optim.pints_optimiser._b2 == 0.9
assert optim.pints_optimiser._lambda == 0.1
# Incorrect values
for i, _match in (("Value", -1),):
with pytest.raises(
Exception, match="must be a numeric value between 0 and 1."
):
optim.pints_optimiser.set_b1(i)
with pytest.raises(
Exception, match="must be a numeric value between 0 and 1."
):
optim.pints_optimiser.set_b2(i)
with pytest.raises(
Exception, match="must be a numeric value between 0 and 1."
):
optim.pints_optimiser.set_lambda(i)
# Check defaults
assert optim.pints_optimiser.n_hyper_parameters() == 5
assert optim.pints_optimiser.x_guessed() == optim.pints_optimiser._x0
with pytest.raises(RuntimeError):
optim.pints_optimiser.tell([0.1])
else:
# Check and update initial values
x0 = cost.parameters.initial_value()
assert optim.x0 == x0
x0_new = np.array([0.6])
optim = optimiser(cost=cost, x0=x0_new)
assert optim.x0 == x0_new
assert optim.x0 != x0
@pytest.mark.unit
def test_cuckoo_no_bounds(self, dataset, cost, model):
optim = pybop.CuckooSearch(cost=cost, bounds=None, max_iterations=1)
optim.run()
assert optim.pints_optimiser._boundaries is None
@pytest.mark.unit
def test_scipy_minimize_with_jac(self, cost):
# Check a method that uses gradient information
optim = pybop.SciPyMinimize(cost=cost, method="L-BFGS-B", jac=True, maxiter=10)
optim.run()
assert optim.result.scipy_result.success is True
with pytest.raises(
ValueError,
match="Expected the jac option to be either True, False or None.",
):
optim = pybop.SciPyMinimize(cost=cost, jac="Invalid string")
@pytest.mark.unit
def test_scipy_minimize_invalid_x0(self, cost):
# Check a starting point that returns an infinite cost
invalid_x0 = np.array([1.1])
optim = pybop.SciPyMinimize(
cost=cost, x0=invalid_x0, maxiter=10, allow_infeasible_solutions=False
)
optim.run()
assert abs(optim._cost0) != np.inf
@pytest.mark.unit
def test_single_parameter(self, cost):
# Test catch for optimisers that can only run with multiple parameters
with pytest.raises(
ValueError,
match=r"requires optimisation of >= 2 parameters at once.",
):
pybop.CMAES(cost=cost)
@pytest.mark.unit
def test_invalid_cost(self):
# Test without valid cost
with pytest.raises(
Exception,
match="The cost is not a recognised cost object or function.",
):
pybop.Optimisation(cost="Invalid string")
def invalid_cost(x):
return [1, 2]
with pytest.raises(
Exception,
match="not a scalar numeric value.",
):
pybop.Optimisation(cost=invalid_cost)
@pytest.mark.unit
def test_default_optimiser(self, cost):
optim = pybop.Optimisation(cost=cost)
assert optim.name() == "Exponential Natural Evolution Strategy (xNES)"
# Test getting incorrect attribute
assert not hasattr(optim, "not_a_valid_attribute")
@pytest.mark.unit
def test_incorrect_optimiser_class(self, cost):
class RandomClass:
pass
with pytest.raises(
ValueError,
match="The pints_optimiser is not a recognised PINTS optimiser class.",
):
pybop.BasePintsOptimiser(cost=cost, pints_optimiser=RandomClass)
with pytest.raises(NotImplementedError):
pybop.BaseOptimiser(cost=cost)
with pytest.raises(ValueError):
pybop.Optimisation(cost=cost, optimiser=RandomClass)
@pytest.mark.unit
@pytest.mark.parametrize(
"mean, sigma, expect_exception",
[
(0.85, 0.2, False),
(0.85, 0.001, True),
(1.0, 0.5, False),
],
)
def test_scipy_prior_resampling(
self, model, dataset, mean, sigma, expect_exception
):
# Set up the parameter with a Gaussian prior
parameter = pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(mean, sigma),
bounds=[0.55, 0.95],
)
# Define the problem and cost
problem = pybop.FittingProblem(model, parameter, dataset)
cost = pybop.SumSquaredError(problem)
# Create the optimisation class with infeasible solutions disabled
opt = pybop.SciPyMinimize(
cost=cost,
allow_infeasible_solutions=False,
max_iterations=1,
)
# If small sigma, expect a ValueError due inability to resample a non np.inf cost
if expect_exception:
with pytest.raises(
ValueError,
match="The initial parameter values return an infinite cost.",
):
opt.run()
else:
opt.run()
# Test cost_wrapper inf return
cost = opt.cost_wrapper(np.array([0.9]))
assert cost in [1.729, 1.81, 1.9]
@pytest.mark.unit
def test_halting(self, cost):
# Test max evalutions
optim = pybop.GradientDescent(cost=cost, max_evaluations=1, verbose=True)
x, __ = optim.run()
assert optim.result.n_iterations == 1
# Test max unchanged iterations
optim = pybop.GradientDescent(
cost=cost, max_unchanged_iterations=1, min_iterations=1
)
x, __ = optim.run()
assert optim.result.n_iterations == 2
# Test guessed values
optim.set_f_guessed_tracking(True)
assert optim.f_guessed_tracking() is True
# Test invalid values
with pytest.raises(ValueError):
optim.set_max_iterations(-1)
with pytest.raises(ValueError):
optim.set_min_iterations(-1)
with pytest.raises(ValueError):
optim.set_max_unchanged_iterations(-1)
with pytest.raises(ValueError):
optim.set_max_unchanged_iterations(1, absolute_tolerance=-1)
with pytest.raises(ValueError):
optim.set_max_unchanged_iterations(1, relative_tolerance=-1)
with pytest.raises(ValueError):
optim.set_max_evaluations(-1)
# Reset optim
optim = pybop.Optimisation(cost=cost, sigma0=0.015, verbose=True)
# Confirm setting threshold == None
optim.set_threshold(None)
assert optim._threshold is None
# Confirm threshold halts
# Redirect stdout to capture print output
captured_output = io.StringIO()
sys.stdout = captured_output
optim.set_threshold(np.inf)
optim.run()
assert (
captured_output.getvalue().strip()
== "Halt: Objective function crossed threshold: inf."
)
optim.set_max_unchanged_iterations()
# Test callback and halting output
def callback_error(iteration, s):
raise Exception("Callback error message")
optim._callback = callback_error
with pytest.raises(Exception, match="Callback error message"):
optim.run()
optim._callback = None
# Trigger optimiser error
def optimiser_error():
return "Optimiser error message"
optim.pints_optimiser.stop = optimiser_error
optim.run()
assert optim.result.n_iterations == 1
# Test no stopping condition
with pytest.raises(
ValueError, match="At least one stopping criterion must be set."
):
optim._max_iterations = None
optim._unchanged_max_iterations = None
optim._max_evaluations = None
optim._threshold = None
optim.run()
@pytest.mark.unit
def test_infeasible_solutions(self, cost):
# Test infeasible solutions
for optimiser in [pybop.SciPyMinimize, pybop.GradientDescent]:
optim = optimiser(cost=cost, allow_infeasible_solutions=False, maxiter=1)
optim.run()
assert optim.result.n_iterations == 1
@pytest.mark.unit
def test_unphysical_result(self, cost):
# Trigger parameters not physically viable warning
optim = pybop.Optimisation(cost=cost)
optim.check_optimal_parameters(np.array([2]))