LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalSolutionMapper.cs
Go to the documentation of this file.
1using System;
2using LotSizingDataModel.Instance;
3using LotSizingDataModel.Solution;
6
8
9/// <summary>
10/// Maps a complete mathematical solver result to a normalized
11/// lot-sizing solution by delegating each decision family to a
12/// registered mathematical decision mapper.
13/// </summary>
14public sealed class MathematicalSolutionMapper :
16{
18 _decisionMapperRegistry;
19
20 /// <summary>
21 /// Initializes a mathematical-solution mapper.
22 /// </summary>
23 /// <param name="decisionMapperRegistry">
24 /// Registry containing the decision-family mappers.
25 /// </param>
26 /// <exception cref="ArgumentNullException">
27 /// Thrown when <paramref name="decisionMapperRegistry"/> is
28 /// <see langword="null"/>.
29 /// </exception>
31 MathematicalDecisionMapperRegistry decisionMapperRegistry)
32 {
33 ArgumentNullException.ThrowIfNull(
34 decisionMapperRegistry);
35
36 _decisionMapperRegistry =
37 decisionMapperRegistry;
38 }
39
40 /// <summary>
41 /// Maps a mathematical-model solve result to a normalized
42 /// lot-sizing solution.
43 /// </summary>
44 /// <param name="instance">
45 /// Source lot-sizing instance.
46 /// </param>
47 /// <param name="model">
48 /// Solver-independent mathematical model that was solved.
49 /// </param>
50 /// <param name="solveResult">
51 /// Generic mathematical-model solve result.
52 /// </param>
53 /// <returns>
54 /// Normalized lot-sizing solution.
55 /// </returns>
56 /// <exception cref="ArgumentNullException">
57 /// Thrown when one of the required arguments is
58 /// <see langword="null"/>.
59 /// </exception>
60 /// <exception cref="InvalidOperationException">
61 /// Thrown when the solve result is not feasible, the source
62 /// instance has no valid planning horizon, or the result
63 /// cannot be mapped consistently.
64 /// </exception>
65 public LotSizingSolution Map(
66 LotSizingInstance instance,
69 {
70 return Map(
71 instance,
72 model,
73 solveResult,
75 }
76
77 /// <summary>
78 /// Maps a mathematical-model solve result to a normalized
79 /// lot-sizing solution using explicit mapping options.
80 /// </summary>
81 /// <param name="instance">
82 /// Source lot-sizing instance.
83 /// </param>
84 /// <param name="model">
85 /// Solver-independent mathematical model that was solved.
86 /// </param>
87 /// <param name="solveResult">
88 /// Generic mathematical-model solve result.
89 /// </param>
90 /// <param name="options">
91 /// Mathematical-solution mapping options.
92 /// </param>
93 /// <returns>
94 /// Normalized lot-sizing solution.
95 /// </returns>
96 /// <exception cref="ArgumentNullException">
97 /// Thrown when one of the required arguments is
98 /// <see langword="null"/>.
99 /// </exception>
100 /// <exception cref="InvalidOperationException">
101 /// Thrown when the solve result is not feasible, the source
102 /// instance has no valid planning horizon, or the result
103 /// cannot be mapped consistently.
104 /// </exception>
105 public LotSizingSolution Map(
106 LotSizingInstance instance,
107 MathematicalModel model,
110 {
111 ArgumentNullException.ThrowIfNull(
112 instance);
113
114 ArgumentNullException.ThrowIfNull(
115 model);
116
117 ArgumentNullException.ThrowIfNull(
118 solveResult);
119
120 ArgumentNullException.ThrowIfNull(
121 options);
122
123 MathematicalSolutionMappingOptions normalizedOptions =
124 options.Clone();
125
126 normalizedOptions.EnsureValid();
127
128 if (!solveResult.HasFeasibleSolution)
129 {
130 throw new InvalidOperationException(
131 "A mathematical solver result without a feasible " +
132 "solution cannot be mapped to LotSizingSolution.");
133 }
134
135 if (instance.PlanningHorizon <= 0)
136 {
137 throw new InvalidOperationException(
138 "The source lot-sizing instance must have a " +
139 "strictly positive planning horizon.");
140 }
141
142 var context =
144 instance,
145 model,
146 solveResult,
147 normalizedOptions);
148
149 string solutionName =
150 !string.IsNullOrWhiteSpace(
151 solveResult.RunName)
152 ? solveResult.RunName.Trim()
153 : !string.IsNullOrWhiteSpace(
154 instance.Name)
155 ? instance.Name.Trim() +
156 " - solver solution"
157 : "Solver solution";
158
159 var solution =
160 new LotSizingSolution(
161 solutionName,
162 instance.InstanceId,
163 instance.PlanningHorizon);
164
165 foreach (
166 IMathematicalDecisionMapper decisionMapper
167 in _decisionMapperRegistry.GetAll())
168 {
169 decisionMapper.Map(
170 context,
171 solution);
172 }
173
175 solution,
176 solveResult);
177
178 return solution;
179 }
180}
Stores the solver result for a solver-independent mathematical model.
bool HasFeasibleSolution
Gets or sets a value indicating whether the solver produced at least one feasible solution.
Stores and resolves mathematical decision mappers by business-domain category.
LotSizingSolution Map(LotSizingInstance instance, MathematicalModel model, MathematicalModelSolveResult solveResult)
Maps a mathematical-model solve result to a normalized lot-sizing solution.
MathematicalSolutionMapper(MathematicalDecisionMapperRegistry decisionMapperRegistry)
Initializes a mathematical-solution mapper.
LotSizingSolution Map(LotSizingInstance instance, MathematicalModel model, MathematicalModelSolveResult solveResult, MathematicalSolutionMappingOptions options)
Maps a mathematical-model solve result to a normalized lot-sizing solution using explicit mapping opt...
Provides the shared state and lookup services required while mapping a mathematical solver result bac...
Defines the options used when mapping a mathematical solver result back to a normalized lot-sizing so...
MathematicalSolutionMappingOptions Clone()
Creates an independent copy of the mapping options.
Maps generic solver execution metadata to the generation metadata of a normalized lot-sizing solution...
static void Apply(LotSizingSolution solution, MathematicalModelSolveResult solveResult)
Applies solver execution metadata to a lot-sizing solution.
Represents a solver-independent mathematical optimization model.
Defines a mapper responsible for one family of lot-sizing decisions identified by a mathematical doma...
void Map(MathematicalSolutionMappingContext context, LotSizingSolution solution)
Maps the decision values handled by this mapper into a lot-sizing solution.
Defines a component that converts a generic mathematical solver result into a normalized lot-sizing s...