Ejemplo n.º 1
0
// {{{ Message Management
// {{{ handleMessage
int handleMessage(msg_t msg) {
	switch (type(msg)) {
		case HELLO:
			return handleHello(msg);
		case HELLOREP:
			return handleHelloRep(msg, NULL);
		case RESOURCE:
			return handleResource(msg);
		case SOLUTION:
			return handleSolution(msg);
		default:
			if (_verbose) fprintf(stderr, "======> Uknown message type <======\n");
			return -1;
	}
}
double runAlgoSequence(AlgoDescriptor const* descriptor, GMMDesc initialSolution, double initialTime,
	std::string const& outputDir, std::string const& outputFile)
{
	// Initialize output
	std::stringstream pathStream;
	pathStream << outputDir << "/csv";
	boost::filesystem::path outPath(pathStream.str());
	boost::filesystem::create_directories(outPath);
	std::stringstream filenameStream;
	filenameStream << pathStream.str() << "/" << outputFile;
	ioutil::openStatisticFile(filenameStream.str());


	// compute maximum spread
	Vector spreads = (input.points.rowwise().maxCoeff()
					 -input.points.rowwise().minCoeff());
					 
	if (commonSettings().verbose)
		std::cout << "maximum spread is " << spreads.maxCoeff() << std::endl;

	// Initialisation
	double totaltime = initialTime;
	GMMDesc last = initialSolution;

	// handle initial solution
	handleSolution(0, last, totaltime, true);

	// start computation of new algorithm sequence
	std::cout << "starting new algorithm sequence:" << std::endl;
			  
	AlgoDescriptor const* nextAlgo = descriptor;
	std:size_t roundOffset = 0;
	std::vector<std::size_t> finalRounds;
	std::vector<double> finalTimes;
	while (nextAlgo!=NULL)
	{
		std::cout << "creating algorithm from descriptor: "
				  << nextAlgo->tag() << std::endl;
				  
		GMMAlgorithm* algorithm = nextAlgo->create(input);
		algorithm->init(last);
		
		// run current algorithm
		for (std::size_t i=0; i<nextAlgo->steps(); ++i)
		{
			algorithm->run();
			handleSolution(roundOffset+i+1, algorithm->getGMMDesc(),
				algorithm->getRuntime()+totaltime, algorithm->hasChanged());
		}
		
		// Save results and delete algorithm
		last = algorithm->getGMMDesc();
		roundOffset += nextAlgo->steps();
		totaltime += algorithm->getRuntime();
		finalRounds.push_back(roundOffset);
		finalTimes.push_back(totaltime);
		std::cout << "   computation took " << algorithm->getRuntime() << " seconds." << std::endl;

		delete algorithm;

		nextAlgo = nextAlgo->getNext();
	}
	std::cout << std::endl;

	if (ioutil::StatisticFileIsOpen())
	{
		// Store statistics markers
		ioutil::storeStatisticMarkerVector(finalRounds, finalTimes, mincost_round, mincost_runtime);

		// Print overall results
		if (!truth.empty())
			std::cout << "         cost of truth = " << getCosts(input, truth) << std::endl;
		std::cout << "cost of final solution = " << costs.back() << std::endl;
		std::cout << "     cheapest solution = " << mincost << " after " << mincost_runtime << " seconds" << std::endl;
	}

	std::cout << std::endl;
	
	// Finalize output
	ioutil::closeAllFiles();
	
	return totaltime-initialTime;
}