Skip to content

Commit

Permalink
[WIP] Add option to specify some slack for linear constraints
Browse files Browse the repository at this point in the history
This has been implemented, hoping to address issues with COBYLA. Maybe
we don't really need this once precision is enabled?
  • Loading branch information
schmoelder committed Jun 29, 2024
1 parent fea0a12 commit 7dd6b54
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions CADETProcess/optimization/optimizationProblem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2058,7 +2058,7 @@ def n_linear_constraints(self):
"""int: Number of linear inequality constraints."""
return len(self.linear_constraints)

def add_linear_constraint(self, opt_vars, lhs=1.0, b=0.0):
def add_linear_constraint(self, opt_vars, lhs=1.0, b=0.0, eps=0.0):
"""Add linear inequality constraints.
Parameters
Expand All @@ -2070,6 +2070,8 @@ def add_linear_constraint(self, opt_vars, lhs=1.0, b=0.0):
If scalar, same coefficient is used for all variables.
b : float, optional
Constraint of inequality constraint. The default is zero.
eps : float, optional
Tolerance to relax linear constraints.
Raises
------
Expand Down Expand Up @@ -2102,6 +2104,7 @@ def add_linear_constraint(self, opt_vars, lhs=1.0, b=0.0):
lincon['opt_vars'] = opt_vars
lincon['lhs'] = lhs
lincon['b'] = float(b)
lincon['eps'] = float(eps)

self._linear_constraints.append(lincon)

Expand Down Expand Up @@ -2262,6 +2265,18 @@ def b_transformed(self):

return b_t

@property
def eps_lin(self):
"""np.array: Relaxation tolerance for linear constraints.
See Also
--------
add_linear_constraint
"""
return np.array([
lincon['eps'] for lincon in self.linear_constraints
])

@untransforms
@gets_dependent_values
def evaluate_linear_constraints(self, x):
Expand Down Expand Up @@ -2313,7 +2328,8 @@ def check_linear_constraints(self, x):
"""
flag = True

if np.any(self.evaluate_linear_constraints(x) > 0):
lhs = self.evaluate_linear_constraints(x)
if np.any(lhs > self.eps_lin):
flag = False

return flag
Expand Down Expand Up @@ -2348,6 +2364,8 @@ def add_linear_equality_constraint(self, opt_vars, lhs=1, beq=0, eps=0.0):
If scalar, same coefficient is used for all variables.
b : float, optional
Constraint of inequality constraint. The default is zero.
eps : float, optional
Tolerance to relax linear equality constraints.
Raises
------
Expand Down

0 comments on commit 7dd6b54

Please sign in to comment.