1using System.Globalization;
28 ArgumentNullException.ThrowIfNull(model);
29 ArgumentException.ThrowIfNullOrWhiteSpace(path);
32 Path.GetFullPath(path);
35 Path.GetDirectoryName(fullPath);
37 if (!
string.IsNullOrWhiteSpace(directory))
39 Directory.CreateDirectory(directory);
47 encoderShouldEmitUTF8Identifier:
false));
50 "\\ Generated by ULSAlgorithms portable solver execution layer");
52 writer.WriteLine(
"Minimize");
53 writer.Write(
" obj: ");
63 writer.WriteLine(
"Subject To");
80 constraint.
Sense switch
82 LinearConstraintSense.LessOrEqual =>
" <= ",
83 LinearConstraintSense.Equal =>
" = ",
84 LinearConstraintSense.GreaterOrEqual =>
" >= ",
86 throw new NotSupportedException(
87 $
"Unsupported constraint sense '{constraint.Sense}'.")
95 writer.WriteLine(
"Bounds");
112 if (generals.Length > 0)
114 writer.WriteLine(
"Generals");
119 $
" {GetVariableName(variable.Id)}");
131 if (binaries.Length > 0)
133 writer.WriteLine(
"Binaries");
138 $
" {GetVariableName(variable.Id)}");
142 writer.WriteLine(
"End");
145 private static void WriteExpression(
147 IEnumerable<LinearTerm> terms)
149 bool wroteTerm =
false;
156 if (coefficient == 0.0)
168 else if (coefficient < 0.0)
174 Math.Abs(coefficient);
176 if (magnitude != 1.0)
179 FormatNumber(magnitude));
197 private static void WriteBounds(
199 LinearVariable variable)
208 $
" {name} = {FormatNumber(variable.LowerBound)}");
213 if (!
double.IsPositiveInfinity(
217 $
" {FormatNumber(variable.LowerBound)} <= {name} <= " +
218 $
"{FormatNumber(variable.UpperBound)}");
224 $
" {FormatNumber(variable.LowerBound)} <= {name}");
227 private static string FormatNumber(
230 if (!
double.IsFinite(value))
232 throw new ArgumentOutOfRangeException(
235 "LP coefficients and finite bounds must be finite.");
238 return value.ToString(
240 CultureInfo.InvariantCulture);
Writes a conservative LP representation understood by all four supported execution backends.
void Write(LinearModel model, string path)
Writes the supplied model to an LP file.
static string GetVariableName(int variableId)
Gets the stable portable variable name.
Describes one portable linear constraint.
LinearConstraintSense Sense
Gets the constraint sense.
IReadOnlyList< LinearTerm > Terms
Gets the nonzero terms.
double RightHandSide
Gets the right-hand side.
Immutable solver-independent linear or mixed-integer linear model.
LinearObjective Objective
Gets the minimization objective.
IReadOnlyList< LinearConstraint > Constraints
Gets all constraints.
IReadOnlyList< LinearTerm > Terms
Gets nonzero objective terms.
Describes one variable in a portable linear mathematical model.
double UpperBound
Gets the upper bound.
LinearVariableType Type
Gets the variable domain.
int Id
Gets the stable zero-based variable identifier.
double LowerBound
Gets the lower bound.
LinearVariableType
Identifies the domain of a variable in a portable linear mathematical model.
Stores one coefficient of a portable linear expression.
int VariableId
Gets the referenced variable identifier.
double Coefficient
Gets the coefficient.