Route * AnnealingAlgorithm::calculateRoute(AdjacencyMatrix *adjacencyMatrix, unsigned int startNode) { emit started(); double t =Temperature; //temperatura początkowa double tempEnd = EndTemperature; double p =AnnealingParameter; //współczynnik //ROZWIAZANIE POCZATKOWE" Route * sol = new Route(adjacencyMatrix->getSize(),adjacencyMatrix); sol->makeRandomRoute(startNode); Route * bestSol = new Route(sol);// wylosowane rozwiązanie jest automatycznie najlepszym, ponieważ jest to jedyne rozwiązanie Route * _sol = new Route(sol); //_sol - rozwiązanie sąsiednie int costSol, cost_Sol, costBestSol; costSol = sol->getCost(costType); costBestSol = costSol; // algorytm wyżarzania double temp = t; while (temp>tempEnd)//!(worseSol > worseAcceptable)) { _sol = makeMutation(sol); // rozwiązanie sąsiednie cost_Sol = _sol->getCost(costType); // sprawdzenie czy nowe rozwiązanie jest lepsze od najlepszego if (cost_Sol < costBestSol) { bestSol = _sol; costBestSol = cost_Sol; } double delta = cost_Sol - costSol; if (delta < 0) { sol = _sol; costSol = cost_Sol; //worseSol = 0; } else { // worseSol++; double x = (rand() % 10000) / 10000.0; // losowa liczba z zakresu <0, 1) if (x < (exp((-delta) / temp))) { sol = _sol; costSol = cost_Sol; } } temp *= p; // zmniejszamy temperature wg zadanego wspolczynnika } //przerywamy dzialanie algorytu jeśli przekroczymy wartość temperatury końcowej emit finished(); return bestSol; }
Route * GeneticAlgorithm::calculateRoute(AdjacencyMatrix *adjacencyMatrix, unsigned int startNode) { emit started(); QList<Route*> routeListToNextRound; QList<Route*> routeList; QList<Route*> routeListToDoOperations; Route * currentBestRoute= NULL; unsigned int populationSizeRatio = 8; unsigned int populationSize = adjacencyMatrix->getSize() * populationSizeRatio; unsigned int roundsWithoutBetterRoute = 0; //int numberOfRoutesToNextRound for(unsigned int i = 0; i < populationSize; i++) { Route * tempRoute = new Route(adjacencyMatrix->getSize(),adjacencyMatrix); tempRoute->makeRandomRoute(startNode); routeListToNextRound.append(tempRoute); } do { int routeListToNextRoundSize = routeListToNextRound.size(); for(int j = 0; j < routeListToNextRoundSize; j++) { routeList.append(routeListToNextRound.at(0)); routeListToNextRound.removeFirst(); } if(costType == Route::WITH_TIME)qSort(routeList.begin(),routeList.end(),routeLessThanWithTime); if(costType == Route::WITHOUT_TIME)qSort(routeList.begin(),routeList.end(),routeLessThanWithoutTime); if (currentBestRoute == NULL) { //currentBestRoute = new Route(*routeList.at(0)); Route * tempRoute = new Route(routeList.at(0)); //*tempRoute = *routeList.at(0); currentBestRoute = tempRoute; } else { if(routeList.at(0)->getCost(costType) < currentBestRoute->getCost(costType)) { delete(currentBestRoute); Route * tempRoute = new Route(routeList.at(0)); //Route * tempRoute = new Route(routeList.at(0)->getSize(),routeList.at(0)->getAdjacencyMatrix()); //*tempRoute = *routeList.at(0); //currentBestRoute = new Route(*routeList.at(0)); currentBestRoute = tempRoute; roundsWithoutBetterRoute = 0; } else { roundsWithoutBetterRoute++; } } int routeListSize = routeList.size(); for(int k = 0; k < routeListSize/10 ; k++) { routeListToDoOperations.append(routeList.at(0)); routeList.removeFirst(); } qDeleteAll(routeList); routeList.clear(); for(int l = 0; l < 10; l++) { routeListToNextRound.append(crossbreedRoutes(routeListToDoOperations.at(0),routeListToDoOperations.at(1))); } for(int l = 0; l < populationSize/2; l++) { routeListToNextRound.append(crossbreedRoutes(routeListToDoOperations.at(qrand() % routeListToDoOperations.size()),routeListToDoOperations.at(qrand() % routeListToDoOperations.size()))); } for(int m = 0; m < populationSize/2; m++) { routeListToNextRound.append(makeMutation(routeListToNextRound.at(qrand() % routeListToNextRound.size()))); } routeListToNextRound.append(new Route(currentBestRoute)); qDeleteAll(routeListToDoOperations); routeListToDoOperations.clear(); } while(roundsWithoutBetterRoute < 12); qDeleteAll(routeListToNextRound); routeListToNextRound.clear(); //qDeleteAll(routeListToDoOperations); //routeListToDoOperations.clear(); emit finished(); return currentBestRoute; }