ULSAlgorithms 1.1.0-g3e5595996d
High-performance exact and heuristic algorithms for uncapacitated lot sizing
Loading...
Searching...
No Matches
LsCutDefinition.cs
Go to the documentation of this file.
2
3/// <summary>
4/// Solver-independent definition of one ULS (l,S) inequality.
5/// </summary>
6/// <remarks>
7/// Period indices are zero-based, consistent with the rest of the ULSAlgorithms
8/// public API.
9/// </remarks>
10public sealed class LsCutDefinition
11{
12 private readonly int[] _s;
13 private readonly CutCoefficient[] _coefficients;
14
15 /// <summary>Initializes an (l,S) cut definition.</summary>
17 int l,
18 IEnumerable<int> s,
19 IEnumerable<CutCoefficient> coefficients,
21 double rightHandSide)
22 {
23 if (l < 0)
24 {
25 throw new ArgumentOutOfRangeException(nameof(l));
26 }
27
28 ArgumentNullException.ThrowIfNull(s);
29 ArgumentNullException.ThrowIfNull(coefficients);
30
31 _s = s.ToArray();
32
33 if (_s.Any(period => period < 0 || period > l))
34 {
35 throw new ArgumentOutOfRangeException(
36 nameof(s),
37 "Every period in S must be between 0 and l.");
38 }
39
40 if (_s.Distinct().Count() != _s.Length)
41 {
42 throw new ArgumentException(
43 "The set S cannot contain duplicate periods.",
44 nameof(s));
45 }
46
47 Array.Sort(_s);
48
49 _coefficients = coefficients
50 .Where(static coefficient => coefficient.Coefficient != 0.0)
51 .ToArray();
52
53 if (_coefficients.Length == 0)
54 {
55 throw new ArgumentException(
56 "A cut must contain at least one nonzero coefficient.",
57 nameof(coefficients));
58 }
59
60 if (!double.IsFinite(rightHandSide))
61 {
62 throw new ArgumentOutOfRangeException(
63 nameof(rightHandSide),
64 "The right-hand side must be finite.");
65 }
66
67 L = l;
68 Sense = sense;
69 RightHandSide = rightHandSide;
70 }
71
72 /// <summary>Gets l in the (l,S) notation.</summary>
73 public int L { get; }
74
75 /// <summary>Gets a sorted defensive snapshot of S.</summary>
76 public IReadOnlyList<int> S => _s;
77
78 /// <summary>Gets nonzero coefficients in solver-independent form.</summary>
79 public IReadOnlyList<CutCoefficient> Coefficients => _coefficients;
80
81 /// <summary>Gets the inequality sense.</summary>
83
84 /// <summary>Gets the right-hand side.</summary>
85 public double RightHandSide { get; }
86
87 /// <summary>
88 /// Returns a deterministic human-readable representation of the generated
89 /// linear inequality.
90 /// </summary>
91 public override string ToString()
92 {
93 string leftHandSide = string.Join(
94 " + ",
95 _coefficients.Select(
96 static term =>
97 $"{term.Coefficient.ToString(System.Globalization.CultureInfo.InvariantCulture)}*" +
98 term.VariableName));
99
100 string relation = Sense switch
101 {
105 _ => throw new InvalidOperationException(
106 $"Unsupported constraint sense '{Sense}'.")
107 };
108
109 return
110 $"{leftHandSide} {relation} " +
111 RightHandSide.ToString(
112 System.Globalization.CultureInfo.InvariantCulture);
113 }
114
115}
int L
Gets l in the (l,S) notation.
LsCutDefinition(int l, IEnumerable< int > s, IEnumerable< CutCoefficient > coefficients, LinearConstraintSense sense, double rightHandSide)
Initializes an (l,S) cut definition.
IReadOnlyList< CutCoefficient > Coefficients
Gets nonzero coefficients in solver-independent form.
LinearConstraintSense Sense
Gets the inequality sense.
override string ToString()
Returns a deterministic human-readable representation of the generated linear inequality.
IReadOnlyList< int > S
Gets a sorted defensive snapshot of S.
double RightHandSide
Gets the right-hand side.
LinearConstraintSense
Identifies the sense of a generated linear inequality.
Stores one nonzero coefficient of a generated linear inequality.