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