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

Select: problem-specific minimize method for SaCeSS #1339

Open
wants to merge 13 commits into
base: select_use_old_calibrations
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 pypesto/select/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from . import postprocessors
from .misc import model_to_pypesto_problem
from .misc import SacessMinimizeMethod, model_to_pypesto_problem
from .problem import Problem

try:
Expand Down
52 changes: 52 additions & 0 deletions pypesto/select/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
from petab_select.constants import PETAB_PROBLEM

from ..objective import Objective
from ..optimize.ess import (
SacessOptimizer,
get_default_ess_options,
)
from ..petab import PetabImporter
from ..problem import Problem

Expand Down Expand Up @@ -163,3 +167,51 @@ def correct_x_guesses(
corrected_x_guess.append(corrected_value)
corrected_x_guesses.append(corrected_x_guess)
return corrected_x_guesses


class SacessMinimizeMethod:
"""Create a minimize method for SaCeSS that adapts to each problem.

When a pyPESTO SaCeSS optimizer is created, it takes the problem
dimension as input. Hence, an optimizer needs to be constructed for
each problem. Objects of this class act like a minimize method for model
selection, but a new problem-specific SaCeSS optimizer will be created
every time a model is minimized.

Class attributes correspond to pyPESTO's SaCeSS optimizer, and are
documented there.
"""

def __init__(
self,
num_workers: int,
local_optimizer,
max_walltime_s: int,
dweindl marked this conversation as resolved.
Show resolved Hide resolved
):
"""Construct a minimize-like object."""
self.num_workers = num_workers
self.local_optimizer = local_optimizer
self.max_walltime_s = max_walltime_s

def __call__(self, problem: Problem, **minimize_options):
"""Create then run a problem-specific sacess optimizer."""
# create optimizer
ess_init_args = get_default_ess_options(
num_workers=self.num_workers,
dim=problem.dim,
)
for x in ess_init_args:
x["local_optimizer"] = self.local_optimizer
ess = SacessOptimizer(
max_walltime_s=self.max_walltime_s,
sacess_loglevel=logging.DEBUG,
ess_loglevel=logging.WARNING,
dilpath marked this conversation as resolved.
Show resolved Hide resolved
ess_init_args=ess_init_args,
)

# optimize
result = ess.minimize(
problem=problem,
**minimize_options,
)
return result
18 changes: 16 additions & 2 deletions pypesto/select/model_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,23 @@ def __init__(
def minimize(self) -> Result:
"""Optimize the model.

Returns:
Returns
-------
The optimization result.
"""
return self.minimize_method(
self.pypesto_problem,
**self.minimize_options,
)

def set_result_from_model(self, model):
def set_result_from_model(self, model) -> None:
"""Set the calibration result from a previously-calibrated model.

Parameters
----------
model:
The previously-calibrated model.
"""
self.model.criteria = model.criteria
self.model.estimated_parameters = model.estimated_parameters
if self.postprocessor is not None:
Expand Down Expand Up @@ -201,6 +209,12 @@ def create_fake_pypesto_result_from_fval(
----------
fval:
The objective function value.
evaluation_time:
CPU time taken to compute the objective function value.

Returns
-------
The dummy result.
"""
result = Result()

Expand Down
Loading