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
Quintiq file version 2.0
#parent: #root
Method InitConstraintsForShelfLifeSumConstraints (
  CapacityPlanningSuboptimizer_CapacityPlanningAlgorithm program,
  const constcontent ProductInStockingPointInPeriods smartplanpispips,
  constcontent ProductInStockingPoint_MPs intermediatepisps,
  const ProductInStockingPointInPeriodPlanning pispip,
  DateTime firstpispipstart,
  DateTime lastpispipstart,
  const LibOpt_Scope scope,
  Real scalefactor_mass,
  Real scalefactor_rhs_constr
) const
{
  Description: 'sum constraints split variables'
  TextBody:
  [*
    // >> sum of split InvQty = InvQty 
    {
      constr := program.InvQtyShelfLifeSumConstraints().New( pispip ); 
      if ( scope.Contains( pispip.PISPIPInOptimizerRun() ) ) 
      {
        constr.NewTerm( -1.0 * scalefactor_mass, program.InvQtyVariables().Get( pispip ) ); 
      }
      else
      {
        constr.RHSValue( pispip.InventoryLevelEnd() * scalefactor_rhs_constr ); // optimizer itself figure out what has expired - so just sum to the total inventory quantity. 
      }
      traverse( pispip, ProductInStockingPoint_MP.IncomingShelfLifeDay, islday )  
      {
        constr.NewTerm( 1.0 * scalefactor_mass, program.InvQtyShelfLifeVariables().Get( pispip, islday ) ); 
      }
    }
    
    // sum of split dependent demand = dependent demand 
    {
      constr := program.DependentDemandInPISPIPShelfLifeSumConstraints().New( pispip );
      if ( scope.Contains( pispip.PISPIPInOptimizerRun() ) ) 
      {
        constr.NewTerm( -1.0 * scalefactor_mass, program.DependentDemandInPISPIPVariables().Get( pispip ) ); 
      }
      else
      {
        constr.RHSValue( scalefactor_rhs_constr * pispip.DependentDemandQuantity() ); 
      }
      traverse( pispip, ProductInStockingPoint_MP.IncomingShelfLifeDay, islday )  
      {
        traverse( pispip, ProductInStockingPoint_MP.OutgoingShelfLifeDay, oslday ) 
        {
          constr.NewTerm( 1.0 * scalefactor_mass, program.DependentDemandInPISPIPShelfLifeVariables().Get( pispip, islday, oslday ) ); 
        }
      }
    }
    // sum of split expired = expired on pispip
    {
      constr := program.ExpiredConstraints().New( pispip ); 
      constr.NewTerm( -1.0 * scalefactor_mass, program.ExpiredVariables().Get( pispip ) ); 
      traverse( pispip, ProductInStockingPoint_MP.IncomingShelfLifeDay, islday ) 
      {
        constr.NewTerm( 1.0 * scalefactor_mass, program.ExpiredForAgeVariables().Get( pispip, islday ) ); 
      }
    }
    
    
    traverse( pispip.GetLeafSalesDemandInPeriod(), Elements, lsdip, not lsdip.IsPostponed() )
    {
      constr := program.SalesDemandQtyShelfLifeSumConstraints().New( lsdip ); 
      if( scope.Contains( pispip.PISPIPInOptimizerRun() ) )  
      {
        constr.NewTerm( -1.0 * scalefactor_mass, program.SalesDemandQtyVariables().Get( lsdip ) ); 
      }
      else
      {
        constr.RHSValue( lsdip.FulfilledQuantity() * scalefactor_rhs_constr ); 
      }
      traverse( lsdip, ProductInStockingPoint_MP.IncomingShelfLifeDay, islday ) 
      {
        constr.NewTerm( 1.0 * scalefactor_mass, program.SalesDemandShelfLifeQtyVariables().Get( lsdip, islday ) ); 
      }
      
      if( lsdip.CanBePostponed() and lsdip.NeedsToBePlanned() )
      {
        nextpispip := lsdip.AsPlanningBaseSalesDemandInPeriod().NextPlanningPISPIP();
        minpostponement := guard( lsdip.SDIPBeforeScopeInRun().OptMinPostponementPeriod(), 1 ); // if object null, then no min postponement 
        for( i := 2; i <= minpostponement and not isnull( nextpispip ); i++ )
        {
          nextpispip := nextpispip.NextPlanningPISPIP();
        }
        
        for( i := minpostponement;
             i <= lsdip.MaxPostponementPeriod()                                           // within the maximum number of postponement periods
             and not isnull( nextpispip );                                          // the next pispip exists
             i++ )
        {
          constrsum := program.DelayedSalesDemandQtyShelfLifeSumConstraints().New( lsdip, nextpispip.Period_MP() );
          if ( scope.Contains(  nextpispip.PISPIPInOptimizerRun() ) ) 
          // can avoid scope.Contains(  nextpispip.PISPIPInOptimizerRun() ) ) because we are in the same transaction
          {
            constrsum.NewTerm( -1.0 * scalefactor_mass, program.DelayedSalesDemandQtyVariables().Get( lsdip, nextpispip.Period_MP() ) );
          }
          else
          {
            postponedty := sum( lsdip, PostponedSalesDemand, psd, psd.ProductInStockingPointInPeriodPlanning() = nextpispip, psd.QuantityToPlan() ); 
            constrsum.RHSValue( postponedty * scalefactor_rhs_constr ); 
          }
          traverse( lsdip, ProductInStockingPoint_MP.IncomingShelfLifeDay, islday ) 
          {
            splitvar := program.DelayedSalesDemandShelfLifeQtyVariables().Get( lsdip, nextpispip.Period_MP(), islday );
            constrsum.NewTerm( 1.0 * scalefactor_mass, splitvar ); 
          }
          nextpispip := nextpispip.NextPlanningPISPIP();
        }
      } 
    }
    
    traverse( pispip.GetDisaggregatedSalesDemandInPeriod(), Elements, dsdip )
    {
      constr := program.DisAggregatedSalesDemandQtyShelfLifeSumConstraints().New( dsdip ); 
      if ( scope.Contains( pispip.PISPIPInOptimizerRun() ) ) 
      // can avoid scope.Contains( pispip.PISPIPInOptimizerRun() ) ) because we are in the same transaction
      {
        constr.NewTerm( -1.0 *scalefactor_mass, program.DisaggregatedSalesDemandQtyVariables().Get( dsdip ) ); 
      }
      else
      {
        constr.RHSValue( dsdip.FulfilledQuantity() * scalefactor_rhs_constr ); 
      }
      traverse( dsdip, ProductInStockingPoint_MP.IncomingShelfLifeDay, islday ) 
      {
        program.DisaggregatedSalesDemandShelfLifeQtyVariables().Get( dsdip, islday );
      }
      
      if ( dsdip.CanBePostponed() and dsdip.NeedsToBePlanned() ) 
      {
    
        nextperiod := dsdip.AsSalesDemandInPeriodBase().Period_MP().NextPlanningPeriod();  // It is not possible to traverse PISPIP from disaggregated but not from Aggregated.
        minpostponement := guard( dsdip.AggregatedSalesDemandInPeriod().SDIPBeforeScopeInRun().OptMinPostponementPeriod(), 1 ); // if object null, then no min postponement 
        for( i := 2; i <= minpostponement and not isnull( nextperiod ); i++ )
        {
          nextperiod := nextperiod.NextPlanningPeriod();
        }
        
        for( i := minpostponement;
             i <= dsdip.MaxPostponementPeriod()                                           // within the maximum number of postponement periods
             and not isnull( nextperiod );                                          // the next pispip exists
             i++ )
        {
          constrsum := program.DelayedDisaggregatedSalesDemandQtyShelfLifeSumConstraints().New( dsdip, nextperiod ); 
          var := program.DelayedDisaggregatedSalesDemandQtyVariables().Get( dsdip, nextperiod ); 
          if ( not isnull( var ) ) // if we are in the optimizer scope we need to use te var other obtain from model 
          {
            constrsum.NewTerm( -1.0 * scalefactor_mass, var ); 
          }
          else
          {
            postponedqty := sum( dsdip, PostponedSalesDemand, psd, psd.ProductInStockingPointInPeriodPlanning().Period_MP() = nextperiod, psd.QuantityToPlan() );  
            constrsum.RHSValue( postponedqty * scalefactor_rhs_constr ); 
          }
          traverse( dsdip, ProductInStockingPoint_MP.IncomingShelfLifeDay, islday ) 
          {
            constrsum.NewTerm( 1.0 * scalefactor_mass, program.DelayedDisaggregatedSalesDemandShelfLifeQtyVariables().Get( dsdip, nextperiod, islday ) ); 
          }
          nextperiod := nextperiod.NextPlanningPeriod();
        }
      }
    }
  *]
  InterfaceProperties { Accessibility: 'Module' }
}