LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
StandardLotSizingObjectiveBuilder.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading;
5using System.Threading.Tasks;
6using LotSizingDataModel.Instance;
9
11
12/// <summary>
13/// Builds the complete linear objective of the standard
14/// lot-sizing formulation from registered objective-term
15/// families.
16/// </summary>
19{
20 private readonly IReadOnlyList<IStandardLotSizingObjectiveTermBuilder>
21 _termBuilders;
22
23 /// <summary>
24 /// Initializes the standard objective builder.
25 /// </summary>
26 /// <param name="termBuilders">
27 /// Ordered objective-term builders.
28 /// </param>
29 /// <exception cref="ArgumentNullException">
30 /// Thrown when <paramref name="termBuilders"/> is
31 /// <see langword="null"/>.
32 /// </exception>
33 /// <exception cref="InvalidOperationException">
34 /// Thrown when the collection contains a null builder or
35 /// duplicate term-family identifiers.
36 /// </exception>
38 IEnumerable<IStandardLotSizingObjectiveTermBuilder> termBuilders)
39 {
40 ArgumentNullException.ThrowIfNull(
41 termBuilders);
42
44 termBuilders.ToArray();
45
46 if (builders.Any(
47 builder =>
48 builder is null))
49 {
50 throw new InvalidOperationException(
51 "The standard objective-term builder collection " +
52 "cannot contain a null entry.");
53 }
54
55 string[] duplicateIds =
56 builders
57 .GroupBy(
58 builder =>
59 builder.TermFamilyId,
60 StringComparer.OrdinalIgnoreCase)
61 .Where(
62 group =>
63 group.Count() > 1)
64 .Select(
65 group =>
66 group.Key)
67 .ToArray();
68
69 if (duplicateIds.Length > 0)
70 {
71 throw new InvalidOperationException(
72 "Duplicate standard objective-term identifiers " +
73 "were registered: " +
74 string.Join(
75 ", ",
76 duplicateIds));
77 }
78
79 _termBuilders =
80 builders;
81 }
82
83 /// <summary>
84 /// Gets the ordered objective-term builders.
85 /// </summary>
86 public IReadOnlyList<IStandardLotSizingObjectiveTermBuilder>
88 _termBuilders;
89
90 /// <summary>
91 /// Builds and assigns the complete minimization objective.
92 /// </summary>
93 /// <param name="instance">
94 /// Source lot-sizing instance.
95 /// </param>
96 /// <param name="context">
97 /// Mathematical-model build context.
98 /// </param>
99 /// <param name="options">
100 /// Standard formulation options.
101 /// </param>
102 /// <param name="cancellationToken">
103 /// Token used to cancel objective construction.
104 /// </param>
105 /// <returns>
106 /// Task representing objective construction.
107 /// </returns>
108 public async ValueTask BuildAsync(
109 LotSizingInstance instance,
112 CancellationToken cancellationToken = default)
113 {
114 ArgumentNullException.ThrowIfNull(
115 instance);
116
117 ArgumentNullException.ThrowIfNull(
118 context);
119
120 ArgumentNullException.ThrowIfNull(
121 options);
122
123 options.EnsureValid();
124
125 var expressionBuilder =
127
128 foreach (
130 in _termBuilders)
131 {
132 cancellationToken.ThrowIfCancellationRequested();
133
134 if (!termBuilder.IsEnabled(
135 instance,
136 options))
137 {
138 continue;
139 }
140
141 await termBuilder.BuildAsync(
142 instance,
143 context,
144 expressionBuilder,
145 options,
146 cancellationToken);
147 }
148
149 var objective =
151 "totalCost",
152 ObjectiveSense.Minimize,
153 expressionBuilder.Build())
154 {
155 Description =
156 "Total cost of the standard lot-sizing " +
157 "formulation."
158 };
159
160 context.SetObjective(
161 objective);
162 }
163}
Provides a fluent builder for linear expressions.
Provides the shared state required while constructing a solver-independent mathematical model.
Configures the standard solver-independent mixed-integer lot-sizing formulation.
StandardLotSizingObjectiveBuilder(IEnumerable< IStandardLotSizingObjectiveTermBuilder > termBuilders)
Initializes the standard objective builder.
IReadOnlyList< IStandardLotSizingObjectiveTermBuilder > TermBuilders
Gets the ordered objective-term builders.
async ValueTask BuildAsync(LotSizingInstance instance, MathematicalModelBuildContext context, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken=default)
Builds and assigns the complete minimization objective.
Represents the objective function of a mathematical optimization model.
Defines the objective-construction component used by the standard solver-independent lot-sizing formu...
Defines one cost-term family of the standard lot-sizing objective function.
ValueTask BuildAsync(LotSizingInstance instance, MathematicalModelBuildContext context, LinearExpressionBuilder expressionBuilder, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken=default)
Adds all linear objective terms belonging to this family.
bool IsEnabled(LotSizingInstance instance, StandardLotSizingFormulationOptions options)
Determines whether this objective-term family is enabled.
ObjectiveSense
Identifies the optimization direction of a mathematical objective.