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

Fixes #320 #321

Merged
merged 15 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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: 1 addition & 1 deletion pybop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
#
from .parameters.parameter import Parameter
from .parameters.parameter_set import ParameterSet
from .parameters.priors import Gaussian, Uniform, Exponential
from .parameters.priors import BasePrior, Gaussian, Uniform, Exponential


#
Expand Down
17 changes: 14 additions & 3 deletions pybop/optimisers/scipy_optimisers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, method=None, bounds=None, maxiter=None, tol=1e-5):
super().__init__()
self.method = method
self.bounds = bounds
self.num_resamples = 40
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is intentionally left without an set/get as #255 enables kwargs to this class, which will be added once that is merged.

self.tol = tol
self.options = {}
self._max_iterations = maxiter
Expand Down Expand Up @@ -56,11 +57,21 @@ def _runoptimise(self, cost_function, x0):
def callback(x):
self.log.append([x])

# Scale the cost function and eliminate nan values
# Check x0 and resample if required
self.cost0 = cost_function(x0)
self.inf_count = 0
if np.isinf(self.cost0):
raise Exception("The initial parameter values return an infinite cost.")
for i in range(1, self.num_resamples):
x0 = cost_function.problem.sample_initial_conditions(seed=i)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be updated in #322 to work with costs without a problem.

self.cost0 = cost_function(x0)
if not np.isinf(self.cost0):
break
if np.isinf(self.cost0):
raise ValueError(
"The initial parameter values return an infinite cost."
)

# Scale the cost function and eliminate nan values
self.inf_count = 0

def cost_wrapper(x):
cost = cost_function(x) / self.cost0
Expand Down
4 changes: 2 additions & 2 deletions pybop/parameters/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(
self.set_bounds(bounds)
self.margin = 1e-4

def rvs(self, n_samples):
def rvs(self, n_samples, random_state=None):
"""
Draw random samples from the parameter's prior distribution.

Expand All @@ -59,7 +59,7 @@ def rvs(self, n_samples):
array-like
An array of samples drawn from the prior distribution within the parameter's bounds.
"""
samples = self.prior.rvs(n_samples)
samples = self.prior.rvs(n_samples, random_state=random_state)

# Constrain samples to be within bounds
if self.bounds is not None:
Expand Down
Loading
Loading