/*Implementation of PStep_TreeNodeSelecter

It contains the member functions of PStep_TreeNodeSelecter and all derived classes

*/

#include "PStep_TreeNodeSelecter.h"

//PStep_TreeNodeSelecter:
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//Select a node from the branching tree.
//It stores the selected TreeNode in CurrentTreeNode.
//This function selects the first node.
void PStep_TreeNodeSelecter::Select(){

	CurrentTreeNode = Nodes.begin();

}

//Select the deepest node
void PStep_TreeNodeSelecter::SelectDeepestNode(){

	//Initialize the first node in the list of nodes as the current deepest node
	//and obtain the corresponding depth.
	auto MaxDepthNode = Nodes.begin();
	int MaxDepth = MaxDepthNode->Depth;

	//Initialize node counter as the second node in the list of Nodes
	auto i = MaxDepthNode;
	++i;
	//Go over all nodes
	for (; i!=Nodes.end(); ++i){
		//Check depth
		if (i->Depth > MaxDepth){
			//Replace current max
			MaxDepthNode = i;
			MaxDepth = i->Depth;
		}
	}

	//Select deepest node
	CurrentTreeNode = MaxDepthNode;
	
}

//Select the node with the lowest lower bound
void PStep_TreeNodeSelecter::SelecteLowestLowerBoundNode(){

	//Initialize the first node in the list of nodes as the current lowest lower bound node
	//and obtain the corresponding lower bound.
	auto MinLBNode = Nodes.begin();
	double MinLB = MinLBNode->ParentLB;

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

	//Select node with the lowest lower bound
	CurrentTreeNode = MinLBNode;

}

//PStep_DeepestNodeSelecter:
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//Select a node from the branching tree.
//It stores the selected TreeNode in CurrentTreeNode.
//This function selects the deepest node.
void PStep_DeepestNodeSelecter::Select(){

	SelectDeepestNode();

}

//PStep_DeepestNodeSelecter:
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//Select a node from the branching tree.
//It stores the selected TreeNode in CurrentTreeNode.
//This function selects the deepest node.
void PStep_LowestLBNodeSelecter::Select(){

	SelecteLowestLowerBoundNode();

}

//PStep_DepthFirstLowestLBSecondNodeSelecter:
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//Select a node from the branching tree.
//It stores the selected TreeNode in CurrentTreeNode.
//If no integer solution is found yet, this function selects the deepest node.
//Otherwise it selects the node with the lowest lower bound.
void PStep_DepthFirstLowestLBSecondNodeSelecter::Select(){

	//Check whether an integer solution is found
	if (IntegerSolutionFound){
		//An integer solution has already been found, select the node with the lowest lower bound.
		SelecteLowestLowerBoundNode();
	}
	else{
		//No integer solution is found, select the deepest node.
		SelectDeepestNode();
	}

}