Example #1
0
void demo(Instance instance, int seed) {
	const int n = instance.N;
	const int m = instance.M;
	const int d = instance.D;
	const int NUM_ANNOTATED = PORC_ANNOTATED * n;
	
	srand(seed);

	int* annotated;
	vector<int> conflictGraph[n];

	DataFrame dataFrame = load(instance);
	annotated = randAnnotated(n, NUM_ANNOTATED);
	imposeConflicts(annotated, dataFrame.getLabel(), conflictGraph, NUM_ANNOTATED);

	int* s0;
	double costS;
	double bestCost = MAX_FLOAT;
	int* bestSolution = new int[n];

	int numRestarts = 100;
	int p = 1;
	int numPert = 1000;
	int numExchanges = 0.3*instance.N;
	double costS_;

	vector<pii> G = loadGraph(dataFrame.getSim(), n);

	// grasp phase
	for(int i = 0; i < numRestarts; i++) {
		s0 = graspConstructive(dataFrame, G);
		KCenterSolver* solver = createSolver(dataFrame, s0);
		solver->localSearch(conflictGraph);
		costS = solver->getCost();

		if(costS < bestCost) {
			bestCost = costS;
			bestSolution = solver->getSolution();
		}

		while(p <= numPert) {
			int* s = perturbation(solver->getSolution(), numExchanges, n, m);
			KCenterSolver* solver2 = createSolver(dataFrame, s);
			solver2->localSearch(conflictGraph);
			costS = solver2->getCost();
		
			if(costS < bestCost) {
				bestCost = costS;
				bestSolution = solver2->getSolution();
			}
			delete solver2;
			//printf("f(%d) = %.15f\n", p, bestCost);
			p++;
		}

		p = 1;
		
		printf("g(%d) = %.15g\n", i, bestCost);

		delete solver;
	}

	/*printf("bestCost = %.15f\n", bestCost);

	int p = 1;
	int numPert = 1000;
	int numExchanges = 0.3*instance.N;
	double costS_;

	// perturbation phase
	while(p <= numPert) {
		int* s = perturbation(bestSolution, numExchanges, n, m);
		KCenterSolver* solver = createSolver(dataFrame, s);
		solver->localSearch(dataFrame, conflictGraph);
		costS_ = solver->getCost();
		
		if(costS_ < bestCost) {
			bestCost = costS_;
			lastImp = p;
			bestSolution = s;
		}
		delete solver;
		printf("f(%d) = %.15f\n", p, bestCost);
		p++;
	}*/

	evalSolution(bestSolution, dataFrame.getLabel(), instance, "perturbation");
	printSolution(bestSolution, instance.N);
}