LotSizingDataModel.Solution 2.0.1
Solution objects for production, setup, inventory and related decisions.
Loading...
Searching...
No Matches
AlgorithmParameter.cs
Go to the documentation of this file.
1using System;
2using System.Xml.Serialization;
3
5
6/// <summary>
7/// Represents one named parameter used by a
8/// solution-generation method.
9/// </summary>
10/// <remarks>
11/// Parameter values are stored as strings so that the class
12/// can represent parameters belonging to any algorithm,
13/// solver, heuristic or external application.
14/// </remarks>
15[Serializable]
16[XmlType(TypeName = "algorithmParameter")]
17public sealed class AlgorithmParameter
18{
19 private string _name = string.Empty;
20 private string _value = string.Empty;
21 private string _dataType = string.Empty;
22 private string _description = string.Empty;
23
24 /// <summary>
25 /// Initializes an empty algorithm parameter.
26 /// </summary>
27 /// <remarks>
28 /// This constructor is required by
29 /// <see cref="XmlSerializer"/>.
30 /// </remarks>
32 {
33 }
34
35 /// <summary>
36 /// Initializes a named algorithm parameter.
37 /// </summary>
38 /// <param name="name">
39 /// Parameter name.
40 /// </param>
41 /// <param name="value">
42 /// Serialized parameter value.
43 /// </param>
44 /// <param name="dataType">
45 /// Optional description of the value type.
46 /// </param>
47 /// <param name="description">
48 /// Optional human-readable description.
49 /// </param>
51 string name,
52 string value,
53 string dataType = "",
54 string description = "")
55 {
56 Name = name;
57 Value = value;
58 DataType = dataType;
59 Description = description;
60 }
61
62 /// <summary>
63 /// Gets or sets the parameter name.
64 /// </summary>
65 [XmlAttribute("name")]
66 public string Name
67 {
68 get => _name;
69 set
70 {
71 if (string.IsNullOrWhiteSpace(value))
72 {
73 throw new ArgumentException(
74 "The parameter name cannot be empty.",
75 nameof(value));
76 }
77
78 _name = value.Trim();
79 }
80 }
81
82 /// <summary>
83 /// Gets or sets the serialized parameter value.
84 /// </summary>
85 [XmlAttribute("value")]
86 public string Value
87 {
88 get => _value;
89 set => _value = value ?? string.Empty;
90 }
91
92 /// <summary>
93 /// Gets or sets an optional description of the value type.
94 /// </summary>
95 /// <remarks>
96 /// Typical values include integer, double, boolean,
97 /// string and enumeration.
98 /// </remarks>
99 [XmlAttribute("dataType")]
100 public string DataType
101 {
102 get => _dataType;
103 set => _dataType = value?.Trim() ?? string.Empty;
104 }
105
106 /// <summary>
107 /// Gets or sets an optional human-readable description
108 /// of the parameter.
109 /// </summary>
110 [XmlElement("description")]
111 public string Description
112 {
113 get => _description;
114 set => _description = value ?? string.Empty;
115 }
116
117 /// <summary>
118 /// Creates an integer algorithm parameter.
119 /// </summary>
120 /// <param name="name">
121 /// Parameter name.
122 /// </param>
123 /// <param name="value">
124 /// Integer parameter value.
125 /// </param>
126 /// <param name="description">
127 /// Optional parameter description.
128 /// </param>
129 /// <returns>
130 /// A parameter containing the serialized integer value.
131 /// </returns>
133 string name,
134 int value,
135 string description = "")
136 {
137 return new AlgorithmParameter(
138 name,
139 value.ToString(
140 System.Globalization.CultureInfo.InvariantCulture),
141 "integer",
142 description);
143 }
144
145 /// <summary>
146 /// Creates a floating-point algorithm parameter.
147 /// </summary>
148 /// <param name="name">
149 /// Parameter name.
150 /// </param>
151 /// <param name="value">
152 /// Floating-point parameter value.
153 /// </param>
154 /// <param name="description">
155 /// Optional parameter description.
156 /// </param>
157 /// <returns>
158 /// A parameter containing the serialized floating-point value.
159 /// </returns>
161 string name,
162 double value,
163 string description = "")
164 {
165 if (!double.IsFinite(value))
166 {
167 throw new ArgumentOutOfRangeException(
168 nameof(value),
169 value,
170 "The parameter value must be finite.");
171 }
172
173 return new AlgorithmParameter(
174 name,
175 value.ToString(
176 "R",
177 System.Globalization.CultureInfo.InvariantCulture),
178 "double",
179 description);
180 }
181
182 /// <summary>
183 /// Creates a Boolean algorithm parameter.
184 /// </summary>
185 /// <param name="name">
186 /// Parameter name.
187 /// </param>
188 /// <param name="value">
189 /// Boolean parameter value.
190 /// </param>
191 /// <param name="description">
192 /// Optional parameter description.
193 /// </param>
194 /// <returns>
195 /// A parameter containing the serialized Boolean value.
196 /// </returns>
198 string name,
199 bool value,
200 string description = "")
201 {
202 return new AlgorithmParameter(
203 name,
204 value ? "true" : "false",
205 "boolean",
206 description);
207 }
208
209 /// <summary>
210 /// Creates a string algorithm parameter.
211 /// </summary>
212 /// <param name="name">
213 /// Parameter name.
214 /// </param>
215 /// <param name="value">
216 /// String parameter value.
217 /// </param>
218 /// <param name="description">
219 /// Optional parameter description.
220 /// </param>
221 /// <returns>
222 /// A parameter containing the string value.
223 /// </returns>
225 string name,
226 string value,
227 string description = "")
228 {
229 return new AlgorithmParameter(
230 name,
231 value ?? string.Empty,
232 "string",
233 description);
234 }
235
236 /// <summary>
237 /// Attempts to read the parameter value as an integer.
238 /// </summary>
239 /// <param name="value">
240 /// Parsed integer value.
241 /// </param>
242 /// <returns>
243 /// True when the value can be parsed; otherwise, false.
244 /// </returns>
245 public bool TryGetInteger(out int value)
246 {
247 return int.TryParse(
248 Value,
249 System.Globalization.NumberStyles.Integer,
250 System.Globalization.CultureInfo.InvariantCulture,
251 out value);
252 }
253
254 /// <summary>
255 /// Attempts to read the parameter value as a
256 /// floating-point number.
257 /// </summary>
258 /// <param name="value">
259 /// Parsed floating-point value.
260 /// </param>
261 /// <returns>
262 /// True when the value can be parsed and is finite;
263 /// otherwise, false.
264 /// </returns>
265 public bool TryGetDouble(out double value)
266 {
267 bool parsed = double.TryParse(
268 Value,
269 System.Globalization.NumberStyles.Float,
270 System.Globalization.CultureInfo.InvariantCulture,
271 out value);
272
273 return parsed &&
274 double.IsFinite(value);
275 }
276
277 /// <summary>
278 /// Attempts to read the parameter value as a Boolean.
279 /// </summary>
280 /// <param name="value">
281 /// Parsed Boolean value.
282 /// </param>
283 /// <returns>
284 /// True when the value can be parsed; otherwise, false.
285 /// </returns>
286 public bool TryGetBoolean(out bool value)
287 {
288 return bool.TryParse(
289 Value,
290 out value);
291 }
292
293 /// <inheritdoc/>
294 public override string ToString()
295 {
296 return string.IsNullOrWhiteSpace(DataType)
297 ? $"{Name} = {Value}"
298 : $"{Name} = {Value} ({DataType})";
299 }
300}
string Value
Gets or sets the serialized parameter value.
AlgorithmParameter(string name, string value, string dataType="", string description="")
Initializes a named algorithm parameter.
string DataType
Gets or sets an optional description of the value type.
bool TryGetInteger(out int value)
Attempts to read the parameter value as an integer.
AlgorithmParameter()
Initializes an empty algorithm parameter.
bool TryGetDouble(out double value)
Attempts to read the parameter value as a floating-point number.
static AlgorithmParameter FromDouble(string name, double value, string description="")
Creates a floating-point algorithm parameter.
static AlgorithmParameter FromBoolean(string name, bool value, string description="")
Creates a Boolean algorithm parameter.
bool TryGetBoolean(out bool value)
Attempts to read the parameter value as a Boolean.
static AlgorithmParameter FromInteger(string name, int value, string description="")
Creates an integer algorithm parameter.
string Description
Gets or sets an optional human-readable description of the parameter.
static AlgorithmParameter FromString(string name, string value, string description="")
Creates a string algorithm parameter.