yanweiyuan3
2023-08-09 588bc7829387dfc761cc25f06f77d4c81818bd10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
Quintiq file version 2.0
#parent: #root
Method SetOptimizerInput (
  LibOpt_Scope scope,
  RunContextForCapacityPlanning runcontext
)
{
  Description: 'Creates scope  for run'
  TextBody:
  [*
    // We need to do some partial propegation to ensure the relations used in this method exist.
    Transaction::Transaction().Propagate( relation( Trip, ArrivalUnitPeriod ) );
    Transaction::Transaction().Propagate( relation( Trip, DepartureUnitPeriod ) );
    Transaction::Transaction().Propagate( relation( ProductInTrip, ArrivalPISPIP ) );
    Transaction::Transaction().Propagate( relation( ProductInTrip, DeparturePISPIP ) );
    
    periodstart := runcontext.FirstPeriod_MP(); 
    periodend := runcontext.LastPeriod_MP(); 
    macroplan := periodstart.MacroPlan();
    
    optpuzzle := scope.GetOptimizerPuzzleInOptimizerRun(); 
    
    units := null(  Units ); 
    pisps := null(  ProductInStockingPoint_MPs ); 
    sps := construct(   StockingPoint_MPs ); 
    
    if ( not isnull( optpuzzle ) ) 
    {
      units := optpuzzle.GetIncludedUnits();  
      sps := optpuzzle.GetIncludedStockingPoints( units );
      pisps := selectset(  sps, Elements.ProductInStockingPoint_MP, pisp, pisp.IsLeaf() and pisp.IsInOptimizerPuzzle() );
    }
    else
    {
      units := selectset( macroplan, Unit, unit, true, true ); 
      sps := selectset(  macroplan, StockingPoint_MP, sp, true, true ); 
      pisps := selectset( macroplan, Product_MP.ProductInStockingPoint_MP, pisp, pisp.IsLeaf() )
    }
    
    traverse( units, Elements, unit )
    {
      scope.Add( unit ); 
    }
    
    // Set the relation from all pisps 
    traverse( pisps, Elements, pisp )
    {
      scope.Add(  pisp );
    }
    
    // Traverse over all periods that should be included in the optimization
    scopeperiods := construct( Period_MPs );
    traverse( macroplan, PlanningPeriod, period,
              period.IsAvailableForOptimization( periodstart, periodend ) )
    {
      // Set a relation to this algorithm run for all periods within the optimization horizon
      scope.Add(  period ); 
      scopeperiods.Add(  period ); 
    
      // Set a relation from all SPinPeriod related to the selected periods to this algorithm run
      traverse( period, StockingPointInPeriod, spip, spip.StockingPoint_MP().isInOptimizerPuzzle() )
      {
        scope.Add(  spip ); 
    
        // Set a relation from all PISPIP related to the selected spip to this algorithm run
        // Only include those pispips that belong to a pisp that is part of this algorithm run
        // Only include those pispips where the product is included.
        traverse( spip, LeafPlanningPISPIPs, pispip,
                  scope.Contains( pispip.ProductInStockingPoint_MP().PISPInOptimizerRun() ) and
                  ( pispip.HasRegularProductForOptimizer() or pispip.GetProductIsIncludedInOptimizerRun( runcontext.IsPostProcessing() )  ) )
        {
          scope.Add( pispip ); 
          scope.SetLeafAggregateSDIPRelation( pispip );
        }
      }
    
      // Set a relation from all UnitPeriods related to the selected period to this algorithmrun
      // Only set that relation if the UnitPeriod belongs to a unit that is part of this algorithm run
      traverse( period, UnitPeriod, up, not up.IsPeriodFrozen() and scope.Contains( up.Unit().UnitInOptimizerRun() )  )
      {
        if ( not up.Unit().Operation( relsize ) = 0
             or  exists( up, PeriodTask_MP.astype( PeriodTaskLaneLeg ).Trip, trip, trip.GetIsValidNonFrozenTrip() ) 
             or exists( up, astype(  UnitPeriodTransportBase ).AsArrivalUnitPeriod, trip, trip.GetIsValidNonFrozenTrip() )
             or exists( up, astype(  UnitPeriodTransportBase ).AsDepartureUnitPeriod, trip, trip.GetIsValidNonFrozenTrip() )  )
        {
          scope.Add( up );
        }
        // put existing period task operations into scope
        traverse( up, 
                  PeriodTaskOperation, 
                  periodtaskoperation, 
                  periodtaskoperation.Operation().GetIsAvailableForOptimization()
                  and forall(  periodtaskoperation.Operation(), OperationInput.AsInputInProductInStockingPoint_MP, pisp, scope.Contains( pisp.PISPInOptimizerRun() ) ) 
                  and forall(  periodtaskoperation.Operation(), OperationOutput.AsOutputInProductInStockingPoint_MP, pisp, scope.Contains( pisp.PISPInOptimizerRun() ) )  )
        {
          scope.Add( periodtaskoperation ); 
        }
      }
    }
    
    // Set the relation from the operations related to the selected units to this algorithmrun
    operationsaddedtoscope := construct( Operations ); 
    traverse(  units, Elements, unit ) 
    {
      traverse( unit, Operation, operation,
                operation.GetIsAvailableForOptimization()
                and forall(  operation, OperationInput.AsInputInProductInStockingPoint_MP, pisp, scope.Contains( pisp.PISPInOptimizerRun() ) )
                and forall ( operation, OperationOutput.AsOutputInProductInStockingPoint_MP, pisp, scope.Contains( pisp.PISPInOptimizerRun() ) ) )
                //)
      {
        scope.Add(  operation ); 
        operationsaddedtoscope.Add( operation ); 
        unit.OptimizerFullRunAddedOperationOrTrip( true ); 
      }
    }
    
    // Set the relation from the trips that are part of the selected units and the selected unit periods to this algorithmrun
    traverse( units, Elements, unit ) 
    {
      traverse( unit, Lane.LaneLeg, laneleg,
                laneleg.GetIsAvailableForOptimization() )
      {
        traverse( laneleg, 
                  Trip, 
                  trip, 
                  trip.HasValidArrival() 
                  and trip.HasValidDeparture()
                  and guard( scope.Contains(  trip.DepartureUnitPeriod().UnitPeriodInOptimizerRun() ), false )
                  and guard( scope.Contains(  trip.ArrivalUnitPeriod().UnitPeriodInOptimizerRun() ), false ) 
                  and trip.LaneLeg().AsOriginStockingPointLeg().StockingPoint_MP().isInOptimizerPuzzle() 
                  and trip.LaneLeg().AsDestinationStockingPointLeg().StockingPoint_MP().isInOptimizerPuzzle() )  
        {
          scope.Add( trip ); 
          unit.OptimizerFullRunAddedOperationOrTrip( true ); 
          
          // Set the relation from all products in this trip to this algorithmrun
          // Both the arrival and departure PISPIP must be part of this algorithm run
          // Only include those productintrips where the product is included.
          traverse( trip, ProductInTrip, productintrip,
                    guard( scope.Contains( productintrip.ArrivalPISPIP().PISPIPInOptimizerRun() ), false )
                    and guard( scope.Contains( productintrip.DeparturePISPIP().PISPIPInOptimizerRun() ), false )
                    and guard( productintrip.HasRegularProductForOptimizer() or productintrip.Product_MP().GetIsInOptimizerRun( runcontext.IsPostProcessing() ), false ) 
                    )
          {
            scope.Add(  productintrip ); 
          }
        }
      }
    }
    
    // Remove the units that have no operations or trips for optimization
    traverse( units, Elements, unit,  // full run uses all units - added above - no need to check scope again
              not unit.OptimizerFullRunAddedOperationOrTrip() ) 
    {
      unit.UnitInOptimizerRun().Delete(); 
      traverse( unit, UnitPeriod.UnitPeriodInOptimizerRun, unitperiodinrun, scope.Contains( unitperiodinrun ) ) 
      {
        unitperiodinrun.Delete(); 
      }
    }
    
    // The PISPIPs that contain the dependent demand of a valid operation-period combination should also be considered
    traverse( operationsaddedtoscope, Elements, operation )
    {
      ddperiodstotal := construct( Period_MPs, constcontent ); 
      periods := this.GetPeriodsForOperation( scopeperiods, operation );
    
      traverse( periods, Elements, period )
      {
        ddperiods := construct( Period_MPs, constcontent ); 
        CapacityPlanningSuboptimizer::GetOperationDependentDemandPeriods( period, operation, &ddperiods, false ); 
        ddperiodstotal := ddperiodstotal.Add( ddperiods ); 
      }
      ddperiodstotal := ddperiodstotal.Unique(); 
      
      if ( ddperiodstotal.Size() > 0 ) 
      {
        pispiptoadd := selectset( operation, 
                                  OperationInput.ProductInStockingPoint_MP.ProductInStockingPointInPeriodPlanning, 
                                  pispip,
                                  ddperiodstotal.Find( pispip.Period_MP() ) > 0
                                  and ( pispip.HasRegularProductForOptimizer() or pispip.GetProductIsIncludedInOptimizerRun( runcontext.IsPostProcessing() ) ) 
                                  and not scope.Contains( pispip.PISPIPInOptimizerRun() ) )
                                    
        traverse( pispiptoadd, Elements, pispip )
        {
            scope.Add(  pispip ); 
            scope.Add(  pispip.StockingPointInPeriod() ); 
            scope.Add(  pispip.ProductInStockingPoint_MP() ); 
        }
      }
    }
    
    // Handle sales demand before the optimization horizon, including those before the planning horizon.
    if( macroplan.FirstPlanningPeriod().Start() < periodstart.Start() ) // If there exists planning periods before the optimization horizon.
    {
      traverse( macroplan, SalesSegment_MP.PostponementSpecification, postponementspec,
                postponementspec.IsValidData()
              )
      {
        traverse( postponementspec, SalesDemandInPeriod.astype( SalesDemandInPeriod ), sdip,
                  not sdip.IsPostponed()
                  and sdip.CanOptimizeWhenPostponed( periodstart )
                  and sdip.NeedsToBePlanned()
                  and sdip.IsWithinThresholdQuantity()
                  and sdip.GetIsPISPAndProductInOptimizerRun( scope, runcontext.IsPostProcessing() )
                )
        {
          scope.AddSDIPBeforeScope( sdip );
          sdip.SDIPBeforeScopeInRun().OptMinPostponementPeriod( periodstart.SequenceNrInPeriodSpecification()
                                                                - sdip.AsSalesDemandInPeriodBase().Period_MP().SequenceNrInPeriodSpecification()
                                                              );
        }
      }
    }
    
    if ( runcontext.IsSlidingWindowsRun() ) 
    {
      this.SetSlidingWindowPeriods( scope, runcontext, periodstart ); // method requires periods already added 
    }
  *]
  InterfaceProperties { Accessibility: 'Module' }
}