ULSAlgorithms 1.1.0-g3e5595996d
High-performance exact and heuristic algorithms for uncapacitated lot sizing
Loading...
Searching...
No Matches
LsSeparationMath.cs
Go to the documentation of this file.
4
6
7internal static class LsSeparationMath
8{
9 internal static double[] BuildCumulativeDemand(
10 UlsProblem problem)
11 {
12 var cumulative =
13 new double[problem.Horizon];
14
15 double running = 0.0;
16
17 for (int period = 0;
18 period < problem.Horizon;
19 period++)
20 {
21 running +=
22 problem.Demands[period];
23
24 if (!double.IsFinite(running))
25 {
26 throw new ArithmeticException(
27 "Cumulative demand overflowed.");
28 }
29
30 cumulative[period] =
31 running;
32 }
33
34 return cumulative;
35 }
36
37 internal static double IntervalDemand(
38 double[] cumulativeDemand,
39 int first,
40 int last)
41 {
42 if (first < 0 ||
43 last < first ||
44 last >= cumulativeDemand.Length)
45 {
46 throw new ArgumentOutOfRangeException();
47 }
48
49 return cumulativeDemand[last] -
50 (first == 0
51 ? 0.0
52 : cumulativeDemand[first - 1]);
53 }
54
55 internal static double GetSemanticValue(
56 IReadOnlyDictionary<int, int> semanticMap,
57 int period,
58 IReadOnlyDictionary<int, double> values,
59 string family)
60 {
61 if (!semanticMap.TryGetValue(
62 period,
63 out int variableId))
64 {
65 throw new InvalidOperationException(
66 $"The formulation has no {family} variable for period {period}.");
67 }
68
69 if (!values.TryGetValue(
70 variableId,
71 out double value) ||
72 !double.IsFinite(value))
73 {
74 throw new InvalidOperationException(
75 $"No finite LP value exists for {family}[{period}].");
76 }
77
78 return value;
79 }
80
81 internal static string GetVariableName(
82 UlsFormulation formulation,
83 IReadOnlyDictionary<int, int> semanticMap,
84 int period,
85 string family)
86 {
87 if (!semanticMap.TryGetValue(
88 period,
89 out int variableId))
90 {
91 throw new InvalidOperationException(
92 $"The formulation has no {family} variable for period {period}.");
93 }
94
95 return formulation.Model
96 .GetVariable(variableId)
97 .Name;
98 }
99
100 internal static double Efficacy(
101 double violation,
102 IEnumerable<CutCoefficient> coefficients)
103 {
104 double squaredNorm = 0.0;
105
106 foreach (CutCoefficient coefficient in coefficients)
107 {
108 squaredNorm +=
109 coefficient.Coefficient *
110 coefficient.Coefficient;
111 }
112
113 return squaredNorm > 0.0
114 ? violation /
115 Math.Sqrt(squaredNorm)
116 : 0.0;
117 }
118
119 internal static bool HasWagnerWhitinCosts(
120 UlsProblem problem)
121 {
122 for (int period = 0;
123 period < problem.Horizon - 1;
124 period++)
125 {
126 double left =
127 problem.UnitProductionCosts[period] +
128 problem.HoldingCosts[period];
129
130 double right =
131 problem.UnitProductionCosts[period + 1];
132
133 double scale =
134 Math.Max(
135 1.0,
136 Math.Max(
137 Math.Abs(left),
138 Math.Abs(right)));
139
140 if (left + 1.0e-12 * scale <
141 right)
142 {
143 return false;
144 }
145 }
146
147 return true;
148 }
149
150 internal static void RequireAggregateVariables(
151 UlsFormulation formulation)
152 {
153 if (formulation.Variables.Production.Count == 0 ||
154 formulation.Variables.Setup.Count == 0)
155 {
156 throw new ArgumentException(
157 "An (l,S) separator requires aggregate production and setup " +
158 "variable mappings.",
159 nameof(formulation));
160 }
161 }
162}
static void RequireAggregateVariables(UlsFormulation formulation)
static string GetVariableName(UlsFormulation formulation, IReadOnlyDictionary< int, int > semanticMap, int period, string family)
static double GetSemanticValue(IReadOnlyDictionary< int, int > semanticMap, int period, IReadOnlyDictionary< int, double > values, string family)
static double Efficacy(double violation, IEnumerable< CutCoefficient > coefficients)
static double IntervalDemand(double[] cumulativeDemand, int first, int last)
Solver-independent ULS mathematical formulation plus semantic variable map.
UlsFormulationVariableMap Variables
Gets semantic variable mappings.
IReadOnlyDictionary< int, int > Setup
Gets setup-variable ids by period.
IReadOnlyDictionary< int, int > Production
Gets production-variable ids by period.
Represents a validated classical uncapacitated lot-sizing problem.
Definition UlsProblem.cs:23
ReadOnlySpan< double > UnitProductionCosts
Gets unit production costs by period.
int Horizon
Gets the number of planning periods.
Definition UlsProblem.cs:82
ReadOnlySpan< double > HoldingCosts
Gets end-of-period unit holding costs by period.
ReadOnlySpan< double > Demands
Gets demand by period.
Definition UlsProblem.cs:92
Stores one nonzero coefficient of a generated linear inequality.
double Coefficient
Gets the coefficient value.