LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
MathematicalObjectiveRecalculator.cs
Go to the documentation of this file.
1using System;
5
7
8/// <summary>
9/// Recomputes the objective from the exact normalized numerical
10/// representation used by the business solution mapping layer.
11/// </summary>
13{
14 private const double DefaultAbsoluteObjectiveTolerance = 1.0e-8;
15 private const double DefaultRelativeObjectiveTolerance = 1.0e-9;
16
17 /// <summary>
18 /// Recomputes and verifies the objective value.
19 /// </summary>
24 {
25 ArgumentNullException.ThrowIfNull(model);
26 ArgumentNullException.ThrowIfNull(solveResult);
27 ArgumentNullException.ThrowIfNull(mappingOptions);
28
29 model.EnsureValid();
30 solveResult.EnsureValid();
31 mappingOptions.EnsureValid();
32
33 var result =
35 {
36 SolverObjectiveValue =
37 solveResult.ObjectiveValue,
38 Status =
40 };
41
42 var normalizer =
44 mappingOptions.ZeroTolerance,
49
50 try
51 {
52 double objective =
54
55 foreach (
56 LinearTerm term
57 in model.Objective.Expression.Terms)
58 {
59 MathematicalVariable? variable =
60 model.FindVariableById(
61 term.VariableId);
62
63 if (variable is null)
64 {
65 throw new InvalidOperationException(
66 $"Objective term references unknown variable " +
67 $"identifier '{term.VariableId}'.");
68 }
69
71 solveResult.FindVariableValue(
72 term.VariableId);
73
74 if (rawValue is null)
75 {
76 throw new InvalidOperationException(
77 $"No solver value is available for objective " +
78 $"variable '{variable.Name}' " +
79 $"(identifier {variable.Id}).");
80 }
81
82 double normalizedValue =
83 normalizer.Normalize(
84 variable,
85 rawValue.Value);
86
87 objective +=
88 term.Coefficient *
89 normalizedValue;
90 }
91
92 if (!double.IsFinite(objective))
93 {
94 throw new InvalidOperationException(
95 "The recomputed objective value is not finite.");
96 }
97
98 result.RecomputedObjectiveValue =
99 objective;
100
101 if (!solveResult.ObjectiveValue.HasValue)
102 {
103 result.Status =
105
106 result.Diagnostics.Add(
107 "The objective was recomputed successfully, " +
108 "but no solver-reported objective value was " +
109 "available for comparison.");
110
111 return result;
112 }
113
114 double reported =
115 solveResult.ObjectiveValue.Value;
116
117 double difference =
118 Math.Abs(
119 reported - objective);
120
121 double scale =
122 Math.Max(
123 1.0,
124 Math.Max(
125 Math.Abs(reported),
126 Math.Abs(objective)));
127
128 double tolerance =
129 Math.Max(
130 DefaultAbsoluteObjectiveTolerance,
131 DefaultRelativeObjectiveTolerance * scale);
132
133 result.AbsoluteDifference =
134 difference;
135
136 result.ComparisonTolerance =
137 tolerance;
138
139 result.Status =
140 difference <= tolerance
142 : ObjectiveVerificationStatus.Inconsistent;
143
144 result.Diagnostics.Add(
145 result.Status ==
147 ? $"Solver objective and recomputed objective " +
148 $"are consistent. Absolute difference=" +
149 $"{difference:G17}; tolerance={tolerance:G17}."
150 : $"Solver objective and recomputed objective " +
151 $"are inconsistent. Absolute difference=" +
152 $"{difference:G17}; tolerance={tolerance:G17}.");
153
154 return result;
155 }
156 catch (Exception exception)
157 {
158 result.Status =
160
161 result.Diagnostics.Add(
162 "Objective-value post-processing failed: " +
163 exception.Message);
164
165 return result;
166 }
167 }
168}
Recomputes the objective from the exact normalized numerical representation used by the business solu...
static MathematicalObjectiveRecalculationResult Recalculate(MathematicalModel model, MathematicalModelSolveResult solveResult, MathematicalSolutionMappingOptions mappingOptions)
Recomputes and verifies the objective value.
Stores the solver result for a solver-independent mathematical model.
void EnsureValid()
Validates the mathematical-model solve result.
MathematicalVariableValue? FindVariableValue(int variableId)
Finds a variable value by mathematical-variable identifier.
Stores the value returned by a solver for one mathematical decision variable.
double Value
Gets or sets the value returned by the solver.
Defines the options used when mapping a mathematical solver result back to a normalized lot-sizing so...
double ZeroTolerance
Gets or sets the absolute tolerance below which a solver value is considered equal to zero.
Normalizes raw mathematical-variable values before they are exposed through domain decision objects o...
const double DefaultNearIntegerTolerance
Gets the default tolerance used to clean continuous values that are numerically indistinguishable fro...
const double DefaultIntegralityTolerance
Gets the default tolerance used for integer-domain values.
List< LinearTerm > Terms
Gets the linear terms of the expression.
double Constant
Gets or sets the constant value of the expression.
Represents one coefficient-variable term in a linear expression.
Definition LinearTerm.cs:13
int VariableId
Gets or sets the identifier of the referenced mathematical variable.
Definition LinearTerm.cs:49
Represents a solver-independent mathematical optimization model.
MathematicalObjective Objective
Gets or sets the mathematical objective.
MathematicalVariable? FindVariableById(int variableId)
Finds a mathematical variable by identifier.
void EnsureValid()
Validates the complete mathematical model.
LinearExpression Expression
Gets or sets the linear objective expression.
Represents one decision variable in a mathematical optimization model.
ObjectiveVerificationStatus
Indicates whether a solver-reported objective value is consistent with an objective value independent...
@ Consistent
The solver-reported and recomputed objective values agree within the configured tolerance.