예제 #1
0
//method takes a graph and finds the best local optimum using 3-opt neighbors
//returns number of coflicts
int threeOptSteepestDecent(graph &g, bool repeat, int numColors) {
	double startTime = (double) (clock() / CLOCKS_PER_SEC);
	graph prevBest;
	while ( (double)(clock() / CLOCKS_PER_SEC) - startTime < 600) {
		do {
			if (repeat) {
				g.randomize(numColors);
			}
			prevBest = g;
			g = prevBest.threeOptNeighbor(600 + startTime);
			if ( (double)(clock() / CLOCKS_PER_SEC) - startTime > 600) {
				break;
			}
		}
		while (prevBest.numConflicts() > g.numConflicts());
		if (!repeat) {
			break;
		}
	}
	return g.numConflicts();
}
예제 #2
0
//method finds coloring of graph to minimize conflicts
//returns number of conflicts
int exhaustiveColoring(graph &g, int numColors, int t) {
	//vector to hold answer with least conflicts seen so far
	//initially ever node is color 1
	vector<int> bestAnswer(g.numNodes(), 1);
	//vector to hold answer currently being tested
	//also set to all color 1
	vector<int> currentAnswer = bestAnswer;
	//int to hold number of conflicts in bestAnswer
	//initialized to max number of cnflicts for given graph
	int conflicts = g.numEdges();
	//initilize starting time
	double startTime = (double) (clock() / CLOCKS_PER_SEC);

	//while time elapsed is within given time
	while ( (double)(clock() / CLOCKS_PER_SEC) - startTime < t) {
		//change graph to have coloration of currentAnswer
		for (int i = 0; i < currentAnswer.size(); i++) {
			g.setColor(i, currentAnswer[i]);
		}
		//if current graph is asgood as or better than best graph
		//set best graph to current graph
		if (g.numConflicts() <= conflicts) {
			conflicts = g.numConflicts();
			bestAnswer = currentAnswer;
		}
		//break if all permutations of colors have been tested
		//algorithm is done
		if (!increment(currentAnswer, numColors)) {
			break;
		}
	}
	//set coloration of graph to best rsult
	for (int i = 0; i < bestAnswer.size(); i++) {
		g.setColor(i, bestAnswer[i]);
	}
	return conflicts;
}
예제 #3
0
//unmodified natural greedy algortihm
//
int naturalGreedyColoring(graph &g, int numColors, int t) {
	//vector to hold answer with least conflicts seen so far
	//initially ever node is color 0
	//can be usd in testing
	//vector<int> answer(g.numNodes(), 0);

	//iterate over all nodes
	for (int n = 0; n < g.numNodes(); n++) {
		//can be used in testing
		//answer[n] = g.getfirstAvailColor(n, numColors);
		
		//pick color with fewest conflicts and assign to node
		g.setColor(n,  g.getfirstAvailColor(n, numColors));
	}
	return g.numConflicts();
}