From 3037d18829ad603048105898319d7b2ec038d0e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Schm=C3=B6lder?= Date: Mon, 26 Feb 2024 08:37:57 +0100 Subject: [PATCH] Add optional pre-processing step before setting optimization variable --- CADETProcess/optimization/optimizationProblem.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/CADETProcess/optimization/optimizationProblem.py b/CADETProcess/optimization/optimizationProblem.py index c1da8e25..e872a1b6 100644 --- a/CADETProcess/optimization/optimizationProblem.py +++ b/CADETProcess/optimization/optimizationProblem.py @@ -282,7 +282,8 @@ def variable_values(self): def add_variable( self, name, evaluation_objects=-1, parameter_path=None, - lb=-math.inf, ub=math.inf, transform=None, indices=None): + lb=-math.inf, ub=math.inf, transform=None, indices=None, + pre_processing=None): """Add optimization variable to the OptimizationProblem. The function encapsulates the creation of OptimizationVariable objects @@ -309,6 +310,9 @@ def add_variable( indices : int or tuple, optional Indices for variables that modify entries of a parameter array. If None, variable is assumed to be index independent. + pre_processing : callable, optional + Additional step to process the value before setting it. This function must + accept a single argument (the value) and return the processed value. Raises ------ @@ -349,6 +353,7 @@ def add_variable( name, evaluation_objects, parameter_path, lb=lb, ub=ub, transform=transform, indices=indices, + pre_processing=pre_processing, ) self._variables.append(var) @@ -3039,6 +3044,9 @@ class OptimizationVariable: precision : int, optional Number of significant figures to which variable can be rounded. If None, variable is not rounded. The default is None. + pre_processing : callable, optional + Additional step to process the value before setting it. This function must + accept a single argument (the value) and return the processed value. Raises ------ @@ -3051,6 +3059,7 @@ class OptimizationVariable: def __init__( self, name, evaluation_objects=None, parameter_path=None, lb=-math.inf, ub=math.inf, transform=None, indices=None, precision=None, + pre_processing=None ): self.name = name self._value = None @@ -3091,6 +3100,8 @@ def __init__( self._dependencies = [] self._dependency_transform = None + self.pre_processing = pre_processing + @property def parameter_path(self): """str: Path of the evaluation_object parameter in dot notation.""" @@ -3450,6 +3461,8 @@ def set_value(self, value): def _set_value(self, evaluation_object, value): """Set the value to the evaluation object.""" + if self.pre_processing is not None: + value = self.pre_processing(value) parameter_descriptor = self._parameter_descriptor(evaluation_object) if parameter_descriptor is not None: performer_obj = self._performer_obj(evaluation_object)