1using System.Globalization;
33 return $
"v_{variableId}";
45 ArgumentNullException.ThrowIfNull(model);
47 if (
string.IsNullOrWhiteSpace(path))
49 throw new ArgumentException(
50 "A destination path is required.",
62 throw new NotSupportedException(
63 $
"Portable LP export does not yet support variable " +
64 $
"type '{variable.VariableType}'.");
69 Path.GetFullPath(path);
72 Path.GetDirectoryName(fullPath);
74 if (!
string.IsNullOrWhiteSpace(directory))
76 Directory.CreateDirectory(directory);
83 new UTF8Encoding(
false));
86 "\\ Generated by LotSizingDataModel portable solver adapter");
93 writer.Write(
" obj: ");
96 model.Objective.Expression,
97 includeConstant:
true);
100 writer.WriteLine(
"Subject To");
102 int constraintIndex = 0;
112 $
" c_{constraintIndex}: ");
117 includeConstant:
false);
119 double adjustedRightHandSide =
120 constraint.RightHandSide -
124 constraint.
Sense switch
126 MathematicalConstraintSense.LessThanOrEqual =>
128 MathematicalConstraintSense.Equal =>
130 MathematicalConstraintSense.GreaterThanOrEqual =>
133 throw new NotSupportedException(
134 $
"Unsupported mathematical constraint sense " +
135 $
"'{constraint.Sense}'.")
139 FormatNumber(adjustedRightHandSide));
144 writer.WriteLine(
"Bounds");
161 if (integerVariables.Length > 0)
163 writer.WriteLine(
"Generals");
168 $
" {GetVariableName(variable.Id)}");
180 if (binaryVariables.Length > 0)
182 writer.WriteLine(
"Binaries");
187 $
" {GetVariableName(variable.Id)}");
191 writer.WriteLine(
"End");
194 private static void WriteExpression(
197 bool includeConstant)
202 if (includeConstant &&
217 if (coefficient == 0.0)
229 else if (coefficient < 0.0)
235 Math.Abs(coefficient);
237 if (magnitude != 1.0)
240 FormatNumber(magnitude));
257 private static void WriteBounds(
259 MathematicalVariable variable)
270 if (
double.IsNegativeInfinity(lower) &&
271 double.IsPositiveInfinity(upper))
282 $
" {name} = {FormatNumber(lower)}");
287 if (!
double.IsNegativeInfinity(lower) &&
288 !
double.IsPositiveInfinity(upper))
291 $
" {FormatNumber(lower)} <= {name} <= " +
292 $
"{FormatNumber(upper)}");
297 if (!
double.IsNegativeInfinity(lower))
300 $
" {FormatNumber(lower)} <= {name}");
306 $
" {name} <= {FormatNumber(upper)}");
309 private static string FormatNumber(
312 if (!
double.IsFinite(value))
314 throw new ArgumentOutOfRangeException(
317 "Only finite coefficients and right-hand sides can " +
318 "be written as LP numeric values.");
321 return value.ToString(
323 CultureInfo.InvariantCulture);
Writes a solver-independent MathematicalModel to a conservative LP file understood by the supported n...
static string GetVariableName(int variableId)
Returns the stable LP name associated with a mathematical variable.
void Write(MathematicalModel model, string path)
Writes the specified model to an LP file.
Represents one linear constraint in a mathematical optimization model.
MathematicalConstraintSense Sense
Gets or sets the relational sense of the constraint.
bool IsEnabled
Gets or sets a value indicating whether the constraint is enabled.
LinearExpression LeftHandSide
Gets or sets the left-hand-side linear expression.
Represents a linear expression composed of variable terms and a constant value.
List< LinearTerm > Terms
Gets the linear terms of the expression.
double Constant
Gets or sets the constant value of the expression.
Represents one coefficient-variable term in a linear expression.
int VariableId
Gets or sets the identifier of the referenced mathematical variable.
double Coefficient
Gets or sets the coefficient multiplying the referenced variable.
Represents a solver-independent mathematical optimization model.
void EnsureValid()
Validates the complete mathematical model.
Represents one decision variable in a mathematical optimization model.
MathematicalVariableType VariableType
Gets or sets the variable domain.
int Id
Gets or sets the unique variable identifier within the mathematical model.
double LowerBound
Gets or sets the variable lower bound.
double UpperBound
Gets or sets the variable upper bound.
ObjectiveSense
Identifies the optimization direction of a mathematical objective.
MathematicalVariableType
Identifies the domain of a mathematical decision variable.