| 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' } | 
| } |