Skip to content

Commit

Permalink
Fix non-linearity check during linear constraints
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ronald-jaepel authored and schmoelder committed Mar 26, 2024
1 parent e6e182d commit e832ac0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
9 changes: 4 additions & 5 deletions CADETProcess/optimization/optimizationProblem.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
)

from CADETProcess.metric import MetricBase

from CADETProcess.optimization import Individual, Population
from CADETProcess.optimization import ResultsCache

Expand Down Expand Up @@ -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."
)
Expand Down Expand Up @@ -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."
)
Expand Down Expand Up @@ -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."
)
Expand Down Expand Up @@ -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."
)
Expand Down
11 changes: 6 additions & 5 deletions tests/optimization_problem_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit e832ac0

Please sign in to comment.