Skip to content

Generator Factory

geecs_scanner.optimization.generators.generator_factory

Generator factory for optimization algorithms.

This module provides a factory interface for creating Xopt generator instances used in automated parameter optimization. It supports various optimization algorithms including random sampling, Bayesian optimization, and specialized generators for specific use cases.

The factory pattern allows for easy configuration and instantiation of different optimization algorithms while maintaining a consistent interface for the optimization framework.

Functions:

Name Description
build_generator_from_config

Create generator instance from configuration dictionary.

Constants

PREDEFINED_GENERATORS : dict Dictionary mapping generator names to factory functions.

Notes

The factory supports the following predefined generators: - "random": Random sampling generator - "bayes_default": Expected improvement Bayesian optimization - "bayes_cheetah": Cheetah-based Bayesian optimization (requires cheetah package) - "multipoint_bax_alignment": Multipoint BAX alignment (requires configuration overrides)

New generators can be added by extending the PREDEFINED_GENERATORS dictionary with appropriate factory functions.

Attributes:

Name Type Description
PREDEFINED_GENERATORS dict[str, Callable[[VOCS, Dict[str, Any]], Any]]

Attributes

PREDEFINED_GENERATORS module-attribute

PREDEFINED_GENERATORS: dict[str, Callable[[VOCS, Dict[str, Any]], Any]] = {'random': lambda vocs, overrides: RandomGenerator(vocs=vocs), 'bayes_default': lambda vocs, overrides: ExpectedImprovementGenerator(vocs=vocs, gp_constructor=StandardModelConstructor(use_low_noise_prior=False)), 'bayes_ucb': lambda vocs, overrides: UpperConfidenceBoundGenerator(vocs=vocs, gp_constructor=StandardModelConstructor(use_low_noise_prior=False), beta=get('beta', 2.0)), 'bayes_ucb_explore': lambda vocs, overrides: UpperConfidenceBoundGenerator(vocs=vocs, gp_constructor=StandardModelConstructor(use_low_noise_prior=False), beta=get('beta', 10.0)), 'bayes_cheetah': lambda vocs, overrides: _load_cheetah_generator(vocs), 'bayes_turbo_standard': lambda vocs, overrides: _make_bayes_turbo(vocs), 'bayes_turbo_ucb': lambda vocs, overrides: _make_bayes_turbo_ucb(vocs, beta=get('beta', 2.0)), 'bayes_turbo_HTU_e_beam_brightness': lambda vocs, overrides: _make_bayes_turbo(vocs, success_tolerance=2, failure_tolerance=2, length=0.25, length_max=2.0, length_min=0.0078125, scale_factor=2.0), 'multipoint_bax_alignment': lambda vocs, overrides: make_multipoint_bax_alignment(vocs, overrides), 'multipoint_bax_alignment_l2': lambda vocs, overrides: make_multipoint_bax_alignment_l2(vocs, overrides), 'multipoint_bax_alignment_simulated': lambda vocs, overrides: make_multipoint_bax_alignment(vocs, overrides)}

Functions

build_generator_from_config

build_generator_from_config(config: Dict[str, Any], vocs: VOCS)

Build Xopt generator instance from configuration dictionary.

Creates and returns an optimization generator based on the specified algorithm name in the configuration. This factory function provides a unified interface for instantiating different types of optimization generators while handling their specific initialization requirements.

Parameters:

Name Type Description Default
config dict

Configuration dictionary containing generator settings. Must include a 'name' field specifying the generator type.

required
vocs VOCS

Variables, Objectives, and Constraints Specification defining the optimization problem structure.

required

Returns:

Type Description
Generator

Configured Xopt generator instance ready for optimization.

Raises:

Type Description
ValueError

If the specified generator name is not recognized or supported.

KeyError

If the configuration dictionary is missing required fields.

Notes

Supported generator names: - "random": Uniform random sampling within variable bounds - "bayes_default": Expected improvement Bayesian optimization - "bayes_ucb": Upper confidence bound (configurable beta via overrides; noise-robust alternative to EI when σ_noise is comparable to signal range) - "bayes_ucb_explore": UCB with a high default beta (10.0) so the acquisition is dominated by σ — practical "pure exploration" on a single-objective VOCS - "bayes_cheetah": Cheetah-based Bayesian optimization (requires cheetah) - "bayes_turbo_standard": EI within a TuRBO trust region - "bayes_turbo_ucb": UCB within a TuRBO trust region (configurable beta) - "multipoint_bax_alignment": Multipoint BAX alignment with custom overrides

The generator instances are created using lambda functions stored in the PREDEFINED_GENERATORS dictionary, allowing for easy extension with new generator types. Any keys in config other than name are passed to the corresponding factory function as generator-specific overrides.

Source code in geecs_scanner/optimization/generators/generator_factory.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def build_generator_from_config(config: Dict[str, Any], vocs: VOCS):
    """
    Build Xopt generator instance from configuration dictionary.

    Creates and returns an optimization generator based on the specified
    algorithm name in the configuration. This factory function provides
    a unified interface for instantiating different types of optimization
    generators while handling their specific initialization requirements.

    Parameters
    ----------
    config : dict
        Configuration dictionary containing generator settings. Must include
        a 'name' field specifying the generator type.
    vocs : VOCS
        Variables, Objectives, and Constraints Specification defining the
        optimization problem structure.

    Returns
    -------
    Generator
        Configured Xopt generator instance ready for optimization.

    Raises
    ------
    ValueError
        If the specified generator name is not recognized or supported.
    KeyError
        If the configuration dictionary is missing required fields.

    Notes
    -----
    Supported generator names:
    - "random": Uniform random sampling within variable bounds
    - "bayes_default": Expected improvement Bayesian optimization
    - "bayes_ucb": Upper confidence bound (configurable ``beta`` via overrides;
      noise-robust alternative to EI when σ_noise is comparable to signal range)
    - "bayes_ucb_explore": UCB with a high default ``beta`` (10.0) so the
      acquisition is dominated by σ — practical "pure exploration" on a
      single-objective VOCS
    - "bayes_cheetah": Cheetah-based Bayesian optimization (requires cheetah)
    - "bayes_turbo_standard": EI within a TuRBO trust region
    - "bayes_turbo_ucb": UCB within a TuRBO trust region (configurable ``beta``)
    - "multipoint_bax_alignment": Multipoint BAX alignment with custom overrides

    The generator instances are created using lambda functions stored in
    the PREDEFINED_GENERATORS dictionary, allowing for easy extension
    with new generator types. Any keys in `config` other than ``name``
    are passed to the corresponding factory function as generator-specific
    overrides.
    """
    generator_name = config["name"]
    overrides = dict(config)
    overrides.pop("name", None)
    try:
        return PREDEFINED_GENERATORS[generator_name](vocs, overrides)
    except KeyError:
        raise ValueError(f"Unsupported or undefined generator name: '{generator_name}'")