//---------------------------------------------------------------------------

#include "stdafx.h"

#pragma hdrstop

using namespace std;

#include "CovMatrix.h"
#include "HybridCover.h"

HybridCover::HybridCover( const CovMatrix *_matrix )
{
	unsigned int i,j;

	
	
	/*Initializing data*/
	CM = _matrix;
	rownum = CM->GetRowNum();
	colnum = CM->GetColNum();

	/*Converting the problem representation*/
	problem=scproblem_new(rownum,colnum);
	assert(problem!=NULL);
	for(i=0;i<rownum;i++) {
		for(j=0;j<colnum;j++) {
			scproblem_set(problem,i,j,(int)_matrix->GetValue(i,j));
		}
	}
	
	for(j=0;j<colnum;j++) {
		scproblem_set_cost(problem,j,(int)_matrix->GetCost(j));
	}

	/*Creating hybrid algorithm*/
	hybrid=hybrid_new(problem);
	assert(hybrid!=NULL);

	/*Default parameters*/
				 /*GAI GAMP GAPS*/
	SetParameters(1000,0.1,100,
		          /*ILGT,LCELG?,LCELGT*/
		          1,0,1,
				  /*SAPFM,SATEMP,SACOOL,SAFRZT,SAITR*/
				  0,1000,0.75,3,1000);
	
	/*Done*/
}

HybridCover::~HybridCover(void) {
	if (problem!=NULL) scproblem_delete(problem);
	if (hybrid!=NULL) hybrid_delete(hybrid);
	if (solution!=NULL) free(solution);
}

void HybridCover::SetParameters(int nbgaMaxIterations,double nbgaMutationProbability,int nbgaPopulationSize,int initialLGHType,int LCEUseLGH,int LCELGHType,int saPerform,double saTemp,double saCool,double saFrozen,int saIterations) {
	
	hybrid_set_parameters(hybrid,nbgaMaxIterations,nbgaMutationProbability, nbgaPopulationSize, initialLGHType, LCELGHType,LCEUseLGH);
	hybrid_set_sa_parameters(hybrid,saPerform,saTemp,saFrozen,saCool,saIterations);

}


list<unsigned int> HybridCover::Run(void) {

	int k=0;
	int cost=0;
	list<unsigned int> lst;

	k=hybrid_solve(hybrid,1);
	solution = hybrid_get_solution(hybrid,&cost);

	/*hybrid_print_csv(hybrid,stdout,"NOFILE",0,"NODATE","NODATE");*/

	for(k=0;k<colnum;k++) {
		if (solution[k]==1) lst.push_back((unsigned int)k);
	}

	return lst;
}