pyfemtet.opt.optimizer package

Subpackages

Module contents

class pyfemtet.opt.optimizer.AbstractOptimizer[source]

Bases: OptimizationData

add_categorical_parameter(*args, **kwargs)[source]
Parameters:

self (AbstractOptimizer) –

add_constant_value(*args, **kwargs)[source]
Parameters:

self (AbstractOptimizer) –

add_constraint(*args, **kwargs)[source]
Parameters:

self (AbstractOptimizer) –

add_expression(*args, **kwargs)[source]
Parameters:

self (AbstractOptimizer) –

add_expression_string(*args, **kwargs)[source]
Parameters:

self (AbstractOptimizer) –

add_expression_sympy(*args, **kwargs)[source]
Parameters:

self (AbstractOptimizer) –

add_fem(fem)[source]
Parameters:

fem (AbstractFEMInterface) –

Return type:

OptimizationDataPerFEM

add_objective(*args, **kwargs)[source]
Parameters:

self (AbstractOptimizer) –

add_objectives(*args, **kwargs)[source]
Parameters:

self (AbstractOptimizer) –

add_other_output(*args, **kwargs)[source]
Parameters:

self (AbstractOptimizer) –

add_parameter(*args, **kwargs)[source]
Parameters:

self (AbstractOptimizer) –

add_sub_fidelity_model(name, sub_fidelity_model, fidelity)[source]
Parameters:
add_trial(parameters)[source]
Parameters:

parameters (dict[str, str | Real]) –

entire_status: WorkerStatus
property fem: AbstractFEMInterface | tuple[AbstractFEMInterface, ...]
fidelity: float | str | None
get_parameter(format='dict')[source]
Parameters:

format (Literal['dict', 'values', 'raw']) –

get_variables(format='dict')[source]
Parameters:

format (Literal['dict', 'values', 'raw']) –

history: History
include_queued_in_n_trials: bool
n_trials: int | None
run()[source]
Return type:

None

seed: int | None
set_solve_condition(fun)[source]
Parameters:

fun (Callable[[History], bool]) –

set_termination_condition(fun)[source]
Parameters:

fun (Callable[[History], bool] | None) –

sub_fidelity_models: SubFidelityModels | None
sub_fidelity_name: str
timeout: float | None
worker_status: WorkerStatus
worker_status_list: list[WorkerStatus]
class pyfemtet.opt.optimizer.OptunaOptimizer(sampler_class=None, sampler_kwargs=None)[source]

Bases: AbstractOptimizer

An optimizer class utilizing Optuna for hyperparameter optimization.

This class provides an interface to conduct optimization studies using Optuna. It manages the study lifecycle, sampler configuration, and trial execution.

study_name

Name of the Optuna study.

Type:

str

storage

Storage URL or object for the Optuna study.

Type:

str | optuna.storages.BaseStorage

storage_path

Path to the Optuna study storage.

Type:

str

current_trial

The current Optuna trial being evaluated.

Type:

optuna.trial.Trial | None

sampler_class

The class of the Optuna sampler to use.

Type:

type[optuna.samplers.BaseSampler]

sampler_kwargs

Keyword arguments to initialize the sampler.

Type:

dict

n_trials

Number of trials to run in the study.

Type:

int | None

timeout

Maximum time allowed for the optimization.

Type:

float | None

callbacks

List of callback functions to invoke during optimization.

Type:

list

Parameters:
  • sampler_class (type[optuna.samplers.BaseSampler], optional) – The sampler class for suggesting parameter values. Defaults to TPESampler if None.

  • sampler_kwargs (dict[str, ...], optional) – Dictionary of keyword arguments for the sampler. Defaults to an empty dictionary.

Raises:

None

Examples

>>> optimizer = OptunaOptimizer()
>>> optimizer.n_trials = 100
>>> optimizer.timeout = 600
>>> # Further configuration and usage...
add_categorical_parameter(name, initial_value, choices, properties=None, *, pass_to_fem=True, fix=False)[source]
Parameters:
Return type:

None

add_parameter(name, initial_value, lower_bound=None, upper_bound=None, step=None, properties=None, *, pass_to_fem=True, fix=False)[source]
Parameters:
  • name (str) –

  • initial_value (float) –

  • lower_bound (float | None) –

  • upper_bound (float | None) –

  • step (float | None) –

  • properties (dict[str, ...] | None) –

  • pass_to_fem (bool) –

  • fix (bool) –

Return type:

None

callbacks: list
current_trial: optuna.trial.Trial | None
entire_status: WorkerStatus
fidelity: Fidelity | None
history: History
include_queued_in_n_trials: bool
n_trials: int | None
run()[source]
sampler_class: type[optuna.samplers.BaseSampler]
sampler_kwargs: dict
seed: int | None
solve_condition: Callable[[History], bool]
storage: str | optuna.storages.BaseStorage
storage_path: str
study_name = 'pyfemtet-study'
sub_fidelity_models: SubFidelityModels | None
sub_fidelity_name: str
termination_condition: Callable[[History], bool]
timeout: float | None
trial_queue: TrialQueue
worker_status: WorkerStatus
worker_status_list: list[WorkerStatus]
class pyfemtet.opt.optimizer.PartialOptimizeACQFConfig(*, gen_candidates='scipy', timeout_sec=None, method=None, scipy_minimize_kwargs=None, constraint_enhancement=None, constraint_scaling=1000000.0)[source]

Bases: object

Parameters:
  • gen_candidates (str) –

  • timeout_sec (float) –

  • method (str) –

  • scipy_minimize_kwargs (dict) –

  • constraint_enhancement (float) –

  • constraint_scaling (float) –

default_method = 'SLSQP'
class pyfemtet.opt.optimizer.PoFBoTorchSampler(*, candidates_func=None, constraints_func=None, n_startup_trials=10, consider_running_trials=False, independent_sampler=None, seed=None, device=None, observation_noise=None, partial_optimize_acqf_kwargs=None, pof_config=None)[source]

Bases: BoTorchSampler

Note

Added in v2.4.0 as an experimental feature. The interface may change in newer versions without prior notice. See https://github.com/optuna/optuna/releases/tag/v2.4.0.

Parameters:
  • candidates_func (CandidateFunc) –

  • constraints_func (Callable[[FrozenTrial], Sequence[float]] | None) –

  • n_startup_trials (int) –

  • consider_running_trials (bool) –

  • independent_sampler (BaseSampler | None) –

  • seed (int | None) –

  • device (torch.device | None) –

  • observation_noise (float | str | None) –

  • partial_optimize_acqf_kwargs (PartialOptimizeACQFConfig) –

  • pof_config (PoFConfig) –

current_gp_model: SingleTaskGP | None
observation_noise: float | str | None
partial_optimize_acqf_kwargs: PartialOptimizeACQFConfig
pof_config: PoFConfig
pyfemtet_optimizer: AbstractOptimizer
sample_relative(study, trial, search_space)[source]

Sample parameters in a given search space.

This method is called once at the beginning of each trial, i.e., right before the evaluation of the objective function. This method is suitable for sampling algorithms that use relationship between parameters such as Gaussian Process and CMA-ES.

Note

The failed trials are ignored by any build-in samplers when they sample new parameters. Thus, failed trials are regarded as deleted in the samplers’ perspective.

Parameters:
  • study (Study) – Target study object.

  • trial (FrozenTrial) – Target trial object. Take a copy before modifying this object.

  • search_space (dict[str, BaseDistribution]) – The search space returned by infer_relative_search_space().

Returns:

A dictionary containing the parameter names and the values.

Return type:

dict[str, Any]

train_model_c(study, search_space, feature_dim_bound=None, feature_param=None)[source]
Parameters:
  • feature_dim_bound (list[float] | None) –

  • feature_param (float | ndarray | None) –

class pyfemtet.opt.optimizer.PoFConfig(consider_pof: 'bool' = True, consider_explicit_hard_constraint: 'bool' = True, _states_to_consider_pof: 'list[PFTrialState]' = <factory>, feasibility_cdf_threshold: 'float | str' = 0.5, feasibility_noise: 'float | str | None' = None, remove_hard_constraints_from_gp: 'bool' = False)[source]

Bases: object

Parameters:
  • consider_pof (bool) –

  • consider_explicit_hard_constraint (bool) –

  • _states_to_consider_pof (list[TrialState]) –

  • feasibility_cdf_threshold (float | str) –

  • feasibility_noise (float | str | None) –

  • remove_hard_constraints_from_gp (bool) –

consider_explicit_hard_constraint: bool = True
consider_pof: bool = True
feasibility_cdf_threshold: float | str = 0.5
feasibility_noise: float | str | None = None
remove_hard_constraints_from_gp: bool = False
class pyfemtet.opt.optimizer.ScipyOptimizer(method=None, tol=None)[source]

Bases: AbstractOptimizer

Optimizer class that utilizes SciPy optimization methods.

This class serves as a wrapper around SciPy’s optimization routines, allowing customization of the optimization method, tolerance, and options. It also provides mechanisms for handling constraints with enhancement and scaling.

method

The optimization method to use (e.g., ‘BFGS’, ‘Nelder-Mead’).

Type:

str

tol

Tolerance for termination.

Type:

float or None

options

Additional options to pass to the SciPy optimizer.

Type:

dict

constraint_enhancement

Small value added to enhance constraint handling.

Type:

float

constraint_scaling

Scaling factor applied to constraints.

Type:

float

Parameters:
  • method (str) – The optimization method to use (e.g., ‘BFGS’, ‘Nelder-Mead’).

  • tol (float or None) – Tolerance for termination.

add_sub_fidelity_model(name, sub_fidelity_model, fidelity)[source]
Parameters:
add_trial(parameters)[source]
Parameters:

parameters (dict[str, str | Real]) –

run()[source]
class pyfemtet.opt.optimizer.SubFidelityModel[source]

Bases: AbstractOptimizer