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
Quintiq file version 2.0
#parent: #root
Method ReExecuteOptimizerSlidingWindows (
  CapacityPlanningSuboptimizer_CapacityPlanningAlgorithm program,
  const RunContextForCapacityPlanning runcontext,
  LibOpt_Task task
)
{
  TextBody:
  [*
    // Unfreeze the slack variables
    // Since we will change some variables from continuous to integer, it could be that the slacks from the previous run are insufficient
    isunfreeze := true;
    scope := task.Scope(); 
    this.FreezeZeroSlack( program, isunfreeze, runcontext, scope  );
    
    periodslastwindow := selectset( scope.GetPeriodInSlidingWindow(), Elements, period, true );
    
    // Select the first period in the next window
    // From the first period in the previous window we move NumberOfPeriodsPerSlide forward
    // Each period that we move forward is added to the set of newly frozen periods
    firstperiod := minselect( periodslastwindow, Elements, period, true, period.Start() );
    
    newfrozenperiods := construct( Period_MPs );
    for( i:= 1;
         i <= runcontext.NumberOfPeriodsPerSlide()
         and not isnull( firstperiod.NextPlanningPeriod() );
         i++ )
    {
      newfrozenperiods.Add( firstperiod );
      firstperiod := firstperiod.NextPlanningPeriod();
    }
    
    // Remove from scope the periods that are no longer in the active window
    traverse( newfrozenperiods, Elements, period, period.GetIsInSlidingWindow( scope ) )
    {
      period.PeriodInSlidingWindow().Delete(); 
    }
    
    // Add periods in the new window to the scope and select them
    this.SetSlidingWindowPeriods( firstperiod, runcontext, scope );
    periodsnextwindow := selectset( scope.GetPeriodInSlidingWindow(), Elements, period, true );
    
    // Freeze the binary/integer variables that are in the newly frozen periods
    // First freeze the semicontinous PTQty variable
    if( runcontext.UseProcessMinimumQuantity() or runcontext.UseLotSize() )
    {
      traverse( newfrozenperiods, Elements, period,
                period.IsWithinLotSizeHorizon() )
      {
        traverse( period, UnitPeriod, unitperiod,
                 scope.Contains( unitperiod.UnitPeriodInOptimizerRun() ) )
        {
          operations := this.GetOperationsForUnitPeriod( scope, unitperiod );
          traverse( operations, Elements, operation )
          {
            // Freeze the period task operation minimum process quantity variables
            if( runcontext.UseProcessMinimumQuantity()
                and operation.HasMinimumQuantity() )
            {
              ptvar := program.PTQtyVariables().Get( operation, period ); 
              optimalvalue := this.GetOptimalValue( ptvar );
              this.FreezeVariableWithSlack( ptvar, optimalvalue );
            }
            
            // Freeze the period task operation lotsize variables
            if( runcontext.UseLotSize()
                and operation.HasLotSize() )
            {
              ptnroflotsvar := program.PTNrOfLotsVariables().Get( operation, period );
              optimalvalue := this.GetOptimalValue( ptnroflotsvar );
              this.FreezeVariableWithSlack( ptnroflotsvar, optimalvalue );
            }
            
            // Freeze the operation input lot size variables
            if( runcontext.UseLotSize()
                and operation.HasInputLotSize() )
            {
              periodsfordd := construct( Period_MPs, constcontent ); // periods for dependent demand
            
              // Consider all relevant periods for the dependent demand
              CapacityPlanningSuboptimizer::GetOperationDependentDemandPeriods( period, operation, &periodsfordd, this.GetPeriodsFromPeriodTaskOperation() ); // period itself may be included twice 
              periodsfordd := periodsfordd.Unique();
              
              traverse( periodsfordd, Elements, ddperiod,
                        period.IsWithinLotSizeHorizon() )
              { 
                // Freeze the operation input lot size variables
                traverse( operation, 
                          OperationInput, 
                          input,
                          input.ProductInStockingPoint_MP().HasInputLotSize() 
                          and ( input.HasRegularProductforOptimizer() or input.GetIsProductInOptimizerRun( runcontext.IsPostProcessing() ) ) ) 
                {
                  operationinputnroflotsvar := program.OperationInputNrOfLotsVariables().Get( input, ddperiod );
                  optimalvalue := this.GetOptimalValue( operationinputnroflotsvar );
                  this.FreezeVariableWithSlack( operationinputnroflotsvar, optimalvalue );
                }
              }
            }
          }
        }
        // Freeze the trip lotsize variables
        if( runcontext.UseLotSize() )
        {
          traverse( period, UnitPeriod.astype( UnitPeriodTransportBase ), unitperiod,
                    unitperiod.HasLotSize() )
          {
            traverse( unitperiod, AsDepartureUnitPeriod, trip,
                      scope.Contains(  trip.TripInOptimizerRun() ) )
            {
              // TripNrOfLots variable UoM: Number
              tripnroflotsvar := program.TripNrOfLotsVariables().Get( trip );
              optimalvalue := this.GetOptimalValue( tripnroflotsvar );
              this.FreezeVariableWithSlack( tripnroflotsvar, optimalvalue );
            }
          }
        }
      }
    }
    
    remainingwindowperiods := periodsnextwindow.Intersect( periodslastwindow );
    newwindowperiods := periodsnextwindow.Difference( remainingwindowperiods );
    
    // Make set variables in the periods that are new in the sliding window to integer
    if( runcontext.UseProcessMinimumQuantity() or runcontext.UseLotSize() )
    {
      traverse( newwindowperiods, Elements, period,
                period.IsWithinLotSizeHorizon() )
      {
        traverse( period, UnitPeriod, unitperiod,
                 scope.Contains(  unitperiod.UnitPeriodInOptimizerRun() ) )
        {
          operations := this.GetOperationsForUnitPeriod( scope, unitperiod );
          traverse( operations, Elements, operation )
          {
            // Set the period task operation minimum process quantity variables that are new to the sliding window to semicontinous
            if( runcontext.UseProcessMinimumQuantity()
                and operation.HasMinimumQuantity() )
            {
              ptvar := program.PTQtyVariables().Get( operation, period );
              ptvar.VariableType( 'SemiContinuous' );
              lowerbound := operation.MinimumQuantity() / operation.QuantityToProcessFactor();   
              if ( lowerbound > 0 ) 
              {
                this.FreezeVariableLowerBound( ptvar, lowerbound );
              }
            }
            // Set the period task operation lotsize variables that are new to the sliding window to integer
            if( runcontext.UseLotSize()
                and operation.HasLotSize() )
            {
              ptnroflotsvar := program.PTNrOfLotsVariables().Get( operation, period );
              ptnroflotsvar.VariableType( 'Integer' );
            }
            
            // Set the operation input lot size variables that are new to the sliding window to integer
            if( runcontext.UseLotSize()
                and operation.HasInputLotSize() )
            {
              periodsfordd := construct( Period_MPs, constcontent ); // periods for dependent demand
            
              // Consider all relevant periods for the dependent demand
              CapacityPlanningSuboptimizer::GetOperationDependentDemandPeriods( period, operation, &periodsfordd, this.GetPeriodsFromPeriodTaskOperation() ); // period may be added twice
              periodsfordd := periodsfordd.Unique(); 
                    
              traverse( periodsfordd, Elements, ddperiod,
                        period.IsWithinLotSizeHorizon() )
              {
                traverse( operation, OperationInput, input,
                          input.GetIsProductInOptimizerRun( runcontext.IsPostProcessing() )
                          and input.ProductInStockingPoint_MP().HasInputLotSize() )
                {
                  operationinputnroflotsvar := program.OperationInputNrOfLotsVariables().Get( input, ddperiod );
                  operationinputnroflotsvar.VariableType( 'Integer' );
                }
              }
            }
          }
        }
        
        // Set the trip lotsize variables that are new to the sliding window to integer
        if( runcontext.UseLotSize() )
        {
          traverse( period, UnitPeriod.astype( UnitPeriodTransportBase ), unitperiod,
                    unitperiod.HasLotSize() )
          {
            traverse( unitperiod, AsDepartureUnitPeriod, trip,
                      scope.Contains(  trip.TripInOptimizerRun() ) )
            {
              // TripNrOfLots variable UoM: Number
              tripnroflotsvar := program.TripNrOfLotsVariables().Get( trip );
              tripnroflotsvar.VariableType( 'Integer' );
            }
          }
        }
      }
    }
    
    // Add the new window periods to the algorithmrun
    traverse( newwindowperiods, Elements, period )
    {
      scope.Add( period.PeriodInSlidingWindow( relnew ) );  
    }
    
    // reset lower bounds on goal variables for the next window
    traverse( this, MacroPlan.StrategyMacroPlan.StrategyLevelMacroPlan, level ) 
    {
      var := program.GoalForLevelVariables().Get( level ); 
      var.LowerBound( Real::MinReal() ); 
    }
    // Remove the active goals
    traverse( program.Goal().Terms(), Elements, term )
    {
      term.Coefficient( 0.0 );
    }
    
    
    
    // Reexecute the algorithm and start from the first level and reactivate the level 1 goals
    this.ReExecute( program );
    this.CreateSubOptimizerLevels( runcontext, task ); 
    this.ActivateGoals( program, runcontext, scope );
    
    info( "----------- Sliding the lotsize window -----------" );
    info( "-------- New window starts at ", minselect( periodsnextwindow, Elements, period, true, period.Start() ).Start() );
    info( "------------- Reexecuting algorithm --------------" );
  *]
  InterfaceProperties { Accessibility: 'Module' }
}