陈清红
2025-04-14 880f3c0257eeb8c37761d484258fdd102a369a19
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
Quintiq file version 2.0
#parent: #root
Method Debug_FreezeDecisionVariables (
  CapacityPlanningSuboptimizer_CapacityPlanningAlgorithm program,
  const Period_MP start,
  const Period_MP end,
  const LibOpt_Scope scope,
  Boolean unfreeze
) const
{
  Description:
  [*
    Freeze the decision variables for debugging purpose
    To check if the current planning is feasible in optimizer
    Update this code if the initialization of period tasks and pispips change.
  *]
  TextBody:
  [*
    
    // Note:
    // 1. Does not support the freezing of postponed sales demands as they will be merged back to the original one.
    // Possible infeasible due to common violation of hard constraints such as:
    // 1. Some of the dependent demands are not fulfilled.
    // 2. The hard constraints found in optimizer setting if activated, such as lot sizes.
    
    // Freeze the variables to its current value if it is within the selected optimization horizon
    // periodtaskqtyvarname := AlgorithmNameVariable::PTQty();
    
    runcontext := this.GetRunContextConst(); 
    epsilonfreeze := 0.0; 
    
    traverse( scope.GetPeriodInOptimizerRunConst(), Elements, period ) 
    {
      traverse( period, UnitPeriod, unitperiod, scope.Contains( unitperiod.UnitPeriodInOptimizerRun() ) )
      {
        operations := this.GetOperationsForUnitPeriod( scope, unitperiod );
        traverse( operations, Elements, operation,
                  not operation.IsFrozenInPeriod( period ) )
        {
          // period task
          pt := PeriodTaskOperation::FindPeriodTaskOperationTypeIndex( period.Start(), operation.ID() );
          quantity := guard( pt.Quantity(), 0.0 );
          var := program.PTQtyVariables().Get( operation, period );
          if ( unfreeze ) 
          {
            this.SetPTQtyVariableProperties( scope, runcontext, operation, period, var, false /*set start value*/, program );
          }
          else
          {
            nonegative := true; 
            this.FreezeIt( var, quantity, nonegative, epsilonfreeze ); 
          }
    
    
        // Dependent demand input group
        /*
          traverse( pt, DependentDemandInputGroup, ddig )
          {
            if( operation.HasLeadTime() )     // Get the dependent demands that is located at different period from the period of period task.
            {
              pispipperiods := construct(  Period_MPs ); 
              CapacityPlanningSuboptimizer::GetOperationDependentDemandPeriods( period, operation, pispipperiods );
    
              traverse( pispipperiods, Elements, pispipperiod )
              {
    
                traverse( ddig, DependentDemand, dd, dd.ProductInStockingPointInPeriodPlanningLeaf().Period_MP() = pispipperiod )
                {
                  input := dd.ProcessInput();
                  varpartialoperationdemandqty := program.PartialOperationDemandQtyVariables().Find( input.astype( OperationInput ), pispipperiod, period );
                  if ( not isnull( varpartialoperationdemandqty ) ) 
                  {
                    quantity := dd.FulfilledQuantity();
                    if ( unfreeze ) 
                    {
                      varpartialoperationdemandqty.LowerBound( 100.0 ); 
                      varpartialoperationdemandqty.LowerBound( 0.0 ); 
                      varpartialoperationdemandqty.UpperBound( 100.0 ); 
                      varpartialoperationdemandqty.UpperBound( Real::MaxReal() ); 
                    }
                    else
                    {
                      this.FreezeIt( varpartialoperationdemandqty, quantity );
                    }
                  }
                }                                                           
              }
            }
            else
            {
              quantity := ddig.OptimizerFulfilledQuantity();
              error('Nikolay Kosarev: Is it unreachable? (should always be 2 indices')
              //operationdemandqtyvar := program.FindVariable( operationdemandqtyvarname, period );
              //if( not isnull( var ) )
              //{
              // this.FreezeVariableWithSlack( operationdemandqtyvar, quantity );
              //}   
            }
          }*/
        }
      } 
      
    
      traverse( scope.GetProductInTripInOptimizerRunConst(), Elements, productintrip, 
                not productintrip.Trip().DepartureUnitPeriod().IsPeriodFrozen() )
      {
        var := program.TripNewSupplyVariables().Get( productintrip );
        if ( unfreeze ) 
        {
          var.LowerBound( -100.0 ); 
          var.UpperBound( -100.0 ); 
          var.LowerBound( productintrip.PITVarInitLowerBound() ); 
          var.UpperBound( productintrip.PITVarInitUpperBound() ); 
        }
        else
        {
          qty := productintrip.Quantity(); 
          nonegative := true; 
          this.FreezeIt( var, qty, nonegative, epsilonfreeze );
        }
      }
    }
    /*
    // Freeze variables for SalesDemands
    traverse( scope.GetLeafSalesDemandInPeriodInRun(), 
              Elements, 
              lsdip, 
              lsdip.AsSalesDemandInPeriodBase().Period_MP().IsAvailableForOptimization( start, end ) )
    {
        var := program.SalesDemandQtyVariables().Get( lsdip );      // Fulfilled quantity
        if ( unfreeze ) 
        {
          var.LowerBound( -100.0 ); 
          var.LowerBound( lsdip.SalesDemandQtyVarInitLowerBound() ); 
          var.UpperBound(  -100.0 ); 
          var.UpperBound( lsdip.SalesDemandQtyVarInitUpperBound() ); 
        }
        else
        {
          qty := lsdip.FulfilledQuantity() ; 
          nonnegative := true; 
          this.FreezeIt( var, qty, nonnegative );
        }
    } 
    
    traverse( scope.GetAggregatedSalesDemandInPeriodInRun(), Elements, asdip, asdip.AsSalesDemandInPeriodBase().Period_MP().IsAvailableForOptimization( start, end ) )
    {
        traverse ( asdip, 
                   DisaggregatedSalesDemandInPeriod, 
                   dasdip,
                   not dasdip.IsPostponed()
                   and scope.Contains(  dasdip.AsPlanningBaseSalesDemandInPeriod().PISPIPInOptimizerRun() ) )
        {
           var := program.DisaggregatedSalesDemandQtyVariables().Get( dasdip );
           if ( unfreeze ) 
           {
             var.LowerBound( -100.0); 
             var.LowerBound( dasdip.SalesDemandQtyVarInitLowerBound() ); 
             var.UpperBound( -100.0 ); 
             var.UpperBound( dasdip.SalesDemandQtyVarInitUpperBound() ); 
           }
           else
           {
             qty := dasdip.FulfilledQuantity(); 
             nonneg := true; 
             this.FreezeIt( var, qty, nonneg ); 
           }
        }
    }
    */
    
    // Freeze the variables to its current value if it is within the selected optimization horizon
    /*
    traverse( scope.GetPISPIPInOptimizerRun(), Elements.astype(  ProductInStockingPointInPeriodPlanningLeaf), pispip ) 
    {
      traverse( pispip, DependentDemand, dd, isnull( dd.ProductInTrip() ) )
      {
        operationdemandqtyvar := program.OperationDemandQtyVariables().Find( dd.ProcessInput(), pispip.Period_MP() );  // Fulfilled quantity
        if ( not isnull( operationdemandqtyvar ) ) 
        {
          if ( unfreeze ) 
          {
            operationdemandqtyvar.LowerBound( -100.0 ); 
            operationdemandqtyvar.LowerBound( 0.0 ); 
            operationdemandqtyvar.UpperBound( -100.0 ); 
            operationdemandqtyvar.UpperBound( Real::MaxReal() ); 
          }
          else
          {
            this.FreezeIt( operationdemandqtyvar, dd.FulfilledQuantity() );
          }
        }
      }
    }
    */
  *]
  InterfaceProperties { Accessibility: 'Module' }
}