27 MathematicalModel model)
29 ArgumentNullException.ThrowIfNull(model);
38 if (!
string.IsNullOrWhiteSpace(model.Name))
45 new Dictionary<int, INumVar>(
48 foreach (MathematicalVariable variable
in model.Variables)
50 INumVar nativeVariable =
65 foreach (LinearConstraint constraint
in model.Constraints)
67 if (!constraint.IsEnabled)
89 private static INumVar CreateVariable(
91 MathematicalVariable variable)
101 return variable.VariableType
switch
103 MathematicalVariableType.Continuous =>
110 MathematicalVariableType.Integer =>
117 MathematicalVariableType.Binary =>
121 MathematicalVariableType.SemiContinuous =>
122 throw new NotSupportedException(
123 "The first CPLEX adapter version does not " +
124 "translate semi-continuous variables."),
126 MathematicalVariableType.SemiInteger =>
127 throw new NotSupportedException(
128 "The first CPLEX adapter version does not " +
129 "translate semi-integer variables."),
132 throw new NotSupportedException(
133 $
"Unsupported mathematical variable type " +
134 $
"'{variable.VariableType}'.")
138 private static void AddObjective(
140 MathematicalObjective objective,
141 IReadOnlyDictionary<int, INumVar> variablesById)
143 INumExpr expression =
146 objective.Expression,
149 switch (objective.Sense)
164 throw new NotSupportedException(
165 $
"Unsupported objective sense " +
166 $
"'{objective.Sense}'.");
170 private static void AddConstraint(
172 LinearConstraint constraint,
173 IReadOnlyDictionary<int, INumVar> variablesById)
175 ILinearNumExpr leftHandSide =
176 BuildLinearExpression(
178 constraint.LeftHandSide,
181 double adjustedRightHandSide =
182 constraint.RightHandSide -
183 constraint.LeftHandSide.Constant;
185 switch (constraint.Sense)
187 case MathematicalConstraintSense.LessThanOrEqual:
190 adjustedRightHandSide,
194 case MathematicalConstraintSense.Equal:
197 adjustedRightHandSide,
201 case MathematicalConstraintSense.GreaterThanOrEqual:
204 adjustedRightHandSide,
209 throw new NotSupportedException(
210 $
"Unsupported constraint sense " +
211 $
"'{constraint.Sense}'.");
215 private static INumExpr BuildExpression(
217 LinearExpression expression,
218 IReadOnlyDictionary<int, INumVar> variablesById)
220 ILinearNumExpr linear =
221 BuildLinearExpression(
226 if (expression.Constant == 0.0)
233 expression.Constant);
236 private static ILinearNumExpr BuildLinearExpression(
238 LinearExpression expression,
239 IReadOnlyDictionary<int, INumVar> variablesById)
242 cplex.LinearNumExpr();
244 foreach (LinearTerm term
in expression.Terms)
246 if (!variablesById.TryGetValue(
248 out INumVar? variable))
250 throw new InvalidOperationException(
251 $
"Expression references unknown mathematical " +
252 $
"variable identifier {term.VariableId}.");
263 private static double NormalizeLowerBound(
266 return double.IsNegativeInfinity(value)
271 private static double NormalizeUpperBound(
274 return double.IsPositiveInfinity(value)