LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
LagrangianBoundTracker.cs
Go to the documentation of this file.
2
3/// <summary>
4/// Tracks the best dual lower bound and primal upper bound for a
5/// minimization Lagrangian relaxation.
6/// </summary>
7public sealed class LagrangianBoundTracker
8{
9 private const double ConsistencyTolerance = 1.0e-7;
10
11 public double? BestDualLowerBound
12 {
13 get;
14 private set;
15 }
16
17 public double? BestPrimalUpperBound
18 {
19 get;
20 private set;
21 }
22
23 public double? AbsoluteGap =>
24 BestDualLowerBound.HasValue &&
25 BestPrimalUpperBound.HasValue
26 ? Math.Max(
27 0.0,
30 : null;
31
32 public double? RelativeGap
33 {
34 get
35 {
36 if (!AbsoluteGap.HasValue ||
37 !BestPrimalUpperBound.HasValue)
38 {
39 return null;
40 }
41
42 return AbsoluteGap.Value /
43 Math.Max(
44 1.0,
45 Math.Abs(
47 }
48 }
49
51 double value)
52 {
53 EnsureFinite(
54 value,
55 nameof(value));
56
57 if (BestPrimalUpperBound.HasValue &&
58 value >
60 ScaledTolerance(
62 {
63 throw new InvalidOperationException(
64 "A claimed Lagrangian dual lower bound exceeds the current primal upper bound.");
65 }
66
67 if (!BestDualLowerBound.HasValue ||
68 value >
70 {
72 value;
73 }
74 }
75
77 double value)
78 {
79 EnsureFinite(
80 value,
81 nameof(value));
82
83 if (BestDualLowerBound.HasValue &&
84 value <
85 BestDualLowerBound.Value -
86 ScaledTolerance(
87 BestDualLowerBound.Value))
88 {
89 throw new InvalidOperationException(
90 "A claimed primal upper bound is below the current Lagrangian dual lower bound.");
91 }
92
93 if (!BestPrimalUpperBound.HasValue ||
94 value <
96 {
98 value;
99 }
100 }
101
102 private static double ScaledTolerance(
103 double value)
104 {
105 return ConsistencyTolerance *
106 Math.Max(
107 1.0,
108 Math.Abs(value));
109 }
110
111 private static void EnsureFinite(
112 double value,
113 string valueName)
114 {
115 if (double.IsNaN(value) ||
116 double.IsInfinity(value))
117 {
118 throw new InvalidOperationException(
119 $"{valueName} must be finite.");
120 }
121 }
122}
Tracks the best dual lower bound and primal upper bound for a minimization Lagrangian relaxation.