bool JobShopData::calculateTables() { try { fillLO(); fillOFs(); fillPI(); fillPS(); fillLP(); prepareQueue(); } catch (...) { return false; } return true; }
static int acceptSocketConnection ( int (*getSocket) (void), int (*prepareQueue) (int socket), void (*unbindAddress) (const struct sockaddr *address), const struct sockaddr *localAddress, socklen_t localSize, struct sockaddr *remoteAddress, socklen_t *remoteSize ) { int serverSocket = -1; int queueSocket; if ((queueSocket = getSocket()) != -1) { if (!prepareQueue || prepareQueue(queueSocket)) { if (bind(queueSocket, localAddress, localSize) != -1) { if (listen(queueSocket, 1) != -1) { int attempts = 0; { char *address = formatSocketAddress(localAddress); if (address) { logMessage(LOG_NOTICE, "listening on: %s", address); free(address); } } while (1) { fd_set readMask; struct timeval timeout; FD_ZERO(&readMask); FD_SET(queueSocket, &readMask); memset(&timeout, 0, sizeof(timeout)); timeout.tv_sec = 10; ++attempts; switch (select(queueSocket+1, &readMask, NULL, NULL, &timeout)) { case -1: if (errno == EINTR) continue; LogSocketError("select"); break; case 0: logMessage(LOG_DEBUG, "no connection yet, still waiting (%d).", attempts); continue; default: { if (!FD_ISSET(queueSocket, &readMask)) continue; if ((serverSocket = accept(queueSocket, remoteAddress, remoteSize)) != -1) { char *address = formatSocketAddress(remoteAddress); if (address) { logMessage(LOG_NOTICE, "client is: %s", address); free(address); } } else { LogSocketError("accept"); } } } break; } } else { LogSocketError("listen"); } if (unbindAddress) unbindAddress(localAddress); } else { LogSocketError("bind"); } } close(queueSocket); } else { LogSocketError("socket"); } operations = &socketOperationsEntry; return serverSocket; }
//TABU SEARCH void JobShopData::TabuSearch() { const unsigned int tabuListLength = 7; unsigned int iterations = 1000; unsigned int task1, task2; mPh2.clear(); //przepisanie tablicy pi wyliczonej przez poprzedni algorytm std::vector<unsigned int>originalPI = mPI; mCurrentBestPI = mPI; std::pair<int, int> BestMove; unsigned int BestCmax = *std::max_element(mC.begin(), mC.end());; unsigned int BestTempCmax = 99999; unsigned int TempCmax = 99999; mTabuList.insert(mTabuList.begin(), tabuListLength, std::make_pair(0,0)); for (auto i = 0; i < iterations; ++i) { fillPS(); fillLP(); prepareQueue(); countCmax(); fillPH(); fillCriticalPath(); fillPH2(); for(auto& pair: mPh2) { std::vector < std::pair<int, int>> all_changes = geneateAllPossibleChangesInBlock(pair); //czy Ph2[i] jest na liscie tabu //if (std::min(Ph2[j], Ph2[j + 1]) == TabuList[k].first && std::max(Ph2[j], Ph2[j + 1]) == TabuList[k].second) if (false == elementInTabuList(pair)) { task1 = std::get<0>(pair); task2 = std::get<1>(pair); std::swap(mPI[mPS[mCriticalPath[task1]]], mPI[mPS[mCriticalPath[task2]]]); fillPS(); fillLP(); prepareQueue(); countCmax(); TempCmax = *std::max_element(mC.begin(), mC.end()); if (BestTempCmax > TempCmax) { BestTempCmax = TempCmax; BestMove = pair; } //odtworz pi mPI = originalPI; } } //Ph2[0] = BestMove.first; //Ph2[1] = BestMove.second; //SwapPi(0); task1 = std::get<0>(BestMove); task2 = std::get<1>(BestMove); std::swap(mPI[mPS[mCriticalPath[task1]]], mPI[mPS[mCriticalPath[task2]]]); if (BestTempCmax < BestCmax) { mCurrentBestPI = mPI; } //for (int i = 0; i < OriginalPi.size(); i++) //{ // OriginalPi[i] = Pi[i]; //} originalPI = mPI; mTabuList.pop_back(); mTabuList.insert(mTabuList.begin(), 1, BestMove); ; } }
///// do innej klasy void JobShopData::AlgorytmZstepujacy() { unsigned int task1, task2; std::vector<unsigned int> tmpCmax; //wektor Cmax'ow dla dla kazdej zamiany z listy Ph2 tmpCmax.clear(); tmpCmax.resize(mPh2.size()); //przepisanie tablicy pi wyliczonej przez poprzedni algorytm mCurrentBestPI = mPI; //zapamietanie wstepnego Cmax unsigned int globalCmax = *std::max_element(mC.begin(), mC.end()); bool isChanged = true; while (isChanged) { for (auto i = 0; i < mPh2.size(); ++i) { auto iterator = mPh2.begin(); std::advance(iterator, i); // przesun iterator o i ??? task1 = std::get<0>(*iterator); task2 = std::get<1>(*iterator); std::swap(mPI[mPS[mCriticalPath[task1]]], mPI[mPS[mCriticalPath[task2]]]); //oO fillPS(); fillLP(); prepareQueue(); countCmax(); tmpCmax[i] = *std::max_element(mC.begin(), mC.end()); mPI = mCurrentBestPI; } //indeks elemetu z tablicy tmpCmax o najmniejszym Cmaxie po zamianie unsigned int IndexOfMinCmax = std::distance(tmpCmax.begin(), std::min_element(tmpCmax.begin(), tmpCmax.end())); //czy nastapila globalna poprawa if (globalCmax > tmpCmax[IndexOfMinCmax]) { globalCmax = tmpCmax[IndexOfMinCmax]; auto iterator = mPh2.begin(); std::advance(iterator, IndexOfMinCmax); // przesun iterator o i ??? task1 = std::get<0>(*iterator); task2 = std::get<1>(*iterator); std::swap(mPI[mPS[task1]], mPI[mPS[task2]]); mCurrentBestPI = mPI; mPh2.clear(); fillPS(); fillLP(); prepareQueue(); countCmax(); fillPH(); fillCriticalPath(); fillPH2(); } else { isChanged = false; } } }