LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
SolverSelectionOptions.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Xml.Serialization;
5
7
8/// <summary>
9/// Configures how a solver adapter is selected when automatic
10/// solver selection is requested.
11/// </summary>
12[Serializable]
13[XmlType(TypeName = "solverSelectionOptions")]
14public sealed class SolverSelectionOptions
15{
16 private readonly List<SolverKind> _solverPriority =
17 new();
18
19 private readonly List<SolverCapability>
20 _requiredCapabilities =
21 new();
22
23 /// <summary>
24 /// Initializes solver-selection options with the default
25 /// solver priority.
26 /// </summary>
28 {
29 _solverPriority.Add(
30 SolverKind.Cplex);
31
32 _solverPriority.Add(
33 SolverKind.Gurobi);
34
35 _solverPriority.Add(
36 SolverKind.Xpress);
37
38 _solverPriority.Add(
39 SolverKind.CoinOrCbc);
40
42 true;
43
45 false;
46 }
47
48 /// <summary>
49 /// Gets the solver priority used during automatic
50 /// selection.
51 /// </summary>
52 /// <remarks>
53 /// Earlier entries have higher priority.
54 /// </remarks>
55 [XmlArray("solverPriority")]
56 [XmlArrayItem("solver")]
57 public List<SolverKind> SolverPriority =>
58 _solverPriority;
59
60 /// <summary>
61 /// Gets the capabilities that a selected adapter must
62 /// support.
63 /// </summary>
64 [XmlArray("requiredCapabilities")]
65 [XmlArrayItem("capability")]
66 public List<SolverCapability> RequiredCapabilities =>
67 _requiredCapabilities;
68
69 /// <summary>
70 /// Gets or sets whether adapters reported as available with
71 /// limitations may be selected.
72 /// </summary>
73 [XmlAttribute("allowLimitedAvailability")]
75 {
76 get;
77 set;
78 }
79
80 /// <summary>
81 /// Gets or sets whether a preferred concrete solver must be
82 /// used without falling back to another solver.
83 /// </summary>
84 [XmlAttribute("requireExactSolverKind")]
86 {
87 get;
88 set;
89 }
90
91 /// <summary>
92 /// Validates the solver-selection options.
93 /// </summary>
94 /// <exception cref="InvalidOperationException">
95 /// Thrown when the priority or capability collections are
96 /// invalid.
97 /// </exception>
98 public void EnsureValid()
99 {
100 if (_solverPriority.Count == 0)
101 {
102 throw new InvalidOperationException(
103 "At least one solver kind must be present in " +
104 "the automatic-selection priority.");
105 }
106
107 var seenSolvers =
108 new HashSet<SolverKind>();
109
110 foreach (
111 SolverKind solverKind
112 in _solverPriority)
113 {
114 if (solverKind is
115 SolverKind.Unknown or
116 SolverKind.Automatic)
117 {
118 throw new InvalidOperationException(
119 "The solver priority must contain only " +
120 "concrete solver kinds.");
121 }
122
123 if (!seenSolvers.Add(
124 solverKind))
125 {
126 throw new InvalidOperationException(
127 $"Solver kind '{solverKind}' appears more " +
128 "than once in the selection priority.");
129 }
130 }
131
132 var seenCapabilities =
133 new HashSet<SolverCapability>();
134
135 foreach (
136 SolverCapability capability
137 in _requiredCapabilities)
138 {
139 if (capability ==
140 SolverCapability.Unknown)
141 {
142 throw new InvalidOperationException(
143 "The required-capability collection cannot " +
144 "contain Unknown.");
145 }
146
147 if (!seenCapabilities.Add(
148 capability))
149 {
150 throw new InvalidOperationException(
151 $"Capability '{capability}' appears more " +
152 "than once in the required-capability " +
153 "collection.");
154 }
155 }
156 }
157}
bool RequireExactSolverKind
Gets or sets whether a preferred concrete solver must be used without falling back to another solver.
SolverSelectionOptions()
Initializes solver-selection options with the default solver priority.
void EnsureValid()
Validates the solver-selection options.
List< SolverCapability > RequiredCapabilities
Gets the capabilities that a selected adapter must support.
bool AllowLimitedAvailability
Gets or sets whether adapters reported as available with limitations may be selected.
List< SolverKind > SolverPriority
Gets the solver priority used during automatic selection.
SolverKind
Identifies a supported mathematical optimization solver.
Definition SolverKind.cs:12
SolverCapability
Identifies an optional capability implemented by a solver adapter.