LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalModelFormulationRegistry.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
6
7/// <summary>
8/// Stores and resolves solver-independent mathematical
9/// formulations.
10/// </summary>
12{
13 private readonly Dictionary<string, IMathematicalModelFormulation>
14 _formulations =
15 new(
16 StringComparer.OrdinalIgnoreCase);
17
18 /// <summary>
19 /// Gets the number of registered formulations.
20 /// </summary>
21 public int Count =>
22 _formulations.Count;
23
24 /// <summary>
25 /// Registers a mathematical formulation.
26 /// </summary>
27 /// <param name="formulation">
28 /// Formulation to register.
29 /// </param>
30 /// <exception cref="ArgumentNullException">
31 /// Thrown when <paramref name="formulation"/> is
32 /// <see langword="null"/>.
33 /// </exception>
34 /// <exception cref="InvalidOperationException">
35 /// Thrown when the formulation identifier is empty or is
36 /// already registered.
37 /// </exception>
38 public void Register(
40 {
41 ArgumentNullException.ThrowIfNull(
42 formulation);
43
44 string formulationId =
45 NormalizeFormulationId(
46 formulation.FormulationId);
47
48 if (!_formulations.TryAdd(
49 formulationId,
50 formulation))
51 {
52 throw new InvalidOperationException(
53 $"Mathematical formulation '{formulationId}' is " +
54 "already registered.");
55 }
56 }
57
58 /// <summary>
59 /// Registers or replaces a mathematical formulation.
60 /// </summary>
61 /// <param name="formulation">
62 /// Formulation to register.
63 /// </param>
64 /// <exception cref="ArgumentNullException">
65 /// Thrown when <paramref name="formulation"/> is
66 /// <see langword="null"/>.
67 /// </exception>
68 /// <exception cref="InvalidOperationException">
69 /// Thrown when the formulation identifier is empty.
70 /// </exception>
71 public void RegisterOrReplace(
73 {
74 ArgumentNullException.ThrowIfNull(
75 formulation);
76
77 string formulationId =
78 NormalizeFormulationId(
79 formulation.FormulationId);
80
81 _formulations[formulationId] =
82 formulation;
83 }
84
85 /// <summary>
86 /// Determines whether a formulation is registered.
87 /// </summary>
88 /// <param name="formulationId">
89 /// Formulation identifier.
90 /// </param>
91 /// <returns>
92 /// <see langword="true"/> when the formulation is
93 /// registered; otherwise, <see langword="false"/>.
94 /// </returns>
95 public bool Contains(
96 string formulationId)
97 {
98 string normalizedFormulationId =
99 NormalizeFormulationId(
100 formulationId);
101
102 return _formulations.ContainsKey(
103 normalizedFormulationId);
104 }
105
106 /// <summary>
107 /// Gets a required mathematical formulation.
108 /// </summary>
109 /// <param name="formulationId">
110 /// Formulation identifier.
111 /// </param>
112 /// <returns>
113 /// Registered mathematical formulation.
114 /// </returns>
115 /// <exception cref="KeyNotFoundException">
116 /// Thrown when no formulation is registered for the supplied
117 /// identifier.
118 /// </exception>
120 string formulationId)
121 {
122 string normalizedFormulationId =
123 NormalizeFormulationId(
124 formulationId);
125
126 if (_formulations.TryGetValue(
127 normalizedFormulationId,
128 out IMathematicalModelFormulation? formulation))
129 {
130 return formulation;
131 }
132
133 throw new KeyNotFoundException(
134 $"No mathematical formulation is registered for " +
135 $"identifier '{normalizedFormulationId}'.");
136 }
137
138 /// <summary>
139 /// Attempts to get a registered mathematical formulation.
140 /// </summary>
141 /// <param name="formulationId">
142 /// Formulation identifier.
143 /// </param>
144 /// <param name="formulation">
145 /// Registered formulation when the method succeeds.
146 /// </param>
147 /// <returns>
148 /// <see langword="true"/> when the formulation is found;
149 /// otherwise, <see langword="false"/>.
150 /// </returns>
151 public bool TryGet(
152 string formulationId,
153 out IMathematicalModelFormulation? formulation)
154 {
155 string normalizedFormulationId =
156 NormalizeFormulationId(
157 formulationId);
158
159 return _formulations.TryGetValue(
160 normalizedFormulationId,
161 out formulation);
162 }
163
164 /// <summary>
165 /// Removes a registered mathematical formulation.
166 /// </summary>
167 /// <param name="formulationId">
168 /// Formulation identifier.
169 /// </param>
170 /// <returns>
171 /// <see langword="true"/> when a formulation was removed;
172 /// otherwise, <see langword="false"/>.
173 /// </returns>
174 public bool Remove(
175 string formulationId)
176 {
177 string normalizedFormulationId =
178 NormalizeFormulationId(
179 formulationId);
180
181 return _formulations.Remove(
182 normalizedFormulationId);
183 }
184
185 /// <summary>
186 /// Returns all registered formulations ordered by name and
187 /// identifier.
188 /// </summary>
189 /// <returns>
190 /// Read-only formulation list.
191 /// </returns>
192 public IReadOnlyList<IMathematicalModelFormulation> GetAll()
193 {
194 return _formulations.Values
195 .OrderBy(
196 formulation =>
197 formulation.Name,
198 StringComparer.OrdinalIgnoreCase)
199 .ThenBy(
200 formulation =>
201 formulation.FormulationId,
202 StringComparer.OrdinalIgnoreCase)
203 .ToArray();
204 }
205
206 /// <summary>
207 /// Removes all registered formulations.
208 /// </summary>
209 public void Clear()
210 {
211 _formulations.Clear();
212 }
213
214 private static string NormalizeFormulationId(
215 string formulationId)
216 {
217 if (string.IsNullOrWhiteSpace(
218 formulationId))
219 {
220 throw new InvalidOperationException(
221 "A mathematical formulation identifier is " +
222 "required.");
223 }
224
225 return formulationId.Trim();
226 }
227}
bool Contains(string formulationId)
Determines whether a formulation is registered.
bool TryGet(string formulationId, out IMathematicalModelFormulation? formulation)
Attempts to get a registered mathematical formulation.
void RegisterOrReplace(IMathematicalModelFormulation formulation)
Registers or replaces a mathematical formulation.
bool Remove(string formulationId)
Removes a registered mathematical formulation.
IMathematicalModelFormulation GetRequired(string formulationId)
Gets a required mathematical formulation.
IReadOnlyList< IMathematicalModelFormulation > GetAll()
Returns all registered formulations ordered by name and identifier.
void Register(IMathematicalModelFormulation formulation)
Registers a mathematical formulation.
Defines a solver-independent mathematical formulation for a lot-sizing instance.