Optimizer

The Optimizer component provides a general framework for closed-loop or supervisory optimization tasks in pyRTC. It is intended for workflows where a control parameter or system configuration needs to be tuned over repeated trials.

Internally, the base class uses Optuna with a CMA-ES sampler and is designed to be subclassed for concrete optimization tasks such as NCPA tuning or loop hyperparameter search.

Soft-RTC Example

The following example shows the general soft-RTC pattern for an optimizer object. In practice, users usually subclass Optimizer and implement task-specific objective and application methods.

from pyRTC.Optimizer import Optimizer

class MyOptimizer(Optimizer):
    def objective(self, trial):
        # Evaluate the current system state and return a scalar score.
        return 0.0

    def applyTrial(self, trial):
        # Push the candidate parameters into the system.
        return

    def applyOptimum(self):
        # Apply the best known parameters once optimization is done.
        return

optimizer = MyOptimizer({"numSteps": 20, "functions": []})
optimizer.optimize()

Hard-RTC Example

For hardware-facing or supervisory workflows, the optimizer can also run in a separate process.

from pyRTC.Pipeline import hardwareLauncher

config = 'path/to/config.yaml'
port = 3006

optimizer = hardwareLauncher('path/to/pyRTC/hardware/myOptimizer.py', config, port)
optimizer.launch()
optimizer.run("optimize")

Implementation Notes

The base class is intentionally generic. A real optimizer subclass usually defines:

  • how a trial is applied to the running system

  • how the objective value is measured

  • how the best result is committed once optimization ends

The examples under pyRTC.hardware are the right starting point for system-specific tuning logic.

Parameters

class pyRTC.Optimizer.Optimizer(conf)[source]

Abstract Optuna-backed optimization driver.

Optimizer is meant to be subclassed by hardware- or algorithm-specific optimizers in pyRTC.hardware. The base class owns the Optuna study, default CMA-ES sampler choice, and the helper methods used to run a full study or advance one trial at a time.

Subclasses are responsible for defining the objective function and for applying candidate parameters to the system under test.

Attributes

namestr

Name of the optimizer component.

studyoptuna.Study

The Optuna study object, initialized with a CMA-ES sampler.

numStepsint

Number of steps/trials to perform during optimization.

Methods

objective():

Defines the objective function for the optimization.

optimize():

Performs the optimization process.

applyOptimum():

Applies the optimum values obtained from the optimization process.

applyTrial(trial):

Applies a given trial.

applyNext():

Requests and applies the next trial from the study.

applyNext()[source]

Requests and applies the next trial from the study.

This method obtains the next trial from the study and applies it using the applyTrial method.

applyOptimum()[source]

Applies the optimum values obtained from the optimization process.

This method should be implemented to apply the optimal parameters found during the optimization to the system or component.

applyTrial(trial)[source]

Applies a given trial.

Parameters:

trial – The trial object containing the parameters to be applied.

objective()[source]

Defines the objective function for the optimization.

This method should be overridden by subclasses to provide the specific objective function for the optimization task.

Returns:

The objective value to be optimized.

optimize()[source]

Performs the optimization process.

This method runs the optimization process using the defined objective function and the number of steps specified in the configuration.

start()

Start the registered real-time functions.

stop()

Stops the registered real-time functions.