/*Implementation of PStep_ColumnGenerationStatistics.h

It contains the member functions of PStep_ColumnGenerationStatistics.
*/

#include "PStep_ColumnGenerationStatistics.h"

//Constructor functions:
//------------------------------------------------------------------------------------

//Default Constructor
PStep_ColumnGenerationStatistics::PStep_ColumnGenerationStatistics(){

	//Set default values
	Flag = 0;
	TotalNumberOfIterations = 0;
	FinalValue = 0;
	ValueBeforeInequalities = 0;
	InitializationTime = 0;
	TotalComputationTime = 0;
	TimeBeforeInequalities = 0;
	TotalLPTime = 0;
	TotalPricingTime = 0;
	TotalSeparateCCTime = 0;
	TotalSeparate2EHTime = 0;
	TotalSeparateSCombTime = 0;
	TotalSeparateFCCTime = 0;
	TotalSeparateHMCTime = 0;
	StartNumberOfPSteps = 0;
	TotalNumberOfPSteps = 0;
	StartNumberOfCC = 0;
	TotalNumberOfCC = 0;
	StartNumberOf2EH = 0;
	TotalNumberOf2EH = 0;
	StartNumberOfSComb = 0;
	TotalNumberOfSComb = 0;
	StartNumberOfFCC = 0;
	TotalNumberOfFCC = 0;
	StartNumberOfHMC = 0;
	TotalNumberOfHMC = 0;

}

//Copy Constructor
PStep_ColumnGenerationStatistics::PStep_ColumnGenerationStatistics(const PStep_ColumnGenerationStatistics &OriginalPStep_ColumnGenerationStatistics){

	CopyEmptyAssumed(OriginalPStep_ColumnGenerationStatistics);

}

//Operator functions:
//------------------------------------------------------------------------------------

//Assignment operator
PStep_ColumnGenerationStatistics PStep_ColumnGenerationStatistics::operator =(const PStep_ColumnGenerationStatistics &OriginalPStep_ColumnGenerationStatistics){

	EmptyStatistics();
	CopyEmptyAssumed(OriginalPStep_ColumnGenerationStatistics);
	return *this;

}

//Destructor functions
//------------------------------------------------------------------------------------

//Default destructor
PStep_ColumnGenerationStatistics::~PStep_ColumnGenerationStatistics(){

	EmptyStatistics();

}

//Other functions
//------------------------------------------------------------------------------------

//Function to write the statistics to file. The format can be deduced from the implementation.
void PStep_ColumnGenerationStatistics::Write(const char *FileName){

	//Create and open file
	std::ofstream File(FileName);
	//Check whether the file could be opened
	if (!File){
		std::cout << "Cannot open file " << FileName << "." << std::endl;
		return;
	}

	//Write the type of termination
	File << "Termination:                       " << Flag;
	if (Flag == 0){
		File << " Optimum_Found" << std::endl;
	}
	else if (Flag == 1){
		File << " Problem_Infeasible" << std::endl;
	}
	else if (Flag == 2){
		File << " Time_Limit_Exceeded" << std::endl;
	}

	//Value
	File << "Value:                             " << FinalValue << std::endl;
	File << "Value_Before_Inequalities:         " << ValueBeforeInequalities << std::endl;

	//Number of iterations
	File << "Iterations:                        " << TotalNumberOfIterations << std::endl;

	//Time
	File << "Total_Initialization_Time:         " << InitializationTime << std::endl;
	File << "Total_Computation_Time:            " << TotalComputationTime << std::endl;
	File << "Total_Time_Before_Inequalities:    " << TimeBeforeInequalities << std::endl;
	File << "Total_LP_Time:                     " << TotalLPTime << std::endl;
	File << "Total_Pricing_Time:                " << TotalPricingTime << std::endl;
	File << "Total_CC_Seperation_Time:          " << TotalSeparateCCTime << std::endl;
	File << "Total_2EH_Seperation_Time:         " << TotalSeparate2EHTime << std::endl;
	File << "Total_Str.Comb_Seperation_Time:    " << TotalSeparateSCombTime << std::endl;
	File << "Total_FCC_Seperation_Time:         " << TotalSeparateFCCTime << std::endl;
	File << "Total_HMC_Seperation_Time:         " << TotalSeparateHMCTime << std::endl;

	//p-steps
	File << "Number_Of_p-Steps_(Initial/Final)  " << StartNumberOfPSteps << " / " << TotalNumberOfPSteps << std::endl;

	//Valid inequalities
	File << "Cuts:                              (Initial/Final)" << std::endl;
	File << "Capacity:                          " << StartNumberOfCC << " / " << TotalNumberOfCC << std::endl;
	File << "2_Edge_Hypotour:                   " << StartNumberOf2EH << " / " << TotalNumberOf2EH << std::endl;
	File << "Strengthened_Comb:                 " << StartNumberOfSComb << " / " << TotalNumberOfSComb << std::endl;
	File << "Framed_Capacity:                   " << StartNumberOfFCC << " / " << TotalNumberOfFCC << std::endl;
	File << "Homogeneous_Multistar:             " << StartNumberOfHMC << " / " << TotalNumberOfHMC << std::endl;

	//Iterations:
	File << std::endl << "Iteration-Value-LPTime-PricingTime-CCTime-2EHTime-Str.CombTime-FCCTime-HMCTime-PStepsInFormulation-PStepsInShortList-PStepsGenerated-CC-2EH-Str.Comb-FCC-HMC" << std::endl;
	int Iteration = 1;
	File << std::fixed;
	for (auto Statistics = IterationStatistics.begin(); Statistics != IterationStatistics.end(); ++Statistics){
		File << Iteration++ << " ";
		File.precision(4);
		File << Statistics->Value << " " << Statistics->LPTime << " " << Statistics->PricingTime << " " << Statistics->SeparateCCTime << " " << Statistics->Separate2EHTime << " " << Statistics->SeparateSCombTime << " " << Statistics->SeparateFCCTime << " " << Statistics->SeparateHMCTime << " ";
		File.precision(0);
		File << Statistics->NumberOfIncludedPSteps << " " << Statistics->NumberOfPStepsInTheShortList << " " << Statistics->NumberOfPStepsGenerated << " " << Statistics->NumberOfCC << " " << Statistics->NumberOf2EH << " " << Statistics->NumberOfSComb << " " << Statistics->NumberOfFCC << " " << Statistics->NumberOfHMC << std::endl;
	}

	File.close();
}

//Auxiliary functions
//------------------------------------------------------------------------------------

//Empty all entries
void PStep_ColumnGenerationStatistics::EmptyStatistics(){

	//delete
	Flag = 0;
	IterationStatistics.clear();
	TotalNumberOfIterations = 0;
	FinalValue = 0;
	ValueBeforeInequalities = 0;
	InitializationTime = 0;
	TotalComputationTime = 0;
	TimeBeforeInequalities = 0;
	TotalLPTime = 0;
	TotalPricingTime = 0;
	StartNumberOfPSteps = 0;
	TotalSeparateCCTime = 0;
	TotalSeparate2EHTime = 0;
	TotalSeparateSCombTime = 0;
	TotalSeparateFCCTime = 0; 
	TotalSeparateHMCTime = 0;
	TotalNumberOfPSteps = 0;
	StartNumberOfCC = 0;
	TotalNumberOfCC = 0;
	StartNumberOf2EH = 0;
	TotalNumberOf2EH = 0;
	StartNumberOfSComb = 0;
	TotalNumberOfSComb = 0;
	StartNumberOfFCC = 0;
	TotalNumberOfFCC = 0;
	StartNumberOfHMC = 0;
	TotalNumberOfHMC = 0;

}

//Copy all entries
//IMPORTANT: it is assumed that the current statistics are empty!
void PStep_ColumnGenerationStatistics::CopyEmptyAssumed(const PStep_ColumnGenerationStatistics &OriginalPStep_ColumnGenerationStatistics){

	//Copy
	Flag = OriginalPStep_ColumnGenerationStatistics.Flag;
	IterationStatistics = OriginalPStep_ColumnGenerationStatistics.IterationStatistics;
	TotalNumberOfIterations = OriginalPStep_ColumnGenerationStatistics.TotalNumberOfIterations;
	FinalValue = OriginalPStep_ColumnGenerationStatistics.FinalValue;
	ValueBeforeInequalities = OriginalPStep_ColumnGenerationStatistics.ValueBeforeInequalities;
	InitializationTime = OriginalPStep_ColumnGenerationStatistics.InitializationTime;
	TotalComputationTime = OriginalPStep_ColumnGenerationStatistics.TotalComputationTime;
	TimeBeforeInequalities = OriginalPStep_ColumnGenerationStatistics.TimeBeforeInequalities;
	TotalLPTime = OriginalPStep_ColumnGenerationStatistics.TotalLPTime;
	TotalPricingTime = OriginalPStep_ColumnGenerationStatistics.TotalPricingTime;
	TotalSeparateCCTime = OriginalPStep_ColumnGenerationStatistics.TotalSeparateCCTime;
	TotalSeparate2EHTime = OriginalPStep_ColumnGenerationStatistics.TotalSeparate2EHTime;
	TotalSeparateSCombTime = OriginalPStep_ColumnGenerationStatistics.TotalSeparateSCombTime;
	TotalSeparateFCCTime = OriginalPStep_ColumnGenerationStatistics.TotalSeparateFCCTime;
	TotalSeparateHMCTime = OriginalPStep_ColumnGenerationStatistics.TotalSeparateHMCTime;
	StartNumberOfPSteps = OriginalPStep_ColumnGenerationStatistics.StartNumberOfPSteps;
	TotalNumberOfPSteps = OriginalPStep_ColumnGenerationStatistics.TotalNumberOfPSteps;
	StartNumberOfCC = OriginalPStep_ColumnGenerationStatistics.StartNumberOfCC;
	TotalNumberOfCC = OriginalPStep_ColumnGenerationStatistics.TotalNumberOfCC;
	StartNumberOf2EH = OriginalPStep_ColumnGenerationStatistics.StartNumberOf2EH;
	TotalNumberOf2EH = OriginalPStep_ColumnGenerationStatistics.TotalNumberOf2EH;
	StartNumberOfSComb = OriginalPStep_ColumnGenerationStatistics.StartNumberOfSComb;
	TotalNumberOfSComb = OriginalPStep_ColumnGenerationStatistics.TotalNumberOfSComb;
	StartNumberOfFCC = OriginalPStep_ColumnGenerationStatistics.StartNumberOfFCC;
	TotalNumberOfFCC = OriginalPStep_ColumnGenerationStatistics.TotalNumberOfFCC;
	StartNumberOfHMC = OriginalPStep_ColumnGenerationStatistics.StartNumberOfHMC;
	TotalNumberOfHMC = OriginalPStep_ColumnGenerationStatistics.TotalNumberOfHMC;

}