LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalSolutionMetadataMapper.cs
Go to the documentation of this file.
1using System;
2using LotSizingDataModel.Solution;
3using LotSizingDataModel.Solution.Common;
4using LotSizingDataModel.Solution.Metadata;
7
9
10/// <summary>
11/// Maps generic solver execution metadata to the generation
12/// metadata of a normalized lot-sizing solution.
13/// </summary>
15{
16 /// <summary>
17 /// Applies solver execution metadata to a lot-sizing
18 /// solution.
19 /// </summary>
20 /// <param name="solution">
21 /// Target lot-sizing solution.
22 /// </param>
23 /// <param name="solveResult">
24 /// Generic mathematical-model solve result.
25 /// </param>
26 /// <exception cref="ArgumentNullException">
27 /// Thrown when <paramref name="solution"/> or
28 /// <paramref name="solveResult"/> is
29 /// <see langword="null"/>.
30 /// </exception>
31 public static void Apply(
32 LotSizingSolution solution,
34 {
35 ArgumentNullException.ThrowIfNull(
36 solution);
37
38 ArgumentNullException.ThrowIfNull(
39 solveResult);
40
41 solveResult.EnsureValid();
42
43 SolutionGenerationMetadata metadata =
44 solution.GenerationMetadata;
45
46 metadata.MethodKind =
47 SolutionMethodKind.ExactOptimization;
48
49 metadata.MethodName =
50 "Mathematical optimization";
51
52 metadata.MethodVersion =
53 solveResult.FormulationId;
54
55 metadata.ImplementationName =
56 solveResult.SolverName;
57
58 metadata.ImplementationVersion =
59 solveResult.SolverVersion;
60
61 metadata.TerminationReason =
63 solveResult.TerminationReason);
64
65 metadata.DurationSeconds =
66 solveResult.SolveDuration.TotalSeconds;
67
68 metadata.IterationCount =
69 solveResult.IterationCount;
70
71 metadata.CreatedAtUtc =
72 DateTime.UtcNow;
73
74 metadata.Comment =
75 BuildComment(
76 solveResult);
77 }
78
79 /// <summary>
80 /// Converts a solver termination reason to the generic
81 /// solution-generation termination reason.
82 /// </summary>
83 /// <param name="terminationReason">
84 /// Solver-specific normalized termination reason.
85 /// </param>
86 /// <returns>
87 /// Solution-generation termination reason.
88 /// </returns>
89 public static TerminationReason MapTerminationReason(
90 SolverTerminationReason terminationReason)
91 {
92 return terminationReason switch
93 {
95 TerminationReason.OptimalityProven,
96
98 TerminationReason.Completed,
99
101 TerminationReason.TimeLimit,
102
104 TerminationReason.IterationLimit,
105
107 TerminationReason.ResourceLimit,
108
110 TerminationReason.ResourceLimit,
111
113 TerminationReason.EvaluationLimit,
114
116 TerminationReason.TargetReached,
117
119 TerminationReason.TargetReached,
120
122 TerminationReason.TargetReached,
123
125 TerminationReason.UserInterrupted,
126
128 TerminationReason.Completed,
129
131 TerminationReason.Completed,
132
134 TerminationReason.Completed,
135
137 TerminationReason.Error,
138
140 TerminationReason.Error,
141
143 TerminationReason.Error,
144
146 TerminationReason.Error,
147
149 TerminationReason.Error,
150
151 _ =>
152 TerminationReason.Unknown
153 };
154 }
155
156 private static string BuildComment(
158 {
159 if (!solveResult.ObjectiveValue.HasValue &&
160 !solveResult.BestBound.HasValue &&
161 !solveResult.RelativeGap.HasValue &&
162 !solveResult.AbsoluteGap.HasValue)
163 {
164 return string.Empty;
165 }
166
167 return
168 $"Objective={FormatNullable(solveResult.ObjectiveValue)}; " +
169 $"BestBound={FormatNullable(solveResult.BestBound)}; " +
170 $"RelativeGap={FormatNullable(solveResult.RelativeGap)}; " +
171 $"AbsoluteGap={FormatNullable(solveResult.AbsoluteGap)}.";
172 }
173
174 private static string FormatNullable(
175 double? value)
176 {
177 return value.HasValue
178 ? value.Value.ToString(
179 "G17",
180 System.Globalization.CultureInfo.InvariantCulture)
181 : "n/a";
182 }
183}
Stores the solver result for a solver-independent mathematical model.
void EnsureValid()
Validates the mathematical-model solve result.
SolverTerminationReason TerminationReason
Gets or sets the termination reason.
long? IterationCount
Gets or sets the number of simplex or barrier iterations, when available.
string FormulationId
Gets or sets the identifier of the formulation that generated the mathematical model.
TimeSpan SolveDuration
Gets or sets the solver wall-clock duration.
Maps generic solver execution metadata to the generation metadata of a normalized lot-sizing solution...
static TerminationReason MapTerminationReason(SolverTerminationReason terminationReason)
Converts a solver termination reason to the generic solution-generation termination reason.
static void Apply(LotSizingSolution solution, MathematicalModelSolveResult solveResult)
Applies solver execution metadata to a lot-sizing solution.
SolverTerminationReason
Describes why a solver execution terminated.
@ UserInterrupted
The execution was interrupted by the user or by a cancellation request.
@ LicenseError
The solver could not run because a valid license was unavailable.
@ RelativeGapLimit
The configured relative optimality-gap limit was reached.
@ ModelError
The mathematical model was invalid or unsupported.
@ ObjectiveLimit
The configured objective limit was reached.
@ Infeasible
The mathematical model was proven infeasible.
@ Unbounded
The mathematical model was proven unbounded.
@ InternalError
The solver stopped because of an internal error.
@ Feasible
A feasible solution was found, but optimality was not proven.
@ SolutionLimit
The configured solution limit was reached.
@ IterationLimit
The configured iteration limit was reached.
@ AbsoluteGapLimit
The configured absolute optimality-gap limit was reached.
@ SolverUnavailable
The requested solver was unavailable on the current computer.
@ NumericalDifficulty
The solver stopped because of numerical difficulties.
@ InfeasibleOrUnbounded
The solver could not distinguish between infeasibility and unboundedness.