/*Implementation of PStep_Algorithm.h

It contains the branching functions of PStep_Algorithm

*/

#include "PStep_Algorithm.h"

//Branching functions:
//------------------------------------------------------------------------------------

//Delete AlgorithmMembers.CurrentTreeNode from AlgorithmMembers.Nodes, and reduce
//AlgorithmMembers.CurrentNumberOfNodes by 1.
//NOTE: be carefull, make sure that AlgorithmMembers.CurrentTreeNode refers to a node in AlgorithmMembers.Nodes.
void PStep_Algorithm::DeleteCurrentTreeNode(){

	AlgorithmMembers.Nodes.erase(AlgorithmMembers.CurrentTreeNode);
	--AlgorithmMembers.CurrentNumberOfNodes;

}

//Find the lowest lower bound.
//It is obtained by considering the ParentLB of each node in AlgorithmMembers.Nodes.
//The lowest lower bound is returned.
double PStep_Algorithm::FindLowestLB(){

	//Initialize the lowest lower bound
	double MinLB;

	//Check whether there are active nodes
	if (AlgorithmMembers.CurrentNumberOfNodes>0){
		//There are active node

		//Initialize the lowest lower bound as the ParentLB of the first node
		//and obtain the corresponding lower bound.
		auto i = AlgorithmMembers.Nodes.begin();
		MinLB = i->ParentLB;

		//Initialize node counter as the second node in the list of Nodes
		++i;
		//Go over all nodes
		for (; i != AlgorithmMembers.Nodes.end(); ++i){
			//Check depth
			if (i->ParentLB < MinLB){
				//Replace current min
				MinLB = i->ParentLB;
			}
		}

	}
	else{
		//There are no active nodes
		//Check whether an integer solution exists
		if (AlgorithmMembers.IntegerSolutionFound){
			MinLB = AlgorithmMembers.BestIntegerSolution.SolutionValue;
		}
		else{
			//No active nodes or feasible solution exist
			MinLB = 0;
		}
	}

	//Return the lowest lower bound.
	return MinLB;

}