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 =
56 cplex.Add(nativeVariable);
68 foreach (LinearConstraint constraint
in model.Constraints)
70 if (!constraint.IsEnabled)
92 private static INumVar CreateVariable(
94 MathematicalVariable variable)
102 variable.UpperBound);
104 return variable.VariableType
switch
106 MathematicalVariableType.Continuous =>
113 MathematicalVariableType.Integer =>
120 MathematicalVariableType.Binary =>
124 MathematicalVariableType.SemiContinuous =>
125 throw new NotSupportedException(
126 "The first CPLEX adapter version does not " +
127 "translate semi-continuous variables."),
129 MathematicalVariableType.SemiInteger =>
130 throw new NotSupportedException(
131 "The first CPLEX adapter version does not " +
132 "translate semi-integer variables."),
135 throw new NotSupportedException(
136 $
"Unsupported mathematical variable type " +
137 $
"'{variable.VariableType}'.")
141 private static void AddObjective(
143 MathematicalObjective objective,
144 IReadOnlyDictionary<int, INumVar> variablesById)
146 INumExpr expression =
149 objective.Expression,
152 switch (objective.Sense)
167 throw new NotSupportedException(
168 $
"Unsupported objective sense " +
169 $
"'{objective.Sense}'.");
173 private static void AddConstraint(
175 LinearConstraint constraint,
176 IReadOnlyDictionary<int, INumVar> variablesById)
178 ILinearNumExpr leftHandSide =
179 BuildLinearExpression(
181 constraint.LeftHandSide,
184 double adjustedRightHandSide =
185 constraint.RightHandSide -
186 constraint.LeftHandSide.Constant;
188 switch (constraint.Sense)
190 case MathematicalConstraintSense.LessThanOrEqual:
193 adjustedRightHandSide,
197 case MathematicalConstraintSense.Equal:
200 adjustedRightHandSide,
204 case MathematicalConstraintSense.GreaterThanOrEqual:
207 adjustedRightHandSide,
212 throw new NotSupportedException(
213 $
"Unsupported constraint sense " +
214 $
"'{constraint.Sense}'.");
218 private static INumExpr BuildExpression(
220 LinearExpression expression,
221 IReadOnlyDictionary<int, INumVar> variablesById)
223 ILinearNumExpr linear =
224 BuildLinearExpression(
229 if (expression.Constant == 0.0)
236 expression.Constant);
239 private static ILinearNumExpr BuildLinearExpression(
241 LinearExpression expression,
242 IReadOnlyDictionary<int, INumVar> variablesById)
245 cplex.LinearNumExpr();
247 foreach (LinearTerm term
in expression.Terms)
249 if (!variablesById.TryGetValue(
251 out INumVar? variable))
253 throw new InvalidOperationException(
254 $
"Expression references unknown mathematical " +
255 $
"variable identifier {term.VariableId}.");
266 private static double NormalizeLowerBound(
269 return double.IsNegativeInfinity(value)
274 private static double NormalizeUpperBound(
277 return double.IsPositiveInfinity(value)