LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
LinearConstraint.cs
Go to the documentation of this file.
1using System;
2using System.Xml.Serialization;
3
5
6/// <summary>
7/// Represents one linear constraint in a mathematical
8/// optimization model.
9/// </summary>
10[Serializable]
11[XmlType(TypeName = "linearConstraint")]
12public sealed class LinearConstraint
13{
14 /// <summary>
15 /// Initializes an empty linear constraint.
16 /// </summary>
18 {
19 Name =
20 string.Empty;
21
23 new LinearExpression();
24
25 Sense =
27 }
28
29 /// <summary>
30 /// Initializes a linear constraint.
31 /// </summary>
32 /// <param name="id">
33 /// Unique constraint identifier within the model.
34 /// </param>
35 /// <param name="name">
36 /// Constraint name.
37 /// </param>
38 /// <param name="leftHandSide">
39 /// Left-hand-side linear expression.
40 /// </param>
41 /// <param name="sense">
42 /// Constraint relational sense.
43 /// </param>
44 /// <param name="rightHandSide">
45 /// Right-hand-side constant value.
46 /// </param>
48 int id,
49 string name,
50 LinearExpression leftHandSide,
52 double rightHandSide)
53 : this()
54 {
55 ArgumentNullException.ThrowIfNull(
56 leftHandSide);
57
58 Id =
59 id;
60
61 Name =
62 name;
63
65 leftHandSide;
66
67 Sense =
68 sense;
69
71 rightHandSide;
72
74 }
75
76 /// <summary>
77 /// Gets or sets the unique constraint identifier within the
78 /// mathematical model.
79 /// </summary>
80 [XmlAttribute("id")]
81 public int Id
82 {
83 get;
84 set;
85 }
86
87 /// <summary>
88 /// Gets or sets the constraint name.
89 /// </summary>
90 [XmlAttribute("name")]
91 public string Name
92 {
93 get;
94 set;
95 }
96
97 /// <summary>
98 /// Gets or sets the left-hand-side linear expression.
99 /// </summary>
100 [XmlElement("leftHandSide")]
102 {
103 get;
104 set;
105 }
106
107 /// <summary>
108 /// Gets or sets the relational sense of the constraint.
109 /// </summary>
110 [XmlAttribute("sense")]
112 {
113 get;
114 set;
115 }
116
117 /// <summary>
118 /// Gets or sets the right-hand-side constant value.
119 /// </summary>
120 [XmlAttribute("rightHandSide")]
121 public double RightHandSide
122 {
123 get;
124 set;
125 }
126
127 /// <summary>
128 /// Gets or sets an optional domain key used to identify the
129 /// business meaning of the constraint.
130 /// </summary>
131 [XmlElement("domainKey")]
132 public string DomainKey
133 {
134 get;
135 set;
136 } =
137 string.Empty;
138
139 /// <summary>
140 /// Gets or sets an optional description.
141 /// </summary>
142 [XmlElement("description")]
143 public string Description
144 {
145 get;
146 set;
147 } =
148 string.Empty;
149
150 /// <summary>
151 /// Gets or sets a value indicating whether the constraint
152 /// is enabled.
153 /// </summary>
154 [XmlAttribute("isEnabled")]
155 public bool IsEnabled
156 {
157 get;
158 set;
159 } =
160 true;
161
162 /// <summary>
163 /// Validates the linear constraint.
164 /// </summary>
165 /// <exception cref="InvalidOperationException">
166 /// Thrown when the constraint definition is invalid.
167 /// </exception>
168 public void EnsureValid()
169 {
170 if (Id <= 0)
171 {
172 throw new InvalidOperationException(
173 "A linear constraint identifier must be " +
174 "strictly positive.");
175 }
176
177 if (string.IsNullOrWhiteSpace(
178 Name))
179 {
180 throw new InvalidOperationException(
181 "A linear constraint name is required.");
182 }
183
184 ArgumentNullException.ThrowIfNull(
186
187 LeftHandSide.EnsureValid();
188
189 if (Sense ==
191 {
192 throw new InvalidOperationException(
193 "A mathematical constraint sense is required.");
194 }
195
196 if (double.IsNaN(
197 RightHandSide) ||
198 double.IsInfinity(
200 {
201 throw new InvalidOperationException(
202 "A linear constraint right-hand side must be a " +
203 "finite number.");
204 }
205 }
206
207 /// <summary>
208 /// Creates an independent copy of this constraint.
209 /// </summary>
210 /// <returns>
211 /// Cloned linear constraint.
212 /// </returns>
214 {
215 return new LinearConstraint
216 {
217 Id =
218 Id,
219
220 Name =
221 Name,
222
224 LeftHandSide.Clone(),
225
226 Sense =
227 Sense,
228
231
232 DomainKey =
233 DomainKey,
234
237
238 IsEnabled =
240 };
241 }
242}
void EnsureValid()
Validates the linear constraint.
string Name
Gets or sets the constraint name.
string DomainKey
Gets or sets an optional domain key used to identify the business meaning of the constraint.
double RightHandSide
Gets or sets the right-hand-side constant value.
LinearConstraint Clone()
Creates an independent copy of this constraint.
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.
LinearConstraint()
Initializes an empty linear constraint.
LinearConstraint(int id, string name, LinearExpression leftHandSide, MathematicalConstraintSense sense, double rightHandSide)
Initializes a linear constraint.
string Description
Gets or sets an optional description.
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.
MathematicalConstraintSense
Identifies the relational sense of a mathematical constraint.