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
Quintiq file version 2.0
#parent: #root
Method InitConstraintsForCampaignMaxQtyWithin (
  CapacityPlanningSuboptimizer_CapacityPlanningAlgorithm program,
  const LibOpt_Scope scope
) const
{
  TextBody:
  [*
    maxqtywithin_constname := typeof( MPMaxCampaignElementTypeQtyWithinConstraint );
    scalefactor_PTQtyWithin_const := this.ScaleConstraintTerm( typeof( MPPTQtyInCampaignElementTypeMidVariable ), maxqtywithin_constname );
    scalefactor_PTQtyEnd_const := this.ScaleConstraintTerm( typeof( MPPTQtyInCampaignElementTypeEndVariable ), maxqtywithin_constname );
    scalefactor_NrOfElementsWithin_const := this.ScaleConstraintTerm( typeof( MPNrOfCampaignElementWithinUnitSubPeriodVariable ), maxqtywithin_constname );
    scalefactor_IsLastElementType_const := this.ScaleConstraintTerm( typeof( MPIsLastCampaignElementTypeVariable ), maxqtywithin_constname );
    scalefactor_MaxElementTypeQtyOverMid_const := this.ScaleConstraintTerm( typeof( MPMaxCampaignElementTypeQtyOverWithinVariable ), maxqtywithin_constname );
    
    
    traverse( scope.GetUnitPeriodInOptimizerRunConst(), Elements.OptCampaignUnitSubPeriod, ocusp )
    {
      //Only do this for campaign / transitions that have a MaxQuantity (and thus, MaxQuantity is finite)
      traverse( ocusp, OptCampaignCombiElement.OptCampaignElementType, ocet, ocet.MaxQuantity().IsFinite() )
      {
        constr := program.MaxCampaignElementTypeQtyWithinConstraints().New( ocet, ocusp );
        constr.RHSValue( 0.0 );
        
        traverse( ocet, OptCampaignOperationInElementType, oiet )
        {
          ptqyvar := program.PTQtyInCampaignElementTypeMidVariables().Get( oiet, ocusp );
          constr.NewTerm( 1.0 * scalefactor_PTQtyWithin_const, ptqyvar );
        }  
        
        //If this is a transition, we want these constraints to be hard ones
        if( not ocet.istype( OptCampaignTransitionType ) )
        {
          constr.NewTerm( -1.0 * scalefactor_MaxElementTypeQtyOverMid_const, program.MaxCampaignElementTypeQtyOverWithinVariables().Get( ocet, ocusp ) );
        }
        constr.NewTerm( -ocet.MaxQuantity() * scalefactor_NrOfElementsWithin_const, program.NrOfCampaignElementWithinUnitSubPeriodVariables().Get( ocet, ocusp ) );
      }
    }
    
    
    traverse( scope.GetUnitPeriodInOptimizerRunConst(), Elements.OptCampaignUnitSubPeriod, ocusp, isnull( ocusp.NextSubPeriod() ) )
    {
      //Only do this for campaign / transitions that have a MaxQuantity (and thus, MaxQuantity is finite)
      traverse( ocusp, OptCampaignCombiElement.OptCampaignElementType, ocet, ocet.MaxQuantity().IsFinite() )
      {
        constr := program.MaxCampaignElementTypeQtyWithinLastConstraints().New( ocet, ocusp );
        constr.RHSValue( 0.0 );
        constr.Sense( '<=' ); 
        
        //If this is a transition, we want these constraints to be hard ones
        if( not ocet.istype( OptCampaignTransitionType ) )
        {
          constr.NewTerm( -1.0 * scalefactor_MaxElementTypeQtyOverMid_const, program.MaxCampaignElementTypeQtyOverWithinLastVariables().Get( ocet, ocusp ) ); 
        }
        
        //add one more term for the last ocusp in horizon
        constr.NewTerm( - ocet.MaxQuantity() * scalefactor_IsLastElementType_const, program.IsLastCampaignElementTypeVariables().Get( ocet, ocusp ) )
        traverse( ocet, OptCampaignOperationInElementType, oiet )
        {
          ptqyvar := program.PTQtyInCampaignElementTypeEndVariables().Get( oiet, ocusp );
          constr.NewTerm( 1.0 * scalefactor_PTQtyEnd_const, ptqyvar );
        }  
      }
    }
  *]
}