ULSAlgorithms 1.1.0-g3e5595996d
High-performance exact and heuristic algorithms for uncapacitated lot sizing
Loading...
Searching...
No Matches
LsCutModelBuilder.cs
Go to the documentation of this file.
4 ULSAlgorithms.Optimization.Modeling.LinearConstraintSense;
6 ULSAlgorithms.CuttingPlanes.LinearConstraintSense;
7
9
10internal static class LsCutModelBuilder
11{
13 LinearModel source)
14 {
15 ArgumentNullException.ThrowIfNull(source);
16
17 LinearVariable[] variables =
18 source.Variables
19 .Select(
20 static variable =>
22 variable.Id,
23 variable.Name,
24 variable.Type ==
26 ? LinearVariableType.Continuous
27 : variable.Type,
28 variable.LowerBound,
29 variable.UpperBound))
30 .ToArray();
31
32 return new LinearModel(
33 source.Name + "-LP",
34 variables,
35 source.Constraints,
36 source.Objective);
37 }
38
39 internal static LinearModel AddCuts(
40 LinearModel source,
41 IEnumerable<(string Name, LsCutDefinition Cut)> cuts)
42 {
43 ArgumentNullException.ThrowIfNull(source);
44 ArgumentNullException.ThrowIfNull(cuts);
45
46 var nameToVariable =
47 source.Variables.ToDictionary(
48 static variable =>
49 variable.Name,
50 StringComparer.Ordinal);
51
52 var constraints =
53 source.Constraints.ToList();
54
55 foreach ((string name, LsCutDefinition cut) in cuts)
56 {
57 var terms =
58 new List<LinearTerm>(
59 cut.Coefficients.Count);
60
61 foreach (CutCoefficient coefficient in
62 cut.Coefficients)
63 {
64 if (!nameToVariable.TryGetValue(
65 coefficient.VariableName,
66 out LinearVariable? variable))
67 {
68 throw new InvalidOperationException(
69 $"Cut '{name}' references unknown variable " +
70 $"'{coefficient.VariableName}'.");
71 }
72
73 terms.Add(
74 new LinearTerm(
75 variable.Id,
76 coefficient.Coefficient));
77 }
78
79 constraints.Add(
81 name,
82 terms,
83 TranslateSense(
84 cut.Sense),
85 cut.RightHandSide));
86 }
87
88 return new LinearModel(
89 source.Name,
90 source.Variables,
91 constraints,
92 source.Objective);
93 }
94
95 private static ModelConstraintSense TranslateSense(
97 {
98 return sense switch
99 {
100 CutConstraintSense.LessOrEqual =>
101 ModelConstraintSense.LessOrEqual,
102
103 CutConstraintSense.Equal =>
105
106 CutConstraintSense.GreaterOrEqual =>
107 ModelConstraintSense.GreaterOrEqual,
108
109 _ =>
110 throw new NotSupportedException(
111 $"Unsupported cut sense '{sense}'.")
112 };
113 }
114}
ULSAlgorithms.CuttingPlanes.LinearConstraintSense CutConstraintSense
ULSAlgorithms.Optimization.Modeling.LinearConstraintSense ModelConstraintSense
static LinearModel AddCuts(LinearModel source, IEnumerable<(string Name, LsCutDefinition Cut)> cuts)
static LinearModel CreateLpRelaxation(LinearModel source)
Solver-independent definition of one ULS (l,S) inequality.
IReadOnlyList< CutCoefficient > Coefficients
Gets nonzero coefficients in solver-independent form.
LinearConstraintSense Sense
Gets the inequality sense.
double RightHandSide
Gets the right-hand side.
Describes one portable linear constraint.
Immutable solver-independent linear or mixed-integer linear model.
Definition LinearModel.cs:7
IReadOnlyList< LinearVariable > Variables
Gets all variables.
LinearObjective Objective
Gets the minimization objective.
IReadOnlyList< LinearConstraint > Constraints
Gets all constraints.
Describes one variable in a portable linear mathematical model.
LinearVariableType
Identifies the domain of a variable in a portable linear mathematical model.
Stores one nonzero coefficient of a generated linear inequality.
double Coefficient
Gets the coefficient value.
string VariableName
Gets the solver-independent variable identifier.
Stores one coefficient of a portable linear expression.
Definition LinearTerm.cs:7