LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalModelSolverSelectionResult.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Xml.Serialization;
7
9
10/// <summary>
11/// Represents the result of selecting a solver adapter that can
12/// solve a solver-independent mathematical model.
13/// </summary>
14/// <remarks>
15/// The general solver-selection result is preserved for
16/// diagnostics and availability metadata, while
17/// <see cref="Adapter"/> exposes the selected adapter through the
18/// stronger mathematical-model solver contract.
19/// </remarks>
20[Serializable]
21[XmlType(TypeName = "mathematicalModelSolverSelectionResult")]
23{
24 private readonly List<string> _diagnostics =
25 new();
26
27 /// <summary>
28 /// Initializes an empty mathematical-model solver-selection
29 /// result.
30 /// </summary>
32 {
34 SolverKind.Unknown;
35
37 SolverKind.Unknown;
38
39 AdapterId =
40 string.Empty;
41
43 string.Empty;
44 }
45
46 /// <summary>
47 /// Gets or sets the solver requested by the caller.
48 /// </summary>
49 [XmlAttribute("requestedSolver")]
51 {
52 get;
53 set;
54 }
55
56 /// <summary>
57 /// Gets or sets the solver selected by the general
58 /// solver-selection service.
59 /// </summary>
60 [XmlAttribute("selectedSolver")]
62 {
63 get;
64 set;
65 }
66
67 /// <summary>
68 /// Gets or sets the selected adapter identifier.
69 /// </summary>
70 [XmlElement("adapterId")]
71 public string AdapterId
72 {
73 get;
74 set;
75 }
76
77 /// <summary>
78 /// Gets or sets the selected adapter display name.
79 /// </summary>
80 [XmlElement("adapterName")]
81 public string AdapterName
82 {
83 get;
84 set;
85 }
86
87 /// <summary>
88 /// Gets or sets the availability information associated with
89 /// the selected solver.
90 /// </summary>
91 [XmlElement("availability")]
93 {
94 get;
95 set;
96 }
97
98 /// <summary>
99 /// Gets or sets the selected live mathematical-model solver
100 /// adapter.
101 /// </summary>
102 /// <remarks>
103 /// Runtime adapter instances are intentionally excluded from
104 /// XML serialization.
105 /// </remarks>
106 [XmlIgnore]
108 {
109 get;
110 set;
111 }
112
113 /// <summary>
114 /// Gets the diagnostics produced during adapter selection.
115 /// </summary>
116 [XmlArray("diagnostics")]
117 [XmlArrayItem("diagnostic")]
118 public List<string> Diagnostics =>
119 _diagnostics;
120
121 /// <summary>
122 /// Gets a value indicating whether a usable
123 /// mathematical-model solver adapter was selected.
124 /// </summary>
125 [XmlIgnore]
126 public bool IsSuccessful =>
127 Adapter is not null &&
128 SelectedSolver is not
129 SolverKind.Unknown and not
131 Availability is not null &&
132 Availability.IsUsable;
133
134 /// <summary>
135 /// Adds a non-empty diagnostic message.
136 /// </summary>
137 /// <param name="message">
138 /// Diagnostic message.
139 /// </param>
140 public void AddDiagnostic(
141 string message)
142 {
143 if (!string.IsNullOrWhiteSpace(
144 message))
145 {
146 _diagnostics.Add(
147 message.Trim());
148 }
149 }
150
151 /// <summary>
152 /// Creates a mathematical-model solver-selection result from
153 /// a general solver-selection result.
154 /// </summary>
155 /// <param name="selection">
156 /// General solver-selection result.
157 /// </param>
158 /// <returns>
159 /// Typed mathematical-model solver-selection result.
160 /// </returns>
161 /// <exception cref="ArgumentNullException">
162 /// Thrown when <paramref name="selection"/> is
163 /// <see langword="null"/>.
164 /// </exception>
166 SolverSelectionResult selection)
167 {
168 ArgumentNullException.ThrowIfNull(
169 selection);
170
171 var result =
173 {
175 selection.RequestedSolver,
176
178 selection.SelectedSolver,
179
180 AdapterId =
181 selection.AdapterId,
182
184 selection.AdapterName,
185
187 selection.Availability
188 };
189
190 foreach (
191 string diagnostic
192 in selection.Diagnostics)
193 {
194 result.AddDiagnostic(
195 diagnostic);
196 }
197
198 if (selection.Adapter is
199 IMathematicalModelSolverAdapter mathematicalAdapter)
200 {
201 result.Adapter =
202 mathematicalAdapter;
203
204 return result;
205 }
206
207 if (selection.Adapter is not null)
208 {
209 result.AddDiagnostic(
210 $"Selected adapter '{selection.AdapterName}' " +
211 "does not implement " +
212 $"{nameof(IMathematicalModelSolverAdapter)}.");
213 }
214
215 return result;
216 }
217}
Describes the availability and detected installation details of a mathematical optimization solver.
Represents the result of selecting a solver adapter for a solve request.
SolverKind RequestedSolver
Gets or sets the solver requested by the caller.
string AdapterName
Gets or sets the display name of the selected adapter.
SolverKind SelectedSolver
Gets or sets the solver selected by the selection service.
string AdapterId
Gets or sets the identifier of the selected adapter.
SolverKind SelectedSolver
Gets or sets the solver selected by the general solver-selection service.
static MathematicalModelSolverSelectionResult From(SolverSelectionResult selection)
Creates a mathematical-model solver-selection result from a general solver-selection result.
bool IsSuccessful
Gets a value indicating whether a usable mathematical-model solver adapter was selected.
IMathematicalModelSolverAdapter? Adapter
Gets or sets the selected live mathematical-model solver adapter.
SolverAvailabilityInfo? Availability
Gets or sets the availability information associated with the selected solver.
MathematicalModelSolverSelectionResult()
Initializes an empty mathematical-model solver-selection result.
List< string > Diagnostics
Gets the diagnostics produced during adapter selection.
Defines a dynamically discoverable solver adapter capable of solving solver-independent mathematical ...
SolverKind
Identifies a supported mathematical optimization solver.
Definition SolverKind.cs:12
@ Automatic
The solver is selected automatically from the adapters available on the current computer.
Definition SolverKind.cs:22
@ Unknown
No solver has been specified.
Definition SolverKind.cs:16