From e832ac09725c0f0abbf05232700e2be21a06d069 Mon Sep 17 00:00:00 2001 From: "r.jaepel" Date: Mon, 25 Mar 2024 16:22:05 +0100 Subject: [PATCH] Fix non-linearity check during linear constraints Fix non-linearity check during linear constraints to only evaluate variables with a non-zero influence in the constraint Expand a test-case to also test that behavior --- CADETProcess/optimization/optimizationProblem.py | 9 ++++----- tests/optimization_problem_fixtures.py | 11 ++++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/CADETProcess/optimization/optimizationProblem.py b/CADETProcess/optimization/optimizationProblem.py index e8f39e70..cbe27860 100644 --- a/CADETProcess/optimization/optimizationProblem.py +++ b/CADETProcess/optimization/optimizationProblem.py @@ -37,7 +37,6 @@ ) from CADETProcess.metric import MetricBase - from CADETProcess.optimization import Individual, Population from CADETProcess.optimization import ResultsCache @@ -2170,7 +2169,7 @@ def A_transformed(self): if isinstance(t, NoTransform): continue - if not t.is_linear: + if a[j] != 0 and not t.is_linear: raise CADETProcessError( "Non-linear transform was used in linear constraints." ) @@ -2250,7 +2249,7 @@ def b_transformed(self): if isinstance(t, NoTransform): continue - if not t.is_linear: + if a[j] != 0 and not t.is_linear: raise CADETProcessError( "Non-linear transform was used in linear constraints." ) @@ -2447,7 +2446,7 @@ def Aeq_transformed(self): if isinstance(t, NoTransform): continue - if not t.is_linear: + if aeq[j] != 0 and not t.is_linear: raise CADETProcessError( "Non-linear transform was used in linear constraints." ) @@ -2527,7 +2526,7 @@ def beq_transformed(self): if isinstance(t, NoTransform): continue - if not t.is_linear: + if aeq[j] != 0 and not t.is_linear: raise CADETProcessError( "Non-linear transform was used in linear constraints." ) diff --git a/tests/optimization_problem_fixtures.py b/tests/optimization_problem_fixtures.py index be75735f..8da45ef6 100644 --- a/tests/optimization_problem_fixtures.py +++ b/tests/optimization_problem_fixtures.py @@ -165,29 +165,30 @@ def __init__(self, transform=None, has_evaluator=False, *args, **kwargs): def setup_variables(self, transform): self.add_variable('var_0', lb=-2, ub=2, transform=transform) self.add_variable('var_1', lb=-2, ub=2, transform=transform) - + self.add_variable('var_2', lb=0, ub=2, transform="log") def setup_linear_constraints(self): self.add_linear_constraint(['var_0', 'var_1'], [-1, -0.5], 0) def _objective_function(self, x): - return x[0] - x[1] + return x[0] - x[1] + x[2] @property def optimal_solution(self): - x = np.array([-1, 2]).reshape(1, self.n_variables) + x = np.array([-1, 2, 0.0]).reshape(1, self.n_variables) f = -3 return x, f @property def x0(self): - return [-0.5, 1.5] + return [-0.5, 1.5, 0.1] @property def conditional_minima(self): f_x0 = lambda x0: x0 - 2 f_x1 = lambda x1: x1 * - 3/2 - return f_x0, f_x1 + f_x2 = lambda x2: x2 + return f_x0, f_x1, f_x2 def test_if_solved(self, optimization_results: OptimizationResults, test_kwargs=default_test_kwargs):