Esempio n. 1
0
BaseMath &
BaseMath::graphReader( const std::string & fn ) {

	std::filebuf fb;
	fb.open(fn, std::ios::in);
	std::istream is(&fb);
	graphReader(is);
	fb.close();
	return *this;
}
Esempio n. 2
0
File: host.cpp Progetto: funkey/host
int main(int argc, char** argv) {

	util::ProgramOptions::init(argc, argv);
	logger::LogManager::init();

	host::Graph            graph;
	host::ArcWeights       arcWeights(graph);
	host::ArcLabels        arcLabels(graph);
	host::ArcTypes         arcTypes(graph);
	host::MultiEdgeFactors multiEdgeFactors;

	if (optionGraphFile) {

		host::WeightedGraphReader graphReader(optionGraphFile.as<std::string>());
		graphReader.fill(graph, arcWeights, arcLabels, arcTypes);

	} else {

		RandomWeightedGraphGenerator randomWeightedGraphGenerator(
				optionRandomGraphNodes,
				optionRandomGraphArcs,
				optionRandomGraphMinWeight,
				optionRandomGraphMaxWeight);

		randomWeightedGraphGenerator.fill(graph, arcWeights, arcLabels, arcTypes);

		std::cout
				<< "generated a random graph with "
				<< lemon::countNodes(graph)
				<< " nodes" << std::endl;
	}

	if (optionMultiEdgeFactorFile) {

		host::MultiEdgeFactorReader factorReader(optionMultiEdgeFactorFile.as<std::string>());
		factorReader.fill(graph, arcLabels, multiEdgeFactors);
	}

	if (lemon::countArcs(graph) <= 100) {

		for (host::Graph::ArcIt arc(graph); arc != lemon::INVALID; ++arc)
			std::cout
					<< graph.id(graph.source(arc)) << " - "
					<< graph.id(graph.target(arc)) << ": "
					<< arcWeights[arc] << std::endl;
	}

	// the minimal spanning tree
	host::ArcSelection mst(graph);

	// search the minimal spanning tree under consideration of conflicting 
	// candidates
	host::HostSearch hostSearch(graph);

	host::ExplicitWeightTerm    weightTerm(graph, arcWeights);
	host::CandidateConflictTerm cctTerm(graph, arcTypes);
	host::MultiEdgeFactorTerm   mefTerm(graph, multiEdgeFactors);

	hostSearch.addTerm(&weightTerm);
	hostSearch.addTerm(&cctTerm);
	hostSearch.addTerm(&mefTerm);

	double length;
	bool constraintsFulfilled = hostSearch.find(mst, length, optionNumIterations.as<unsigned int>());

	if (constraintsFulfilled)
		std::cout << "found a minimal spanning tree that fulfills the constraints" << std::endl;

	if (lemon::countArcs(graph) <= 100) {

		std::cout << "minimal spanning tree is:" << std::endl;
		for (host::Graph::ArcIt arc(graph); arc != lemon::INVALID; ++arc)
			std::cout
					<< graph.id(graph.source(arc)) << " - "
					<< graph.id(graph.target(arc)) << ": "
					<< mst[arc] << std::endl;
	}

	std::cout << "length of minimal spanning tree is " << length << std::endl;

	if (optionWriteResult) {

		host::WeightedGraphWriter graphWriter(optionWriteResult.as<std::string>());
		graphWriter.write(graph, arcWeights, mst);

		std::cout << "wrote result to " << optionWriteResult.as<std::string>() << std::endl;
	}

	return 0;
}