LotSizingDataModel.Checker 2.1.0
Reusable validation engine for structural, mathematical and objective consistency checks.
Loading...
Searching...
No Matches
MathematicalSolutionValueProjector.cs
Go to the documentation of this file.
2using LotSizingDataModel.Core.PhysicalModel;
3using LotSizingDataModel.Solution;
4using LotSizingDataModel.Solution.Decisions;
5using LotSizingDataModel.Solver.Mapping;
6using LotSizingDataModel.Solver.Modeling;
7
9
10/// <summary>
11/// Projects a normalized <see cref="LotSizingSolution"/> onto the
12/// variables of a <see cref="MathematicalModel"/> by using the
13/// canonical mathematical domain-key convention.
14/// </summary>
15/// <remarks>
16/// <para>
17/// This is intentionally the inverse bridge of the Solver mapping
18/// layer. The mathematical formulation remains the single source
19/// of truth for variable meaning.
20/// </para>
21/// <para>
22/// The projector does not test feasibility. It only resolves the
23/// candidate value associated with each mathematical variable.
24/// Constraint and objective checking are implemented by later
25/// checker stages.
26/// </para>
27/// </remarks>
30{
31 /// <inheritdoc/>
33 MathematicalModel model,
34 LotSizingSolution solution)
35 {
36 ArgumentNullException.ThrowIfNull(model);
37 ArgumentNullException.ThrowIfNull(solution);
38
39 model.EnsureValid();
40 var fleetByKey = solution.PhysicalFleetDecisions.ToLookup(decision => decision.DomainKey, StringComparer.Ordinal);
41
42 var result =
44
45 foreach (
46 MathematicalVariable variable
47 in model.Variables)
48 {
49 try
50 {
51 if (string.IsNullOrWhiteSpace(
52 variable.DomainKey))
53 {
54 throw new InvalidOperationException(
55 "The mathematical variable has no domain key.");
56 }
57
58 MathematicalDomainKey domainKey =
59 MathematicalDomainKey.Parse(
60 variable.DomainKey);
61
62 double value = domainKey.Category switch
63 {
64 MathematicalDecisionCategory.AuxiliaryProductionFamilySetup =>
65 ResolveFamilySetup(model, solution, variable.Id, domainKey),
66 MathematicalDecisionCategory.PhysicalFleet =>
67 fleetByKey[domainKey.ToString()].SingleOrDefault()?.VehicleCount ?? 0,
68 _ => ResolveValue(solution, domainKey)
69 };
70
71 result.AddValue(
72 variable.Id,
73 value);
74 }
75 catch (Exception exception)
76 {
77 result.AddIssue(
79 {
80 VariableId =
81 variable.Id,
82 VariableName =
83 variable.Name,
84 DomainKey =
85 variable.DomainKey,
86 Message =
87 exception.Message
88 });
89 }
90 }
91
92 return result;
93 }
94
95 private static double ResolveFamilySetup(
96 MathematicalModel model, LotSizingSolution solution, int variableId, MathematicalDomainKey key)
97 {
98 var familyId = key.GetRequiredInt32(MathematicalDomainKeySegment.SetupFamily);
99 var period = GetPeriod(key);
100 var prefix = $"productionFamilySetupLink_f{familyId}_r";
101 var suffix = $"_t{period}";
102 var memberIds = model.Constraints.Where(row =>
103 row.IsEnabled && row.Name.StartsWith(prefix, StringComparison.Ordinal) &&
104 row.Name.EndsWith(suffix, StringComparison.Ordinal) &&
105 row.LeftHandSide.Terms.Any(term => term.VariableId == variableId && term.Coefficient < 0))
106 .SelectMany(row => row.LeftHandSide.Terms)
107 .Where(term => term.Coefficient > 0)
108 .Select(term => term.VariableId).ToHashSet();
109 return model.Variables.Where(variable => memberIds.Contains(variable.Id))
110 .Any(variable => ResolveValue(solution, MathematicalDomainKey.Parse(variable.DomainKey!)) > 0) ? 1.0 : 0.0;
111 }
112
113 private static double ResolveValue(
114 LotSizingSolution solution,
115 MathematicalDomainKey key)
116 {
117 return key.Category switch
118 {
119 MathematicalDecisionCategory.Production =>
120 ResolveProduction(
121 solution,
122 key,
123 production =>
124 production.GetQuantity(
125 GetPeriod(key))),
126
127 MathematicalDecisionCategory.Setup =>
128 ResolveProduction(
129 solution,
130 key,
131 production =>
132 production.IsSetupActivated(
133 GetPeriod(key))
134 ? 1.0
135 : 0.0),
136
137 MathematicalDecisionCategory.AuxiliaryLotSizeMultiplier =>
138 ResolveProduction(
139 solution,
140 key,
141 production =>
142 production.GetLotMultipleCount(
143 GetPeriod(key))),
144
145 MathematicalDecisionCategory
146 .AuxiliarySmallBucketProductionActivation =>
147 ResolveProduction(
148 solution,
149 key,
150 production =>
151 Math.Abs(
152 production.GetQuantity(
153 GetPeriod(key))) > 1e-9
154 ? 1.0
155 : 0.0),
156
157 MathematicalDecisionCategory
158 .AuxiliarySchedulingSetupStart =>
159 ResolveProduction(
160 solution,
161 key,
162 production =>
163 IsSchedulingSetupStart(
164 production,
165 key)
166 ? 1.0
167 : 0.0),
168
169 MathematicalDecisionCategory.MicroPeriodProduction =>
170 ResolveMicroProduction(solution, key),
171
172 MathematicalDecisionCategory.MicroPeriodSetupState =>
173 ResolveMicroSetupState(solution, key),
174
175 MathematicalDecisionCategory.AuxiliaryMicroPeriodChangeover =>
176 ResolveMicroChangeover(solution, key),
177
178 MathematicalDecisionCategory.AuxiliaryMicroPeriodSetupStart =>
179 ResolveMicroSetupStart(solution, key),
180
181 MathematicalDecisionCategory.AuxiliaryMacroProductionActivation =>
182 ResolveProduction(solution, key, production => Math.Abs(production.GetQuantity(GetPeriod(key))) > 1e-9 ? 1.0 : 0.0),
183
184 MathematicalDecisionCategory.AuxiliaryProductionStartUp =>
185 ResolveProductionStartUp(solution, key),
186
187 MathematicalDecisionCategory.InitialInventory =>
188 ResolveInitialInventory(solution, key),
189
190 MathematicalDecisionCategory.CashBalance =>
191 ResolveCashBalance(solution, key),
192
193 MathematicalDecisionCategory.Inventory =>
194 ResolveInventory(
195 solution,
196 key,
197 inventory =>
198 inventory.GetLevel(
199 GetPeriod(key))),
200
201 MathematicalDecisionCategory.InventorySetup =>
202 ResolveInventory(
203 solution,
204 key,
205 inventory =>
206 inventory.IsSetupActivated(
207 GetPeriod(key))
208 ? 1.0
209 : 0.0),
210
211 MathematicalDecisionCategory.InventorySafetyStockViolation =>
212 ResolveInventory(
213 solution,
214 key,
215 inventory =>
216 inventory.GetSafetyStockViolation(
217 GetPeriod(key))),
218
219 MathematicalDecisionCategory.InventoryAdditionalCapacity =>
220 ResolveInventory(
221 solution,
222 key,
223 inventory =>
224 inventory.GetAdditionalCapacityUsed(
225 GetPeriod(key))),
226
227 MathematicalDecisionCategory.Delivery =>
228 ResolveDistribution(
229 solution,
230 key,
231 decision =>
232 decision.GetDeliveredQuantity(
233 GetPeriod(key))),
234
235 MathematicalDecisionCategory.Backlog =>
236 ResolveDistribution(
237 solution,
238 key,
239 decision =>
240 decision.GetBacklogLevel(
241 GetPeriod(key))),
242
243 MathematicalDecisionCategory.Shortage =>
244 ResolveDistribution(
245 solution,
246 key,
247 decision =>
248 decision.GetShortageQuantity(
249 GetPeriod(key))),
250
251 MathematicalDecisionCategory.Transport =>
252 ResolveTransport(
253 solution,
254 key,
255 decision =>
256 decision.GetTransportedQuantity(
257 GetPeriod(key))),
258
259 MathematicalDecisionCategory.TransportSetup =>
260 ResolveTransport(
261 solution,
262 key,
263 decision =>
264 decision.IsSetupActivated(
265 GetPeriod(key))
266 ? 1.0
267 : 0.0),
268
269 MathematicalDecisionCategory.TransportAdditionalCapacity =>
270 ResolveTransport(
271 solution,
272 key,
273 decision =>
274 decision.GetAdditionalCapacityUsed(
275 GetPeriod(key))),
276
277 MathematicalDecisionCategory.Procurement =>
278 ResolveProcurement(
279 solution,
280 key),
281
282 MathematicalDecisionCategory.WorkCenterActivation =>
283 ResolveWorkCenterCapacity(
284 solution,
285 key,
286 decision =>
287 decision.IsActivated(
288 GetPeriod(key))
289 ? 1.0
290 : 0.0),
291
292 MathematicalDecisionCategory.WorkCenterAdditionalCapacity =>
293 ResolveWorkCenterCapacity(
294 solution,
295 key,
296 decision =>
297 decision.GetAdditionalCapacityUsed(
298 GetPeriod(key))),
299
300 MathematicalDecisionCategory.WarehouseActivation =>
301 ResolveWarehouseCapacity(
302 solution,
303 key,
304 decision =>
305 decision.IsActivated(
306 GetPeriod(key))
307 ? 1.0
308 : 0.0),
309
310 MathematicalDecisionCategory.WarehouseAdditionalCapacity =>
311 ResolveWarehouseCapacity(
312 solution,
313 key,
314 decision =>
315 decision.GetAdditionalCapacityUsed(
316 GetPeriod(key))),
317
318 MathematicalDecisionCategory.TransportResourceActivation =>
319 ResolveTransportResourceCapacity(
320 solution,
321 key,
322 decision =>
323 decision.IsActivated(
324 GetPeriod(key))
325 ? 1.0
326 : 0.0),
327
328 MathematicalDecisionCategory.TransportResourceAdditionalCapacity =>
329 ResolveTransportResourceCapacity(
330 solution,
331 key,
332 decision =>
333 decision.GetAdditionalCapacityUsed(
334 GetPeriod(key))),
335
336 MathematicalDecisionCategory.Subcontracting =>
337 throw new NotSupportedException(
338 "The current normalized LotSizingSolution model " +
339 "does not expose a subcontracting decision family."),
340
341 _ =>
342 throw new NotSupportedException(
343 $"Mathematical decision category " +
344 $"'{key.Category}' is not supported by the " +
345 "solution value projector.")
346 };
347 }
348
349 private static double ResolveMicroProduction(
350 LotSizingSolution solution,
351 MathematicalDomainKey key)
352 {
353 int routingId = key.GetRequiredInt32(MathematicalDomainKeySegment.Routing);
354 ProductionMicroPeriodDecision decision = ResolveMicroDecision(solution, key);
355 return decision.RoutingId == routingId ? decision.Quantity : 0.0;
356 }
357
358 private static double ResolveMicroSetupState(
359 LotSizingSolution solution,
360 MathematicalDomainKey key)
361 {
362 int itemId = key.GetRequiredInt32(MathematicalDomainKeySegment.Item);
363 ProductionMicroPeriodDecision decision = ResolveMicroDecision(solution, key);
364 return decision.SetupItemId == itemId ? 1.0 : 0.0;
365 }
366
367 private static double ResolveMicroChangeover(
368 LotSizingSolution solution,
369 MathematicalDomainKey key)
370 {
371 int fromItemId = key.GetRequiredInt32(MathematicalDomainKeySegment.FromItem);
372 int toItemId = key.GetRequiredInt32(MathematicalDomainKeySegment.ToItem);
373 int plantId = key.GetRequiredInt32(MathematicalDomainKeySegment.Plant);
374 int workCenterId = key.GetRequiredInt32(MathematicalDomainKeySegment.WorkCenter);
375 int period = key.GetRequiredInt32(MathematicalDomainKeySegment.Period);
376 int micro = key.GetRequiredInt32(MathematicalDomainKeySegment.MicroPeriod);
377 WorkCenterSchedulingDecision schedule = solution.WorkCenterSchedulingDecisions.SingleOrDefault(candidate => candidate.WorkCenter.PlantId == plantId && candidate.WorkCenter.WorkCenterId == workCenterId) ?? throw new InvalidOperationException("No candidate macro/micro schedule matches the mathematical domain key.");
378 ProductionMicroPeriodDecision[] ordered = schedule.MicroPeriods.OrderBy(candidate => candidate.MicroPeriod.MacroPeriod).ThenBy(candidate => candidate.MicroPeriod.MicroPeriodIndex).ToArray();
379 int currentIndex = Array.FindIndex(ordered, candidate => candidate.MicroPeriod.MacroPeriod == period && candidate.MicroPeriod.MicroPeriodIndex == micro);
380 if (currentIndex < 0) { throw new InvalidOperationException("No candidate micro-period decision matches the changeover domain key."); }
381 if (currentIndex == 0) { return ordered[0].SetupItemId == toItemId ? 1.0 : 0.0; }
382 return ordered[currentIndex - 1].SetupItemId == fromItemId && ordered[currentIndex].SetupItemId == toItemId ? 1.0 : 0.0;
383 }
384
385 private static double ResolveCashBalance(
386 LotSizingSolution solution,
387 MathematicalDomainKey key)
388 {
389 int period =
390 key.GetRequiredInt32(
391 MathematicalDomainKeySegment.Period);
392
393 return solution.TryGetCashBalance(
394 period,
395 out double balance)
396 ? balance
397 : 0.0;
398 }
399 private static double ResolveInitialInventory(
400 LotSizingSolution solution,
401 MathematicalDomainKey key)
402 {
403 int itemId =
404 key.GetRequiredInt32(
405 MathematicalDomainKeySegment.Item);
406
407 bool hasWarehouse =
408 key.TryGetInt32(
409 MathematicalDomainKeySegment.Warehouse,
410 out int warehouseId);
411
412 bool hasPlant =
413 key.TryGetInt32(
414 MathematicalDomainKeySegment.Plant,
415 out int plantId);
416
417 if (hasWarehouse == hasPlant)
418 {
419 throw new InvalidOperationException(
420 "An initial-inventory key must identify exactly one warehouse.");
421 }
422
423 var decision =
424 solution.InventoryDecisions.SingleOrDefault(
425 candidate =>
426 candidate.ItemId == itemId &&
427 (hasWarehouse
428 ? candidate.Warehouse.Kind ==
429 WarehouseReferenceKind.StandaloneWarehouse &&
430 candidate.Warehouse.ReferenceId == warehouseId
431 : candidate.Warehouse.Kind ==
432 WarehouseReferenceKind.PlantWarehouse &&
433 candidate.Warehouse.ReferenceId == plantId));
434
435 return decision?.InitialInventoryLevel ?? 0.0;
436 }
437 private static double ResolveProductionStartUp(
438 LotSizingSolution solution,
439 MathematicalDomainKey key)
440 {
441 if (key.TryGetInt32(MathematicalDomainKeySegment.MicroPeriod, out _))
442 {
443 return ResolveMicroSetupStart(solution, key);
444 }
445
446 return ResolveProduction(
447 solution,
448 key,
449 production =>
450 IsSchedulingSetupStart(production, key)
451 ? 1.0
452 : 0.0);
453 }
454
455 private static double ResolveMicroSetupStart(
456 LotSizingSolution solution,
457 MathematicalDomainKey key)
458 {
459 int itemId = key.GetRequiredInt32(MathematicalDomainKeySegment.Item);
460 int plantId = key.GetRequiredInt32(MathematicalDomainKeySegment.Plant);
461 int workCenterId = key.GetRequiredInt32(MathematicalDomainKeySegment.WorkCenter);
462 int period = key.GetRequiredInt32(MathematicalDomainKeySegment.Period);
463 int micro = key.GetRequiredInt32(MathematicalDomainKeySegment.MicroPeriod);
464 WorkCenterSchedulingDecision schedule = solution.WorkCenterSchedulingDecisions.SingleOrDefault(candidate => candidate.WorkCenter.PlantId == plantId && candidate.WorkCenter.WorkCenterId == workCenterId) ?? throw new InvalidOperationException("No candidate macro/micro schedule matches the mathematical domain key.");
465 ProductionMicroPeriodDecision[] ordered = schedule.MicroPeriods.OrderBy(candidate => candidate.MicroPeriod.MacroPeriod).ThenBy(candidate => candidate.MicroPeriod.MicroPeriodIndex).ToArray();
466 int currentIndex = Array.FindIndex(ordered, candidate => candidate.MicroPeriod.MacroPeriod == period && candidate.MicroPeriod.MicroPeriodIndex == micro);
467 if (currentIndex < 0) { throw new InvalidOperationException("No candidate micro-period decision matches the setup-start domain key."); }
468 if (ordered[currentIndex].SetupItemId != itemId) { return 0.0; }
469 if (key.TryGetInt32(MathematicalDomainKeySegment.SetupReset, out int reset) && reset != 0) { return 1.0; }
470 if (key.TryGetInt32(MathematicalDomainKeySegment.FromItem, out int fromItem)) { return itemId == fromItem ? 0.0 : 1.0; }
471 if (currentIndex == 0) { return 1.0; }
472 return ordered[currentIndex - 1].SetupItemId == itemId ? 0.0 : 1.0;
473 }
474
475 private static ProductionMicroPeriodDecision ResolveMicroDecision(
476 LotSizingSolution solution,
477 MathematicalDomainKey key)
478 {
479 int plantId = key.GetRequiredInt32(MathematicalDomainKeySegment.Plant);
480 int workCenterId = key.GetRequiredInt32(MathematicalDomainKeySegment.WorkCenter);
481 int period = key.GetRequiredInt32(MathematicalDomainKeySegment.Period);
482 int micro = key.GetRequiredInt32(MathematicalDomainKeySegment.MicroPeriod);
483 WorkCenterSchedulingDecision schedule = solution.WorkCenterSchedulingDecisions.SingleOrDefault(candidate => candidate.WorkCenter.PlantId == plantId && candidate.WorkCenter.WorkCenterId == workCenterId) ?? throw new InvalidOperationException("No candidate macro/micro schedule matches the mathematical domain key.");
484 return schedule.MicroPeriods.SingleOrDefault(candidate => candidate.MicroPeriod.MacroPeriod == period && candidate.MicroPeriod.MicroPeriodIndex == micro) ?? throw new InvalidOperationException("No candidate micro-period decision matches the mathematical domain key.");
485 }
486
487 private static bool IsSchedulingSetupStart(
488 ProductionDecision production,
489 MathematicalDomainKey key)
490 {
491 int period = GetPeriod(key);
492 if (!production.IsSetupActivated(period)) { return false; }
493 if (key.TryGetInt32(MathematicalDomainKeySegment.SetupReset, out int reset) && reset != 0) { return true; }
494 if (period == 1 && key.TryGetInt32(MathematicalDomainKeySegment.FromItem, out int fromItem) && key.TryGetInt32(MathematicalDomainKeySegment.Item, out int itemId)) { return itemId != fromItem; }
495 return period == 1 || !production.IsSetupActivated(period - 1);
496 }
497
498 private static double ResolveProduction(
499 LotSizingSolution solution,
500 MathematicalDomainKey key,
501 Func<ProductionDecision, double> selector)
502 {
503 int routingId =
504 key.GetRequiredInt32(
505 MathematicalDomainKeySegment.Routing);
506
507 ProductionDecision decision =
508 solution.ProductionDecisions
509 .SingleOrDefault(
510 candidate =>
511 candidate.RoutingId ==
512 routingId)
513 ?? throw MissingDecision(
514 key,
515 $"production routing {routingId}");
516
517 return selector(decision);
518 }
519
520 private static double ResolveInventory(
521 LotSizingSolution solution,
522 MathematicalDomainKey key,
523 Func<InventoryDecision, double> selector)
524 {
525 int itemId =
526 key.GetRequiredInt32(
527 MathematicalDomainKeySegment.Item);
528
529 WarehouseReference warehouse =
530 ResolveWarehouse(
531 key,
532 MathematicalDomainKeySegment.Warehouse,
533 MathematicalDomainKeySegment.Plant,
534 "inventory");
535
536 InventoryDecision decision =
537 solution.InventoryDecisions
538 .SingleOrDefault(
539 candidate =>
540 candidate.ItemId ==
541 itemId &&
542 SameWarehouse(
543 candidate.Warehouse,
544 warehouse))
545 ?? throw MissingDecision(
546 key,
547 $"inventory item {itemId}");
548
549 return selector(decision);
550 }
551
552 private static double ResolveDistribution(
553 LotSizingSolution solution,
554 MathematicalDomainKey key,
555 Func<DistributionDecision, double> selector)
556 {
557 int distributionCenterId =
558 key.GetRequiredInt32(
559 MathematicalDomainKeySegment.DistributionCenter);
560
561 int itemId =
562 key.GetRequiredInt32(
563 MathematicalDomainKeySegment.Item);
564
565 WarehouseReference warehouse =
566 ResolveWarehouse(
567 key,
568 MathematicalDomainKeySegment.Warehouse,
569 MathematicalDomainKeySegment.Plant,
570 "distribution source");
571
572 DistributionDecision decision =
573 solution.DistributionDecisions
574 .SingleOrDefault(
575 candidate =>
576 candidate.Matches(
577 distributionCenterId,
578 itemId,
579 warehouse))
580 ?? throw MissingDecision(
581 key,
582 $"distribution center {distributionCenterId}, " +
583 $"item {itemId}");
584
585 return selector(decision);
586 }
587
588 private static double ResolveTransport(
589 LotSizingSolution solution,
590 MathematicalDomainKey key,
591 Func<TransportDecision, double> selector)
592 {
593 int itemId =
594 key.GetRequiredInt32(
595 MathematicalDomainKeySegment.Item);
596
597 int transportResourceId =
598 key.GetRequiredInt32(
599 MathematicalDomainKeySegment.TransportResource);
600
601 WarehouseReference origin =
602 ResolveWarehouse(
603 key,
604 MathematicalDomainKeySegment.OriginWarehouse,
605 MathematicalDomainKeySegment.OriginPlant,
606 "transport origin");
607
608 WarehouseReference destination =
609 ResolveWarehouse(
610 key,
611 MathematicalDomainKeySegment.DestinationWarehouse,
612 MathematicalDomainKeySegment.DestinationPlant,
613 "transport destination");
614
615 TransportDecision decision =
616 solution.TransportDecisions
617 .SingleOrDefault(
618 candidate =>
619 candidate.Matches(
620 itemId,
621 transportResourceId,
622 origin,
623 destination))
624 ?? throw MissingDecision(
625 key,
626 $"transport item {itemId}, resource " +
627 $"{transportResourceId}");
628
629 return selector(decision);
630 }
631
632 private static double ResolveProcurement(
633 LotSizingSolution solution,
634 MathematicalDomainKey key)
635 {
636 int supplierId =
637 key.GetRequiredInt32(
638 MathematicalDomainKeySegment.Supplier);
639
640 int itemId =
641 key.GetRequiredInt32(
642 MathematicalDomainKeySegment.Item);
643
644 WarehouseReference destination =
645 ResolveWarehouse(
646 key,
647 MathematicalDomainKeySegment.DestinationWarehouse,
648 MathematicalDomainKeySegment.DestinationPlant,
649 "procurement destination");
650
651 PurchaseDecision decision =
652 solution.PurchaseDecisions
653 .SingleOrDefault(
654 candidate =>
655 candidate.Matches(
656 supplierId,
657 itemId,
658 destination))
659 ?? throw MissingDecision(
660 key,
661 $"supplier {supplierId}, item {itemId}");
662
663 return decision.GetPurchasedQuantity(
664 GetPeriod(key));
665 }
666
667 private static double ResolveWorkCenterCapacity(
668 LotSizingSolution solution,
669 MathematicalDomainKey key,
670 Func<WorkCenterCapacityDecision, double> selector)
671 {
672 int plantId =
673 key.GetRequiredInt32(
674 MathematicalDomainKeySegment.Plant);
675
676 int workCenterId =
677 key.GetRequiredInt32(
678 MathematicalDomainKeySegment.WorkCenter);
679
680 WorkCenterCapacityDecision decision =
681 solution.WorkCenterCapacityDecisions
682 .SingleOrDefault(
683 candidate =>
684 candidate.WorkCenter.PlantId ==
685 plantId &&
686 candidate.WorkCenter.WorkCenterId ==
687 workCenterId)
688 ?? throw MissingDecision(
689 key,
690 $"work center {plantId}/{workCenterId}");
691
692 return selector(decision);
693 }
694
695 private static double ResolveWarehouseCapacity(
696 LotSizingSolution solution,
697 MathematicalDomainKey key,
698 Func<WarehouseCapacityDecision, double> selector)
699 {
700 WarehouseReference warehouse =
701 ResolveWarehouse(
702 key,
703 MathematicalDomainKeySegment.Warehouse,
704 MathematicalDomainKeySegment.Plant,
705 "warehouse capacity");
706
707 WarehouseCapacityDecision decision =
708 solution.WarehouseCapacityDecisions
709 .SingleOrDefault(
710 candidate =>
711 candidate.Matches(
712 warehouse))
713 ?? throw MissingDecision(
714 key,
715 "warehouse capacity");
716
717 return selector(decision);
718 }
719
720 private static double ResolveTransportResourceCapacity(
721 LotSizingSolution solution,
722 MathematicalDomainKey key,
723 Func<TransportResourceCapacityDecision, double> selector)
724 {
725 int transportResourceId =
726 key.GetRequiredInt32(
727 MathematicalDomainKeySegment.TransportResource);
728
729 TransportResourceCapacityDecision decision =
730 solution.TransportResourceCapacityDecisions
731 .SingleOrDefault(
732 candidate =>
733 candidate.Matches(
734 transportResourceId))
735 ?? throw MissingDecision(
736 key,
737 $"transport resource {transportResourceId}");
738
739 return selector(decision);
740 }
741
742 private static int GetPeriod(
743 MathematicalDomainKey key)
744 {
745 return key.GetRequiredInt32(
746 MathematicalDomainKeySegment.Period);
747 }
748
749 private static WarehouseReference ResolveWarehouse(
750 MathematicalDomainKey key,
751 string standaloneSegment,
752 string plantSegment,
753 string role)
754 {
755 bool hasStandalone =
756 key.TryGetInt32(
757 standaloneSegment,
758 out int warehouseId);
759
760 bool hasPlant =
761 key.TryGetInt32(
762 plantSegment,
763 out int plantId);
764
765 if (hasStandalone == hasPlant)
766 {
767 throw new InvalidOperationException(
768 $"The domain key must identify exactly one {role} " +
769 $"warehouse using either '{standaloneSegment}' or " +
770 $"'{plantSegment}'.");
771 }
772
773 return hasStandalone
774 ? WarehouseReference.ForStandaloneWarehouse(
775 warehouseId)
776 : WarehouseReference.ForPlantWarehouse(
777 plantId);
778 }
779
780 private static bool SameWarehouse(
781 WarehouseReference left,
782 WarehouseReference right)
783 {
784 return left.Kind ==
785 right.Kind &&
786 left.ReferenceId ==
787 right.ReferenceId;
788 }
789
790 private static InvalidOperationException MissingDecision(
791 MathematicalDomainKey key,
792 string decisionDescription)
793 {
794 return new InvalidOperationException(
795 $"No candidate {decisionDescription} decision matches " +
796 $"mathematical domain key '{FormatKey(key)}'.");
797 }
798
799 private static string FormatKey(
800 MathematicalDomainKey key)
801 {
802 return string.Join(
803 "|",
804 new[]
805 {
806 key.Category
807 }.Concat(
808 key.Segments.Select(
809 pair =>
810 $"{pair.Key}={pair.Value}")));
811 }
812}
Describes one issue encountered while projecting a business solution onto mathematical-model variable...
Contains mathematical-variable values projected from one normalized lot-sizing solution.
Projects a normalized LotSizingSolution onto the variables of a MathematicalModel by using the canoni...
MathematicalSolutionProjectionResult Project(MathematicalModel model, LotSizingSolution solution)
Projects candidate business-decision values onto all mathematical variables that have supported domai...
Defines projection of a normalized lot-sizing solution onto the variables of a solver-independent mat...