ULSAlgorithms 1.1.0-g3e5595996d
High-performance exact and heuristic algorithms for uncapacitated lot sizing
Loading...
Searching...
No Matches
SolverAdapterRegistry.cs
Go to the documentation of this file.
2
3/// <summary>
4/// Stores optimization-solver adapters and provides deterministic lookup by
5/// adapter identifier and solver kind.
6/// </summary>
7public sealed class SolverAdapterRegistry
8{
9 private readonly Dictionary<string, IOptimizationSolverAdapter> _byId =
10 new(StringComparer.OrdinalIgnoreCase);
11
12 private readonly Dictionary<SolverKind, List<IOptimizationSolverAdapter>> _byKind =
13 new();
14
15 /// <summary>Gets all currently registered adapters.</summary>
16 public IReadOnlyCollection<IOptimizationSolverAdapter> Adapters =>
17 _byId.Values.ToArray();
18
19 /// <summary>Gets the number of registered adapters.</summary>
20 public int Count => _byId.Count;
21
22 /// <summary>Registers an adapter.</summary>
24 {
25 ArgumentNullException.ThrowIfNull(adapter);
26
27 if (string.IsNullOrWhiteSpace(adapter.AdapterId))
28 {
29 throw new InvalidOperationException(
30 "A solver adapter identifier is required.");
31 }
32
33 if (adapter.SolverKind is SolverKind.Unknown or SolverKind.Automatic)
34 {
35 throw new InvalidOperationException(
36 "A solver adapter must target a concrete solver.");
37 }
38
39 string adapterId = adapter.AdapterId.Trim();
40
41 if (_byId.ContainsKey(adapterId))
42 {
43 throw new InvalidOperationException(
44 $"An adapter with identifier '{adapterId}' is already registered.");
45 }
46
47 _byId.Add(adapterId, adapter);
48
49 if (!_byKind.TryGetValue(
50 adapter.SolverKind,
51 out List<IOptimizationSolverAdapter>? adapters))
52 {
53 adapters = [];
54 _byKind.Add(adapter.SolverKind, adapters);
55 }
56
57 adapters.Add(adapter);
58 }
59
60 /// <summary>Gets registered adapters targeting one solver kind.</summary>
61 public IReadOnlyList<IOptimizationSolverAdapter> FindBySolverKind(
62 SolverKind solverKind)
63 {
64 return _byKind.TryGetValue(
65 solverKind,
66 out List<IOptimizationSolverAdapter>? adapters)
67 ? adapters.ToArray()
68 : [];
69 }
70
71 /// <summary>Removes all adapters.</summary>
72 public void Clear()
73 {
74 _byId.Clear();
75 _byKind.Clear();
76 }
77}
Stores optimization-solver adapters and provides deterministic lookup by adapter identifier and solve...
IReadOnlyList< IOptimizationSolverAdapter > FindBySolverKind(SolverKind solverKind)
Gets registered adapters targeting one solver kind.
void Register(IOptimizationSolverAdapter adapter)
Registers an adapter.
IReadOnlyCollection< IOptimizationSolverAdapter > Adapters
Gets all currently registered adapters.
int Count
Gets the number of registered adapters.
Defines the common metadata and availability contract implemented by every optimization-solver adapte...
SolverKind SolverKind
Gets the concrete solver targeted by this adapter.
SolverKind
Identifies a mathematical optimization solver that can be used by solver-backed ULS algorithms.
Definition SolverKind.cs:15