| Quintiq file version 2.0 | 
| #parent: #root | 
| Method InitConstraintsGoalsForServiceLevels ( | 
|   CapacityPlanningSuboptimizer_CapacityPlanningAlgorithm program, | 
|   const RunContextForCapacityPlanning runcontext, | 
|   const LibOpt_Scope scope | 
| ) const | 
| { | 
|   Description: 'Init constraints goals for service level' | 
|   TextBody: | 
|   [* | 
|     collectvaluesmodel := runcontext.IsMetaIteration();  | 
|      | 
|     totalfulfillmentvariablesvalue := 0.0;  | 
|     scalefactor_ftvar := CapacityPlanningSuboptimizer::GetVariableScaleFactor( typeof( MPTotalFulfillmentTargetVariable ) ); | 
|      | 
|     // The service level constraint is defined for the deterministic goal (Fulfillment target) | 
|     // slconst constraint UoM: This is equal value for each service level now. | 
|     sldetconst := program.TotalFulfillmentTargetConstraints().New(); | 
|     sldetconst.Sense( '=' ); | 
|     sldetconst.RHSValue( this.ScaleConstraintRHS( typeofexpression( sldetconst ), 0.0 ) ); | 
|     // Term UoM: Monetary | 
|     sldetconst.NewTerm( 1.0 * this.ScaleConstraintTerm( typeof( MPTotalFulfillmentTargetVariable ), typeofexpression( sldetconst) ) | 
|                         , program.TotalFulfillmentTargetVariables().Get() ); | 
|      | 
|     scalefactor_servicelevelqty_slconst_det := this.ScaleConstraintTerm( typeof( MPFulfillmentTargetVariable ), typeofexpression( sldetconst ) ); | 
|      | 
|     // The service level constraint is also defined for the stoichastic goal (Inventory optimization) | 
|     // slconst constraint UoM: This is equal value for each service level now. | 
|     slstoconst := program.TotalServiceLevelConstraints().New(); | 
|     slstoconst.Sense( '=' ); | 
|     slstoconst.RHSValue( this.ScaleConstraintRHS( typeofexpression( slstoconst ), 0.0 ) ); | 
|     // Term UoM: Monetary | 
|     slstoconst.NewTerm( 1.0 * this.ScaleConstraintTerm( typeof( MPTotalServiceLevelVariable ), typeofexpression( slstoconst ) ) | 
|                         , program.TotalServiceLevelVariables().Get() ); | 
|      | 
|     scalefactor_servicelevelqty_slconst_sto := this.ScaleConstraintTerm( typeof( MPServiceLevelQtyVariable ), typeofexpression( slstoconst ) ); | 
|      | 
|     metaoptimizer_isdefined := ( runcontext.IsMetaIteration()  | 
|                                  and ( this.MacroPlan().OptimizerMetaEIS().ServiceLevelWeight() > 0  | 
|                                        or this.MacroPlan().OptimizerMetaEIS().FulfillmentTargetWeight() > 0  ) );  | 
|      | 
|     if ( not runcontext.IsMetaIteration() or metaoptimizer_isdefined )  | 
|     { | 
|       traverse( this, MacroPlan.AllServiceLevelBase, sl, sl.GetIsInOptimizerRun( scope ) ) | 
|       { | 
|         sl_optimizer_used  := ifexpr(  sl.IsUsedForPlanningFulfillmentSystem(),  | 
|                                          program.FulfillmentTargetFromSDQtyConstraints().Get( sl ).Enabled(),  | 
|                                          program.ServiceLevelFromSDQtyConstraints().Get( sl ).Enabled() );  | 
|        | 
|         if ( sl_optimizer_used )  | 
|         {                                 | 
|           // If this is a fulfillment goal related service level then add the related constraints | 
|           if( sl.IsUsedForPlanningFulfillmentSystem() ) | 
|           { | 
|             // Term:      -1        *   ServiceLevelQty variable | 
|             // UoM:    [Default]    *        [Default] | 
|             sldetconst.NewTerm( -scalefactor_servicelevelqty_slconst_det, program.FulfillmentTargetVariables().Get( sl ) ); | 
|             if ( collectvaluesmodel )  | 
|             { | 
|               totalfulfillmentvariablesvalue := totalfulfillmentvariablesvalue +  | 
|               minvalue( sl.TotalFulfilledQuantity(), sl.TotalDemandQuantity() * sl.TargetPercentage() / 100 ) * scalefactor_servicelevelqty_slconst_det / scalefactor_ftvar; | 
|             } | 
|           } | 
|           // Else if this is an inventory optimization related service level, add the related constraints | 
|           else if( sl.IsUsedForSafetyStockCalculation() ) | 
|           { | 
|             // Term:      -1        *   ServiceLevelQty variable | 
|             // UoM:    [Default]    *        [Default]   | 
|             slstoconst.NewTerm( -scalefactor_servicelevelqty_slconst_sto, program.ServiceLevelQtyVariables().Get( sl ) ); | 
|           } | 
|         } | 
|       } | 
|     } | 
|      | 
|     // workaround because cannot write to attribute  | 
|     this.StoreValueInProgram( program, 'collect_values_model_MPTotalFulfillmentTargetVariable', totalfulfillmentvariablesvalue ); | 
|   *] | 
|   InterfaceProperties { Accessibility: 'Module' } | 
| } |