/*Implementation of PStep_ColumnGenerator.h

It contains the member functions of PStep_ColumnGenerator21

*/

#include "PStep_ColumnGenerator.h"

//PStep_ColumnGenerator21:
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//Generate columns
//------------------------------------------------------------------------------------
//Apply local search first, if it fails apply decremetal state-space relaxation.
//In this case, first apply heuristic labeling with a limited number of arcs and not
//considering reachability when checking dominance.
//Do not apply exact labeling. This is only done in GenerateColumnsPhaseTwo.
//When labeling, completion bounds are used as in column generator 19.
void PStep_ColumnGenerator21::GenerateColumns(){

	//Obtain the number of p-steps currently added
	int NumberOfCurrentPStepsBefore = AlgorithmMembers.NumberOfCurrentPSteps;

	//Apply column generator 2, local search.
	GenerateColumns2();

	//Check if p-steps are added
	if (AlgorithmMembers.NumberOfCurrentPSteps == NumberOfCurrentPStepsBefore){

		//Reinitialize the decremental state-space information, if required
		if (AlgorithmMembers.DSSRUsed && AlgorithmMembers.AlgorithmSettings.DSSRReset == 3){
			ResetDSSRNeighbourhoods();
		}

		//Initialize the decremental state-space relaxation loop
		RerunRequired = true;
		SetRerunRequiredPerPricingProblem(true);

		//Copy the current DSSRNeighbourhoods to old DSSRNeighbourhoods for all relevant pricing problems
		//which require a rerun.
		CopyCurrentDSSRNeighbourhoodsToOld();

		//Store the original setting of DoNotCheckReachability
		bool OriginalDoNotCheckReachability = DoNotCheckReachability;
		//Store the original setting of UseLimitedArcs
		bool OriginalUseLimitedArcs = UseLimitedArcs;
		//Update the column generator setting to only use a limited number of arcs
		DoNotCheckReachability = true;
		//Update the column generator setting to only use a limited number of arcs
		UseLimitedArcs = true;

		//Use column generator 14,
		//with limited arcs and while not checking reachability
		GenerateColumns14();

		//Resolve the pricing problem as long as RerunRequired is true
		while (RerunRequired){

			//Obtain the second half of the completion bounds.
			//They are used for computing the (relevant) first half of completion bounds
			ObtainEverySecondHalfOfCompletionBounds();

			//Continue and finish the labeling, the result will be used to find
			//the first half of the completion bounds
			ContinueForwardAndBackwardLabeling();

			//Obtain the first half of the completion bounds.
			ObtainEveryFirstHalfOfCompletionBounds();

			//Copy the current DSSRNeighbourhoods to old DSSRNeighbourhoods for all relevant pricining problems
			//which require a rerun.
			CopyCurrentDSSRNeighbourhoodsToOld();

			//Apply bi-directional labeling with completion bounds,
			//with limited arcs and while not checking rreachability
			GenerateColumns19();

		}

		//Restore the original setting of DoNotCheckReachability
		DoNotCheckReachability = OriginalDoNotCheckReachability;
		//Restore the original setting of UseLimitedArcs
		UseLimitedArcs = OriginalUseLimitedArcs;

	}

}

//Bi-directional layered labeling is used.
//When labeling, completion bounds are used as in column generator 19.
void PStep_ColumnGenerator21::GenerateColumnsPhaseTwo(){

	//Initialize the decremental state-space relaxation loop
	RerunRequired = true;
	SetRerunRequiredPerPricingProblem(true);

	//Copy the current DSSRNeighbourhoods to old DSSRNeighbourhoods for all relevant pricing problems
	//which require a rerun.
	CopyCurrentDSSRNeighbourhoodsToOld();

	//Perform an initial run of bidirectional labeling
	GenerateColumns14();

	//Resolve the pricing problem as long as RerunRequired is true
	while (RerunRequired){

		//Obtain the second half of the completion bounds.
		//They are used for computing the (relevant) first half of completion bounds
		ObtainEverySecondHalfOfCompletionBounds();

		//Continue and finish the labeling, the result will be used to find
		//the first half of the completion bounds
		ContinueForwardAndBackwardLabeling();

		//Obtain the first half of the completion bounds.
		ObtainEveryFirstHalfOfCompletionBounds();

		//Copy the current DSSRNeighbourhoods to old DSSRNeighbourhoods for all relevant pricining problems
		//which require a rerun.
		CopyCurrentDSSRNeighbourhoodsToOld();

		//Apply bi-directional labeling with completion bounds
		GenerateColumns19();

	}

}