LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
SolverRuntimeContext.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
5
7
8/// <summary>
9/// Represents the solver runtime infrastructure available to
10/// the application after adapter discovery and loading.
11/// </summary>
12/// <remarks>
13/// This class groups the loaded adapter registry and the
14/// normalized native-solver availability information so they
15/// can be passed together to the high-level solver service.
16/// </remarks>
17public sealed class SolverRuntimeContext
18{
19 private readonly IReadOnlyList<SolverAvailabilityInfo>
20 _availabilityInformation;
21
22 /// <summary>
23 /// Initializes a solver runtime context.
24 /// </summary>
25 /// <param name="adapterRegistry">
26 /// Registry containing the solver adapters that were loaded
27 /// successfully.
28 /// </param>
29 /// <param name="availabilityInformation">
30 /// Native-solver availability information.
31 /// </param>
32 /// <exception cref="ArgumentNullException">
33 /// Thrown when <paramref name="adapterRegistry"/> or
34 /// <paramref name="availabilityInformation"/> is
35 /// <see langword="null"/>.
36 /// </exception>
37 /// <exception cref="InvalidOperationException">
38 /// Thrown when the availability collection contains an
39 /// invalid entry.
40 /// </exception>
42 SolverAdapterRegistry adapterRegistry,
43 IEnumerable<SolverAvailabilityInfo> availabilityInformation)
44 {
45 ArgumentNullException.ThrowIfNull(
46 adapterRegistry);
47
48 ArgumentNullException.ThrowIfNull(
49 availabilityInformation);
50
51 SolverAvailabilityInfo[] availability =
52 availabilityInformation.ToArray();
53
54 if (availability.Any(
55 item =>
56 item is null))
57 {
58 throw new InvalidOperationException(
59 "The solver availability collection cannot " +
60 "contain a null entry.");
61 }
62
64 adapterRegistry;
65
66 _availabilityInformation =
67 availability;
68 }
69
70 /// <summary>
71 /// Gets the registry containing all solver adapters loaded
72 /// successfully for the current runtime.
73 /// </summary>
75 {
76 get;
77 }
78
79 /// <summary>
80 /// Gets the normalized availability information for the
81 /// detected native solvers.
82 /// </summary>
83 public IReadOnlyList<SolverAvailabilityInfo>
85 _availabilityInformation;
86
87 /// <summary>
88 /// Gets a value indicating whether at least one loaded
89 /// adapter is available.
90 /// </summary>
91 public bool HasLoadedAdapter =>
92 AdapterRegistry.Count > 0;
93
94 /// <summary>
95 /// Gets a value indicating whether at least one detected
96 /// native solver is currently reported as usable.
97 /// </summary>
98 public bool HasUsableSolver =>
99 _availabilityInformation.Any(
100 availability =>
101 availability.IsUsable);
102
103 /// <summary>
104 /// Gets a value indicating whether the runtime contains at
105 /// least one loaded adapter whose solver is reported as
106 /// usable.
107 /// </summary>
108 public bool CanSolve =>
109 AdapterRegistry.Adapters.Any(
110 adapter =>
111 _availabilityInformation.Any(
112 availability =>
113 availability.SolverKind ==
114 adapter.SolverKind &&
115 availability.IsUsable));
116
117 /// <summary>
118 /// Finds the availability information for a solver kind.
119 /// </summary>
120 /// <param name="solverKind">
121 /// Solver kind to locate.
122 /// </param>
123 /// <returns>
124 /// Matching availability information, or
125 /// <see langword="null"/> when no availability information
126 /// exists for the requested solver.
127 /// </returns>
129 SolverKind solverKind)
130 {
131 return _availabilityInformation
132 .FirstOrDefault(
133 availability =>
134 availability.SolverKind ==
135 solverKind);
136 }
137}
Describes the availability and detected installation details of a mathematical optimization solver.
Stores solver adapters that were loaded successfully and provides lookup services by adapter identifi...
bool HasLoadedAdapter
Gets a value indicating whether at least one loaded adapter is available.
SolverAdapterRegistry AdapterRegistry
Gets the registry containing all solver adapters loaded successfully for the current runtime.
IReadOnlyList< SolverAvailabilityInfo > AvailabilityInformation
Gets the normalized availability information for the detected native solvers.
SolverRuntimeContext(SolverAdapterRegistry adapterRegistry, IEnumerable< SolverAvailabilityInfo > availabilityInformation)
Initializes a solver runtime context.
bool CanSolve
Gets a value indicating whether the runtime contains at least one loaded adapter whose solver is repo...
SolverAvailabilityInfo? FindAvailability(SolverKind solverKind)
Finds the availability information for a solver kind.
bool HasUsableSolver
Gets a value indicating whether at least one detected native solver is currently reported as usable.
SolverKind
Identifies a supported mathematical optimization solver.
Definition SolverKind.cs:12