LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalModelIntrinsicFeasibilityAnalyzer.cs
Go to the documentation of this file.
2
4
5/// <summary>
6/// Proves intrinsic infeasibility from variable-bound interval
7/// arithmetic. Absence of a proof is reported as Unknown.
8/// </summary>
10{
11 private const double DefaultTolerance = 1.0e-9;
12
15 double tolerance = DefaultTolerance)
16 {
17 ArgumentNullException.ThrowIfNull(model);
18
19 if (!double.IsFinite(tolerance) ||
20 tolerance < 0.0)
21 {
22 throw new ArgumentOutOfRangeException(
23 nameof(tolerance));
24 }
25
26 model.EnsureValid();
27
28 var variables =
29 model.Variables.ToDictionary(
30 variable => variable.Id);
31
32 var diagnostics =
33 new List<IntrinsicFeasibilityDiagnostic>();
34
35 int evaluated = 0;
36
37 foreach (LinearConstraint constraint
38 in model.Constraints)
39 {
40 if (!constraint.IsEnabled)
41 {
42 continue;
43 }
44
45 evaluated++;
46
49 constraint.LeftHandSide,
50 variables);
51
53 TryProveConstraintInfeasible(
54 constraint,
55 interval,
56 tolerance);
57
58 if (diagnostic is not null)
59 {
60 diagnostics.Add(diagnostic);
61 }
62 }
63
65 diagnostics.Count > 0
68 diagnostics,
69 evaluated);
70 }
71
73 LinearExpression expression,
74 IReadOnlyDictionary<int, MathematicalVariable> variables)
75 {
76 ArgumentNullException.ThrowIfNull(expression);
77 ArgumentNullException.ThrowIfNull(variables);
78
79 double minimum = expression.Constant;
80 double maximum = expression.Constant;
81
82 foreach (LinearTerm term
83 in expression.Terms)
84 {
85 if (term.Coefficient == 0.0)
86 {
87 continue;
88 }
89
90 if (!variables.TryGetValue(
91 term.VariableId,
92 out MathematicalVariable? variable))
93 {
94 throw new InvalidOperationException(
95 $"Expression references unknown variable identifier '{term.VariableId}'.");
96 }
97
98 double lowContribution;
99 double highContribution;
100
101 if (term.Coefficient > 0.0)
102 {
103 lowContribution =
104 term.Coefficient *
105 variable.LowerBound;
106
107 highContribution =
108 term.Coefficient *
109 variable.UpperBound;
110 }
111 else
112 {
113 lowContribution =
114 term.Coefficient *
115 variable.UpperBound;
116
117 highContribution =
118 term.Coefficient *
119 variable.LowerBound;
120 }
121
122 minimum += lowContribution;
123 maximum += highContribution;
124
125 if (double.IsNaN(minimum) ||
126 double.IsNaN(maximum))
127 {
129 double.NegativeInfinity,
130 double.PositiveInfinity);
131 }
132 }
133
135 minimum,
136 maximum);
137 }
138
139 private static IntrinsicFeasibilityDiagnostic?
140 TryProveConstraintInfeasible(
141 LinearConstraint constraint,
143 double tolerance)
144 {
145 double rhs =
146 constraint.RightHandSide;
147
148 switch (constraint.Sense)
149 {
150 case MathematicalConstraintSense.LessThanOrEqual:
151 if (interval.Minimum >
152 rhs + tolerance)
153 {
154 return CreateDiagnostic(
155 "LSDM-FEAS-001",
156 constraint,
157 interval,
158 "Minimum attainable left-hand side exceeds the <= right-hand side.");
159 }
160
161 return null;
162
163 case MathematicalConstraintSense.GreaterThanOrEqual:
164 if (interval.Maximum <
165 rhs - tolerance)
166 {
167 return CreateDiagnostic(
168 "LSDM-FEAS-002",
169 constraint,
170 interval,
171 "Maximum attainable left-hand side is below the >= right-hand side.");
172 }
173
174 return null;
175
177 if (rhs <
178 interval.Minimum - tolerance)
179 {
180 return CreateDiagnostic(
181 "LSDM-FEAS-003",
182 constraint,
183 interval,
184 "Equality right-hand side is below the attainable left-hand-side interval.");
185 }
186
187 if (rhs >
188 interval.Maximum + tolerance)
189 {
190 return CreateDiagnostic(
191 "LSDM-FEAS-004",
192 constraint,
193 interval,
194 "Equality right-hand side is above the attainable left-hand-side interval.");
195 }
196
197 return null;
198
199 default:
200 throw new NotSupportedException(
201 $"Constraint sense '{constraint.Sense}' is not supported by intrinsic feasibility analysis.");
202 }
203 }
204
205 private static IntrinsicFeasibilityDiagnostic
206 CreateDiagnostic(
207 string code,
208 LinearConstraint constraint,
209 LinearExpressionBoundInterval interval,
210 string reason)
211 {
212 return new IntrinsicFeasibilityDiagnostic(
213 code,
214 $"Constraint '{constraint.Name}' is intrinsically infeasible: {reason}",
215 constraint.Id,
216 interval.Minimum,
217 interval.Maximum,
218 constraint.RightHandSide);
219 }
220}
Proves intrinsic infeasibility from variable-bound interval arithmetic. Absence of a proof is reporte...
LinearExpressionBoundInterval EvaluateInterval(LinearExpression expression, IReadOnlyDictionary< int, MathematicalVariable > variables)
IntrinsicFeasibilityAnalysisResult Analyze(MathematicalModel model, double tolerance=DefaultTolerance)
Represents one linear constraint in a mathematical optimization model.
double RightHandSide
Gets or sets the right-hand-side constant value.
MathematicalConstraintSense Sense
Gets or sets the relational sense of the constraint.
bool IsEnabled
Gets or sets a value indicating whether the constraint is enabled.
LinearExpression LeftHandSide
Gets or sets the left-hand-side linear expression.
int Id
Gets or sets the unique constraint identifier within the mathematical model.
Represents a linear expression composed of variable terms and a constant value.
double Constant
Gets or sets the constant value of the expression.
Represents one coefficient-variable term in a linear expression.
Definition LinearTerm.cs:13
int VariableId
Gets or sets the identifier of the referenced mathematical variable.
Definition LinearTerm.cs:49
double Coefficient
Gets or sets the coefficient multiplying the referenced variable.
Definition LinearTerm.cs:60
Represents a solver-independent mathematical optimization model.
List< MathematicalVariable > Variables
Gets the mathematical variables.
void EnsureValid()
Validates the complete mathematical model.
Represents one decision variable in a mathematical optimization model.
IntrinsicFeasibilityStatus
Conservative pre-solve feasibility status.
MathematicalConstraintSense
Identifies the relational sense of a mathematical constraint.