LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalVariable.cs
Go to the documentation of this file.
1using System;
2using System.Xml.Serialization;
3
5
6/// <summary>
7/// Represents one decision variable in a mathematical
8/// optimization model.
9/// </summary>
10[Serializable]
11[XmlType(TypeName = "mathematicalVariable")]
12public sealed class MathematicalVariable
13{
14 /// <summary>
15 /// Initializes an empty mathematical variable.
16 /// </summary>
18 {
19 Name =
20 string.Empty;
21
23 0.0;
24
26 double.PositiveInfinity;
27
29 MathematicalVariableType.Continuous;
30 }
31
32 /// <summary>
33 /// Initializes a mathematical variable.
34 /// </summary>
35 /// <param name="id">
36 /// Unique variable identifier within the model.
37 /// </param>
38 /// <param name="name">
39 /// Variable name.
40 /// </param>
41 /// <param name="variableType">
42 /// Variable domain.
43 /// </param>
44 /// <param name="lowerBound">
45 /// Variable lower bound.
46 /// </param>
47 /// <param name="upperBound">
48 /// Variable upper bound.
49 /// </param>
51 int id,
52 string name,
53 MathematicalVariableType variableType,
54 double lowerBound = 0.0,
55 double upperBound = double.PositiveInfinity)
56 : this()
57 {
58 Id =
59 id;
60
61 Name =
62 name;
63
65 variableType;
66
68 lowerBound;
69
71 upperBound;
72
74 }
75
76 /// <summary>
77 /// Gets or sets the unique variable 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 variable name.
89 /// </summary>
90 [XmlAttribute("name")]
91 public string Name
92 {
93 get;
94 set;
95 }
96
97 /// <summary>
98 /// Gets or sets the variable domain.
99 /// </summary>
100 [XmlAttribute("variableType")]
102 {
103 get;
104 set;
105 }
106
107 /// <summary>
108 /// Gets or sets the variable lower bound.
109 /// </summary>
110 [XmlAttribute("lowerBound")]
111 public double LowerBound
112 {
113 get;
114 set;
115 }
116
117 /// <summary>
118 /// Gets or sets the variable upper bound.
119 /// </summary>
120 [XmlAttribute("upperBound")]
121 public double UpperBound
122 {
123 get;
124 set;
125 }
126
127 /// <summary>
128 /// Gets or sets an optional domain key used to map the
129 /// mathematical variable back to a lot-sizing decision.
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 a value indicating whether the variable has a
152 /// finite lower bound.
153 /// </summary>
154 [XmlIgnore]
155 public bool HasFiniteLowerBound =>
156 !double.IsNegativeInfinity(
157 LowerBound);
158
159 /// <summary>
160 /// Gets a value indicating whether the variable has a
161 /// finite upper bound.
162 /// </summary>
163 [XmlIgnore]
164 public bool HasFiniteUpperBound =>
165 !double.IsPositiveInfinity(
166 UpperBound);
167
168 /// <summary>
169 /// Validates the mathematical variable.
170 /// </summary>
171 /// <exception cref="InvalidOperationException">
172 /// Thrown when the variable definition is invalid.
173 /// </exception>
174 public void EnsureValid()
175 {
176 if (Id <= 0)
177 {
178 throw new InvalidOperationException(
179 "A mathematical variable identifier must be " +
180 "strictly positive.");
181 }
182
183 if (string.IsNullOrWhiteSpace(
184 Name))
185 {
186 throw new InvalidOperationException(
187 "A mathematical variable name is required.");
188 }
189
190 if (VariableType ==
192 {
193 throw new InvalidOperationException(
194 "A mathematical variable type is required.");
195 }
196
197 if (double.IsNaN(
198 LowerBound) ||
199 double.IsNaN(
200 UpperBound))
201 {
202 throw new InvalidOperationException(
203 "Mathematical variable bounds cannot be NaN.");
204 }
205
206 if (LowerBound >
208 {
209 throw new InvalidOperationException(
210 "A mathematical variable lower bound cannot " +
211 "exceed its upper bound.");
212 }
213
214 if (VariableType ==
216 (LowerBound < 0.0 ||
217 UpperBound > 1.0))
218 {
219 throw new InvalidOperationException(
220 "A binary variable must have bounds within " +
221 "[0, 1].");
222 }
223
224 if (VariableType is
225 MathematicalVariableType.Integer or
227 MathematicalVariableType.SemiInteger)
228 {
230 LowerBound !=
231 Math.Truncate(
232 LowerBound))
233 {
234 throw new InvalidOperationException(
235 "An integer variable must have an integer " +
236 "lower bound.");
237 }
238
240 UpperBound !=
241 Math.Truncate(
242 UpperBound))
243 {
244 throw new InvalidOperationException(
245 "An integer variable must have an integer " +
246 "upper bound.");
247 }
248 }
249 }
250}
MathematicalVariableType VariableType
Gets or sets the variable domain.
bool HasFiniteUpperBound
Gets a value indicating whether the variable has a finite upper bound.
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.
MathematicalVariable(int id, string name, MathematicalVariableType variableType, double lowerBound=0.0, double upperBound=double.PositiveInfinity)
Initializes a mathematical variable.
bool HasFiniteLowerBound
Gets a value indicating whether the variable has a finite lower bound.
string Description
Gets or sets an optional description.
void EnsureValid()
Validates the mathematical variable.
MathematicalVariable()
Initializes an empty mathematical variable.
double UpperBound
Gets or sets the variable upper bound.
MathematicalVariableType
Identifies the domain of a mathematical decision variable.