admin
2025-01-22 7e31442f0e9b07764e9c6a9680d3d4aeba5fe1de
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
Quintiq file version 2.0
#parent: #root
Method InitConstraintsforCampaignCombis (
  CapacityPlanningSuboptimizer_CapacityPlanningAlgorithm program,
  const LibOpt_Scope scope
) const
{
  TextBody:
  [*
    nrelewithin_constname :=  typeof( MPNrOfCampaignElementsWithinUnitSubPeriodConstraint );
    isfirstelementtype_constname := typeof( MPIsFirstCampaignElementTypeConstraint );
    ismultiperiodfirst_constname := typeof( MPIsMultiPeriodsFirstCampaignElementTypeFirstPeriodConstraint );
    islastelementtype_constname := typeof( MPIsLastCampaignElementTypeConstraint );
    compatiblewithprevocusplastelement_constname := typeof( MPCompatibleWithPrevOptCampaignUnitSubPeriodLastElementConstraint )
    
    scalefactor_variscombiselected_constnrofelementswithin := this.ScaleConstraintTerm( typeof( MPIsCampaignCombiSelectedVariable ), nrelewithin_constname );
    scalefactor_varnrofelementswithin_constnrofelementswithin := this.ScaleConstraintTerm( typeof( MPNrOfCampaignElementWithinUnitSubPeriodVariable ), nrelewithin_constname );
    
    scalefactor_variscombiselected_constisfirstelementtype := this.ScaleConstraintTerm( typeof( MPIsCampaignCombiSelectedVariable ), isfirstelementtype_constname );
    scalefactor_varisfirstelementtype_constisfirstelementtype := this.ScaleConstraintTerm( typeof( MPIsFirstCampaignElementTypeVariable ), isfirstelementtype_constname );
    
    scalefactor_variscombiselected_constismultiperiodfirst := this.ScaleConstraintTerm( typeof( MPIsCampaignCombiSelectedVariable ), ismultiperiodfirst_constname );
    scalefactor_varismultiperiodfirst_constismultiperiodfirst := this.ScaleConstraintTerm( typeof( MPIsMultiPeriodsFirstCampaignElementTypeFirstPeriodVariable ), ismultiperiodfirst_constname );
    
    scalefactor_variscombiselected_constislastelementtype := this.ScaleConstraintTerm( typeof( MPIsCampaignCombiSelectedVariable ), islastelementtype_constname );
    scalefactor_varislastelementtype_constislastelementtype := this.ScaleConstraintTerm( typeof( MPIsLastCampaignElementTypeVariable ), islastelementtype_constname );
    
    scalefactor_varisfirstelementtype_constcompatiblewithprevocusplastelement := this.ScaleConstraintTerm( typeof( MPIsFirstCampaignElementTypeVariable ), compatiblewithprevocusplastelement_constname );
    scalefactor_varislastelementtype_constcompatiblewithprevocusplastelement := this.ScaleConstraintTerm( typeof( MPIsLastCampaignElementTypeVariable ), compatiblewithprevocusplastelement_constname );
    scalefactor_varisincompatiblewithprevelement_constcompatiblewithprevocusplastelement := this.ScaleConstraintTerm( typeof( MPIsIncompatibleWithPrevOptSubUnitPeriodLastElementVariable ), compatiblewithprevocusplastelement_constname );
    scalefactor_rhs_constcompatiblewithprevocusplastelement := this.ScaleConstraintRHS( compatiblewithprevocusplastelement_constname, 1.0 );
    
    traverse( scope.GetUnitPeriodInOptimizerRunConst(), Elements.OptCampaignUnitSubPeriod, ocusp )
    {
    
      traverse( ocusp, OptCampaignCombiElement.OptCampaignElementType, ocet )
      {
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //NrOfElementsWithin:
        //Calculate the number of Campaigns/Transitions that are selected for a particular CampaignType/TransitionType in an OptCampaignUnitSubPeriod.
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //Only Campaigns/Transitions that start and end in the OptCampaignUnitSubPeriod are counted.
        ctrnrofelewithin := program.NrOfCampaignElementsWithinUnitSubPeriodConstraints().New( ocet, ocusp );
        ctrnrofelewithin.Sense( "=" );
        ctrnrofelewithin.RHSValue( 0.0 );
        varnrofelewithin := program.NrOfCampaignElementWithinUnitSubPeriodVariables().Get( ocet, ocusp );
        ctrnrofelewithin.NewTerm( -1.0 * scalefactor_varnrofelementswithin_constnrofelementswithin, varnrofelewithin );
        iselementtypeelements := selectset( ocusp, OptCampaignCombiElementActive, occe,
                                            occe.OptCampaignElementType() = ocet );
        withelementtypecombis := selectset( iselementtypeelements, Elements.OptCampaignCombi, combi, true );
        
        traverse( withelementtypecombis, Elements, combi )
        {
          nrofelementsincombi := counter( combi, OptCampaignCombiElement, occe,
                                          not occe.IsFirstElementInUnitSubPeriod()
                                          and not occe.IsLastElementInUnitSubPeriod()
                                          and occe.OptCampaignUnitSubPeriod() = ocusp
                                          and occe.OptCampaignElementType() = ocet );
          
          variscombiselected := program.IsCampaignCombiSelectedVariables().Get( combi );
          ctrnrofelewithin.NewTerm( nrofelementsincombi * scalefactor_variscombiselected_constnrofelementswithin,
                                               variscombiselected );
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //IsFirstCampaingElementType
        //Determine if the first element in an OptCampaignUnitSubPeriod is a Campaign/Transition that belongs to a particular CampaignType/TransitionType.
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ctrisfirstelementtype := program.IsFirstCampaignElementTypeConstraints().New( ocet, ocusp );
        varisfirstelementtype := program.IsFirstCampaignElementTypeVariables().Get( ocet, ocusp )
        ctrisfirstelementtype.NewTerm( -1.0 * scalefactor_varisfirstelementtype_constisfirstelementtype, varisfirstelementtype )
        ctrisfirstelementtype.Sense( "=" );
        ctrisfirstelementtype.RHSValue( 0.0 );
        firstelements := selectset( ocusp, OptCampaignCombiElementActive, occe,
                                   occe.OptCampaignElementType() = ocet
                                   and occe.IsFirstElementInUnitSubPeriod() );
        startwithelementtypecombis := selectset( firstelements, Elements.OptCampaignCombi, combi, true );
        
        traverse( startwithelementtypecombis, Elements, combi )
        {
          variscombiselected := program.IsCampaignCombiSelectedVariables().Get( combi );
          ctrisfirstelementtype.NewTerm( 1.0 * scalefactor_variscombiselected_constisfirstelementtype,
                                              variscombiselected ); 
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //IsMultiPeriodsFirstCampaignElementTypeFirstPeriod
        //Determine if the first element in an OptCampaignUnitSubPeriod is a Campaign/Transition that belongs to a particular CampaignType/TransitionType, and from a multiperiod combi.
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ctrismultiperiodfirst := program.IsMultiPeriodsFirstCampaignElementTypeFirstPeriodConstraints().New( ocet, ocusp )
        varismultiperiodfirst := program.IsMultiPeriodsFirstCampaignElementTypeFirstPeriodVariables().Get( ocet, ocusp )
        ctrismultiperiodfirst.NewTerm( -1.0 * scalefactor_varismultiperiodfirst_constismultiperiodfirst, varismultiperiodfirst )
        firstelements := selectset( ocusp, OptCampaignCombiElementActive, occe,
                                   occe.OptCampaignElementType() = ocet
                                   and occe.IsFirstElementInUnitSubPeriod() );
        startwithelementtypecombis := selectset( firstelements, 
                                                 Elements.OptCampaignCombi, 
                                                 combi, 
                                                 combi.IsConsideredForLimitationConstraints()
                                                 and combi.OptCampaignUnitSubPeriod() = ocusp );
        traverse( startwithelementtypecombis, Elements, combi )
        {
          variscombiselected := program.IsCampaignCombiSelectedVariables().Get( combi );
          ctrismultiperiodfirst.NewTerm( 1.0 * scalefactor_variscombiselected_constismultiperiodfirst,
                                              variscombiselected ); 
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //IsLastCampaingElementType
        //Determine if the last element in an OptCampaignUnitSubPeriod is a Campaign/Transition that belongs to a particular CampaignType/TransitionType.
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ctrislastelementtype := program.IsLastCampaignElementTypeConstraints().New( ocet, ocusp );
        varislastelementtype := program.IsLastCampaignElementTypeVariables().Get( ocet, ocusp )
        ctrislastelementtype.NewTerm( -1.0*scalefactor_varislastelementtype_constislastelementtype, varislastelementtype )
        ctrislastelementtype.Sense( "=" );
        ctrislastelementtype.RHSValue( 0.0 );
        lastelements := selectset( ocusp, OptCampaignCombiElementActive, occe,
                                   occe.OptCampaignElementType() = ocet
                                   and occe.IsLastElementInUnitSubPeriod() );
        endwithelementtypecombis := selectset( lastelements, Elements.OptCampaignCombi, combi, true );
        
        traverse( endwithelementtypecombis, Elements, combi )
        {
          variscombiselected := program.IsCampaignCombiSelectedVariables().Get( combi );
          ctrislastelementtype.NewTerm( 1.0 * scalefactor_variscombiselected_constislastelementtype,
                                              variscombiselected ); 
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //CompatibleWithPrevOptCampaignUnitSubPeriodLastElement
        //Ensure that the first Campaign/Transition of an OptCampaignUnitSubPeriod is compatible with the last Campaign/Transition of the previous OptCampaignUnitSubPeriod.
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        previousperiod := ocusp.PreviousSubPeriod();
        //there is no need for a constraint for the first period
        if( not isnull( previousperiod ) )
        {
          ctrcompatiblewithprevocusp := program.CompatibleWithPrevOptCampaignUnitSubPeriodLastElementConstraints().New( ocet, ocusp );
          ctrcompatiblewithprevocusp.Sense( '<=' ); 
          varisincompatible := program.IsIncompatibleWithPrevOptSubUnitPeriodLastElementVariables().Get( ocusp );
          varisprevioussubperiodlastelementtype := program.IsLastCampaignElementTypeVariables().Find( ocet, previousperiod );
          //the variable for last elementtype in previous ocusp could be null if there is no combi element of this type in previous ocusp
          //in that case we don't include the variable
          if( not isnull( varisprevioussubperiodlastelementtype ) )
          {
            ctrcompatiblewithprevocusp.NewTerm( -1.0*scalefactor_varislastelementtype_constcompatiblewithprevocusplastelement, varisprevioussubperiodlastelementtype );
          }
          else if( previousperiod.IsFixed() and OptCampaignElementType::GetIsCompatibleElementTypes( previousperiod, ocet ) )
          {
            ctrcompatiblewithprevocusp.RHSValue( 1.0 * scalefactor_rhs_constcompatiblewithprevocusplastelement );
          }
          
          ctrcompatiblewithprevocusp.NewTerm( -1.0*scalefactor_varisincompatiblewithprevelement_constcompatiblewithprevocusplastelement, varisincompatible );
          ctrcompatiblewithprevocusp.NewTerm( 1.0*scalefactor_varisfirstelementtype_constcompatiblewithprevocusplastelement, varisfirstelementtype );
        }
      }
    }
  *]
  InterfaceProperties { Accessibility: 'Module' }
}