Example #1
0
void demo(int seed, clock_t begin) {
	const Instance instance = getDatasets()[0];
	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);

	const int sizePopulation = 40;
	const int maxPopulation = 200;
	const int itNoImprovement = 500;
	const int itDiv = 0.4*itNoImprovement;

	int it = 0;
	int lastImprovement = 0;
	int lastDiv = 0;
	double bestCost = MAX_FLOAT;
	double delta = 0.000000000;
	
	vector<int*> population;
	vector<pdi> costHeap;
	double costS;

	int* bestSolution;
	vector<KCenterSolver*> solvers;

	//make_heap(costHeap.begin(), costHeap.end(), smallerOp());

	/*vector<pii> G = loadGraph(dataFrame.getSim(), n);
	for(int i = 0; i < sizePopulation; i++) {
		int* s0 = graspConstructive(dataFrame, G);
		KCenterSolver* ind = createSolver(dataFrame, s0);
		ind->localSearch(conflictGraph);
		population.push_back(ind->getSolution());
		costS = ind->getCost();
		solvers.push_back(ind);

		if(costS < bestCost) {
			bestCost = costS;
			bestSolution = ind->getSolution();
		}
		//delete ind;
		printf("g(%d) = %.15f\n", i, costS);
	}*/

	population = getPopulation(instance, sizePopulation);
	
	for(int i = 0; i < sizePopulation; i++) {
		KCenterSolver* ind = createSolver(dataFrame, population[i]);
		ind->localSearch(conflictGraph);
		population[i] = ind->getSolution();
		costS = ind->getCost();
		solvers.push_back(ind);

		if(costS < bestCost) {
			bestCost = costS;
			bestSolution = ind->getSolution();
		}
		//delete ind;
		printf("g(%d) = %.15f\n", i, costS);
		double elapsedSecs = double(clock() - begin) / CLOCKS_PER_SEC;
		cout << elapsedSecs << endl;
	}

	while((it-lastImprovement) < itNoImprovement) {
		
		int* offspring1 = new int[n];
		//int* offspring2 = new int[n];

		//costHeap = fill(solvers, sizePopulation, m, d);

		int* p1 = tournamentSelection2(population, 2, population.size(), dataFrame);
		int* p2 = tournamentSelection2(population, 2, population.size(), dataFrame);

		crossoverX(p1, p2, offspring1, n, m);
		//crossoverUniform(p1, p2, offspring1, offspring2, 0.3, n);

		KCenterSolver* off1 = createSolver(dataFrame, offspring1);
		off1->localSearch(conflictGraph);
		double off1Cost = off1->getCost();
		solvers.push_back(off1);

		if(off1Cost < bestCost) {
			bestCost = off1Cost;
			bestSolution = off1->getSolution();
			lastImprovement = it;
		}

		population.push_back(off1->getSolution());

		if(population.size() >= maxPopulation) {
			//cout << "-- selectSurvivors()" << endl;
			costHeap = fill(solvers, sizePopulation, m, d);
			population = selectSurvivors(costHeap, population, sizePopulation, dataFrame);
			solvers = renewSolvers(solvers, population, dataFrame);
		}

		double minCost = MAX_FLOAT;
		int minIndex = 0;

		if( ((it-lastImprovement) >= itDiv) && ((it-lastDiv) >= itDiv) ) {
			lastDiv = it;
			//cout << "-- diversifyPopulation()" << endl;
			costHeap = fill(solvers, sizePopulation, m, d);
			population = diversifyPopulation(costHeap, population, sizePopulation/4, 6*sizePopulation, dataFrame, conflictGraph);
			solvers = renewSolvers(solvers, population, dataFrame);

			for(int i1 = 0; i1 < solvers.size(); i1++) {
				if(solvers[i1]->getCost() < minCost) {
					minCost = solvers[i1]->getCost();
					minIndex = i1;
				}
			}

			if(minCost < bestCost) {
				lastImprovement = it;
				bestCost = minCost;
				bestSolution = solvers[minIndex]->getSolution();
			}
		}

		it++;
		printf("%d) %.15f %s %d %s %d \n", it, bestCost, "size_pop =", (int)population.size(), "last_imp =", lastImprovement);
	}

	delete [] annotated;

	printf("%.15f ", bestCost);
	//printSolution(bestSolution, n);
	//evalSolution(bestSolution, dataFrame.getLabel(), instance, "ga");
}
Example #2
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);
}