Skip to content

Commit

Permalink
Remove init_soc from problem
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolaCourtier committed Jul 17, 2024
1 parent 6c7c3c3 commit 21d87d3
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 81 deletions.
6 changes: 4 additions & 2 deletions examples/notebooks/multi_model_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@
},
"outputs": [],
"source": [
"synth_model.build(dataset, init_soc=init_soc)\n",
"synth_model.set_init_soc(init_soc)\n",
"synth_model.build(dataset)\n",
"synth_model.signal = [\"Voltage [V]\"]\n",
"values = synth_model.simulate(t_eval=t_eval, inputs={})"
]
Expand Down Expand Up @@ -3811,7 +3812,8 @@
"xs = []\n",
"for model in models:\n",
" print(f\"Running {model.name}\")\n",
" problem = pybop.FittingProblem(model, parameters, dataset, init_soc=init_soc)\n",
" model.set_init_soc(init_soc)\n",
" problem = pybop.FittingProblem(model, parameters, dataset)\n",
" cost = pybop.SumSquaredError(problem)\n",
" optim = pybop.XNES(\n",
" cost, verbose=True, max_iterations=60, max_unchanged_iterations=15\n",
Expand Down
6 changes: 3 additions & 3 deletions examples/notebooks/multi_optimiser_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@
"outputs": [],
"source": [
"t_eval = np.arange(0, 2000, 10)\n",
"init_soc = 1.0\n",
"values = synth_model.predict(t_eval=t_eval, init_soc=init_soc)"
"model.set_init_soc(1.0)\n",
"values = synth_model.predict(t_eval=t_eval)"
]
},
{
Expand Down Expand Up @@ -510,7 +510,7 @@
"source": [
"optims = []\n",
"xs = []\n",
"problem = pybop.FittingProblem(model, parameters, dataset, init_soc=init_soc)\n",
"problem = pybop.FittingProblem(model, parameters, dataset)\n",
"cost = pybop.SumSquaredError(problem)\n",
"for optimiser in gradient_optimisers:\n",
" print(f\"Running {optimiser.__name__}\")\n",
Expand Down
8 changes: 4 additions & 4 deletions examples/notebooks/optimiser_calibration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
" }\n",
")\n",
"model = pybop.lithium_ion.SPM(parameter_set=parameter_set)\n",
"init_soc = 0.4\n",
"model.set_init_soc(0.4)\n",
"experiment = pybop.Experiment(\n",
" [\n",
" (\n",
Expand All @@ -143,7 +143,7 @@
" ]\n",
" * 2\n",
")\n",
"values = model.predict(init_soc=init_soc, experiment=experiment)"
"values = model.predict(experiment=experiment)"
]
},
{
Expand Down Expand Up @@ -298,7 +298,7 @@
}
],
"source": [
"problem = pybop.FittingProblem(model, parameters, dataset, init_soc=init_soc)\n",
"problem = pybop.FittingProblem(model, parameters, dataset)\n",
"cost = pybop.SumSquaredError(problem)\n",
"optim = pybop.GradientDescent(cost, sigma0=0.2, max_iterations=100)"
]
Expand Down Expand Up @@ -457,7 +457,7 @@
"optims = []\n",
"for sigma in sigmas:\n",
" print(sigma)\n",
" problem = pybop.FittingProblem(model, parameters, dataset, init_soc=init_soc)\n",
" problem = pybop.FittingProblem(model, parameters, dataset)\n",
" cost = pybop.SumSquaredError(problem)\n",
" optim = pybop.GradientDescent(cost, sigma0=sigma, max_iterations=100)\n",
" x, final_cost = optim.run()\n",
Expand Down
3 changes: 2 additions & 1 deletion examples/notebooks/spm_electrode_design.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@
},
"outputs": [],
"source": [
"problem = pybop.DesignProblem(model, parameters, experiment, init_soc=1.0)\n",
"model.set_init_soc(1.0)\n",
"problem = pybop.DesignProblem(model, parameters, experiment)\n",
"cost = pybop.GravimetricEnergyDensity(problem)"
]
},
Expand Down
8 changes: 3 additions & 5 deletions examples/scripts/cuckoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
true_value=0.67,
),
)
init_soc = 0.7
model.set_init_soc(0.7)
experiment = pybop.Experiment(
[
(
Expand All @@ -32,9 +32,7 @@
),
]
)
values = model.predict(
init_soc=init_soc, experiment=experiment, inputs=parameters.as_dict("true")
)
values = model.predict(experiment=experiment, inputs=parameters.as_dict("true"))

sigma = 0.002
corrupt_values = values["Voltage [V]"].data + np.random.normal(
Expand All @@ -51,7 +49,7 @@
)

# Generate problem, cost function, and optimisation class
problem = pybop.FittingProblem(model, parameters, dataset, init_soc=init_soc)
problem = pybop.FittingProblem(model, parameters, dataset)
cost = pybop.GaussianLogLikelihood(problem, sigma0=sigma * 4)
optim = pybop.Optimisation(
cost,
Expand Down
8 changes: 3 additions & 5 deletions examples/scripts/spm_AdamW.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)

# Generate data
init_soc = 0.5
model.set_init_soc(0.5)
sigma = 0.003
experiment = pybop.Experiment(
[
Expand All @@ -30,7 +30,7 @@
]
* 2
)
values = model.predict(init_soc=init_soc, experiment=experiment)
values = model.predict(experiment=experiment)


def noise(sigma):
Expand All @@ -50,9 +50,7 @@ def noise(sigma):

signal = ["Voltage [V]", "Bulk open-circuit voltage [V]"]
# Generate problem, cost function, and optimisation class
problem = pybop.FittingProblem(
model, parameters, dataset, signal=signal, init_soc=init_soc
)
problem = pybop.FittingProblem(model, parameters, dataset, signal=signal)
cost = pybop.RootMeanSquaredError(problem)
optim = pybop.AdamW(
cost,
Expand Down
8 changes: 3 additions & 5 deletions examples/scripts/spm_NelderMead.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)

# Generate data
init_soc = 0.5
model.set_init_soc(0.5)
sigma = 0.003
experiment = pybop.Experiment(
[
Expand All @@ -30,7 +30,7 @@
]
* 2
)
values = model.predict(init_soc=init_soc, experiment=experiment)
values = model.predict(experiment=experiment)


def noise(sigma):
Expand All @@ -50,9 +50,7 @@ def noise(sigma):

signal = ["Voltage [V]", "Bulk open-circuit voltage [V]"]
# Generate problem, cost function, and optimisation class
problem = pybop.FittingProblem(
model, parameters, dataset, signal=signal, init_soc=init_soc
)
problem = pybop.FittingProblem(model, parameters, dataset, signal=signal)
cost = pybop.RootMeanSquaredError(problem)
optim = pybop.NelderMead(
cost,
Expand Down
3 changes: 2 additions & 1 deletion examples/scripts/spm_scipymin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@

# Define the cost to optimise
signal = ["Voltage [V]"]
problem = pybop.FittingProblem(model, parameters, dataset, signal=signal, init_soc=0.98)
model.set_init_soc(0.98)
problem = pybop.FittingProblem(model, parameters, dataset, signal=signal)
cost = pybop.RootMeanSquaredError(problem)

# Build the optimisation problem
Expand Down
6 changes: 2 additions & 4 deletions examples/scripts/spme_max_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@
experiment = pybop.Experiment(
["Discharge at 1C until 2.5 V (5 seconds period)"],
)
init_soc = 1 # start from full charge
model.set_init_soc(1.0) # start from full charge
signal = ["Voltage [V]", "Current [A]"]

# Generate problem
problem = pybop.DesignProblem(
model, parameters, experiment, signal=signal, init_soc=init_soc
)
problem = pybop.DesignProblem(model, parameters, experiment, signal=signal)

# Generate cost function and optimisation class:
cost = pybop.GravimetricEnergyDensity(problem)
Expand Down
5 changes: 1 addition & 4 deletions examples/standalone/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ def __init__(
check_model=True,
signal=None,
additional_variables=None,
init_soc=None,
):
super().__init__(
parameters, model, check_model, signal, additional_variables, init_soc
)
super().__init__(parameters, model, check_model, signal, additional_variables)
self._dataset = dataset.data

# Check that the dataset contains time and current
Expand Down
7 changes: 1 addition & 6 deletions pybop/observers/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class Observer(BaseProblem):
The signal to observe.
additional_variables : list[str], optional
Additional variables to observe and store in the solution (default: []).
init_soc : float, optional
Initial state of charge (default: None).
"""

# define a subtype for covariance matrices for use by derived classes
Expand All @@ -40,11 +38,8 @@ def __init__(
check_model: bool = True,
signal: Optional[list[str]] = None,
additional_variables: Optional[list[str]] = None,
init_soc: Optional[float] = None,
) -> None:
super().__init__(
parameters, model, check_model, signal, additional_variables, init_soc
)
super().__init__(parameters, model, check_model, signal, additional_variables)
if model._built_model is None:
raise ValueError("Only built models can be used in Observers")

Expand Down
30 changes: 21 additions & 9 deletions pybop/observers/unscented_kalman.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class UnscentedKalmanFilterObserver(Observer):
Flag to indicate if the model should be checked (default: True).
signal: str
The signal to observe.
init_soc : float, optional
Initial state of charge (default: None).
"""

Covariance = np.ndarray
Expand All @@ -51,16 +49,30 @@ def __init__(
check_model: bool = True,
signal: Optional[list[str]] = None,
additional_variables: Optional[list[str]] = None,
init_soc: Optional[float] = None,
) -> None:
super().__init__(
parameters, model, check_model, signal, additional_variables, init_soc
)
if dataset is not None:
self._dataset = dataset.data
# Check that the dataset contains necessary variables
dataset.check([*signal, "Current function [A]"])
dataset = dataset.data

if model is not None:
# Clear any existing built model and its properties
if model._built_model is not None:
model._model_with_set_params = None
model._built_model = None
model._mesh = None
model._disc = None

# Build the model from scratch
model.build(
dataset=dataset,
parameters=parameters,
check_model=check_model,
)

# Check that the dataset contains time and current
dataset.check([*self.signal, "Current function [A]"])
super().__init__(parameters, model, check_model, signal, additional_variables)
if dataset is not None:
self._dataset = dataset

self._time_data = self._dataset["Time [s]"]
self.n_time_data = len(self._time_data)
Expand Down
6 changes: 1 addition & 5 deletions pybop/problems/base_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class BaseProblem:
The signal to observe.
additional_variables : list[str], optional
Additional variables to observe and store in the solution (default: []).
init_soc : float, optional
Initial state of charge (default: None).
Attributes
----------
Expand All @@ -36,7 +34,6 @@ def __init__(
check_model: bool = True,
signal: Optional[list[str]] = None,
additional_variables: Optional[list[str]] = None,
init_soc: Optional[float] = None,
):
if signal is None:
signal = ["Voltage [V]"]
Expand Down Expand Up @@ -71,7 +68,6 @@ def __init__(
if additional_variables is not None:
self.variables.extend(additional_variables)
self.variables = list(set(self.variables))
self.init_soc = init_soc
self.n_outputs = len(self.signal)
self._dataset = None
self._time_data = None
Expand Down Expand Up @@ -180,7 +176,7 @@ def set_target(self, dataset: Dataset):
Parameters
----------
target : np.ndarray
target : Dataset
The target dataset array.
"""
if self.signal is None:
Expand Down
9 changes: 1 addition & 8 deletions pybop/problems/design_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class DesignProblem(BaseProblem):
The signal to fit (default: "Voltage [V]").
additional_variables : list[str], optional
Additional variables to observe and store in the solution (default additions are: ["Time [s]", "Current [A]"]).
init_soc : float, optional
Initial state of charge (default: None).
"""

def __init__(
Expand All @@ -38,11 +36,8 @@ def __init__(
check_model: bool = True,
signal: Optional[list[str]] = None,
additional_variables: Optional[list[str]] = None,
init_soc: Optional[float] = None,
):
super().__init__(
parameters, model, check_model, signal, additional_variables, init_soc
)
super().__init__(parameters, model, check_model, signal, additional_variables)

# Add time and current as additional variables and remove duplicates
self.variables.extend(["Time [s]", "Current [A]"])
Expand All @@ -60,7 +55,6 @@ def __init__(
experiment=self.experiment,
parameters=self.parameters,
check_model=self.check_model,
init_soc=self.init_soc,
)

# Add an example dataset for plotting comparison
Expand All @@ -86,7 +80,6 @@ def _evaluate(self, inputs: Inputs):
sol = self._model.predict(
inputs=inputs,
experiment=self.experiment,
init_soc=self.init_soc,
)

if sol == [np.inf]:
Expand Down
8 changes: 1 addition & 7 deletions pybop/problems/fitting_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class FittingProblem(BaseProblem):
The variable used for fitting (default: "Voltage [V]").
additional_variables : list[str], optional
Additional variables to observe and store in the solution (default: []).
init_soc : float, optional
Initial state of charge (default: None).
Additional Attributes
---------------------
Expand All @@ -50,11 +48,8 @@ def __init__(
check_model: bool = True,
signal: Optional[list[str]] = None,
additional_variables: Optional[list[str]] = None,
init_soc: Optional[float] = None,
):
super().__init__(
parameters, model, check_model, signal, additional_variables, init_soc
)
super().__init__(parameters, model, check_model, signal, additional_variables)
self._dataset = dataset.data
self.parameters.initial_value()

Expand All @@ -79,7 +74,6 @@ def __init__(
dataset=self._dataset,
parameters=self.parameters,
check_model=self.check_model,
init_soc=self.init_soc,
)

def _evaluate(self, inputs: Inputs):
Expand Down
Loading

0 comments on commit 21d87d3

Please sign in to comment.