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
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
Quintiq file version 2.0
#parent: #root
Method InitVariablesForCampaignCombiSelection (
  CapacityPlanningSuboptimizer_CapacityPlanningAlgorithm program,
  const RunContextForCapacityPlanning runcontext,
  const LibOpt_Scope scope
) const
{
  Description: 'init variables method for campaign sequencing optimizer'
  TextBody:
  [*
    if( runcontext.UseCampaignSequenceOptimizer() and runcontext.UseCampaign() )
    {
      operationsinscope := scope.GetOperationInOptimizerRunConst();
      computenvar := this.MacroPlan().Optimization().DebugMode(); 
    
      program.RegisterTypeDescriptor( attribute( OptCampaignUnitSubPeriod, Name ) );
      program.RegisterTypeDescriptor( attribute( OptCampaignCombi, Name ) );
      program.RegisterTypeDescriptor( attribute( OptCampaignCombiElement, Name ) );
      program.RegisterTypeDescriptor( attribute( OptCampaignElementType, Name ) );
      program.RegisterTypeDescriptor( attribute( OptCampaignUnit, UnitID ) );
      program.RegisterTypeDescriptor( attribute( OptCampaignOperationInElementType, Name ) );
    
      traverse( scope.GetUnitPeriodInOptimizerRunConst(), Elements.OptCampaignUnitSubPeriod, ocusp ) 
      {
        assert( forall( ocusp, OptCampaignCombiSelected, combiselected, combiselected.IsActive() ), 'failed assertion: selected combi is not active ' ); 
        program.UnderSelectionOfCampaignCombiVariables().New( ocusp ) 
        program.IsIncompatibleWithPrevOptSubUnitPeriodLastElementVariables().New( ocusp )
        
        traverse( ocusp, OptCampaignCombiActive, combi )
        { 
          var := program.IsCampaignCombiSelectedVariables().New( combi );
          var.StartValue( ifexpr( combi.IsSelectedByOptimizer(), 1.0, 0.0 ) ); 
          if ( combi.DebugIsFixedPlanned() ) 
          {
            valueplanned := ifexpr( combi.IsSelectedByOptimizer(), 1.0, 0.0 );
            var.UpperBound( valueplanned ); 
            var.LowerBound( valueplanned ); 
          }
        }
        traverse( ocusp, OptCampaignCombiElement.OptCampaignElementType, ocet ) 
        {
          //If this is a transition, we want these constraints to be hard ones
          if( not ocet.istype( OptCampaignTransitionType ) )
          {
            program.MinCampaignElementTypeQtyUnderWithinVariables().New( ocet, ocusp );
            program.MinCampaignElementTypeQtyUnderMultiVariables().New( ocet, ocusp );
            
            program.MinCampaignElementTypeDurUnderWithinVariables().New( ocet, ocusp );
            program.MinCampaignElementTypeDurUnderMultiVariables().New( ocet, ocusp );
            
            program.MaxCampaignElementTypeQtyOverWithinVariables().New( ocet, ocusp );
            if ( isnull( ocusp.NextSubPeriod() ) ) 
            {
              program.MaxCampaignElementTypeQtyOverWithinLastVariables().New( ocet, ocusp ); 
            }
            program.MaxCampaignElementTypeQtyOverMultiPeriodVariables().New( ocet, ocusp );
            
            program.MaxCampaignElementTypeDurOverWithinVariables().New( ocet, ocusp );
            program.MaxCampaignElementTypeDurOverMultiVariables().New( ocet, ocusp );
          }
          
          program.CampaignElementTypePeriodOverloadedStartVariables().New( ocet, ocusp );
          program.CampaignElementTypePeriodOverloadedMidVariables().New( ocet, ocusp );
          program.CampaignElementTypePeriodOverloadedEndVariables().New( ocet, ocusp );
          
          program.CampaignElementTypePeriodUnderloadedStartVariables().New( ocet, ocusp );
          program.CampaignElementTypePeriodUnderloadedMidVariables().New( ocet, ocusp );
          program.CampaignElementTypePeriodUnderloadedEndVariables().New( ocet, ocusp );
          
          program.DurationOfCampaignElementTypeMidVariables().New( ocet, ocusp );
          program.DurationOfCampaignElementTypeEndVariables().New( ocet, ocusp );
          program.DurationOfCampaignElementTypeStartVariables().New( ocet, ocusp );
               
          program.NrOfCampaignElementWithinUnitSubPeriodVariables().New( ocet, ocusp )
          
          program.IsFirstCampaignElementTypeVariables().New( ocet, ocusp )
          program.IsLastCampaignElementTypeVariables().New( ocet, ocusp )
          
          program.IsMultiPeriodsFirstCampaignElementTypeFirstPeriodVariables().New( ocet, ocusp )
          
          traverse( ocet, OptCampaignOperationInElementType, ocoet )
          {
            varstart := program.PTQtyInCampaignElementTypeStartVariables().New( ocoet, ocusp );
            varmid := program.PTQtyInCampaignElementTypeMidVariables().New( ocoet, ocusp );
            varend := program.PTQtyInCampaignElementTypeEndVariables().New( ocoet, ocusp ); 
              
            if( operationsinscope.Find( ocoet.Operation() ) < 0 )
            {
              varstart.UpperBound( 0.0 );
              varmid.UpperBound( 0.0 );
              varend.UpperBound( 0.0 );
            }
          }
        }
      }  
      
      // fixing debug values 
      traverse( scope.GetUnitPeriodInOptimizerRunConst(), Elements.OptCampaignUnitSubPeriod, ocusp ) 
      {
        traverse( ocusp, OptCampaignCombiActive, combi ) 
        {
          traverse( combi, OptCampaignCombiElement, occelt ) 
          {
            ocet := occelt.OptCampaignElementType(); 
            subperiod := occelt.OptCampaignUnitSubPeriod(); 
            midvar := program.DurationOfCampaignElementTypeMidVariables().Get( ocet, subperiod );
            endvar := program.DurationOfCampaignElementTypeEndVariables().Get( ocet, subperiod );
            startvar := program.DurationOfCampaignElementTypeStartVariables().Get( ocet, subperiod );
           
            // note that these variables are shared among combi elements with the same type and sub period. 
            // in the handle feasible we write back duplicated to the combi element objects
            // here we only set for the combi that has been fixed - which for normal debugging it would mean we only set
            // values ones because there will be at most 1 combi per unit period
            
            if ( occelt.DebugFixStartDuration() ) 
            {
              startvar.LowerBound( occelt.DebugDurationOfCampaignElementTypeStartVariables() ); 
              startvar.UpperBound( startvar.LowerBound() ); 
              
              if ( startvar.UpperBound() = 0.0 ) 
              {
                traverse( ocet, OptCampaignOperationInElementType, ocoet )
                { 
                  program.PTQtyInCampaignElementTypeStartVariables().Get( ocoet, subperiod ).UpperBound ( 0.0 ); 
                }
              }
            }
            if ( occelt.DebugFixMidDuration() ) 
            {
              midvar.LowerBound( occelt.DebugDurationOfCampaignElementTypeMidVariables() ); 
              midvar.UpperBound( midvar.LowerBound() ); 
              
              if ( midvar.UpperBound() = 0.0 ) 
              {
                traverse( ocet, OptCampaignOperationInElementType, ocoet )
                { 
                  program.PTQtyInCampaignElementTypeMidVariables().Get( ocoet, subperiod ).UpperBound ( 0.0 ); 
                }
              }
            }
            if ( occelt.DebugFixEndDuration() ) 
            {
              endvar.LowerBound( occelt.DebugDurationOfCampaignElementTypeEndVariables() ); 
              endvar.UpperBound( endvar.LowerBound() ); 
              
              if ( endvar.UpperBound() = 0.0 ) 
              {
                traverse( ocet, OptCampaignOperationInElementType, ocoet )
                { 
                  program.PTQtyInCampaignElementTypeEndVariables().Get( ocoet, subperiod ).UpperBound ( 0.0 ); 
                }
              }
            } 
          }
        }
      }
      
      // Creating Goal variables
      program.TotalUnitPeriodCampaignUtilizationSlackVariables().New();
      program.TotalCampaignSequencingCombiSlackVariables().New();
      program.TotalCampaignMinQtyUnderVariables().New();
      program.TotalCampaignMaxQtyOverVariables().New();
      program.TotalCampaignMinDurationUnderVariables().New();
      program.TotalCampaignMaxDurationOverVariables().New();
      
      if( computenvar )
      {
        periods := counter( scope.GetUnitPeriodInOptimizerRunConst(), Elements.OptCampaignUnitSubPeriod, ocusp, true );
        debuginfo( " -Number of campaign subunitperiods: ", periods ); 
        combis := sum( scope.GetUnitPeriodInOptimizerRunConst(), Elements.OptCampaignUnitSubPeriod, ocusp, true, counter( ocusp, OptCampaignCombiActive, combi, true ) );
        debuginfo( " -Number of campaign combis ", combis   )  
      }
    }
  *]
}