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
Quintiq file version 2.0
#parent: #root
Method InitConstraintsForCampaignMaxDurationWithin (
  CapacityPlanningSuboptimizer_CapacityPlanningAlgorithm program,
  const LibOpt_Scope scope
) const
{
  TextBody:
  [*
    maxdurwithin_constname := typeof( MPMaxCampaignElementTypeDurationWithinConstraint );
    scalefactor_DurationWithin_const := this.ScaleConstraintTerm( typeof( MPDurationOfCampaignElementTypeMidVariable ), maxdurwithin_constname );
    scalefactor_DurationEnd_const := this.ScaleConstraintTerm( typeof( MPDurationOfCampaignElementTypeEndVariable ), maxdurwithin_constname );
    scalefactor_NrOfElementsWithin_const := this.ScaleConstraintTerm( typeof( MPNrOfCampaignElementWithinUnitSubPeriodVariable ), maxdurwithin_constname );
    scalefactor_IsLastElementType_const := this.ScaleConstraintTerm( typeof( MPIsLastCampaignElementTypeVariable ), maxdurwithin_constname );
    scalefactor_MaxElementTypeDurOverMid_const := this.ScaleConstraintTerm( typeof( MPMaxCampaignElementTypeDurOverWithinVariable ), maxdurwithin_constname );
    
    traverse( scope.GetUnitPeriodInOptimizerRunConst(), Elements.OptCampaignUnitSubPeriod, ocusp )
    {
      //Only do this for campaign / transitions that have a MaxDuration (and thus, MaxDuration is finite)
      traverse( ocusp, OptCampaignCombiElement.OptCampaignElementType, ocet, ocet.HasMaxDuration() )
      {
        constr := program.MaxCampaignElementTypeDurationWithinConstraints().New( ocet, ocusp );
        constr.RHSValue( 0.0 );
        
        durationvar := program.DurationOfCampaignElementTypeMidVariables().Get( ocet, ocusp );
        constr.NewTerm( 1.0 * scalefactor_DurationWithin_const, durationvar );  
        
        //If this is a transition, we want these constraints to be hard ones
        if( not ocet.istype( OptCampaignTransitionType ) )
        {
          constr.NewTerm( -1.0 * scalefactor_MaxElementTypeDurOverMid_const, program.MaxCampaignElementTypeQtyOverWithinVariables().Get( ocet, ocusp ) );
        }
        constr.NewTerm( -ocet.MaxDuration().HoursAsReal() * scalefactor_NrOfElementsWithin_const, program.NrOfCampaignElementWithinUnitSubPeriodVariables().Get( ocet, ocusp ) );
        
        //add one more term for the last ocusp in horizon
        if( isnull( ocusp.NextSubPeriod() ) )
        {
          constr.NewTerm( - ocet.MaxDuration().HoursAsReal() * scalefactor_IsLastElementType_const, program.IsLastCampaignElementTypeVariables().Get( ocet, ocusp ) )
          durvar := program.DurationOfCampaignElementTypeEndVariables().Get( ocet, ocusp );
          constr.NewTerm( 1.0 * scalefactor_DurationEnd_const, durvar ); 
        }
      }
    }
  *]
}