2using System.Collections.Generic;
4using System.Xml.Serialization;
17[XmlRoot(
"mathematicalModel")]
18[XmlType(TypeName =
"mathematicalModel")]
21 private readonly List<MathematicalVariable> _variables =
24 private readonly List<LinearConstraint> _constraints =
45 [XmlAttribute(
"name")]
55 [XmlElement(
"description")]
65 [XmlElement(
"objective")]
75 [XmlArray(
"variables")]
76 [XmlArrayItem(
"variable")]
83 [XmlArray(
"constraints")]
84 [XmlArrayItem(
"constraint")]
102 constraint.IsEnabled);
111 variable.VariableType ==
121 variable.VariableType is
132 variable.VariableType is
153 ArgumentNullException.ThrowIfNull(
160 existingVariable.Id ==
163 throw new InvalidOperationException(
164 $
"Variable identifier '{variable.Id}' is " +
171 existingVariable.Name,
173 StringComparison.Ordinal)))
175 throw new InvalidOperationException(
176 $
"Variable name '{variable.Name}' is already " +
202 ArgumentNullException.ThrowIfNull(
207 if (_constraints.Any(
208 existingConstraint =>
209 existingConstraint.Id ==
212 throw new InvalidOperationException(
213 $
"Constraint identifier '{constraint.Id}' is " +
217 if (_constraints.Any(
218 existingConstraint =>
220 existingConstraint.Name,
222 StringComparison.Ordinal)))
224 throw new InvalidOperationException(
225 $
"Constraint name '{constraint.Name}' is " +
229 EnsureExpressionReferencesKnownVariables(
231 $
"constraint '{constraint.Name}'");
250 return _variables.FirstOrDefault(
269 if (
string.IsNullOrWhiteSpace(
272 throw new ArgumentException(
273 "A variable name is required.",
277 return _variables.FirstOrDefault(
282 StringComparison.Ordinal));
294 if (
string.IsNullOrWhiteSpace(
297 throw new InvalidOperationException(
298 "A mathematical model name is required.");
301 ArgumentNullException.ThrowIfNull(
306 EnsureUniqueVariables();
307 EnsureUniqueConstraints();
313 if (variable is
null)
315 throw new InvalidOperationException(
316 "The variable collection cannot contain a " +
327 if (constraint is
null)
329 throw new InvalidOperationException(
330 "The constraint collection cannot contain " +
336 EnsureExpressionReferencesKnownVariables(
338 $
"constraint '{constraint.Name}'");
341 EnsureExpressionReferencesKnownVariables(
401 clone.Constraints.Add(
408 private void EnsureExpressionReferencesKnownVariables(
419 throw new InvalidOperationException(
420 $
"The {context} references unknown variable " +
421 $
"identifier '{term.VariableId}'.");
426 private void EnsureUniqueVariables()
436 throw new InvalidOperationException(
437 "Mathematical variable identifiers must be " +
445 StringComparer.Ordinal)
450 throw new InvalidOperationException(
451 "Mathematical variable names must be unique.");
455 private void EnsureUniqueConstraints()
465 throw new InvalidOperationException(
466 "Linear constraint identifiers must be unique.");
473 StringComparer.Ordinal)
478 throw new InvalidOperationException(
479 "Linear constraint names must be unique.");
Represents one linear constraint in a mathematical optimization model.
void EnsureValid()
Validates the linear constraint.
string Name
Gets or sets the constraint name.
LinearConstraint Clone()
Creates an independent copy of this constraint.
LinearExpression LeftHandSide
Gets or sets the left-hand-side linear expression.
int Id
Gets or sets the unique constraint identifier within the mathematical model.
Represents a linear expression composed of variable terms and a constant value.
List< LinearTerm > Terms
Gets the linear terms of the expression.
Represents one coefficient-variable term in a linear expression.
int VariableId
Gets or sets the identifier of the referenced mathematical variable.
List< MathematicalVariable > Variables
Gets the mathematical variables.
int IntegerVariableCount
Gets the number of general integer variables.
void AddVariable(MathematicalVariable variable)
Adds a mathematical variable.
int VariableCount
Gets the number of mathematical variables.
MathematicalModel()
Initializes an empty mathematical model.
MathematicalModel Clone()
Creates an independent copy of the mathematical model.
void AddConstraint(LinearConstraint constraint)
Adds a linear constraint.
MathematicalObjective Objective
Gets or sets the mathematical objective.
int BinaryVariableCount
Gets the number of binary variables.
MathematicalVariable? FindVariableById(int variableId)
Finds a mathematical variable by identifier.
List< LinearConstraint > Constraints
Gets the linear constraints.
void EnsureValid()
Validates the complete mathematical model.
string Description
Gets or sets an optional model description.
int EnabledConstraintCount
Gets the number of enabled linear constraints.
int ContinuousVariableCount
Gets the number of continuous variables.
MathematicalVariable? FindVariableByName(string name)
Finds a mathematical variable by name.
string Name
Gets or sets the model name.
Represents the objective function of a mathematical optimization model.
Represents one decision variable in a mathematical optimization model.
MathematicalVariableType VariableType
Gets or sets the variable domain.
string DomainKey
Gets or sets an optional domain key used to map the mathematical variable back to a lot-sizing decisi...
int Id
Gets or sets the unique variable identifier within the mathematical model.
double LowerBound
Gets or sets the variable lower bound.
string Name
Gets or sets the variable name.
void EnsureValid()
Validates the mathematical variable.
double UpperBound
Gets or sets the variable upper bound.
MathematicalVariableType
Identifies the domain of a mathematical decision variable.