33 MathematicalModel model,
34 LotSizingSolution solution)
36 ArgumentNullException.ThrowIfNull(model);
37 ArgumentNullException.ThrowIfNull(solution);
40 var fleetByKey = solution.PhysicalFleetDecisions.ToLookup(decision => decision.DomainKey, StringComparer.Ordinal);
46 MathematicalVariable variable
51 if (
string.IsNullOrWhiteSpace(
54 throw new InvalidOperationException(
55 "The mathematical variable has no domain key.");
58 MathematicalDomainKey domainKey =
59 MathematicalDomainKey.Parse(
62 double value = domainKey.Category
switch
64 MathematicalDecisionCategory.AuxiliaryProductionFamilySetup =>
65 ResolveFamilySetup(model, solution, variable.Id, domainKey),
66 MathematicalDecisionCategory.PhysicalFleet =>
67 fleetByKey[domainKey.ToString()].SingleOrDefault()?.VehicleCount ?? 0,
68 _ => ResolveValue(solution, domainKey)
75 catch (Exception exception)
95 private static double ResolveFamilySetup(
96 MathematicalModel model, LotSizingSolution solution,
int variableId, MathematicalDomainKey key)
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;
113 private static double ResolveValue(
114 LotSizingSolution solution,
115 MathematicalDomainKey key)
117 return key.Category
switch
119 MathematicalDecisionCategory.Production =>
124 production.GetQuantity(
127 MathematicalDecisionCategory.Setup =>
132 production.IsSetupActivated(
137 MathematicalDecisionCategory.AuxiliaryLotSizeMultiplier =>
142 production.GetLotMultipleCount(
145 MathematicalDecisionCategory
146 .AuxiliarySmallBucketProductionActivation =>
152 production.GetQuantity(
153 GetPeriod(key))) > 1e-9
157 MathematicalDecisionCategory
158 .AuxiliarySchedulingSetupStart =>
163 IsSchedulingSetupStart(
169 MathematicalDecisionCategory.MicroPeriodProduction =>
170 ResolveMicroProduction(solution, key),
172 MathematicalDecisionCategory.MicroPeriodSetupState =>
173 ResolveMicroSetupState(solution, key),
175 MathematicalDecisionCategory.AuxiliaryMicroPeriodChangeover =>
176 ResolveMicroChangeover(solution, key),
178 MathematicalDecisionCategory.AuxiliaryMicroPeriodSetupStart =>
179 ResolveMicroSetupStart(solution, key),
181 MathematicalDecisionCategory.AuxiliaryMacroProductionActivation =>
182 ResolveProduction(solution, key, production => Math.Abs(production.GetQuantity(GetPeriod(key))) > 1e-9 ? 1.0 : 0.0),
184 MathematicalDecisionCategory.AuxiliaryProductionStartUp =>
185 ResolveProductionStartUp(solution, key),
187 MathematicalDecisionCategory.InitialInventory =>
188 ResolveInitialInventory(solution, key),
190 MathematicalDecisionCategory.CashBalance =>
191 ResolveCashBalance(solution, key),
193 MathematicalDecisionCategory.Inventory =>
201 MathematicalDecisionCategory.InventorySetup =>
206 inventory.IsSetupActivated(
211 MathematicalDecisionCategory.InventorySafetyStockViolation =>
216 inventory.GetSafetyStockViolation(
219 MathematicalDecisionCategory.InventoryAdditionalCapacity =>
224 inventory.GetAdditionalCapacityUsed(
227 MathematicalDecisionCategory.Delivery =>
232 decision.GetDeliveredQuantity(
235 MathematicalDecisionCategory.Backlog =>
240 decision.GetBacklogLevel(
243 MathematicalDecisionCategory.Shortage =>
248 decision.GetShortageQuantity(
251 MathematicalDecisionCategory.Transport =>
256 decision.GetTransportedQuantity(
259 MathematicalDecisionCategory.TransportSetup =>
264 decision.IsSetupActivated(
269 MathematicalDecisionCategory.TransportAdditionalCapacity =>
274 decision.GetAdditionalCapacityUsed(
277 MathematicalDecisionCategory.Procurement =>
282 MathematicalDecisionCategory.WorkCenterActivation =>
283 ResolveWorkCenterCapacity(
287 decision.IsActivated(
292 MathematicalDecisionCategory.WorkCenterAdditionalCapacity =>
293 ResolveWorkCenterCapacity(
297 decision.GetAdditionalCapacityUsed(
300 MathematicalDecisionCategory.WarehouseActivation =>
301 ResolveWarehouseCapacity(
305 decision.IsActivated(
310 MathematicalDecisionCategory.WarehouseAdditionalCapacity =>
311 ResolveWarehouseCapacity(
315 decision.GetAdditionalCapacityUsed(
318 MathematicalDecisionCategory.TransportResourceActivation =>
319 ResolveTransportResourceCapacity(
323 decision.IsActivated(
328 MathematicalDecisionCategory.TransportResourceAdditionalCapacity =>
329 ResolveTransportResourceCapacity(
333 decision.GetAdditionalCapacityUsed(
336 MathematicalDecisionCategory.Subcontracting =>
337 throw new NotSupportedException(
338 "The current normalized LotSizingSolution model " +
339 "does not expose a subcontracting decision family."),
342 throw new NotSupportedException(
343 $
"Mathematical decision category " +
344 $
"'{key.Category}' is not supported by the " +
345 "solution value projector.")
349 private static double ResolveMicroProduction(
350 LotSizingSolution solution,
351 MathematicalDomainKey key)
353 int routingId = key.GetRequiredInt32(MathematicalDomainKeySegment.Routing);
354 ProductionMicroPeriodDecision decision = ResolveMicroDecision(solution, key);
355 return decision.RoutingId == routingId ? decision.Quantity : 0.0;
358 private static double ResolveMicroSetupState(
359 LotSizingSolution solution,
360 MathematicalDomainKey key)
362 int itemId = key.GetRequiredInt32(MathematicalDomainKeySegment.Item);
363 ProductionMicroPeriodDecision decision = ResolveMicroDecision(solution, key);
364 return decision.SetupItemId == itemId ? 1.0 : 0.0;
367 private static double ResolveMicroChangeover(
368 LotSizingSolution solution,
369 MathematicalDomainKey key)
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;
385 private static double ResolveCashBalance(
386 LotSizingSolution solution,
387 MathematicalDomainKey key)
390 key.GetRequiredInt32(
391 MathematicalDomainKeySegment.Period);
393 return solution.TryGetCashBalance(
399 private static double ResolveInitialInventory(
400 LotSizingSolution solution,
401 MathematicalDomainKey key)
404 key.GetRequiredInt32(
405 MathematicalDomainKeySegment.Item);
409 MathematicalDomainKeySegment.Warehouse,
410 out
int warehouseId);
414 MathematicalDomainKeySegment.Plant,
417 if (hasWarehouse == hasPlant)
419 throw new InvalidOperationException(
420 "An initial-inventory key must identify exactly one warehouse.");
424 solution.InventoryDecisions.SingleOrDefault(
426 candidate.ItemId == itemId &&
428 ? candidate.Warehouse.Kind ==
429 WarehouseReferenceKind.StandaloneWarehouse &&
430 candidate.Warehouse.ReferenceId == warehouseId
431 : candidate.Warehouse.Kind ==
432 WarehouseReferenceKind.PlantWarehouse &&
433 candidate.Warehouse.ReferenceId == plantId));
435 return decision?.InitialInventoryLevel ?? 0.0;
437 private static double ResolveProductionStartUp(
438 LotSizingSolution solution,
439 MathematicalDomainKey key)
441 if (key.TryGetInt32(MathematicalDomainKeySegment.MicroPeriod, out _))
443 return ResolveMicroSetupStart(solution, key);
446 return ResolveProduction(
450 IsSchedulingSetupStart(production, key)
455 private static double ResolveMicroSetupStart(
456 LotSizingSolution solution,
457 MathematicalDomainKey key)
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;
475 private static ProductionMicroPeriodDecision ResolveMicroDecision(
476 LotSizingSolution solution,
477 MathematicalDomainKey key)
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.");
487 private static bool IsSchedulingSetupStart(
488 ProductionDecision production,
489 MathematicalDomainKey key)
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);
498 private static double ResolveProduction(
499 LotSizingSolution solution,
500 MathematicalDomainKey key,
501 Func<ProductionDecision, double> selector)
504 key.GetRequiredInt32(
505 MathematicalDomainKeySegment.Routing);
507 ProductionDecision decision =
508 solution.ProductionDecisions
511 candidate.RoutingId ==
513 ??
throw MissingDecision(
515 $
"production routing {routingId}");
517 return selector(decision);
520 private static double ResolveInventory(
521 LotSizingSolution solution,
522 MathematicalDomainKey key,
523 Func<InventoryDecision, double> selector)
526 key.GetRequiredInt32(
527 MathematicalDomainKeySegment.Item);
529 WarehouseReference warehouse =
532 MathematicalDomainKeySegment.Warehouse,
533 MathematicalDomainKeySegment.Plant,
536 InventoryDecision decision =
537 solution.InventoryDecisions
545 ??
throw MissingDecision(
547 $
"inventory item {itemId}");
549 return selector(decision);
552 private static double ResolveDistribution(
553 LotSizingSolution solution,
554 MathematicalDomainKey key,
555 Func<DistributionDecision, double> selector)
557 int distributionCenterId =
558 key.GetRequiredInt32(
559 MathematicalDomainKeySegment.DistributionCenter);
562 key.GetRequiredInt32(
563 MathematicalDomainKeySegment.Item);
565 WarehouseReference warehouse =
568 MathematicalDomainKeySegment.Warehouse,
569 MathematicalDomainKeySegment.Plant,
570 "distribution source");
572 DistributionDecision decision =
573 solution.DistributionDecisions
577 distributionCenterId,
580 ??
throw MissingDecision(
582 $
"distribution center {distributionCenterId}, " +
585 return selector(decision);
588 private static double ResolveTransport(
589 LotSizingSolution solution,
590 MathematicalDomainKey key,
591 Func<TransportDecision, double> selector)
594 key.GetRequiredInt32(
595 MathematicalDomainKeySegment.Item);
597 int transportResourceId =
598 key.GetRequiredInt32(
599 MathematicalDomainKeySegment.TransportResource);
601 WarehouseReference origin =
604 MathematicalDomainKeySegment.OriginWarehouse,
605 MathematicalDomainKeySegment.OriginPlant,
608 WarehouseReference destination =
611 MathematicalDomainKeySegment.DestinationWarehouse,
612 MathematicalDomainKeySegment.DestinationPlant,
613 "transport destination");
615 TransportDecision decision =
616 solution.TransportDecisions
624 ??
throw MissingDecision(
626 $
"transport item {itemId}, resource " +
627 $
"{transportResourceId}");
629 return selector(decision);
632 private static double ResolveProcurement(
633 LotSizingSolution solution,
634 MathematicalDomainKey key)
637 key.GetRequiredInt32(
638 MathematicalDomainKeySegment.Supplier);
641 key.GetRequiredInt32(
642 MathematicalDomainKeySegment.Item);
644 WarehouseReference destination =
647 MathematicalDomainKeySegment.DestinationWarehouse,
648 MathematicalDomainKeySegment.DestinationPlant,
649 "procurement destination");
651 PurchaseDecision decision =
652 solution.PurchaseDecisions
659 ??
throw MissingDecision(
661 $
"supplier {supplierId}, item {itemId}");
663 return decision.GetPurchasedQuantity(
667 private static double ResolveWorkCenterCapacity(
668 LotSizingSolution solution,
669 MathematicalDomainKey key,
670 Func<WorkCenterCapacityDecision, double> selector)
673 key.GetRequiredInt32(
674 MathematicalDomainKeySegment.Plant);
677 key.GetRequiredInt32(
678 MathematicalDomainKeySegment.WorkCenter);
680 WorkCenterCapacityDecision decision =
681 solution.WorkCenterCapacityDecisions
684 candidate.WorkCenter.PlantId ==
686 candidate.WorkCenter.WorkCenterId ==
688 ??
throw MissingDecision(
690 $
"work center {plantId}/{workCenterId}");
692 return selector(decision);
695 private static double ResolveWarehouseCapacity(
696 LotSizingSolution solution,
697 MathematicalDomainKey key,
698 Func<WarehouseCapacityDecision, double> selector)
700 WarehouseReference warehouse =
703 MathematicalDomainKeySegment.Warehouse,
704 MathematicalDomainKeySegment.Plant,
705 "warehouse capacity");
707 WarehouseCapacityDecision decision =
708 solution.WarehouseCapacityDecisions
713 ??
throw MissingDecision(
715 "warehouse capacity");
717 return selector(decision);
720 private static double ResolveTransportResourceCapacity(
721 LotSizingSolution solution,
722 MathematicalDomainKey key,
723 Func<TransportResourceCapacityDecision, double> selector)
725 int transportResourceId =
726 key.GetRequiredInt32(
727 MathematicalDomainKeySegment.TransportResource);
729 TransportResourceCapacityDecision decision =
730 solution.TransportResourceCapacityDecisions
734 transportResourceId))
735 ??
throw MissingDecision(
737 $
"transport resource {transportResourceId}");
739 return selector(decision);
742 private static int GetPeriod(
743 MathematicalDomainKey key)
745 return key.GetRequiredInt32(
746 MathematicalDomainKeySegment.Period);
749 private static WarehouseReference ResolveWarehouse(
750 MathematicalDomainKey key,
751 string standaloneSegment,
758 out
int warehouseId);
765 if (hasStandalone == hasPlant)
767 throw new InvalidOperationException(
768 $
"The domain key must identify exactly one {role} " +
769 $
"warehouse using either '{standaloneSegment}' or " +
770 $
"'{plantSegment}'.");
774 ? WarehouseReference.ForStandaloneWarehouse(
776 : WarehouseReference.ForPlantWarehouse(
780 private static bool SameWarehouse(
781 WarehouseReference left,
782 WarehouseReference right)
790 private static InvalidOperationException MissingDecision(
791 MathematicalDomainKey key,
792 string decisionDescription)
794 return new InvalidOperationException(
795 $
"No candidate {decisionDescription} decision matches " +
796 $
"mathematical domain key '{FormatKey(key)}'.");
799 private static string FormatKey(
800 MathematicalDomainKey key)
810 $
"{pair.Key}={pair.Value}")));