Skip to content

Commit

Permalink
Updates example script file names (#637)
Browse files Browse the repository at this point in the history
* examples: improve script name descriptions

* examples: remove exponential decay UKF, add ask-tell interface script
  • Loading branch information
BradyPlanden authored Feb 3, 2025
1 parent d5343e1 commit 5da090e
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 89 deletions.
83 changes: 0 additions & 83 deletions examples/scripts/comparison_examples/exp_UKF.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@
# Plot the timeseries output (requires model that returns Voltage)
pybop.plot.quick(observer, problem_inputs=results.x, title="Optimised Comparison")

# # Plot convergence
# pybop.plot.convergence(optim)
# Plot convergence
pybop.plot.convergence(optim)

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

# # Plot the cost landscape with optimisation path
# pybop.plot.surface(optim)
# Plot the cost landscape with optimisation path
pybop.plot.surface(optim)
87 changes: 87 additions & 0 deletions examples/scripts/getting_started/ask-tell-interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import numpy as np
import pybamm

import pybop

# Define model and use high-performant solver for sensitivities
solver = pybamm.IDAKLUSolver()
parameter_set = pybop.ParameterSet.pybamm("Chen2020")
model = pybop.lithium_ion.SPM(parameter_set=parameter_set, solver=solver)

# Fitting parameters
parameters = pybop.Parameters(
pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(0.55, 0.05),
),
pybop.Parameter(
"Positive electrode active material volume fraction",
prior=pybop.Gaussian(0.55, 0.05),
),
)

# Generate data
sigma = 0.003
experiment = pybop.Experiment(
[
(
"Discharge at 0.5C for 3 minutes (3 second period)",
"Charge at 0.5C for 3 minutes (3 second period)",
),
]
* 2
)
values = model.predict(initial_state={"Initial SoC": 0.5}, 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),
"Bulk open-circuit voltage [V]": values["Bulk open-circuit voltage [V]"].data
+ noise(sigma),
}
)

signal = ["Voltage [V]", "Bulk open-circuit voltage [V]"]
# Construct the problem and cost classes
problem = pybop.FittingProblem(model, parameters, dataset, signal=signal)
cost = pybop.Minkowski(problem, p=2)

# We construct the optimiser class the same as normal
# but will be using the `optimiser` attribute directly
# for this example. This interface works for all
# non SciPy-based optimisers.
# Warning: not all arguments are supported via this
# interface.
optim = pybop.AdamW(cost)

# Create storage vars
x_best = []
f_best = []

# Run optimisation
for i in range(100):
x = optim.optimiser.ask()
f = [cost(x[0], calculate_grad=True)]
optim.optimiser.tell(f)

# Store best solution so far
x_best.append(optim.optimiser.x_best())
f_best.append(optim.optimiser.x_best())

if i % 10 == 0:
print(
f"Iteration: {i} | Cost: {optim.optimiser.f_best()} | Parameters: {optim.optimiser.x_best()}"
)

# Plot the timeseries output
pybop.plot.quick(
problem, problem_inputs=optim.optimiser.x_best(), title="Optimised Comparison"
)
File renamed without changes.

0 comments on commit 5da090e

Please sign in to comment.