LotSizingDataModel.Core 2.1.0
Core domain model, shared abstractions and XML-serializable entities.
Loading...
Searching...
No Matches
WorkCenter.DecisionModel.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Xml.Serialization;
9
11
12/// <summary>
13/// Adds the optional decision-model parameters associated
14/// with a work center.
15/// </summary>
16public sealed partial class WorkCenter :
17 IPlanningHorizonAware
18{
19 private CapacityConstraint? _capacityConstraint;
20 private AdditionalCapacity? _additionalCapacity;
21 private FixedUsageCost? _fixedUsageCost;
22 private AdditionalCapacityCost? _additionalCapacityCost;
23
24 /// <summary>
25 /// Gets or sets the maximum regular capacity available
26 /// during each planning period.
27 ///
28 /// A null value means that the work center is not subject
29 /// to an explicit capacity constraint.
30 /// </summary>
31 [XmlIgnore]
33 {
34 get => _capacityConstraint;
35 set => SetDecisionParameter(
36 ref _capacityConstraint,
37 value,
38 nameof(CapacityConstraint));
39 }
40
41 /// <summary>XML capacity series with INF for unbounded periods.</summary>
42 [XmlElement("capacityConstraint")]
48
49 /// <summary>Returns the maximum production capacity, or null when the period is unbounded.</summary>
50 public double? GetProductionCapacity(int period) => CapacityConstraint?.GetFiniteMaximumCapacity(period);
51
52 /// <summary>
53 /// Gets or sets the maximum additional capacity that may
54 /// be allocated during each planning period.
55 ///
56 /// This parameter should only be defined when a regular
57 /// capacity constraint is also defined.
58 /// </summary>
59 [XmlElement("additionalCapacity")]
61 {
62 get => _additionalCapacity;
63 set => SetDecisionParameter(
64 ref _additionalCapacity,
65 value,
66 nameof(AdditionalCapacity));
67 }
68
69 /// <summary>
70 /// Gets or sets the fixed cost incurred when the work center
71 /// is used during a planning period.
72 /// </summary>
73 [XmlElement("fixedUsageCost")]
75 {
76 get => _fixedUsageCost;
77 set => SetDecisionParameter(
78 ref _fixedUsageCost,
79 value,
80 nameof(FixedUsageCost));
81 }
82
83 /// <summary>
84 /// Gets or sets the cost of using one unit of additional
85 /// capacity during each planning period.
86 ///
87 /// This parameter should only be defined when additional
88 /// capacity is authorized.
89 /// </summary>
90 [XmlElement("additionalCapacityCost")]
92 {
93 get => _additionalCapacityCost;
94 set => SetDecisionParameter(
95 ref _additionalCapacityCost,
96 value,
98 }
99
100 /// <summary>
101 /// Gets the planning horizon represented by the first
102 /// active period-dependent parameter.
103 ///
104 /// Returns zero when no decision parameter is active.
105 /// </summary>
106 [XmlIgnore]
108 {
109 get
110 {
111 // Return the first active parameter's horizon.
112 foreach (IPlanningHorizonAware parameter
113 in GetDecisionParameters())
114 {
115 return parameter.PlanningHorizon;
116 }
117
118 // No active parameter → horizon is zero.
119 return 0;
120 }
121 }
122
123 /// <summary>
124 /// Gets a value indicating whether at least one
125 /// decision-model parameter is active.
126 /// </summary>
127 [XmlIgnore]
129 CapacityConstraint is not null ||
130 AdditionalCapacity is not null ||
131 FixedUsageCost is not null ||
132 AdditionalCapacityCost is not null ||
133 SchedulingProfile is not null;
134
135 /// <summary>
136 /// Gets a value indicating whether every active parameter
137 /// uses the same planning horizon.
138 /// </summary>
139 [XmlIgnore]
141 {
142 get
143 {
144 int? expectedHorizon = null;
145
146 // Compare each active parameter's horizon against the first.
147 foreach (IPlanningHorizonAware parameter
148 in GetDecisionParameters())
149 {
150 if (expectedHorizon is null)
151 {
152 expectedHorizon =
153 parameter.PlanningHorizon;
154
155 continue;
156 }
157
158 if (parameter.PlanningHorizon !=
159 expectedHorizon.Value)
160 {
161 return false;
162 }
163 }
164
165 return true;
166 }
167 }
168
169 /// <summary>
170 /// Gets a value indicating whether the dependencies between
171 /// capacity-related parameters are respected.
172 /// </summary>
173 [XmlIgnore]
175 (AdditionalCapacity is null ||
176 CapacityConstraint is not null) &&
177 (AdditionalCapacityCost is null ||
178 AdditionalCapacity is not null);
179
180 /// <summary>
181 /// Resizes every active period-dependent parameter.
182 ///
183 /// Existing values are preserved.
184 /// </summary>
185 /// <param name="periodCount">
186 /// New number of planning periods.
187 /// </param>
188 public void ResizeTimeSeries(int periodCount)
189 {
190 if (periodCount < 0)
191 {
192 throw new ArgumentOutOfRangeException(
193 nameof(periodCount),
194 periodCount,
195 "The planning horizon cannot be negative.");
196 }
197
198 int previousCapacityHorizon = CapacityConstraint?.PlanningHorizon ?? 0;
199
200 // Resize every active decision parameter.
201 foreach (IPlanningHorizonAware parameter
202 in GetDecisionParameters())
203 {
204 parameter.ResizeTimeSeries(periodCount);
205 }
206
207 if (CapacityConstraint is not null)
208 {
209 for (int period = previousCapacityHorizon + 1; period <= periodCount; period++)
210 CapacityConstraint.Values.SetUnboundedCapacityPeriod(period);
211 }
212
213 // Notify dependent computed properties.
214 OnPropertyChanged(nameof(PlanningHorizon));
215 OnPropertyChanged(
217 }
218
219 /// <summary>
220 /// Removes all decision-model parameters from
221 /// this work center.
222 /// </summary>
224 {
225 CapacityConstraint = null;
226 AdditionalCapacity = null;
227 FixedUsageCost = null;
229 SchedulingProfile = null;
230 }
231
232 private void SetDecisionParameter<T>(
233 ref T? field,
234 T? value,
235 string propertyName)
237 {
238 if (ReferenceEquals(field, value))
239 {
240 return;
241 }
242
243 // Unsubscribe from old parameter.
244 if (field is not null)
245 {
246 field.PropertyChanged -=
247 OnDecisionParameterPropertyChanged;
248 }
249
250 field = value;
251
252 // Subscribe to new parameter.
253 if (field is not null)
254 {
255 field.PropertyChanged +=
256 OnDecisionParameterPropertyChanged;
257 }
258
259 // Notify the specific property plus all dependent ones.
260 OnPropertyChanged(propertyName);
261 OnPropertyChanged(nameof(PlanningHorizon));
262 OnPropertyChanged(nameof(HasDecisionParameters));
263 OnPropertyChanged(
265 OnPropertyChanged(
267 }
268
269 private IEnumerable<IPlanningHorizonAware>
270 GetDecisionParameters()
271 {
272 if (CapacityConstraint is not null)
273 {
274 yield return CapacityConstraint;
275 }
276
277 if (AdditionalCapacity is not null)
278 {
279 yield return AdditionalCapacity;
280 }
281
282 if (FixedUsageCost is not null)
283 {
284 yield return FixedUsageCost;
285 }
286
287 if (AdditionalCapacityCost is not null)
288 {
289 yield return AdditionalCapacityCost;
290 }
291
292 if (SchedulingProfile is not null)
293 {
294 yield return SchedulingProfile;
295 }
296 }
297
298 private void OnDecisionParameterPropertyChanged(
299 object? sender,
300 PropertyChangedEventArgs e)
301 {
302 // Forward notification for the specific parameter that changed.
303 if (ReferenceEquals(sender, CapacityConstraint))
304 {
305 OnPropertyChanged(nameof(CapacityConstraint));
306 }
307 else if (ReferenceEquals(sender, AdditionalCapacity))
308 {
309 OnPropertyChanged(nameof(AdditionalCapacity));
310 }
311 else if (ReferenceEquals(sender, FixedUsageCost))
312 {
313 OnPropertyChanged(nameof(FixedUsageCost));
314 }
315 else if (ReferenceEquals(
316 sender,
318 {
319 OnPropertyChanged(nameof(AdditionalCapacityCost));
320 }
321 else if (ReferenceEquals(sender, SchedulingProfile))
322 {
323 OnPropertyChanged(nameof(SchedulingProfile));
324 }
325
326 // Update all computed properties that may be affected.
327 OnPropertyChanged(nameof(PlanningHorizon));
328 OnPropertyChanged(
330 OnPropertyChanged(
332 }
333}
Base class for model objects that notify listeners when one of their properties changes.
XML capacity series for work centers and warehouses, with INF for unbounded periods.
static ? ResourceCapacitySeries FromConstraint(CapacityConstraint? constraint)
Represents a production work center belonging to a plant.
Definition WorkCenter.cs:15
bool HasConsistentPlanningHorizon
Gets a value indicating whether every active parameter uses the same planning horizon.
CapacityConstraint? CapacityConstraint
Gets or sets the maximum regular capacity available during each planning period.
bool HasValidCapacityConfiguration
Gets a value indicating whether the dependencies between capacity-related parameters are respected.
FixedUsageCost? FixedUsageCost
Gets or sets the fixed cost incurred when the work center is used during a planning period.
AdditionalCapacity? AdditionalCapacity
Gets or sets the maximum additional capacity that may be allocated during each planning period.
void ResizeTimeSeries(int periodCount)
Resizes every active period-dependent parameter.
ResourceCapacitySeries? SerializedCapacity
XML capacity series with INF for unbounded periods.
AdditionalCapacityCost? AdditionalCapacityCost
Gets or sets the cost of using one unit of additional capacity during each planning period.
double? GetProductionCapacity(int period)
Returns the maximum production capacity, or null when the period is unbounded.
int PlanningHorizon
Gets the planning horizon represented by the first active period-dependent parameter.
void ClearDecisionParameters()
Removes all decision-model parameters from this work center.
bool HasDecisionParameters
Gets a value indicating whether at least one decision-model parameter is active.
Defines a model object containing data indexed by planning period.
int PlanningHorizon
Gets the number of planning periods currently represented by the object's time-dependent data.
void ResizeTimeSeries(int periodCount)
Resizes every time-dependent series contained in the object.