LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalDecisionMapperBase.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using LotSizingDataModel.Solution;
5
7
8/// <summary>
9/// Provides a reusable base implementation for mathematical
10/// decision mappers handling one domain-key category.
11/// </summary>
12public abstract class MathematicalDecisionMapperBase :
14{
15 /// <summary>
16 /// Gets the mathematical domain-key category handled by this
17 /// mapper.
18 /// </summary>
19 public abstract string Category
20 {
21 get;
22 }
23
24 /// <summary>
25 /// Gets the absolute tolerance below which a solver value is
26 /// considered equal to zero.
27 /// </summary>
28 protected virtual double ZeroTolerance =>
29 1.0e-9;
30
31 /// <summary>
32 /// Maps the decision values handled by this mapper into a
33 /// lot-sizing solution.
34 /// </summary>
35 /// <param name="context">
36 /// Mathematical-solution mapping context.
37 /// </param>
38 /// <param name="solution">
39 /// Target lot-sizing solution.
40 /// </param>
41 /// <exception cref="ArgumentNullException">
42 /// Thrown when <paramref name="context"/> or
43 /// <paramref name="solution"/> is
44 /// <see langword="null"/>.
45 /// </exception>
46 /// <exception cref="InvalidOperationException">
47 /// Thrown when the mapper category or zero tolerance is
48 /// invalid.
49 /// </exception>
50 public void Map(
52 LotSizingSolution solution)
53 {
54 ArgumentNullException.ThrowIfNull(
55 context);
56
57 ArgumentNullException.ThrowIfNull(
58 solution);
59
60 EnsureConfigurationIsValid();
61
62 double effectiveZeroTolerance =
63 context.Options.ZeroTolerance;
64
65 IReadOnlyList<MathematicalVariableValue> values =
66 context.GetValuesByCategory(
69 effectiveZeroTolerance);
70
72 context,
73 solution,
74 values);
75
76 foreach (
77 MathematicalVariableValue variableValue
78 in values)
79 {
80 MathematicalDomainKey domainKey =
82 variableValue.DomainKey);
83
85 context,
86 solution,
87 domainKey,
88 variableValue);
89 }
90
92 context,
93 solution,
94 values);
95 }
96
97 /// <summary>
98 /// Maps one mathematical-variable value selected by the
99 /// active mapping options.
100 /// </summary>
101 /// <param name="context">
102 /// Mathematical-solution mapping context.
103 /// </param>
104 /// <param name="solution">
105 /// Target lot-sizing solution.
106 /// </param>
107 /// <param name="domainKey">
108 /// Parsed business-domain key.
109 /// </param>
110 /// <param name="variableValue">
111 /// Mathematical-variable value returned by the solver.
112 /// </param>
113 protected abstract void MapValue(
115 LotSizingSolution solution,
116 MathematicalDomainKey domainKey,
117 MathematicalVariableValue variableValue);
118
119 /// <summary>
120 /// Executes optional initialization before values are mapped.
121 /// </summary>
122 /// <param name="context">
123 /// Mathematical-solution mapping context.
124 /// </param>
125 /// <param name="solution">
126 /// Target lot-sizing solution.
127 /// </param>
128 /// <param name="values">
129 /// Values handled by this mapper.
130 /// </param>
131 protected virtual void BeforeMap(
133 LotSizingSolution solution,
134 IReadOnlyList<MathematicalVariableValue> values)
135 {
136 }
137
138 /// <summary>
139 /// Executes optional finalization after all values have been
140 /// mapped.
141 /// </summary>
142 /// <param name="context">
143 /// Mathematical-solution mapping context.
144 /// </param>
145 /// <param name="solution">
146 /// Target lot-sizing solution.
147 /// </param>
148 /// <param name="values">
149 /// Values handled by this mapper.
150 /// </param>
151 protected virtual void AfterMap(
153 LotSizingSolution solution,
154 IReadOnlyList<MathematicalVariableValue> values)
155 {
156 }
157
158 private void EnsureConfigurationIsValid()
159 {
160 if (string.IsNullOrWhiteSpace(
161 Category))
162 {
163 throw new InvalidOperationException(
164 "A mathematical decision-mapper category is " +
165 "required.");
166 }
167
168 if (double.IsNaN(
169 ZeroTolerance) ||
170 double.IsInfinity(
171 ZeroTolerance) ||
172 ZeroTolerance < 0.0)
173 {
174 throw new InvalidOperationException(
175 "The mathematical decision-mapper zero tolerance " +
176 "must be finite and non-negative.");
177 }
178 }
179}
Stores the value returned by a solver for one mathematical decision variable.
string DomainKey
Gets or sets the business-domain key associated with the mathematical variable.
Provides a reusable base implementation for mathematical decision mappers handling one domain-key cat...
string Category
Gets the mathematical domain-key category handled by this mapper.
virtual void AfterMap(MathematicalSolutionMappingContext context, LotSizingSolution solution, IReadOnlyList< MathematicalVariableValue > values)
Executes optional finalization after all values have been mapped.
virtual double ZeroTolerance
Gets the absolute tolerance below which a solver value is considered equal to zero.
void MapValue(MathematicalSolutionMappingContext context, LotSizingSolution solution, MathematicalDomainKey domainKey, MathematicalVariableValue variableValue)
Maps one mathematical-variable value selected by the active mapping options.
void Map(MathematicalSolutionMappingContext context, LotSizingSolution solution)
Maps the decision values handled by this mapper into a lot-sizing solution.
virtual void BeforeMap(MathematicalSolutionMappingContext context, LotSizingSolution solution, IReadOnlyList< MathematicalVariableValue > values)
Executes optional initialization before values are mapped.
Represents a parsed mathematical-model business-domain key.
static MathematicalDomainKey Parse(string domainKey)
Parses a canonical mathematical domain key.
Provides the shared state and lookup services required while mapping a mathematical solver result bac...
MathematicalSolutionMappingOptions Options
Gets the normalized mapping options used by this context.
IReadOnlyList< MathematicalVariableValue > GetValuesByCategory(string category, bool includeZeroValues, double zeroTolerance=1.0e-9)
Returns solver values whose domain-key category matches the supplied category.
double ZeroTolerance
Gets or sets the absolute tolerance below which a solver value is considered equal to zero.
bool IncludeZeroValues
Gets or sets a value indicating whether solver values considered equal to zero must also be mapped.
Defines a mapper responsible for one family of lot-sizing decisions identified by a mathematical doma...