예제 #1
0
void TabuSearch(){
        int debug;
        int number_of_iterations = 100;
        int TabuLength = n;
        get_first_path();
        vector<vector<int> > tabuList(TabuLength, std::vector<int>(TabuLength));
        vector <int> bestSol((int)current_path.size());
        bestSol = current_path;
    int shortest_path = GetTotalDistance(current_path);
 
    for (int i = 0; i < number_of_iterations;i++) {
 
        current_path = getBestNeighbour(tabuList, current_path);
 
                int currCost = GetTotalDistance(current_path);
 
 
        if (currCost < shortest_path) {
                        bestSol = current_path;
            shortest_path = currCost;
        }
    }
 
    printf("shortest_path = %d", shortest_path);
        getchar();
        getchar();
}
예제 #2
0
vector<int> bruteForce(vector<vector<int> > graph, int type){
	int n = graph.size() ;
	vector<int> solution (n);
	vector<int> bestSol (n);

	vector<int> p (n-1);
	for (int i = 0; i < n -1 ; i++)
		p[i] = solution[i] = bestSol[i] = i;
	solution[n-1] = bestSol[n-1] = n-1 ;
	int min = poids(graph, solution, type);

	unsigned int i = 1 ;
	while(i < ((unsigned int) n - 1)){
		--p[i];
		int j = (i % 2 == 1) ? p[i] : 0;
		swap(&solution, i+1, j+1);

		int tmp = poids(graph, solution, type);

		if (tmp < min){
			min = tmp;
			bestSol = solution ;
		}
		i = 1;
		while (i < p.size() && p[i] == 0){
			p[i] = i;
			i++;
		}
	}
	return bestSol ;
}
예제 #3
0
std::vector<int> getBestNeighbour(vector<vector<int> > tabuList, vector <int> initSolution) {
 
 
        vector <int> bestSol((int)initSolution.size());
                bestSol = initSolution;
        int bestCost = GetTotalDistance(initSolution);
        int city1 = 0;
        int city2 = 0;
        bool firstNeighbor = true;
 
        for (int i = 1; i < (int)bestSol.size() - 1; i++) {
            for (int j = 2; j < (int)bestSol.size() - 1; j++) {
                if (i == j) {
                    continue;
                }
 
                vector <int> newBestSol(bestSol.size());
                                newBestSol = bestSol;
 
                newBestSol = swapOperator(i, j, initSolution); //sproboj zamienic miasta
                int newBestCost = GetTotalDistance(newBestSol);
 
 
 
                if ((newBestCost > bestCost || firstNeighbor) && tabuList[i][j] == 0) { //jesli znaleziono lepszy ruch - zapisz
                    firstNeighbor = false;
                    city1 = i;
                    city2 = j;
                                        bestSol = newBestSol;
                    bestCost = newBestCost;
                }
            }
        }
 
        if (city1 != 0) {
            decrementTabu(tabuList);
            tabuMove(city1, city2, tabuList);
        }
        return bestSol;
    }