ULSAlgorithms 1.1.0-g3e5595996d
High-performance exact and heuristic algorithms for uncapacitated lot sizing
Loading...
Searching...
No Matches
LinearModel.cs
Go to the documentation of this file.
2
3/// <summary>
4/// Immutable solver-independent linear or mixed-integer linear model.
5/// </summary>
6public sealed class LinearModel
7{
8 private readonly LinearVariable[] _variables;
9 private readonly LinearConstraint[] _constraints;
10
11 /// <summary>Initializes a portable model.</summary>
13 string name,
14 IEnumerable<LinearVariable> variables,
15 IEnumerable<LinearConstraint> constraints,
16 LinearObjective objective)
17 {
18 if (string.IsNullOrWhiteSpace(name))
19 {
20 throw new ArgumentException(
21 "A model name is required.",
22 nameof(name));
23 }
24
25 ArgumentNullException.ThrowIfNull(variables);
26 ArgumentNullException.ThrowIfNull(constraints);
27 ArgumentNullException.ThrowIfNull(objective);
28
29 _variables = variables.ToArray();
30 _constraints = constraints.ToArray();
31
32 if (_variables
33 .Select(
34 static variable =>
35 variable.Id)
36 .Distinct()
37 .Count() != _variables.Length)
38 {
39 throw new ArgumentException(
40 "Variable identifiers must be unique.",
41 nameof(variables));
42 }
43
44 if (_variables
45 .Select(
46 static variable =>
47 variable.Name)
48 .Distinct(
49 StringComparer.Ordinal)
50 .Count() != _variables.Length)
51 {
52 throw new ArgumentException(
53 "Variable names must be unique.",
54 nameof(variables));
55 }
56
57 var ids =
58 _variables
59 .Select(
60 static variable =>
61 variable.Id)
62 .ToHashSet();
63
64 foreach (LinearConstraint constraint in _constraints)
65 {
66 foreach (LinearTerm term in constraint.Terms)
67 {
68 if (!ids.Contains(term.VariableId))
69 {
70 throw new ArgumentException(
71 $"Constraint '{constraint.Name}' references unknown " +
72 $"variable id {term.VariableId}.",
73 nameof(constraints));
74 }
75 }
76 }
77
78 foreach (LinearTerm term in objective.Terms)
79 {
80 if (!ids.Contains(term.VariableId))
81 {
82 throw new ArgumentException(
83 $"The objective references unknown variable id " +
84 $"{term.VariableId}.",
85 nameof(objective));
86 }
87 }
88
89 Name = name.Trim();
90 Objective = objective;
91 }
92
93 /// <summary>Gets the model name.</summary>
94 public string Name { get; }
95
96 /// <summary>Gets all variables.</summary>
97 public IReadOnlyList<LinearVariable> Variables => _variables;
98
99 /// <summary>Gets all constraints.</summary>
100 public IReadOnlyList<LinearConstraint> Constraints => _constraints;
101
102 /// <summary>Gets the minimization objective.</summary>
104
105 /// <summary>Gets the number of variables.</summary>
106 public int VariableCount => _variables.Length;
107
108 /// <summary>Gets the number of constraints.</summary>
109 public int ConstraintCount => _constraints.Length;
110
111 /// <summary>Gets whether the model contains integer or binary variables.</summary>
112 public bool IsMixedInteger =>
113 _variables.Any(
114 static variable =>
115 variable.Type != LinearVariableType.Continuous);
116
117 /// <summary>Finds a variable by id.</summary>
119 int id)
120 {
121 return _variables.First(
122 variable =>
123 variable.Id == id);
124 }
125}
Describes one portable linear constraint.
IReadOnlyList< LinearTerm > Terms
Gets the nonzero terms.
IReadOnlyList< LinearVariable > Variables
Gets all variables.
LinearVariable GetVariable(int id)
Finds a variable by id.
int ConstraintCount
Gets the number of constraints.
LinearObjective Objective
Gets the minimization objective.
LinearModel(string name, IEnumerable< LinearVariable > variables, IEnumerable< LinearConstraint > constraints, LinearObjective objective)
Initializes a portable model.
IReadOnlyList< LinearConstraint > Constraints
Gets all constraints.
bool IsMixedInteger
Gets whether the model contains integer or binary variables.
int VariableCount
Gets the number of variables.
Describes the minimization objective of a portable linear model.
Describes one variable in a portable linear mathematical model.
LinearVariableType
Identifies the domain of a variable in a portable linear mathematical model.
Stores one coefficient of a portable linear expression.
Definition LinearTerm.cs:7
int VariableId
Gets the referenced variable identifier.
Definition LinearTerm.cs:31