/*Implementation of PStep_ColumnManager.h

It contains the member functions of PStep_ColumnManager1

*/

#include "PStep_ColumnManager.h"

//PStep_ColumnManager1:
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//Remove functions
//------------------------------------------------------------------------------------

//Remove p-steps included in x from the formulation in PStepLP.
//All columns with reduced costs higher than Threshold are removed.
//Only CurrentPSteps are considered from (and including) FirstCurrentPStep up to (and excluding) LastCurrentPStep.
//ReducedCosts[CurrentPStepsxIndex[i]] should correspond to CurrentPSteps[i] and x[CurrentPSteps[i]].
void PStep_ColumnManager1::Remove(std::list<std::list<PStep>::iterator>::iterator FirstCurrentPStep, std::list<std::list<PStep>::iterator>::iterator LastCurrentPStep, IloNumVarArray& x, IloNumArray& ReducedCosts, IloEnv& env){

	//Obtain CurrentPSteps, -xIndices, the ShortList and their counters.
	std::list<std::list<PStep>::iterator>& CurrentPSteps = AlgorithmMembers.CurrentPSteps;
	std::list<int>& CurrentPStepsxIndices = AlgorithmMembers.CurrentPStepsxIndices;
	int& NumberOfCurrentPSteps = AlgorithmMembers.NumberOfCurrentPSteps;
	std::list<std::list<PStep>::iterator>& PStepShortList = AlgorithmMembers.PStepShortList;
	int& NumberOfPStepsInShortList = AlgorithmMembers.NumberOfPStepsInShortList;
	

	//Initialize index
	auto CurrentPStepxIndex = CurrentPStepsxIndices.begin();
	int Counter = 0;
	//Initialize array of x variables to delete
	IloNumVarArray Deletex(env);
	//Initialize the current p-step
	auto CurrentPStep = FirstCurrentPStep;
	//Go over all current p-steps
	while (CurrentPStep != LastCurrentPStep){

		//Check whether the current p-step has reduced costs higher than the threshold
		if (ReducedCosts[Counter]>Threshold){
			//Remove the current p-step from the formulation and place it on the shortlist:
			
			//Place the current p-step on the shortlist
			PStepShortList.push_back(*CurrentPStep);
			++NumberOfPStepsInShortList;
			
			//Mark the p-step variable for removal from the formulation
			Deletex.add(x[*CurrentPStepxIndex]);

			//Remove the current p-step and x index from the lists.
			CurrentPStep = CurrentPSteps.erase(CurrentPStep);
			CurrentPStepxIndex = CurrentPStepsxIndices.erase(CurrentPStepxIndex);
			--NumberOfCurrentPSteps;

		}
		else{
			//The current p-step is not removed, update p-step and index
			++CurrentPStep;
			++CurrentPStepxIndex;
		}

		//Update counter
		++Counter;

	}

	//Remove all marked p-step variables from the formulation
	Deletex.endElements();

}