LotSizingDataModel.Solver 2.0.1
Solver-independent modeling, execution, monitoring and adapter infrastructure.
Loading...
Searching...
No Matches
TransportSetupVariableFamilyBuilder.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3using System.Threading;
4using System.Threading.Tasks;
5using LotSizingDataModel.Core.PhysicalModel;
6using LotSizingDataModel.Core.Relationships;
7using LotSizingDataModel.Instance;
10
12
13/// <summary>
14/// Builds item-specific binary transport-setup variables.
15/// </summary>
18{
19 /// <summary>
20 /// Gets the transport-setup variable-family identifier.
21 /// </summary>
22 public override string FamilyId =>
24
25 /// <summary>
26 /// Determines whether transport setup variables are enabled.
27 /// </summary>
28 public override bool IsEnabled(
29 LotSizingInstance instance,
31 {
32 ArgumentNullException.ThrowIfNull(instance);
33 ArgumentNullException.ThrowIfNull(options);
34
35 return
36 options.IncludeTransport &&
37 options.IncludeTransportSetups &&
38 instance.SupplyChain.TransportCharacteristics.Any(
39 characteristic =>
40 characteristic.FixedSetupCost is not null ||
41 characteristic.SetupTime is not null);
42 }
43
44 /// <summary>
45 /// Builds transport-setup variables.
46 /// </summary>
47 protected override ValueTask BuildFamilyAsync(
48 LotSizingInstance instance,
51 CancellationToken cancellationToken)
52 {
53 int planningHorizon =
54 instance.PlanningHorizon;
55
56 foreach (
57 TransportCharacteristic characteristic
58 in instance.SupplyChain.TransportCharacteristics)
59 {
60 if (characteristic.FixedSetupCost is null &&
61 characteristic.SetupTime is null)
62 {
63 continue;
64 }
65
66 TransportResource resource =
67 instance.SupplyChain.TransportResources
68 .FirstOrDefault(
69 candidate =>
70 candidate.Id ==
71 characteristic.TransportResourceId)
72 ?? throw new InvalidOperationException(
73 $"Transport resource " +
74 $"{characteristic.TransportResourceId} " +
75 "does not exist.");
76
77 foreach (
78 AssignedTransportLane lane
79 in instance.SupplyChain.GetTransportLanes(resource.Id))
80 {
81 for (
82 int period = 1;
83 period <= planningHorizon;
84 period++)
85 {
86 cancellationToken.ThrowIfCancellationRequested();
87
88 string domainKey =
89 CreateTransportKey(
91 characteristic.ItemId,
92 resource,
93 lane)
94 .Add(
96 period)
97 .Build();
98
100 context,
101 $"YT_i{characteristic.ItemId}_r{resource.Id}" +
102 $"_l{lane.Id}_t{period}",
103 domainKey,
104 $"Transport setup for item " +
105 $"{characteristic.ItemId}, resource " +
106 $"{resource.Id}, period {period}.");
107 }
108 }
109 }
110
111 return ValueTask.CompletedTask;
112 }
113
114
115 private static MathematicalDomainKeyBuilder CreateTransportKey(
116 string category,
117 int itemId,
118 TransportResource resource,
119 AssignedTransportLane lane)
120 {
121 var keyBuilder =
123 category)
124 .Add(
126 itemId)
127 .Add(
129 resource.Id);
130
132 keyBuilder,
133 lane.Origin);
134
136 keyBuilder,
137 lane.Destination);
138
139 return keyBuilder;
140 }
141
142}
Builds stable business-domain keys for mathematical variables and constraints.
MathematicalDomainKeyBuilder Add(string name, string value)
Adds a text segment.
Provides the shared state required while constructing a solver-independent mathematical model.
Creates canonical domain-key fragments shared by standard formulation builders and provides warehouse...
static MathematicalDomainKeyBuilder AddDestinationWarehouse(MathematicalDomainKeyBuilder builder, WarehouseReference warehouse)
Adds a destination warehouse reference.
static MathematicalDomainKeyBuilder AddOriginWarehouse(MathematicalDomainKeyBuilder builder, WarehouseReference warehouse)
Adds an origin warehouse reference.
Configures the standard solver-independent mixed-integer lot-sizing formulation.
Provides common validation and variable-registration helpers for one variable family of the standard ...
static MathematicalVariable AddBinaryVariable(MathematicalModelBuildContext context, string name, string domainKey, string description="")
Creates and registers one binary variable.
override bool IsEnabled(LotSizingInstance instance, StandardLotSizingFormulationOptions options)
Determines whether transport setup variables are enabled.
override string FamilyId
Gets the transport-setup variable-family identifier.
override ValueTask BuildFamilyAsync(LotSizingInstance instance, MathematicalModelBuildContext context, StandardLotSizingFormulationOptions options, CancellationToken cancellationToken)
Builds transport-setup variables.
Provides the canonical mathematical domain-key categories used to identify lot-sizing decision famili...
const string TransportSetup
Identifies transport setup decision variables.
Provides the canonical mathematical domain-key segment names used by lot-sizing variables and constra...
const string TransportResource
Transport-resource identifier segment.