LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalModelFormulationSelectionService.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using LotSizingDataModel.Instance;
5
7
8/// <summary>
9/// Selects a compatible mathematical formulation for a
10/// lot-sizing instance.
11/// </summary>
13{
14 /// <summary>
15 /// Selects a mathematical formulation.
16 /// </summary>
17 /// <param name="instance">
18 /// Lot-sizing instance to formulate.
19 /// </param>
20 /// <param name="registry">
21 /// Formulation registry.
22 /// </param>
23 /// <param name="requestedFormulationId">
24 /// Optional requested formulation identifier. An empty value
25 /// enables automatic selection.
26 /// </param>
27 /// <param name="allowFallback">
28 /// Indicates whether another compatible formulation may be
29 /// selected when the requested formulation is unavailable or
30 /// incompatible.
31 /// </param>
32 /// <returns>
33 /// Formulation-selection result.
34 /// </returns>
35 /// <exception cref="ArgumentNullException">
36 /// Thrown when <paramref name="instance"/> or
37 /// <paramref name="registry"/> is <see langword="null"/>.
38 /// </exception>
40 LotSizingInstance instance,
42 string requestedFormulationId = "",
43 bool allowFallback = true)
44 {
45 ArgumentNullException.ThrowIfNull(
46 instance);
47
48 ArgumentNullException.ThrowIfNull(
49 registry);
50
51 string normalizedRequestedFormulationId =
52 requestedFormulationId?.Trim() ??
53 string.Empty;
54
55 var diagnostics =
56 new List<string>();
57
58 if (!string.IsNullOrWhiteSpace(
59 normalizedRequestedFormulationId))
60 {
61 if (registry.TryGet(
62 normalizedRequestedFormulationId,
64 requestedFormulation))
65 {
66 if (requestedFormulation!.CanBuild(
67 instance))
68 {
70 success =
72 .Success(
73 normalizedRequestedFormulationId,
74 requestedFormulation,
75 usedFallback:
76 false);
77
78 success.AddDiagnostic(
79 $"Requested formulation " +
80 $"'{requestedFormulation.FormulationId}' " +
81 "was selected.");
82
83 return success;
84 }
85
86 diagnostics.Add(
87 $"Requested formulation " +
88 $"'{requestedFormulation.FormulationId}' does " +
89 "not support the supplied lot-sizing " +
90 "instance.");
91 }
92 else
93 {
94 diagnostics.Add(
95 $"Requested formulation " +
96 $"'{normalizedRequestedFormulationId}' is not " +
97 "registered.");
98 }
99
100 if (!allowFallback)
101 {
103 failure =
105 .Failure(
106 normalizedRequestedFormulationId,
107 "No compatible requested " +
108 "formulation could be selected.");
109
110 AddDiagnostics(
111 failure,
112 diagnostics);
113
114 return failure;
115 }
116 }
117
118 IMathematicalModelFormulation? fallbackFormulation =
119 registry.GetAll()
120 .FirstOrDefault(
121 formulation =>
122 formulation.CanBuild(
123 instance));
124
125 if (fallbackFormulation is null)
126 {
129 .Failure(
130 normalizedRequestedFormulationId,
131 "No registered mathematical formulation " +
132 "supports the supplied lot-sizing " +
133 "instance.");
134
135 AddDiagnostics(
136 failure,
137 diagnostics);
138
139 return failure;
140 }
141
142 bool usedFallback =
143 !string.IsNullOrWhiteSpace(
144 normalizedRequestedFormulationId);
145
148 normalizedRequestedFormulationId,
149 fallbackFormulation,
150 usedFallback);
151
152 AddDiagnostics(
153 result,
154 diagnostics);
155
156 result.AddDiagnostic(
157 usedFallback
158 ? $"Fallback formulation " +
159 $"'{fallbackFormulation.FormulationId}' was " +
160 "selected."
161 : $"Formulation " +
162 $"'{fallbackFormulation.FormulationId}' was " +
163 "selected automatically.");
164
165 return result;
166 }
167
168 private static void AddDiagnostics(
170 IEnumerable<string> diagnostics)
171 {
172 foreach (
173 string diagnostic
174 in diagnostics)
175 {
176 result.AddDiagnostic(
177 diagnostic);
178 }
179 }
180}
bool TryGet(string formulationId, out IMathematicalModelFormulation? formulation)
Attempts to get a registered mathematical formulation.
IReadOnlyList< IMathematicalModelFormulation > GetAll()
Returns all registered formulations ordered by name and identifier.
Describes the result of selecting a mathematical formulation for a lot-sizing instance.
static MathematicalModelFormulationSelectionResult Failure(string requestedFormulationId, string diagnostic)
Creates a failed formulation-selection result.
static MathematicalModelFormulationSelectionResult Success(string requestedFormulationId, IMathematicalModelFormulation formulation, bool usedFallback)
Creates a successful formulation-selection result.
MathematicalModelFormulationSelectionResult Select(LotSizingInstance instance, MathematicalModelFormulationRegistry registry, string requestedFormulationId="", bool allowFallback=true)
Selects a mathematical formulation.
Defines a solver-independent mathematical formulation for a lot-sizing instance.