LotSizingDataModel.Solver.Cplex 2.1.0
IBM ILOG CPLEX adapter and installation discovery integration.
Loading...
Searching...
No Matches
CplexModelTranslator.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using ILOG.Concert;
4using ILOG.CPLEX;
5using NativeCplex = global::ILOG.CPLEX.Cplex;
6using LotSizingDataModel.Solver.Modeling;
7using GenericObjectiveSense = global::LotSizingDataModel.Solver.Modeling.ObjectiveSense;
8
10
11/// <summary>
12/// Translates a solver-independent linear mathematical model to
13/// IBM ILOG Concert/CPLEX objects.
14/// </summary>
15public sealed class CplexModelTranslator
16{
17 /// <summary>
18 /// Translates a complete mathematical model.
19 /// </summary>
20 /// <param name="model">
21 /// Validated generic mathematical model.
22 /// </param>
23 /// <returns>
24 /// Native CPLEX model and variable mapping.
25 /// </returns>
27 MathematicalModel model)
28 {
29 ArgumentNullException.ThrowIfNull(model);
30
31 model.EnsureValid();
32
33 var cplex =
34 new NativeCplex();
35
36 try
37 {
38 if (!string.IsNullOrWhiteSpace(model.Name))
39 {
40 cplex.Name =
41 model.Name;
42 }
43
44 var variablesById =
45 new Dictionary<int, INumVar>(
46 model.VariableCount);
47
48 foreach (MathematicalVariable variable in model.Variables)
49 {
50 INumVar nativeVariable =
51 CreateVariable(
52 cplex,
53 variable);
54
55 // Extract even unused variables so every mapped value can be read after solve.
56 cplex.Add(nativeVariable);
57
58 variablesById.Add(
59 variable.Id,
60 nativeVariable);
61 }
62
63 AddObjective(
64 cplex,
65 model.Objective,
66 variablesById);
67
68 foreach (LinearConstraint constraint in model.Constraints)
69 {
70 if (!constraint.IsEnabled)
71 {
72 continue;
73 }
74
75 AddConstraint(
76 cplex,
77 constraint,
78 variablesById);
79 }
80
82 cplex,
83 variablesById);
84 }
85 catch
86 {
87 cplex.End();
88 throw;
89 }
90 }
91
92 private static INumVar CreateVariable(
93 NativeCplex cplex,
94 MathematicalVariable variable)
95 {
96 double lowerBound =
97 NormalizeLowerBound(
98 variable.LowerBound);
99
100 double upperBound =
101 NormalizeUpperBound(
102 variable.UpperBound);
103
104 return variable.VariableType switch
105 {
106 MathematicalVariableType.Continuous =>
107 cplex.NumVar(
108 lowerBound,
109 upperBound,
110 NumVarType.Float,
111 variable.Name),
112
113 MathematicalVariableType.Integer =>
114 cplex.NumVar(
115 lowerBound,
116 upperBound,
117 NumVarType.Int,
118 variable.Name),
119
120 MathematicalVariableType.Binary =>
121 cplex.BoolVar(
122 variable.Name),
123
124 MathematicalVariableType.SemiContinuous =>
125 throw new NotSupportedException(
126 "The first CPLEX adapter version does not " +
127 "translate semi-continuous variables."),
128
129 MathematicalVariableType.SemiInteger =>
130 throw new NotSupportedException(
131 "The first CPLEX adapter version does not " +
132 "translate semi-integer variables."),
133
134 _ =>
135 throw new NotSupportedException(
136 $"Unsupported mathematical variable type " +
137 $"'{variable.VariableType}'.")
138 };
139 }
140
141 private static void AddObjective(
142 NativeCplex cplex,
143 MathematicalObjective objective,
144 IReadOnlyDictionary<int, INumVar> variablesById)
145 {
146 INumExpr expression =
147 BuildExpression(
148 cplex,
149 objective.Expression,
150 variablesById);
151
152 switch (objective.Sense)
153 {
154 case GenericObjectiveSense.Minimize:
155 cplex.AddMinimize(
156 expression,
157 objective.Name);
158 break;
159
160 case GenericObjectiveSense.Maximize:
161 cplex.AddMaximize(
162 expression,
163 objective.Name);
164 break;
165
166 default:
167 throw new NotSupportedException(
168 $"Unsupported objective sense " +
169 $"'{objective.Sense}'.");
170 }
171 }
172
173 private static void AddConstraint(
174 NativeCplex cplex,
175 LinearConstraint constraint,
176 IReadOnlyDictionary<int, INumVar> variablesById)
177 {
178 ILinearNumExpr leftHandSide =
179 BuildLinearExpression(
180 cplex,
181 constraint.LeftHandSide,
182 variablesById);
183
184 double adjustedRightHandSide =
185 constraint.RightHandSide -
186 constraint.LeftHandSide.Constant;
187
188 switch (constraint.Sense)
189 {
190 case MathematicalConstraintSense.LessThanOrEqual:
191 cplex.AddLe(
192 leftHandSide,
193 adjustedRightHandSide,
194 constraint.Name);
195 break;
196
197 case MathematicalConstraintSense.Equal:
198 cplex.AddEq(
199 leftHandSide,
200 adjustedRightHandSide,
201 constraint.Name);
202 break;
203
204 case MathematicalConstraintSense.GreaterThanOrEqual:
205 cplex.AddGe(
206 leftHandSide,
207 adjustedRightHandSide,
208 constraint.Name);
209 break;
210
211 default:
212 throw new NotSupportedException(
213 $"Unsupported constraint sense " +
214 $"'{constraint.Sense}'.");
215 }
216 }
217
218 private static INumExpr BuildExpression(
219 NativeCplex cplex,
220 LinearExpression expression,
221 IReadOnlyDictionary<int, INumVar> variablesById)
222 {
223 ILinearNumExpr linear =
224 BuildLinearExpression(
225 cplex,
226 expression,
227 variablesById);
228
229 if (expression.Constant == 0.0)
230 {
231 return linear;
232 }
233
234 return cplex.Sum(
235 linear,
236 expression.Constant);
237 }
238
239 private static ILinearNumExpr BuildLinearExpression(
240 NativeCplex cplex,
241 LinearExpression expression,
242 IReadOnlyDictionary<int, INumVar> variablesById)
243 {
244 var result =
245 cplex.LinearNumExpr();
246
247 foreach (LinearTerm term in expression.Terms)
248 {
249 if (!variablesById.TryGetValue(
250 term.VariableId,
251 out INumVar? variable))
252 {
253 throw new InvalidOperationException(
254 $"Expression references unknown mathematical " +
255 $"variable identifier {term.VariableId}.");
256 }
257
258 result.AddTerm(
259 term.Coefficient,
260 variable);
261 }
262
263 return result;
264 }
265
266 private static double NormalizeLowerBound(
267 double value)
268 {
269 return double.IsNegativeInfinity(value)
270 ? -double.MaxValue
271 : value;
272 }
273
274 private static double NormalizeUpperBound(
275 double value)
276 {
277 return double.IsPositiveInfinity(value)
278 ? double.MaxValue
279 : value;
280 }
281}
global::ILOG.CPLEX.Cplex NativeCplex
global::LotSizingDataModel.Solver.Modeling.ObjectiveSense GenericObjectiveSense
Stores the native CPLEX model objects created from one solver-independent mathematical model.
Translates a solver-independent linear mathematical model to IBM ILOG Concert/CPLEX objects.
CplexModelTranslationResult Translate(MathematicalModel model)
Translates a complete mathematical model.