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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
Quintiq file version 2.0
#parent: #root
Method InitVariablesForPISPIP (
  CapacityPlanningSuboptimizer_CapacityPlanningAlgorithm program,
  const constcontent ProductInStockingPointInPeriods pispipssmartplan,
  const LibOpt_Scope scope,
  const constcontent ProductInStockingPointInPeriodPlannings pispipsinrun,
  constcontent ProductInStockingPointInPeriodPlanningLeafs leafpispipsinrun,
  const constcontent ProductInStockingPoint_MPs pispsinrun
) const
{
  Description: 'Initialize all variables related to pispip, including fulfillment of sales demand, target inventory level, etc'
  TextBody:
  [*
    runcontext := this.GetRunContextConst(); 
    info( pispipsinrun.Size() );
    info( leafpispipsinrun.Size() );
    
    // Shelf life variable  
    traverse( pispsinrun, Elements, pisp, pisp.IsOptShelfLife() or pisp.IsOptMaturation() ) 
    {
      if ( pisp.IsOptShelfLife() ) 
      {
        program.PISPTotalExpiryVariables().New( pisp );
      }
      initpispips := pisp.PISPInOptimizerRun().GetPISPIPForShelfLifeOptimizer( scope ); 
      traverse( initpispips, Elements, pispip )
      {
        traverse( pispip, ProductInStockingPoint_MP.IncomingShelfLifeDay, islday ) 
        {
          program.CumulativeProductionVariables().New( pispip, islday );
        }
      
        if ( pisp.IsOptShelfLife() ) 
        {
          program.ExpiredVariables().New( pispip );
          program.InvQtyBlockedVariables().New( pispip ); 
        }
        traverse( pispip, ProductInStockingPoint_MP.IncomingShelfLifeDay, islday ) 
        {    
          if ( pisp.IsOptMaturation() or pisp.IsOptShelfLife() ) 
          {
            traverse( pisp, OutgoingShelfLifeDay, oslday ) 
            {
              program.DependentDemandInPISPIPShelfLifeVariables().New( pispip, islday, oslday );
            }
          }
          if ( pisp.IsOptShelfLife() ) 
          {                            
            program.CumulativeDemandVariables().New( pispip, islday );
            program.CumulativeWasteVariables().New( pispip,islday );
            program.WasteVariables().New( pispip, islday );
            program.ExpiredForAgeVariables().New( pispip, islday ); 
            // >> split variables for balance 
         
            program.InvQtyShelfLifeVariables().New( pispip, islday );
            program.DemandSlackShelfLifeVariables().New( pispip, islday );  
          }
        }
      
        // Maturation variables
        if( pispip.ProductInStockingPoint_MP().IsOptMaturation() )
        {
          traverse( pispip, ProductInStockingPoint_MP.OutgoingShelfLifeDay, oslday ) 
          {
            program.CumulativeDemandMaturationVariables().New( pispip, oslday ); 
            program.MaturationSlackVariables().New( pispip, oslday ); 
          }
        }
      }
    }
    
    traverse( leafpispipsinrun, Elements, pispip ) 
    {
      // DependentDemandInPISPIP variable UoM: PISP
      program.DependentDemandInPISPIPVariables().New( pispip );   // Total dependent demand quantity in pispip
      // DemandSlack variable UoM: PISP
      program.DemandSlackVariables().New( pispip );     // Total decreased quantity of dependent demand to make the balance constraint feasible
          
      if( pispip.MustInitializeUserSupplyConstraint( runcontext, scope ) )
      {
        // UserTotalSupply slack variables UoM: PISP
        program.UserTotalSupplyUnderVariables().New( pispip );
        program.UserTotalSupplyOverVariables().New( pispip );
      }
      
      // Inventory quantity
      // InvQty variable UoM: PISP
      varinvqty := program.InvQtyVariables().New( pispip );
    
      // If this pispip allows negative inventory then invqty and unallocqty are allowed to be negative
      if( pispip.ProductInStockingPoint_MP().IsNegativeInventoryAllowed() )
      {
        lowerbound := -Real::MaxReal();
        this.FreezeVariableLowerBound( varinvqty, lowerbound );
        
        // Positive inventory quantity
        program.PosInvQtyVariables().New( pispip );
      }
    }
    
    // declare PosInvQtyPastLast in case we need inventory holding costs
    if ( runcontext.IsMetaIteration() and this.GetInitializeFinancialConstraints( runcontext ) ) 
    {
      traverse( pispsinrun, Elements, pisp ) 
      {
        last := pisp.LatestPISPIPInScope(); 
        current := guard( last.Next().astype(  ProductInStockingPointInPeriodPlanningLeaf ), null(  ProductInStockingPointInPeriodPlanningLeaf ) ); 
        
        while ( not isnull( current ) ) 
        {
          program.PosInvQtyPastLastVariables().New( current ); 
          current := current.NextPlanningPISPIP().astype(  ProductInStockingPointInPeriodPlanningLeaf ); 
        }
      }
    }
    
    //Only plan demand for selected PISPIP(s) during smart planning if requested by user.
    if( runcontext.IsSmartPlan()
        and this.IsOnlyPlanDemandForSmartPlanPISPIPs() )
    {
      castedpispipsmartplan := construct( ProductInStockingPointInPeriodPlanningLeafs, constcontent ) ;
      traverse( pispipssmartplan, Elements.astype( ProductInStockingPointInPeriodPlanningLeaf ), p ) 
      {
        castedpispipsmartplan.Add( p ); 
      }
      pispipnotselected := leafpispipsinrun.Difference( castedpispipsmartplan ); 
    
      //First bound all input pispips sales demand variables.
      traverse( pispipnotselected, Elements, pispip ) 
      {
        traverse( pispip, PlanningBaseSalesDemandInPeriodForOptimization, sd )
        {      
          var := null( MPVariable );
          if( sd.istype( LeafSalesDemandInPeriod ) )
          {
            var := program.SalesDemandQtyVariables().Get( sd.astype( LeafSalesDemandInPeriod ) ); //pispipsmartplan elements should be a subset of the direct optimizer PISPIP relation.
          }
          else if( sd.istype( DisaggregatedSalesDemandInPeriod ) )
          {
            var := program.DisaggregatedSalesDemandQtyVariables().Get( sd.astype( DisaggregatedSalesDemandInPeriod ) ); //pispipsmartplan elements should be a subset of the direct optimizer PISPIP relation.
          }
          if( not isnull( var ) )
          {
            lowerbound := sd.FulfilledQuantity();
            upperbound := sd.FulfilledQuantity();    
            this.FreezeVariableLowerUpperBound( var, lowerbound, upperbound );
          }
          // Freeze delayed sales demand variables
          sd.FreezeVariablesForPostponedSalesDemands( this, program, scope ); 
        }
      }
    }    
    
    // Create slack variables for the inventory specification constraints
    // These specifications can also be on high level products, so we cannot use the regular traverse over the PISPIPsInOptimizerRun
    leafpispips := null( ProductInStockingPointInPeriodPlannings, constcontent, owning );
    pispips := this.GetPISPIPsForInventorySpecifications( scope, false, &leafpispips );
    
    traverse( leafpispips, Elements, pispip, pispip.GetHasTargetInventory() ) // in case of meta optimizer call these do not include pispips prior to the scope (all in scope)
    {
      // TargetInvQty variable UoM: PISP
      vartarget := program.TargetInvQtyVariables().New( pispip );
      if( pispip.ProductInStockingPoint_MP().IsNegativeInventoryAllowed() ) // note carriedfwd inventory constraints states TargetInvQty < = InvQty at a pispip
      {
        vartarget.LowerBound( Real::MinReal() ); 
      }
    }
    
    optscopestart := runcontext.FirstPeriod_MP().Start(); 
    traverse( pispips, Elements, pispip )
    {
      hastargetinventory := pispip.GetHasTargetInventory();
      hasminlevel := pispip.GetHasMinLevel(); 
      hasmaxlevel := pispip.GetHasMaxLevel(); 
      hasspec := hasmaxlevel or hasminlevel or hastargetinventory; 
      if ( hasspec and pispip.Start() < optscopestart ) 
      {
        program.InvQtySpecPriorToHorizonSlackVariables().New( pispip ); // slack variable ( e.g. feedback makes we cannot limit violation prior to the horizon by a hard constraint) 
      }
      if( hastargetinventory )
      {
        // TargetInvQtyUnder variable UoM: PISP
        program.InvQtyUnderTargetVariables().New( pispip );    // Penalty of not reaching the target
      }
    
      if( hasminlevel )
      {
        // MinInvQtyUnder variable UoM: PISP
        program.MinInvQtyUnderVariables().New( pispip );       // Penalty for not reaching the minimum inventory level
      }
      
      if( hasmaxlevel )
      {
        // MaxInvQtyOver variable UoM: PISP
        program.MaxInvQtyOverVariables().New( pispip );        // Penalty for not reaching the maximum inventory level
      }
    }
    
    pispips := this.GetPISPIPsForDemandFulfillment( scope, pispipsinrun );
    traverse( pispips, Elements, pispip )
    {
      // DemandFulfillmentInPISPIP variable UoM: PISP
      program.DemandFulfillmentInPISPIPVariables().New( pispip );  // Total demand fulfillment quantity   
    }
    
    // Define the servicelevel variables for each servicelevel that that has at least one sdip on a pispip in this optimizer run
    traverse( this.MacroPlan(), AllServiceLevelBase, sl, sl.GetIsInOptimizerRun( scope ) )
    {
      // If this is a fulfillment goal related service level then add the related variables
      if( sl.IsUsedForPlanningFulfillmentSystem() )
      {
        program.FulfillmentTargetVariables().New( sl );
      }
      else if( sl.IsUsedForSafetyStockCalculation() )
      {
        program.ServiceLevelQtyVariables().New( sl );
      }
    }
    
    if( runcontext.UseInventoryMixBalancing() )
    {
      allperiods := construct( Period_MPs, constcontent ); 
      productcategories := Product_MP::GetProductCategoriesForOptimization( this.MacroPlan(), scope );
          
      traverse( productcategories, Elements, productcategory )
      {   
        // Retrieve all pispips that are part of this product category and that are part of the optimizer run
        pispips := selectset( productcategory.GetLeavesConst(), 
                              Elements.ProductInStockingPoint_MP.ProductInStockingPointInPeriodPlanning, 
                              pispip,
                              scope.Contains( pispip.PISPIPInOptimizerRun() ) ) 
                              // we can avoid scope.Contains(  pispip.PISPIPInOptimizerRun() ) ) because we are in the same transaction
        // Select all periods related to these pispips
        periods := selectset( pispips, Elements.Period_MP, period, true );
      
        traverse( periods, Elements, period )
        {     
          allperiods.Add( period ); 
          // Create the variables per product category and period which are used to balance the inventory mix within a category in a period
          varmax := program.MaxInvInDemandDaysVariables().New( productcategory, period );
          varmin := program.MinInvInDemandDaysVariables().New( productcategory, period );
          varmax.LowerBound( Real::MinReal() );
          varmin.LowerBound( Real::MinReal() );
          program.DifferenceInDemandDaysVariables().New( productcategory, period );
        }
      }
      allperiods := allperiods.Unique();   
    
      traverse( allperiods, Elements, period )   
      {
         // Create the min and max inventory in days variables which are used to balance the inventory quantities per product category
         // Create the overall variables that are used in the balancing of the categories
         overallmaxvar := program.OverallMaxInvInDemandDaysVariables().New( period );
         overallmaxvar.LowerBound( Real::MinReal() );
         overallminvar := program.OverallMinInvInDemandDaysVariables().New( period );
         overallminvar.LowerBound( Real::MinReal() );
         program.OverallDiffInvInDemandDaysVariables().New( period );
      }
    }
    
    // for debugging to fix a pispip fulfilled sales demand
    /* 
    if( runcontext.IsMetaIteration() )
    {
      //First bound all input pispips sales demand variables.
      traverse( leafpispipsinrun, Elements, pispip) 
      {
        detected :=  pispip.ProductInStockingPoint_MP().Product_MP().Name() = 'FG-2-24-110-4614-12-0' 
                     and pispip.StockingPointInPeriod().StockingPoint_MP().Name() = '624790' 
                     and pispip.Start().Date() = Date::Construct( 2020, 7, 1 ); 
    
        if ( detected ) 
        {
          traverse( pispip, PlanningBaseSalesDemandInPeriodForOptimization, sd )
          {      
            var := null( MPVariable );
            if( sd.istype( LeafSalesDemandInPeriod ) )
            {
              var := program.SalesDemandQtyVariables().Get( sd.astype( LeafSalesDemandInPeriod ) ); //pispipsmartplan elements should be a subset of the direct optimizer PISPIP relation.
            }
            else if( sd.istype( DisaggregatedSalesDemandInPeriod ) )
            {
              var := program.DisaggregatedSalesDemandQtyVariables().Get( sd.astype( DisaggregatedSalesDemandInPeriod ) ); //pispipsmartplan elements should be a subset of the direct optimizer PISPIP relation.
            }
            if( not isnull( var ) )
            {
              lowerbound := ifexpr( detected, sd.Quantity(), sd.FulfilledQuantity() );
              upperbound := sd.Quantity();    
              this.FreezeVariableLowerUpperBound( var, lowerbound, upperbound );
              if( detected ) 
              {
                debuginfo(  'FIXING sales demand - DEBUGGING', pispip.ProductInStockingPoint_MP().Name(), pispip.Start(), ' to ', sd.Quantity() ); 
              }
            }
          }
        }
      }
    }
  *]
  InterfaceProperties { Accessibility: 'Module' }
}