haorenhui
2023-10-30 6d6cc10d9e8e242661da7fd655dec155a09d676c
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
Quintiq file version 2.0
#parent: #root
Method SetPTQtyVariableProperties (
  const LibOpt_Scope scope,
  const RunContextForCapacityPlanning runcontext,
  const Operation operation,
  const Period_MP period,
  MPVariable var,
  Boolean setstart,
  CapacityPlanningSuboptimizer_CapacityPlanningAlgorithm program
) const
{
  TextBody:
  [*
    lowerbound := 0.0;
    var.LowerBound( -100.0 ); 
    var.LowerBound( lowerbound ); 
    var.UpperBound( Real::MaxReal() );
    
    // Minimum quantity on operation
    if( runcontext.UseProcessMinimumQuantity()
        and operation.HasMinimumQuantity()
        and period.IsWithinLotSizeHorizon()
        and ( not runcontext.IsSlidingWindowsRun()
              or period.GetIsInSlidingWindow( scope ) ) )
    {
      lowerbound := operation.MinimumQuantity() / operation.QuantityToProcessFactor();   // If PeriodTask.Quantity > 0, it must fulfill: PeriodTask.Quantity * Operation.QuantityToProcessFactor >= Operation.MinimumQuantity
    
      if ( lowerbound > 0 ) 
      {
        scalefactor_timeconstbigm_rhs := this.ScaleConstraintRHS( typeof(  MPProcessMinQtyBIGMTimeConstraint ), 1.0 ); 
        scalefactor_quantityconstbigm_rhs := this.ScaleConstraintRHS( typeof(  MPProcessMinQtyBIGMQuantityConstraint ), 1.0 ); 
        
        scalefactor_ptqty_bigmtime_const := this.ScaleConstraintTerm( typeof( MPPTQtyVariable ), typeof( MPProcessMinQtyBIGMTimeConstraint )  );
        scalefactor_ptqty_bigmquantity_const := this.ScaleConstraintTerm( typeof( MPPTQtyVariable ), typeof( MPProcessMinQtyBIGMQuantityConstraint )  );
    
        unitperiod := UnitPeriod::FindUnitPeriodTypeIndex( operation.UnitID(), period.Start(), period.End() ); 
        
        quantitycurrent := guard(  PeriodTaskOperation::FindPeriodTaskOperationTypeIndex( unitperiod.Start(), operation.ID() ).Quantity(), 0.0 ); 
        bigM := CapacityPlanningSuboptimizer::GetBigMProcessMinQty( operation, unitperiod, lowerbound, quantitycurrent );  // for time based capacity this is an upper bound on number of hours, for Quantity based an upper bound on quantity
    
        iszerovar := program.PTQtyIsZero_MinProcessBigMVariables().New( operation, period ); 
        iszerovar.StartValue( ifexpr( quantitycurrent = 0.0, 1.0, 0.0 ) ); 
        var.LowerBound( 0.0 ); // PTQY needs to be >= 0 
    
          //     
          // scaledlowerbound <= ptqty + scaledlowerbound * iszerovar
          //
          c := program.ProcessMinQtyEnforceQuantityConstraints().New( operation, period ); 
          scalefactor_scaledforcplex := CapacityPlanningSuboptimizer::GetVariableScaleFactor( typeofexpression( var ) ); 
          scaledlowerbound := lowerbound / scalefactor_scaledforcplex; 
          c.Sense( '<=' ); 
          c.RHSValue( -scaledlowerbound ); 
          c.NewTerm(  -1.0, var ); 
          c.NewTerm(  -1.0 * scaledlowerbound, iszerovar ); // no restriction if iszerovar=1, otherwise need to be at least lower bound
          this.FilterCPLEXNoise( c, 1.0 ); // this constraint is excluded from the parallel init, so need to do the filtering separate   
    
        if ( operation.Unit().HasCapacityTypeTime() ) 
        {
          capusqageperqty := operation.GetCapacityUsagePerQuantity( unitperiod );
    
          d := program.ProcessMinQtyBIGMTimeConstraints().New( operation, period ); 
          d.Sense( '<=' ); 
          d.RHSValue( bigM * scalefactor_timeconstbigm_rhs ); 
          d.NewTerm( 1.0 * scalefactor_ptqty_bigmtime_const * capusqageperqty, var ); 
          d.NewTerm( 1.0 * bigM * scalefactor_timeconstbigm_rhs, iszerovar );   
          
          this.FilterCPLEXNoise( d, 1.0 ); // this constraint is excluded from the parallel init, so need to do the filtering separate
        }
        else
        {
          //
          // ptyqty <= bigM - bigM*iszerovar
          //
          d := program.ProcessMinQtyBIGMQuantityConstraints().New( operation, period ); 
          d.Sense( '<=' ); 
          d.RHSValue( bigM * scalefactor_quantityconstbigm_rhs ); 
          d.NewTerm( 1.0 * scalefactor_ptqty_bigmquantity_const, var ); 
          d.NewTerm( 1.0 * bigM * scalefactor_quantityconstbigm_rhs, iszerovar );   
          
          this.FilterCPLEXNoise( d, 1.0 ); // this constraint is excluded from the parallel init, so need to do the filtering separate
        }
      }
    }
    
     // Set feedback bounds that apply to the variable
    if( operation.FeedbackPeriodTaskOperation( relsize ) > 0 )
    {
      feedbackset := selectset(  operation, 
                                 FeedbackPeriodTaskOperation, 
                                 feedback,
                                 true, 
                                 feedback.GetIsValidFeedback() and feedback.UnitPeriod().Period_MP() = period ); 
                   
      feedbackqty := sum( feedbackset, Elements, feedback, true, feedback.FeedbackQuantity() );
    
      pto := PeriodTaskOperation::FindPeriodTaskOperationTypeIndex( period.Start(), operation.ID() ); 
    
      islockedfeedback := guard( pto.IsFeedbackLocked(), 
                                 exists( feedbackset, Elements, fb, true, fb.IsLocked() ) ); 
        
      lowerboundfixedsofar := lowerbound; 
    
      if( feedbackqty > 0 
          or islockedfeedback ) // if it is not locked setting lower bound >=0 & making continuous interfers with process min qty
      {
        iszerovar := program.FindVariable(  'PTQtyIsZero', operation, period ); 
        if ( not isnull( iszerovar ) ) 
        {
          var.UpperBound( 0.0 ); 
          constr := program.ProcessMinQtyEnforceQuantityConstraints().Get( operation, period ); 
          constr.Enabled( false ); 
          if ( operation.Unit().HasCapacityTypeQuantity() ) 
          {
            constr2 := program.ProcessMinQtyBIGMQuantityConstraints().Get( operation, period ); 
            constr2.Enabled( false );
          }
          else if ( operation.Unit().HasCapacityTypeTime() ) 
          {
            constr2 := program.ProcessMinQtyBIGMTimeConstraints().Get( operation, period ); 
            constr2.Enabled( false ); 
          } 
        }
        if ( islockedfeedback ) 
        {
          this.FreezeVariableLowerUpperBound( var, feedbackqty, feedbackqty ); 
        }
        else
        {
          this.FreezeVariableLowerBound( var, maxvalue(  feedbackqty, lowerboundfixedsofar ) ); 
        }
      }
    }
    
    // set start value for semi continuous variables
    if ( setstart and var.VariableType() = 'SemiContinuous' ) 
    {
      periodtask := select(  operation, PeriodTaskOperation, pto, true, pto.UnitPeriod().Period_MP() = period ); 
      startval := 0.0; 
      if ( not isnull( periodtask )  )
      {
        startval := periodtask.Quantity(); 
      }
      var.StartValue( startval  ); 
    }
    
    contextisusecampaign := runcontext.UseCampaign(); 
    ismanualcampaignsequence := not runcontext.UseCampaignSequenceOptimizer()
    if ( ismanualcampaignsequence and contextisusecampaign and period.GetIsInCampaignTypeHorizonUnit( operation.Unit() ) ) 
    {
      unit := operation.Unit(); 
      up := select(  operation, Unit.UnitPeriod, uperiod, true, uperiod.Period_MP() = period ); 
      iscampaignunit := unit.Campaign_MP( relsize ) > 0; 
      
      if ( iscampaignunit 
           and operation.OperationInCampaignType( relsize ) + operation.OperationInTransitionType( relsize ) = 0  // operation not in campaign or transition
           and up.Period_MP().GetIsInCampaignTypeHorizonUnit( up.Unit() ) )                     // but it is in the campaign horizon 
      {   
         // set upper bound = 0 (for user period tasks exception is possible because set below)
        debuginfo(  'Setting = 0', operation.ID(), period.Start() ) ;
        ptqtyvar := program.PTQtyVariables().Get( operation, period );  
        ptqtyvar.UpperBound( 0.0 ); 
        ptqtyvar.LowerBound( 0.0 );
      }
    }
  *]
  InterfaceProperties { Accessibility: 'Module' }
}