| 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' } | 
| } |