Skip to content

Commit

Permalink
OptimiseResult().__str__ across examples, adds OCP balancing script (#…
Browse files Browse the repository at this point in the history
…536)

* adds OCP balancing example, remove redundant code.

* feat: add initial parameters to OptimisationResults

* examples: updates for OptimisationResults __str__

* refactor: OptimisationResult prints with BaseOptimsiser verbose arg, verbose is True as default

* style: pre-commit fixes

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
BradyPlanden and pre-commit-ci[bot] committed Oct 22, 2024
1 parent 55f5bd9 commit 898b237
Show file tree
Hide file tree
Showing 50 changed files with 783 additions and 354 deletions.
33 changes: 24 additions & 9 deletions examples/notebooks/adamw_identification.ipynb

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions examples/notebooks/comparing_cost_functions.ipynb

Large diffs are not rendered by default.

25 changes: 15 additions & 10 deletions examples/notebooks/ecm_trust-constr.ipynb

Large diffs are not rendered by default.

21 changes: 12 additions & 9 deletions examples/notebooks/electrode_balancing.ipynb

Large diffs are not rendered by default.

29 changes: 14 additions & 15 deletions examples/notebooks/energy_based_electrode_design.ipynb

Large diffs are not rendered by default.

28 changes: 16 additions & 12 deletions examples/notebooks/equivalent_circuit_identification.ipynb

Large diffs are not rendered by default.

32 changes: 18 additions & 14 deletions examples/notebooks/equivalent_circuit_identification_hppc.ipynb

Large diffs are not rendered by default.

Large diffs are not rendered by default.

58 changes: 33 additions & 25 deletions examples/notebooks/maximum_a_posteriori.ipynb

Large diffs are not rendered by default.

36 changes: 25 additions & 11 deletions examples/notebooks/multi_model_identification.ipynb

Large diffs are not rendered by default.

280 changes: 192 additions & 88 deletions examples/notebooks/multi_optimiser_identification.ipynb

Large diffs are not rendered by default.

205 changes: 152 additions & 53 deletions examples/notebooks/optimiser_calibration.ipynb

Large diffs are not rendered by default.

34 changes: 32 additions & 2 deletions examples/notebooks/optimiser_interface.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,22 @@
"execution_count": null,
"id": "7",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Halt: No significant change for 15 iterations.\n",
"OptimisationResult:\n",
" Initial parameters: [0.00032648]\n",
" Optimised parameters: [0.00099965]\n",
" Final cost: 1.363466506257511e-09\n",
" Optimisation time: 1.6153881549835205 seconds\n",
" Number of iterations: 23\n",
" SciPy result available: No\n"
]
}
],
"source": [
"optim_one = pybop.XNES(\n",
" cost, max_iterations=50\n",
Expand All @@ -183,7 +198,22 @@
"execution_count": null,
"id": "9",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Halt: No significant change for 15 iterations.\n",
"OptimisationResult:\n",
" Initial parameters: [0.00032648]\n",
" Optimised parameters: [0.00099985]\n",
" Final cost: 2.431998416403876e-10\n",
" Optimisation time: 1.972628116607666 seconds\n",
" Number of iterations: 25\n",
" SciPy result available: No\n"
]
}
],
"source": [
"optim_two = pybop.Optimisation(\n",
" cost, optimiser=pybop.XNES, max_iterations=50\n",
Expand Down
52 changes: 37 additions & 15 deletions examples/notebooks/pouch_cell_identification.ipynb

Large diffs are not rendered by default.

36 changes: 20 additions & 16 deletions examples/notebooks/single_pulse_circuit_model.ipynb

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions examples/notebooks/solver_selection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -194,28 +194,28 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Time Evaluate IDA KLU solver: 0.306\n"
"Time Evaluate IDA KLU solver: 0.320\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time Evaluate CasADi solver with 'safe' mode: 1.097\n"
"Time Evaluate CasADi solver with 'safe' mode: 1.176\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time Evaluate CasADi solver with 'fast' mode: 1.090\n"
"Time Evaluate CasADi solver with 'fast' mode: 0.912\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time Evaluate CasADi solver with 'fast with events' mode: 0.915\n"
"Time Evaluate CasADi solver with 'fast with events' mode: 0.943\n"
]
}
],
Expand Down Expand Up @@ -248,28 +248,28 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Time EvaluateS1 IDA KLU solver: 0.622\n"
"Time EvaluateS1 IDA KLU solver: 0.786\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time EvaluateS1 CasADi solver with 'safe' mode: 3.886\n"
"Time EvaluateS1 CasADi solver with 'safe' mode: 3.751\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time EvaluateS1 CasADi solver with 'fast' mode: 3.498\n"
"Time EvaluateS1 CasADi solver with 'fast' mode: 3.326\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time EvaluateS1 CasADi solver with 'fast with events' mode: 3.475\n"
"Time EvaluateS1 CasADi solver with 'fast with events' mode: 3.349\n"
]
}
],
Expand Down
12 changes: 6 additions & 6 deletions examples/notebooks/transformation_introduction.ipynb

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion examples/scripts/BPX_spm.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
# Run the optimisation
results = optim.run()
print("True parameters:", parameters.true_value())
print("Estimated parameters:", results.x)

# Plot the timeseries output
pybop.plot.quick(problem, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/cuckoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
)

results = optim.run()
print("Estimated parameters:", results.x)

# Plot the timeseries output
pybop.plot.quick(problem, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/ecm_CMAES.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
optim = pybop.CMAES(cost, max_iterations=100)

results = optim.run()
print("Estimated parameters:", results.x)

# Export the parameters to JSON
parameter_set.export_parameters(
Expand Down
2 changes: 0 additions & 2 deletions examples/scripts/ecm_tau_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ def check_params(
)

results = optim.run()
print("Estimated parameters:", results.x)


# Plot the time series
pybop.plot.dataset(dataset)
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/eis_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def noise(sigma, values):
optim = pybop.CMAES(cost, max_iterations=100, sigma0=0.25, max_unchanged_iterations=30)

results = optim.run()
print("Estimated parameters:", results.x)

# Plot the nyquist
pybop.plot.nyquist(problem, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/exp_UKF.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@

# Run optimisation
results = optim.run()
print("Estimated parameters:", results.x)

# Plot the timeseries output (requires model that returns Voltage)
pybop.plot.quick(observer, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/functional_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ def positive_electrode_exchange_current_density(c_e, c_s_surf, c_s_max, T):

# Run optimisation
results = optim.run()
print("Estimated parameters:", results.x)

# Plot the timeseries output
pybop.plot.quick(problem, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/gitt.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@

# Run the optimisation problem
results = optim.run()
print("Estimated parameters:", results.x)

# Plot the timeseries output
pybop.plot.quick(problem, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/linked_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
cost, verbose=True, allow_infeasible_solutions=False, max_iterations=10
)
results = optim.run()
print("Estimated parameters:", results.x)
print(f"Initial gravimetric energy density: {cost(optim.x0):.2f} Wh.kg-1")
print(f"Optimised gravimetric energy density: {cost(results.x):.2f} Wh.kg-1")

Expand Down
1 change: 0 additions & 1 deletion examples/scripts/maximising_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
cost, verbose=True, allow_infeasible_solutions=False, max_iterations=10
)
results = optim.run()
print("Estimated parameters:", results.x)
print(f"Initial gravimetric energy density: {cost1(optim.x0):.2f} Wh.kg-1")
print(f"Optimised gravimetric energy density: {cost1(results.x):.2f} Wh.kg-1")
print(f"Initial volumetric energy density: {cost2(optim.x0):.2f} Wh.m-3")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/maximising_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
cost, verbose=True, allow_infeasible_solutions=False, max_iterations=10
)
results = optim.run()
print("Estimated parameters:", results.x)
print(f"Initial gravimetric power density: {cost1(optim.x0):.2f} W.kg-1")
print(f"Optimised gravimetric power density: {cost1(results.x):.2f} W.kg-1")
print(f"Initial volumetric power density: {cost2(optim.x0):.2f} W.m-3")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/maximum_a_posteriori.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
# Run the optimisation
results = optim.run()
print("True parameters:", parameters.true_value())
print("Estimated parameters:", results.x)

# Plot the timeseries output
pybop.plot.quick(problem, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/maximum_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ def noise(sigma):

# Run the optimisation
results = optim.run()
print("Estimated parameters:", results.x)

# Plot the timeseries output
pybop.plot.quick(problem, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/multi_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
# Run optimisation
results = optim.run()
print("True parameters:", parameters.true_value())
print("Estimated parameters:", results.x)

# Plot the timeseries output
pybop.plot.quick(problem_1, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
100 changes: 100 additions & 0 deletions examples/scripts/ocp_balancing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import numpy as np

import pybop

# Parameter set and model definition
parameter_set = pybop.ParameterSet.pybamm("Chen2020")
model = pybop.lithium_ion.SPM(parameter_set=parameter_set)

# Set objects for initial conditions
init_soc = {"Initial SoC": 1}
parameter_set.set_initial_stoichiometries(initial_value=init_soc["Initial SoC"])
cs_n_max = parameter_set["Maximum concentration in negative electrode [mol.m-3]"]
cs_p_max = parameter_set["Maximum concentration in positive electrode [mol.m-3]"]
v_min = parameter_set["Open-circuit voltage at 0% SOC [V]"]
v_max = parameter_set["Open-circuit voltage at 100% SOC [V]"]

# Define fitting parameters for OCP balancing
parameters = pybop.Parameters(
pybop.Parameter(
"Open-circuit voltage at 0% SOC [V]",
prior=pybop.Gaussian(v_min, 1e-1),
bounds=[2.35, 2.65],
true_value=v_min,
transformation=pybop.LogTransformation(),
),
pybop.Parameter(
"Open-circuit voltage at 100% SOC [V]",
prior=pybop.Gaussian(v_max, 1e-1),
bounds=[4.1, 4.3],
true_value=v_max,
transformation=pybop.LogTransformation(),
),
pybop.Parameter(
"Maximum concentration in negative electrode [mol.m-3]",
prior=pybop.Gaussian(cs_n_max, 6e3),
bounds=[cs_n_max * 0.75, cs_n_max * 1.25],
true_value=cs_n_max,
transformation=pybop.LogTransformation(),
),
pybop.Parameter(
"Maximum concentration in positive electrode [mol.m-3]",
prior=pybop.Gaussian(cs_p_max, 6e3),
bounds=[cs_p_max * 0.75, cs_p_max * 1.25],
true_value=cs_p_max,
transformation=pybop.LogTransformation(),
),
)

# Generate synthetic data
sigma = 5e-4 # Volts
experiment = pybop.Experiment(
[
(
"Discharge at 0.1C until 2.5V (3 min period)",
"Charge at 0.1C until 4.2V (3 min period)",
),
]
)
values = model.predict(initial_state=init_soc, experiment=experiment)


def noise(sigma):
return np.random.normal(0, sigma, len(values["Voltage [V]"].data))


# Form dataset
dataset = pybop.Dataset(
{
"Time [s]": values["Time [s]"].data,
"Current function [A]": values["Current [A]"].data,
"Voltage [V]": values["Voltage [V]"].data + noise(sigma),
}
)

# Generate problem, cost function, and optimisation class
problem = pybop.FittingProblem(model, parameters, dataset, initial_state=init_soc)
cost = pybop.GaussianLogLikelihoodKnownSigma(problem, sigma0=sigma)
optim = pybop.CMAES(
cost,
verbose=True,
sigma0=1e-1,
max_iterations=100,
max_unchanged_iterations=20,
)

# Run optimisation for Maximum Likelihood Estimate (MLE)
results = optim.run()
print("True parameters:", parameters.true_value())

# Plot the timeseries output
pybop.plot.quick(problem, problem_inputs=results.x, title="Optimised Comparison")

# Plot convergence
pybop.plot.convergence(optim)

# Plot the parameter traces
pybop.plot.parameters(optim)

# Plot the cost landscape with optimisation path
pybop.plot.contour(optim, steps=5)
1 change: 0 additions & 1 deletion examples/scripts/spm_AdamW.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def noise(sigma):

# Run optimisation
results = optim.run()
print("Estimated parameters:", results.x)

# Plot the timeseries output
pybop.plot.quick(problem, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/spm_CMAES.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
# Run the optimisation
results = optim.run()
print("True parameters:", parameters.true_value())
print("Estimated parameters:", results.x)

# Plot the time series
pybop.plot.dataset(dataset)
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/spm_IRPropMin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
optim = pybop.IRPropMin(cost, max_iterations=100)

results = optim.run()
print("Estimated parameters:", results.x)

# Plot the timeseries output
pybop.plot.quick(problem, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/spm_NelderMead.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ def noise(sigma):

# Run optimisation
results = optim.run()
print("Estimated parameters:", results.x)

# Plot the timeseries output
pybop.plot.quick(problem, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
2 changes: 0 additions & 2 deletions examples/scripts/spm_SNES.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
optim = pybop.SNES(cost, max_iterations=100)

results = optim.run()
print("Estimated parameters:", results.x)

# Plot the timeseries output
pybop.plot.quick(problem, problem_inputs=results.x, title="Optimised Comparison")

Expand Down
1 change: 0 additions & 1 deletion examples/scripts/spm_UKF.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@

# Run optimisation
results = optim.run()
print("Estimated parameters:", results.x)

# Plot the timeseries output (requires model that returns Voltage)
pybop.plot.quick(observer, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/spm_XNES.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
optim = pybop.XNES(cost, max_iterations=100)

results = optim.run()
print("Estimated parameters:", results.x)

# Plot the timeseries output
pybop.plot.quick(problem, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
1 change: 0 additions & 1 deletion examples/scripts/spm_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@

# Run optimisation
results = optim.run()
print("Estimated parameters:", results.x)

# Plot the timeseries output
pybop.plot.quick(problem, problem_inputs=results.x, title="Optimised Comparison")
Expand Down
Loading

0 comments on commit 898b237

Please sign in to comment.