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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
Quintiq file version 2.0
#parent: #root
Method InitConstraintsForGoalsForLevelGoal (
  CapacityPlanningSuboptimizer_CapacityPlanningAlgorithm program,
  const RunContextForCapacityPlanning runcontext,
  const LibOpt_Scope scope
) const
{
  Description: 'Initialize constraints for calculating goal value for each level'
  TextBody:
  [*
    traverse( this, MacroPlan.StrategyMacroPlan.StrategyLevelMacroPlan, level ) 
    { 
      // below we define the single variable that is goal to consider during level 'level' by adding goal terms to the defining constraint 'const'
      levelgoalvar := program.GoalForLevelVariables().Get( level );  
    
      const := program.GoalConstraints().New(  level );  
      const.NewTerm( -1.0, levelgoalvar ); 
      const.Sense( '=' );    
      
      // workaround because cannot write to attribute on level - 1.0 means true
      this.StoreValueInProgram( program, 'collect_values_model_SetCPLEXBoundFromPlan' + [String] level.Level(), 1.0 ); 
      this.StoreValueInProgram( program, 'collect_values_model_TotalBoundFromPlan' + [String] level.Level(), 0.0 ); 
      
      if ( runcontext.IsMetaIteration() 
           and level.Level() >= runcontext.WeightLevelNonFinancial().StockingPointCapacityLevel() 
           and runcontext.WeightLevelNonFinancial().StockingPointCapacityWeight() > 0 
           and runcontext.WeightLevelNonFinancial().StockingPointCapacityLevel() > 0 ) 
      {
        weight := this.GetRunContextMeta().OptionRestrictSPcapacityWeight(); 
        const.NewTerm( -1.0 * weight, program.TotalStockingPointCapacityMetaVariables().Get() ); // use (1-epsilon) max capacity limits. Keep supressing SP inventory
      }
    }
    
    weightlevels := this.GetRunContextConst().WeightLevelNonFinancial();  
      
      // non-financial kpi
    // Goal term for maximization = 1.0 * weight * variable scale factor for kpi / goal scaling factor
    
    // workaround because we cannot write to attribute  
    //>>
    value_collect_values_model_TotalBlendingViolation := 0.0; 
    value_collect_values_model_MPTotalFulfillmentVariables := 0.0;
    value_collect_values_model_MPTotalSalesDemandPriorityVariable := 0.0;
    value_collect_values_model_MPTotalPostponentPenaltyVariable := 0.0; 
    value_collect_values_model_MPTotalMaximumInventoryLevelVariable := 0.0;
    value_collect_values_model_MPTotalMinimumInventoryLevelVariable := 0.0;
    value_collect_values_model_MPTotalTargetInventoryLevelVariable := 0.0; 
    value_collect_values_model_MPTotalFulfillmentTargetVariable := 0.0; 
    value_collect_values_model_MPTotalUnitCapacityVariable := 0.0; 
    value_collect_values_model_MPTotalUnitSecondaryCapacityVariable := 0.0; 
    value_collect_values_models_MPTotalMinimumUnitCapacityVariable := 0.0; 
    value_collect_values_models_MPTotalMinimumUnitSecondaryCapacityVariable := 0.0; 
    value_collect_values_model_TotalLotSizeVariables := 0.0; 
    value_collect_values_model_TotalProcessMaximumQuantityVariables := 0.0; 
    value_collect_values_model_TotalStockingPointCapacityVariables := 0.0; 
    value_collect_values_model_TotalMaximumSupplyVariables := 0.0; 
    value_collect_values_model_TotalMinimumSupplyVariables := 0.0; 
    value_collect_values_model_TotalTargetSupplyVariables := 0.0; 
    value_collect_values_model_MPTotalCO2EmissionVariables := 0.0;
    value_collect_values_model_TotalShiftPatternChangesPenaltyVariable := 0.0;
    
    if ( runcontext.IsMetaIteration() ) 
    {
      if( runcontext.UseBlending() )
      {
        value_collect_values_model_TotalBlendingViolation := program.RetrieveReal( 'collectvalues_model_TotalBlendingViolation' ); 
      }
      value_collect_values_model_MPTotalFulfillmentVariables := program.RetrieveReal( 'collect_values_model_MPTotalFulfillmentVariables' ); 
      value_collect_values_model_MPTotalSalesDemandPriorityVariable := program.RetrieveReal( 'collect_values_model_MPTotalSalesDemandPriorityVariable' ); 
      value_collect_values_model_MPTotalPostponentPenaltyVariable := program.RetrieveReal( 'collect_values_model_MPTotalPostponementPenaltyVariables' ); 
      value_collect_values_model_MPTotalMaximumInventoryLevelVariable := program.RetrieveReal( 'collect_values_model_MPTotalMaximumInventoryLevelVariable' ); 
      value_collect_values_model_MPTotalMinimumInventoryLevelVariable := program.RetrieveReal( 'collect_values_model_MPTotalMinimumInventoryLevelVariable' ); 
      value_collect_values_model_MPTotalTargetInventoryLevelVariable := program.RetrieveReal( 'collect_values_model_MPTotalTargetInventoryLevelVariable' ); 
      value_collect_values_model_MPTotalFulfillmentTargetVariable := program.RetrieveReal( 'collect_values_model_MPTotalFulfillmentTargetVariable' ); 
      
      value_collect_values_model_MPTotalUnitCapacityVariable := program.RetrieveReal( 'collect_values_model_MPTotalUnitCapacityVariable' ); 
      value_collect_values_model_MPTotalUnitSecondaryCapacityVariable := program.RetrieveReal( 'collect_values_model_MPTotalUnitSecondaryCapacityVariable' ); 
      value_collect_values_models_MPTotalMinimumUnitCapacityVariable := program.RetrieveReal( 'collect_values_models_MPTotalMinimumUnitCapacityVariable' ); 
      value_collect_values_models_MPTotalMinimumUnitSecondaryCapacityVariable := program.RetrieveReal( 'collect_values_models_MPTotalMinimumUnitSecondaryCapacityVariable');
      
      value_collect_values_model_TotalLotSizeVariables := program.RetrieveReal( 'collect_values_model_TotalLotSizeVariables' ); 
      value_collect_values_model_TotalProcessMaximumQuantityVariables := program.RetrieveReal( 'collect_values_model_TotalProcessMaximumQuantityVariables' );
      
      value_collect_values_model_TotalStockingPointCapacityVariables := program.RetrieveReal( 'collect_values_model_TotalStockingPointCapacityVariables'); 
      
      value_collect_values_model_TotalMaximumSupplyVariables := program.RetrieveReal( 'collect_values_model_TotalMaximumSupplyVariables' ); 
      value_collect_values_model_TotalMinimumSupplyVariables := program.RetrieveReal( 'collect_values_model_TotalMinimumSupplyVariables' ); 
      value_collect_values_model_TotalTargetSupplyVariables := program.RetrieveReal( 'collect_values_model_TotalTargetSupplyVariables' ); 
      value_collect_values_model_MPTotalCO2EmissionVariables := program.RetrieveReal( 'collect_values_model_MPTotalCO2EmissionVariable' );
      
      if( runcontext.UseShiftOptimization() )
      {
        value_collect_values_model_TotalShiftPatternChangesPenaltyVariable := program.RetrieveReal( 'collect_values_model_MPTotalShiftPatternChangesPenaltyVariables' );
      }
    }
    //<<
    
    this.AddGoalToLevel( program, weightlevels.BlendingLevel(), weightlevels.BlendingWeight(),
                           -1.0, program.TotalBlendingVariables().Get(), true, value_collect_values_model_TotalBlendingViolation );
                           
    this.AddGoalToLevel( program, weightlevels.CampaignLevel(), weightlevels.CampaignWeight(),
                       -1.0, program.TotalCampaignVariables().Get(), false, 0.0 );
    
    
    
    this.AddGoalToLevel( program, weightlevels.FulfillmentLevel(), weightlevels.FulfillmentWeight(),
                       1.0, program.TotalFulfillmentVariables().Get(), true, value_collect_values_model_MPTotalFulfillmentVariables );
    
    this.AddGoalToLevel( program, weightlevels.FulfillmentTargetLevel(), weightlevels.FulfillmentTargetWeight(),
                       1.0, program.TotalFulfillmentTargetVariables().Get(), true, value_collect_values_model_MPTotalFulfillmentTargetVariable );
    
    this.AddGoalToLevel( program, weightlevels.LotSizeLevel(), weightlevels.LotSizeWeight(),
                       -1.0, program.TotalLotSizeVariables().Get(), true, value_collect_values_model_TotalLotSizeVariables );
    
    this.AddGoalToLevel( program, weightlevels.InventoryMixBalancingLevel(), weightlevels.InventoryMixBalancingWeight(),
                       -1.0, program.TotalInventoryMixBalancingVariables().Get(), false, 0.0 );
                 
    this.AddGoalToLevel( program, weightlevels.MaximumInventoryLevel(), weightlevels.MaximumInventoryLevelWeight(),
                       -1.0, program.TotalMaximumInventoryLevelVariables().Get(), true, value_collect_values_model_MPTotalMaximumInventoryLevelVariable );
    
    this.AddGoalToLevel( program, weightlevels.MinimumInventoryLevel(), weightlevels.MinimumInventoryLevelWeight(),
                       -1.0, program.TotalMinimumInventoryLevelVariables().Get(), true, value_collect_values_model_MPTotalMinimumInventoryLevelVariable );
    
    this.AddGoalToLevel( program, weightlevels.MaximumSupplyLevel(), weightlevels.MaximumSupplyWeight(),
                       -1.0, program.TotalMaximumSupplyVariables().Get(), true, value_collect_values_model_TotalMaximumSupplyVariables );
    
    this.AddGoalToLevel( program, weightlevels.MinimumSupplyLevel(), weightlevels.MinimumSupplyWeight(),
                       -1.0, program.TotalMinimumSupplyVariables().Get(), true, value_collect_values_model_TotalMinimumSupplyVariables );
    
    this.AddGoalToLevel( program, weightlevels.MinimumUnitCapacityLevel(), weightlevels.MinimumUnitCapacityWeight(),
                       -1.0, program.TotalMinimumUnitCapacityVariables().Get(), true, value_collect_values_models_MPTotalMinimumUnitCapacityVariable );
    
    this.AddGoalToLevel( program, weightlevels.MinimumUnitCapacityLevel(), weightlevels.MinimumUnitCapacityWeight(),
                       -1.0, program.TotalMinimumUnitSecondaryCapacityVariables().Get(), true, value_collect_values_models_MPTotalMinimumUnitSecondaryCapacityVariable );
    
    this.AddGoalToLevel( program, weightlevels.PostponementPenaltyLevel(), weightlevels.PostponementPenaltyWeight(),
                       -1.0, program.TotalPostponementPenaltyVariables().Get(), true, value_collect_values_model_MPTotalPostponentPenaltyVariable );
    
    this.AddGoalToLevel( program, weightlevels.ProcessMaximumQuantityLevel(), weightlevels.ProcessMaximumQuantityWeight(),
                       -1.0, program.TotalProcessMaximumQuantityVariables().Get(), true, value_collect_values_model_TotalProcessMaximumQuantityVariables );
    
    this.AddGoalToLevel( program, weightlevels.SalesDemandPriorityLevel(), weightlevels.SalesDemandPriorityWeight(),
                       1.0, program.TotalSalesDemandPriorityVariables().Get(), true, value_collect_values_model_MPTotalSalesDemandPriorityVariable );
    
    this.AddGoalToLevel( program, weightlevels.ServiceLevelLevel(), weightlevels.ServiceLevelWeight(),
                       1.0, program.TotalServiceLevelVariables().Get(), false, 0.0 );
    
    this.AddGoalToLevel( program, weightlevels.SlackLevel(), weightlevels.SlackWeight(),
                       -1.0, program.TotalSlackVariables().Get(), false, 0.0 );
    
    this.AddGoalToLevel( program, weightlevels.StockingPointCapacityLevel(), weightlevels.StockingPointCapacityWeight(),
                       -1.0, program.TotalStockingPointCapacityVariables().Get(), true, value_collect_values_model_TotalStockingPointCapacityVariables );
    
    this.AddGoalToLevel( program, weightlevels.SupplyTargetLevel(), weightlevels.SupplyTargetWeight(),
                       -1.0, program.TotalSupplyTargetVariables().Get(), true, value_collect_values_model_TotalTargetSupplyVariables );
    
    this.AddGoalToLevel( program, weightlevels.TargetInventoryLevel(), weightlevels.TargetInventoryLevelWeight(),
                       -1.0, program.TotalTargetInvLevelVariables().Get(), true, value_collect_values_model_MPTotalTargetInventoryLevelVariable );
    
    this.AddGoalToLevel( program, weightlevels.UnitCapacityLevel(), weightlevels.UnitCapacityWeight(),
                       -1.0, program.TotalUnitCapacityVariables().Get(), true, value_collect_values_model_MPTotalUnitCapacityVariable );
    
    this.AddGoalToLevel( program, weightlevels.UnitCapacityLevel(), weightlevels.UnitCapacityWeight(),
                       -1.0, program.TotalUnitSecondaryCapacityVariables().Get(), true, value_collect_values_model_MPTotalUnitSecondaryCapacityVariable );
    
    this.AddGoalToLevel( program, weightlevels.CapacitySmoothingLevel(), weightlevels.CapacitySmoothingWeight(),
                       -1.0, program.TotalCapacitySmoothingSlackVariables().Get(), false, 0.0 );
    
    this.AddGoalToLevel( program, weightlevels.ExpiredQtyLevel(), weightlevels.ExpiredQtyWeight(),
                       -1.0, program.TotalExpiredQtyVariables().Get(), false, 0.0 );
    
    this.AddGoalToLevel( program, weightlevels.CO2EmissionLevel(), weightlevels.CO2EmissionWeight(),
                       -1.0, program.TotalCO2EmissionVariables().Get(), true, value_collect_values_model_MPTotalCO2EmissionVariables );
    
    this.AddGoalToLevel( program, weightlevels.ShiftPatternChangesPenaltyLevel(), weightlevels.ShiftPatternChangesPenaltyWeight(),
                         -1.0, program.TotalShiftPatternChangesPenaltyVariables().Get(), true, value_collect_values_model_TotalShiftPatternChangesPenaltyVariable );
    
    // Accounts kpi
    traverse( scope.GetAccountsInOptimizerRunConst(), Elements, account ) 
    {
      factor := ifexpr( account.IsMaximize(), 1.0, -1.0 );
    
      traverse( account, AccountAssignment.AccountCostDriver, driver )
      {
        var := null( MPVariable );
                             
        if( driver.Name() = Translations::MP_AccountAssignmentCostDriverFixed() )                 // fixed
        {
          // we don't need fixed cost in the optimizer.
        }
        else if ( driver.Name() = Translations::MP_AccountAssignmentCostDriverInventorySupply() ) 
        {
          // cost driver inventory supply not applicable in optimizer                             // inventory supply
        }
        else if( driver.Name() = Translations::MP_AccountAssignmentCostDriverStaffing() )         // staffing
        {
          var := program.DriverStaffingVariables().Get( account, driver );
        }
        else if( driver.Name() = Translations::MP_AccountAssignmentCostDriverInventoryValue() )   // inventory value
        {
          var := program.DriverInventoryValueVariables().Get( account, driver );
        }
        else if( driver.Name() = Translations::MP_AccountAssignmentCostDriverInventoryHolding() ) // inventory holding
        {
          var := program.DriverInventoryHoldingVariables().Get( account, driver );
        }
        else if( driver.Name() = Translations::MP_AccountAssignmentCostDriverNrOfUnits() )        // nr of units
        {
          var := program.DriverNrOfUnitsVariables().Get( account, driver );
        }
        else if( driver.Name() = Translations::MP_AccountAssignmentCostDriverSales() )            // sales
        {
          var := program.DriverSalesVariables().Get( account, driver );
        }
        else if( driver.Name() = Translations::MP_AccountAssignmentCostDriverTime() )             // time
        {
          var := program.DriverTimeVariables().Get( account, driver );
        }
        else if( driver.Name() = Translations::MP_AccountAssignmentCostDriverVolume() )           // volume
        {
          var := program.DriverVolumeVariables().Get( account, driver );
        }
        else if( driver.Name() = Translations::MP_AccountAssignmentCostDriverLot() )              // Lot
        {
          var := program.DriverLotVariables().Get( account, driver );
        }
        else if( driver.Name() = Translations::MP_AccountAssignmentCostDriverChangeover() )        // Changeover
        {
          var := program.DriverChangeoverVariables().Get( account, driver );
        }
        else if ( driver.Name() = Translations::MP_AccountAssignmentCostDriverOneTime() )         // One-time
        {
          var := program.DriverOneTimeVariables().Get( account, driver ); 
        }
        else
        {
          info( '>>> Missing goal value assignment for account ', account.Name(), ' driver name:', driver.Name() );  
        }
        if( not isnull( var ) )
        {
          this.AddGoalToLevel( program,
                               account.Level(),
                               account.Weight(),
                               factor,
                               var, 
                               false, 
                               0.0 );  
        }
      }   
    }
    
    this.SetKPILowerBounds( program, runcontext, scope.IsGeneratedByPreProductionSelector() );
  *]
  InterfaceProperties { Accessibility: 'Module' }
}