示例#1
0
int main(int argc, char *argv[]) {

	if( argc < 10){ 
		cout << "Call: ./ompRelaxmap <seed> <network.net> <# threads> <# attempts> <threshold> <vThresh> <maxIter> <outDir> <prior/normal>  [selflinks]" << endl;
		exit(-1);
	}
	
	string outDir = string(argv[8]);
	int maxIter = atoi(argv[7]);	// Set the maximum number of iteration..
	int Ntrials = atoi(argv[4]);  // Set number of partition attempts
	int numThreads = atoi(argv[3]);	// Set number of threads...
	string line;
	string buf;
  
	MTRand *R = new MTRand(stou(argv[1]));

	string infile = string(argv[2]);
	string networkFile = string(argv[2]);
	string networkName(networkFile.begin() + networkFile.find_last_of("/"), networkFile.begin() + networkFile.find_last_of("."));
	string networkType(infile.begin() + infile.find_last_of("."), infile.end());

	double threshold = atof(argv[5]);
	double vThresh = atof(argv[6]);		// vertex-threshold: threshold for each vertex-movement.

	cout << "Threshold = " << threshold << ", Vertex-Threshold = " << vThresh << endl;
	vThresh *= -1;	// change the threshold value for negative.

	string priorFlag = string(argv[9]);

	bool prior = false;
	if (priorFlag == "prior")
		prior = true;

	bool includeSelfLinks = false;
	if(argc == 11) {
		string selfLinks(argv[10]);
		if(selfLinks == "selflinks")
			includeSelfLinks = true;
	}

	Network origNetwork;	// default constructor is called.

	origNetwork.R = R;
	
	// time values for measuring elapsed times for each step...
	struct timeval allStart, allEnd;
	struct timeval noIOstart, noIOend;
	struct timeval start, end;

	gettimeofday(&allStart, NULL);
	gettimeofday(&start, NULL);

	if(networkType == ".net"){
		load_pajek_format_network(networkFile, origNetwork);    
	}
	else{
		load_linkList_format_network(networkFile, origNetwork); 
	}

	gettimeofday(&end, NULL);

	cout << "Time for reading input data : " << elapsedTimeInSec(start, end) << " (sec)" << endl;

	gettimeofday(&noIOstart, NULL);

	int nNode = origNetwork.NNode();
	double totNodeWeights = origNetwork.TotNodeWeights();
  
	cout << "total Node weights = " << totNodeWeights << endl;

	gettimeofday(&start, NULL);

	for (int i = 0; i < nNode; i++) {
		origNetwork.nodes[i].setTeleportWeight(origNetwork.nodes[i].NodeWeight()/totNodeWeights);
	}

	int NselfLinks = 0;
	for(map<pair<int,int>,double>::iterator it = origNetwork.Edges.begin(); it != origNetwork.Edges.end(); it++){
        
		int from = it->first.first;
		int to = it->first.second;
		double weight = it->second;
		if(weight > 0.0){
			if(from == to){
				NselfLinks++;
                //if(includeSelfLinks)
                //	origNetwork.nodes[from]->selfLink += weight;
			}   
			else{
				origNetwork.nodes[from].outLinks.push_back(make_pair(to,weight));
				// we will going to update inLinks, after we got final flow of the network.
				//origNetwork.nodes[to].inLinks.push_back(make_pair(from,weight));
			}   
		}   
	}
  

    if(includeSelfLinks)
		//cout << "including " <<  NselfLinks << " self link(s)." << endl;  
		cout << "current version always excludes self links.\nignoring " << NselfLinks << " self link(s)." << endl;
    else
        cout << "ignoring " <<  NselfLinks << " self link(s)." << endl;

	//Swap vector to free memory
	map<pair<int,int>,double>().swap(origNetwork.Edges);
        
	cout << "DONE: Parsing the given network  ..." << endl;

	gettimeofday(&end, NULL);
	cout << "Time for parsing the given network : " << elapsedTimeInSec(start, end) << " (sec)" << endl;


	gettimeofday(&start, NULL);
	// Initialization.
	origNetwork.initiate(numThreads);

	// Now update inLinks..
	for (int i = 0; i < nNode; i++) {
		int nOutLinks = origNetwork.nodes[i].outLinks.size();
		for (int j = 0; j < nOutLinks; j++)
			origNetwork.nodes[origNetwork.nodes[i].outLinks[j].first].inLinks.push_back(make_pair(i,origNetwork.nodes[i].outLinks[j].second));
	}

	gettimeofday(&end, NULL);

	cout << "DONE: Initiate() ... in " << elapsedTimeInSec(start, end) << " (sec)" << endl;
	cout << "Initial Code Length: " << origNetwork.CodeLength()/log(2.0) << " in " << origNetwork.NModule() << " modules.\n";

	
	// copy size of each node for print in order.
	vector<double> nodeSize(nNode);
	for (int i = 0; i < nNode; i++)
		nodeSize[i] = origNetwork.nodes[i].Size();

	
	cout << "Now partition the network starts...\n";
	gettimeofday(&start, NULL);


	
	bool fineTune = true;
	bool fast = false;		// This will be true only for sub-module partitioning...

	int step = 1;

	// Initial SuperStep running ...
	double oldCodeLength = origNetwork.CodeLength();

	stochastic_greedy_partition(origNetwork, numThreads, threshold, vThresh, maxIter, prior, fineTune, fast);
	cout << "SuperStep [" << step << "] - codeLength = " << origNetwork.CodeLength()/log(2.0) << " in " << origNetwork.NModule() << " modules." << endl;

	bool nextIter = true;

	if ((oldCodeLength - origNetwork.CodeLength())/log(2.0) < threshold)
		nextIter = false;

	struct timeval subStart, subEnd;

	while (nextIter) {	
		oldCodeLength = origNetwork.CodeLength();

		stochastic_greedy_partition(origNetwork, numThreads, threshold, vThresh, maxIter, prior, fineTune, fast);

		step++;
		cout << "SuperStep [" << step << "] - codeLength = " << origNetwork.CodeLength()/log(2.0) << " in " << origNetwork.NModule() << " modules." << endl;

		if ((oldCodeLength - origNetwork.CodeLength())/log(2.0) < threshold)
			nextIter = false;

		fineTune = !fineTune;	// fine-tune and coarse-tune will be done alternatively.

		if (nextIter && !fineTune) {
			// Next iteration will be Coarse Tune.
			gettimeofday(&subStart, NULL);
			generate_sub_modules(origNetwork, numThreads, threshold, maxIter);
			gettimeofday(&subEnd, NULL);
			cout << "Time for finding sub-modules: " << elapsedTimeInSec(subStart, subEnd) << " (sec)" << endl;
		}
	}
	
	gettimeofday(&end, NULL);
	cout << "Time for partitioning : " << elapsedTimeInSec(start, end) << " (sec)" << endl;

	cout << "DONE: Code Length = " << origNetwork.CodeLength()/log(2.0) << " in ";
	cout << origNetwork.NModule() << " modules, with " << nNode << " nodes.\n" << endl;

	gettimeofday(&noIOend, NULL);
	gettimeofday(&allEnd, NULL);
	cout << "Overall Elapsed Time for Module Detection (w/o file IO): " << elapsedTimeInSec(noIOstart, noIOend) << " (sec)" << endl;
	cout << "Overall Elapsed Time for Module Detection (w/ file Reading): " << elapsedTimeInSec(allStart, allEnd) << " (sec)" << endl;

	
	cout << "\nComputed Code Length = " << origNetwork.calculateCodeLength()/log(2.0) << endl;

	//Print two-level clustering result in .tree file
	print_twoLevel_Cluster(origNetwork, networkName, outDir);


	// Print partition in Pajek's .clu format
	ofstream outFile;
	ostringstream oss;

	oss.str("");
	oss << outDir << "/" << networkName << ".clu";
	outFile.open(oss.str().c_str());
	outFile << "*Vertices " << nNode << "\x0D\x0A";
	for(int i=0;i<nNode;i++)
		outFile << origNetwork.nodes[i].ModIdx() + 1 << "\x0D\x0A";
	outFile.close();


}