LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
Constraint.m
1classdef Constraint < handle
2 % Constraint Abstract base for line-opt constraints. Each computes a
3 % non-negative violation (0 if satisfied). Mirrors native-Python
4 % line_solver.opt.objectives.Constraint.
5
6 properties
7 name = '';
8 end
9
10 methods
11 function obj = Constraint(name)
12 if nargin >= 1 && ~isempty(name)
13 obj.name = name;
14 end
15 end
16 function n = getName(obj)
17 if isempty(obj.name)
18 n = obj.generateName();
19 else
20 n = obj.name;
21 end
22 end
23 function tf = isSatisfied(obj, result, variableValues, tol)
24 if nargin < 4, tol = 1e-6; end
25 tf = obj.evaluate(result, variableValues) <= tol;
26 end
27 end
28
29 methods (Abstract)
30 n = generateName(obj)
31 v = evaluate(obj, result, variableValues)
32 end
33
34 methods (Static)
35 function v = upperBoundViolation(actual, bound)
36 if ~isfinite(actual)
37 v = inf;
38 else
39 v = max(0.0, actual - bound);
40 end
41 end
42 function v = lowerBoundViolation(actual, bound)
43 if ~isfinite(actual)
44 v = inf;
45 else
46 v = max(0.0, bound - actual);
47 end
48 end
49 function nm = nameOf(x)
50 if ischar(x) || isstring(x)
51 nm = char(x);
52 elseif isempty(x)
53 nm = '';
54 else
55 nm = x.getName();
56 end
57 end
58 end
59end