admin
2025-01-22 7e31442f0e9b07764e9c6a9680d3d4aeba5fe1de
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
Quintiq file version 2.0
#parent: #root
Method CapacityPlanningAlgorithmHandleFeasibleInventoryOptimization (
  RunContextForCapacityPlanning runcontext,
  LibOpt_Scope scope
)
{
  Description: 'Handle feasible related to inventory optimization'
  TextBody:
  [*
    // We should stop the run if the time limit is exceeded (unless we should stop when the current iteration is finished) or if the user has stopped the run
    macroplan := this.MacroPlan();
    algorithminventory := macroplan.DEPRECATED_InventoryOptimization(); 
    iteration := algorithminventory.LastIteration();
    istimelimitexceeded := DateTime::ActualTime() > algorithminventory.StartInventoryOptimization() + algorithminventory.TimeLimitInventoryOptimization()
                           and not iteration.IsStopAfterThisIteration();
    isstoprun := algorithminventory.IsStopInventoryOptimization() or istimelimitexceeded;
    
    // If this was the rerun with the frozen variables
    // then save the results in the AlgorithmSimulation object
        
    if( runcontext.IsFrozenRun() )
    {
      // Update the simulation results 
      simulation := Simulation_MP::Create( algorithminventory.LastIteration() );
      simulation.SaveSimulationResults();
      islastsimulation := iteration.Simulation( relsize ) = algorithminventory.NumberOfSimulations();
      if( islastsimulation )
      {
        // If this was the last simulation of this iteration, then evaluate the iteration
        iteration.Evaluate();
      }
      // If this is the last simulation of this iteration and we should stop after this iteration
      // then we should stop this run
      isstoprun := isstoprun 
                   or ( islastsimulation and iteration.IsStopAfterThisIteration() );
      // Rerun the optimizer if we should not stop
      if( not isstoprun )
      {
        smartplanpispips := scope.GetSmartPlanPISPIPsInOptimizerRun(); 
        
        periodstart := this.MacroPlan().StartOfPlanningPeriod();
        periodend := algorithminventory.GetPeriodEndInventoryOptimization( periodstart );
        
        // Reset the plan and the sales demands before we start the next simulation
        // Reset the sales demands to their original quantities
        traverse( this, MacroPlan.SalesDemand, sd )
        {
          sd.ResetToExpectedQuantity();
          traverse( sd, SalesDemandInPeriod, sdip )
          {
            sdip.OptimizerQuantity( 0.0 );
          }
        }
        macroplan.ResetPeriodTasksAndResetKPI( false, true, null( OptimizerPuzzle ) );
        //cmt := 'check with martijn 2 attributes below set to false'; 
        macroplan->RunOptimizer( runcontext.IsSmartPlan(), 
                                 runcontext.IsSmartPlanForPeriodTask(), 
                                 runcontext.HasTotalSupplyUser(), 
                                 runcontext.TotalSupplyUser(), 
                                 smartplanpispips, 
                                 false, 
                                 false, 
                                 false, 
                                 false,   // we do not run the scaling algorithm on a Smart plan run
                                 true, 
                                 null( Strategy ), // re-using previous strategy
                                 this.MacroPlan().GetLastAlgorithmRun().HasOverwrittenManualPlanning(),
                                 periodstart, 
                                 periodend,
                                 runcontext.IsInventoryOptimizationRun(), 
                                 false, 
                                 null( Units ), 
                                 null( Process_MP ),
                                 this.MacroPlan().LastUsedOptimizerPuzzle() );
      }
      // Otherwise finalize the inventory optimization
      else
      {
        // Finalize the inventory optimization
        algorithminventory.FinalizeInventoryOptimization(); 
      }
    }
    // Else, if we should stop the run
    else if( isstoprun ) 
    {
      // Then finalize the inventory optimization
      algorithminventory.FinalizeInventoryOptimization();
    }
    // Else if this is not yet the last period in the shrinking horizon approach
    else if( runcontext.FirstPeriod_MP() <> this.MacroPlan().LastBasePeriod() )
    {
      // Rerun the optimizer for the shrinking horizon approach
      macroplan->ReRunOptimizerWithShrinkingHorizon( runcontext.IsSmartPlan(),
                                                     scope.GetSmartPlanPISPIPsInOptimizerRun(), 
                                                     runcontext.HasOverwrittenManualPlanning(),
                                                     runcontext.FirstPeriod_MP(),
                                                     runcontext.LastPeriod_MP(),
                                                     false, 
                                                     null( Units ),
                                                     null( Process_MP ));
    }
    // Else, this was the last step of the current simulation
    // and we should freeze the variables and rerun the optimizer to evaluate the KPIs over the complete horizon
    else
    {
      // Else we freeze the decisions and rerun the optimizer
      macroplan.GetLastAlgorithmRun().IsFrozenRun( true ); 
      macroplan->ReRunOptimizerWithShrinkingHorizon( runcontext.IsSmartPlan(),
                                                     scope.GetSmartPlanPISPIPsInOptimizerRun(), 
                                                     runcontext.HasOverwrittenManualPlanning(),
                                                     runcontext.FirstPeriod_MP(),
                                                     runcontext.LastPeriod_MP(),
                                                     false, 
                                                     null( Units ),
                                                     null( Process_MP ));
    }
  *]
  InterfaceProperties { Accessibility: 'Module' }
}