Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactors OptimisationResult class, adds finite cost check for identified parameters #527

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

## Breaking Changes

- [#526](https://github.com/pybop-team/PyBOP/pull/526) - Refactor `OptimisationResults` classes, with `optim.run()` now return the full object. Adds finite cost value check for optimised parameters.

# [v24.9.1](https://github.com/pybop-team/PyBOP/tree/v24.9.0) - 2024-09-16

## Features
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/benchmark_track_parameterisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,5 @@ def results_tracking(self, model, parameter_set, optimiser):
parameter_set (str): The name of the parameter set being used (unused).
optimiser (pybop.Optimiser): The optimizer class being used (unused).
"""
x, _ = self.optim.run()
return x
results = self.optim.run()
return results.x
6 changes: 3 additions & 3 deletions examples/notebooks/adamw_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
},
"outputs": [],
"source": [
"x, final_cost = optim.run()"
"results = optim.run()"
]
},
{
Expand Down Expand Up @@ -338,7 +338,7 @@
}
],
"source": [
"x # This will output the estimated parameters"
"results.x # This will output the estimated parameters"
]
},
{
Expand Down Expand Up @@ -434,7 +434,7 @@
}
],
"source": [
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=results.x, title=\"Optimised Comparison\");"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions examples/notebooks/ecm_trust-constr.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,14 @@
}
],
"source": [
"x, final_cost = optim.run()\n",
"print(\"Estimated parameters:\", x)\n",
"results = optim.run()\n",
"print(\"Estimated parameters:\", results.x)\n",
"\n",
"# Plot the time series\n",
"pybop.plot_dataset(dataset)\n",
"\n",
"# Plot the timeseries output\n",
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\")\n",
"pybop.quick_plot(problem, problem_inputs=results.x, title=\"Optimised Comparison\")\n",
"\n",
"# Plot convergence\n",
"pybop.plot_convergence(optim)\n",
Expand Down
10 changes: 5 additions & 5 deletions examples/notebooks/electrode_balancing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@
],
"source": [
"optim = pybop.SciPyMinimize(cost, max_iterations=300)\n",
"x, final_cost = optim.run()\n",
"results = optim.run()\n",
"print(\"Initial parameters:\", optim.x0)\n",
"print(\"Estimated parameters:\", x)"
"print(\"Estimated parameters:\", results.x)"
]
},
{
Expand Down Expand Up @@ -315,7 +315,7 @@
}
],
"source": [
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=results.x, title=\"Optimised Comparison\");"
]
},
{
Expand Down Expand Up @@ -387,8 +387,8 @@
" ),\n",
")\n",
"\n",
"Q = x[1]\n",
"sto_0 = x[0]\n",
"Q = results.x[1]\n",
"sto_0 = results.x[0]\n",
"\n",
"sto = measured_data[\"Capacity [A.h]\"].to_numpy() / Q + sto_0\n",
"\n",
Expand Down
8 changes: 4 additions & 4 deletions examples/notebooks/energy_based_electrode_design.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@
}
],
"source": [
"x, final_cost = optim.run()\n",
"print(\"Estimated parameters:\", x)\n",
"results = optim.run()\n",
"print(\"Estimated parameters:\", results.x)\n",
"print(f\"Initial gravimetric energy density: {cost(optim.x0):.2f} Wh.kg-1\")\n",
"print(f\"Optimised gravimetric energy density: {final_cost:.2f} Wh.kg-1\")"
"print(f\"Optimised gravimetric energy density: {results.final_cost:.2f} Wh.kg-1\")"
]
},
{
Expand Down Expand Up @@ -339,7 +339,7 @@
}
],
"source": [
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=results.x, title=\"Optimised Comparison\");"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions examples/notebooks/equivalent_circuit_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@
" max_unchanged_iterations=50,\n",
" max_iterations=250,\n",
")\n",
"x, final_cost = optim.run()\n",
"results = optim.run()\n",
"print(\"Initial parameters:\", optim.x0)\n",
"print(\"Estimated parameters:\", x)"
"print(\"Estimated parameters:\", results.x)"
]
},
{
Expand Down Expand Up @@ -402,7 +402,7 @@
}
],
"source": [
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=results.x, title=\"Optimised Comparison\");"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@
" max_unchanged_iterations=30,\n",
" max_iterations=100,\n",
")\n",
"x, final_cost = optim.run()\n",
"results = optim.run()\n",
"print(\"Initial parameters:\", optim.x0)\n",
"print(\"Estimated parameters:\", x)"
"print(\"Estimated parameters:\", results.x)"
]
},
{
Expand Down Expand Up @@ -332,7 +332,7 @@
}
],
"source": [
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=results.x, title=\"Optimised Comparison\");"
]
},
{
Expand Down Expand Up @@ -508,7 +508,7 @@
],
"source": [
"sol = model.predict(\n",
" inputs=x,\n",
" inputs=results.x,\n",
" experiment=experiment,\n",
" parameter_set=parameter_set,\n",
" initial_state={\"Initial SoC\": 0.95},\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,9 @@
" max_unchanged_iterations=20,\n",
" max_iterations=100,\n",
")\n",
"x, final_cost = optim.run()\n",
"results = optim.run()\n",
"print(\"Initial parameters:\", optim.x0)\n",
"print(\"Estimated parameters:\", x)"
"print(\"Estimated parameters:\", results.x)"
]
},
{
Expand Down Expand Up @@ -469,7 +469,7 @@
}
],
"source": [
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=results.x, title=\"Optimised Comparison\");"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions examples/notebooks/maximum_a_posteriori.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@
" max_iterations=100,\n",
")\n",
"start_time = time.time()\n",
"x, final_cost = optim.run()\n",
"print(f\"Inferred Parameters: {x} in {time.time() - start_time} seconds\")\n",
"results = optim.run()\n",
"print(f\"Inferred Parameters: {results.x} in {time.time() - start_time} seconds\")\n",
"print(f\"True Parameters: {parameters.true_value()}\")\n",
"pybop.plot_parameters(optim);"
]
Expand Down
4 changes: 2 additions & 2 deletions examples/notebooks/multi_model_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,9 @@
" optim = pybop.XNES(\n",
" cost, verbose=True, max_iterations=60, max_unchanged_iterations=15\n",
" )\n",
" x, final_cost = optim.run()\n",
" results = optim.run()\n",
" optims.append(optim)\n",
" xs.append(x)"
" xs.append(results.x)"
]
},
{
Expand Down
12 changes: 6 additions & 6 deletions examples/notebooks/multi_optimiser_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,9 @@
" optim = optimiser(\n",
" cost, sigma0=sigma0, max_unchanged_iterations=20, max_iterations=60\n",
" )\n",
" x, _ = optim.run()\n",
" results = optim.run()\n",
" optims.append(optim)\n",
" xs.append(x)"
" xs.append(results.x)"
]
},
{
Expand Down Expand Up @@ -394,9 +394,9 @@
"for optimiser in non_gradient_optimisers:\n",
" print(f\"Running {optimiser.__name__}\")\n",
" optim = optimiser(cost, max_unchanged_iterations=20, max_iterations=60)\n",
" x, _ = optim.run()\n",
" results = optim.run()\n",
" optims.append(optim)\n",
" xs.append(x)"
" xs.append(results.x)"
]
},
{
Expand Down Expand Up @@ -424,9 +424,9 @@
"for optimiser in scipy_optimisers:\n",
" print(f\"Running {optimiser.__name__}\")\n",
" optim = optimiser(cost, max_iterations=60)\n",
" x, _ = optim.run()\n",
" results = optim.run()\n",
" optims.append(optim)\n",
" xs.append(x)"
" xs.append(results.x)"
]
},
{
Expand Down
14 changes: 7 additions & 7 deletions examples/notebooks/optimiser_calibration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@
},
"outputs": [],
"source": [
"x, final_cost = optim.run()"
"results = optim.run()"
]
},
{
Expand Down Expand Up @@ -332,7 +332,7 @@
}
],
"source": [
"x # This will output the estimated parameters"
"results.x # This will output the estimated parameters"
]
},
{
Expand Down Expand Up @@ -406,7 +406,7 @@
}
],
"source": [
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=results.x, title=\"Optimised Comparison\");"
]
},
{
Expand Down Expand Up @@ -497,9 +497,9 @@
" 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",
" results = optim.run()\n",
" optims.append(optim)\n",
" xs.append(x)"
" xs.append(results.x)"
]
},
{
Expand Down Expand Up @@ -1419,8 +1419,8 @@
],
"source": [
"optim = pybop.Optimisation(cost, optimiser=pybop.GradientDescent, sigma0=0.08)\n",
"x, final_cost = optim.run()\n",
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
"results = optim.run()\n",
"pybop.quick_plot(problem, problem_inputs=results.x, title=\"Optimised Comparison\");"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions examples/notebooks/optimiser_interface.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
"optim_one.set_max_iterations(\n",
" 50\n",
") # Alternative set() / get() methods for PINTS optimisers\n",
"x1, final_cost = optim_one.run()"
"results = optim_one.run()"
]
},
{
Expand All @@ -191,7 +191,7 @@
"optim_two.set_max_iterations(\n",
" 50\n",
") # Alternative set() / get() methods for PINTS optimisers\n",
"x2, final_cost = optim_two.run()"
"results2 = optim_two.run()"
]
},
{
Expand Down Expand Up @@ -247,8 +247,8 @@
}
],
"source": [
"print(\"Estimated parameters x1:\", x1)\n",
"print(\"Estimated parameters x2:\", x2)"
"print(\"Estimated parameters x1:\", results.x)\n",
"print(\"Estimated parameters x2:\", results2.x)"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions examples/notebooks/pouch_cell_identification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@
},
"outputs": [],
"source": [
"x, final_cost = optim.run()"
"results = optim.run()"
]
},
{
Expand Down Expand Up @@ -365,7 +365,7 @@
}
],
"source": [
"x # This will output the estimated parameters"
"results.x # This will output the estimated parameters"
]
},
{
Expand Down Expand Up @@ -461,7 +461,7 @@
}
],
"source": [
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=results.x, title=\"Optimised Comparison\");"
]
},
{
Expand Down Expand Up @@ -513,7 +513,7 @@
}
],
"source": [
"sol = problem.evaluate(parameters.as_dict(x))\n",
"sol = problem.evaluate(parameters.as_dict(results.x))\n",
"\n",
"go.Figure(\n",
" [\n",
Expand Down
8 changes: 4 additions & 4 deletions examples/notebooks/single_pulse_circuit_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,9 @@
],
"source": [
"optim = pybop.PSO(cost, max_unchanged_iterations=55, threshold=1e-6)\n",
"x, final_cost = optim.run()\n",
"results = optim.run()\n",
"print(\"Initial parameters:\", optim.x0)\n",
"print(\"Estimated parameters:\", x)"
"print(\"Estimated parameters:\", results.x)"
]
},
{
Expand Down Expand Up @@ -539,7 +539,7 @@
}
],
"source": [
"pybop.quick_plot(problem, problem_inputs=x, title=\"Optimised Comparison\");"
"pybop.quick_plot(problem, problem_inputs=results.x, title=\"Optimised Comparison\");"
]
},
{
Expand Down Expand Up @@ -746,7 +746,7 @@
}
],
"source": [
"pybop.quick_plot(problem, problem_inputs=x, title=\"Parameter Extrapolation\");"
"pybop.quick_plot(problem, problem_inputs=results.x, title=\"Parameter Extrapolation\");"
]
},
{
Expand Down
Loading
Loading