sol::Solution apply(sol::Solution const & solution) { sol::Solution currentSolution(solution); int i = 0; int numMovedProcess = 0; do { int process = _distProcess(_gen); int machine = _distMachine(_gen); if (currentSolution.isFeasible(process, machine)) { sol::ObjValue deltaObjValue( currentSolution.evaluateFeasibleMove(process, machine)); currentSolution.moveProcess(process, machine, deltaObjValue); numMovedProcess++; } i++; boost::this_thread::interruption_point(); } while (numMovedProcess < _numMoves && i < 1000); return currentSolution; }
void apply(sol::Solution const & solution) { int numIter = 0; int lastBestIter = -1; sol::Solution bestSolution(solution); sol::Solution currentSolution(_localSearch->apply(solution)); if (isBetter(currentSolution, bestSolution)) { lastBestIter = 0; bestSolution = currentSolution; } do { currentSolution = _perturbation->apply(currentSolution); currentSolution = _localSearch->apply(currentSolution); _pool->addSolution(currentSolution); if (isBetter(currentSolution, bestSolution)) { lastBestIter = numIter; bestSolution = currentSolution; } numIter++; boost::this_thread::interruption_point(); } while ((numIter - lastBestIter) <= _maxNumNonImprovIter); }
bool DcModel::solveWithoutCuts (OsiCuts & cuts, int numberTries, DcTreeNode * node, int & numberOldActiveCuts, int & numberNewCuts, int & maximumWhich, int *& whichGenerator, const bool cutDuringRampup, int & found ) { found = -10; bool feasible; feasible = resolve(); if(!feasible) { return false; // If lost feasibility, bail out right now } // check integer feasibility of solution found = -1; // If found a solution, Record it before we free the vector int numberIntegerInfeasibilities = 0; bool integerFeasible = feasibleSolution(numberIntegerInfeasibilities); bool better; double currentObjValue = getCurrentObjValue(); double cutoff = getCutoff(); if (integerFeasible) { if (currentObjValue < cutoff) { better = setBestSolution(DC_BRANCH, currentObjValue, currentSolution()); } } reducedCostFix(); double minimumDrop = minimumDrop_; if (numberTries < 0) { numberTries = -numberTries; minimumDrop = -1.0; } incrementNodeCount(); return feasible; }
void SimpleTabuSearchReverse<TNeighborhood, TImprovement, TTabuList, TAspirationCriteria, TSolution>::run(const size_t numberOfSteps) { bool isMoveUp = false; TSolution currentSolution(bestSolution); size_t currentNumberOfUnchanged = 0; for (size_t i = 0; i < numberOfSteps; ++i) { std::cout << "Number of move " << i << std::endl; auto bestLocalMovePtr = getBestMove(currentSolution); if (nullptr == bestLocalMovePtr) { continue; } // apply move currentSolution.applyMove(*bestLocalMovePtr); ++currentNumberOfUnchanged; // std::cout << "number of unchanged " << currentNumberOfUnchanged << std::endl; if (currentSolution.getObjectiveValue() <= bestSolution.getObjectiveValue()) { bestSolution = currentSolution; currentNumberOfUnchanged = 0; } if (i >= 50) { isMoveUp = true; } double overheadsCoefficient = currentSolution.getOverheadsCoefficient() + 0.1; if (isMoveUp) { std::cout << "MOVE UP!!!" << std::endl; currentSolution.setOverheadsCoefficient(overheadsCoefficient); bestSolution.setOverheadsCoefficient(overheadsCoefficient); } if (0 == bestSolution.getObjectiveValue()) { // the objectives are achieved break; } // update tabuList and AspirationCriteria tabuList.update(*bestLocalMovePtr); aspirationCriteria.update(currentSolution); } }
sol::Solution apply(sol::Solution const & solution) { inst::integer bestValue; std::pair<int, int> bestMove; sol::ObjValue bestDeltaObjValue; sol::Solution currentSolution(solution); int numTries = 0; do { bestValue = std::numeric_limits<inst::integer>::max(); shuffleProcesses(); for (int i = 0; i < _numProcesses; i++) { int process = _processes[i]; shuffleMachines(); for (int j = 0; j < _numMachines; j++) { int machine = _machines[j]; if (currentSolution.assignment()[process] == machine) continue; if (!currentSolution.isFeasible(process, machine)) continue; sol::ObjValue deltaObjValue( currentSolution.evaluateFeasibleMove(process, machine)); inst::integer value = deltaObjValue.objValue(); if (value < bestValue) { bestValue = value; bestMove = std::make_pair(process, machine); bestDeltaObjValue = deltaObjValue; } } } if (bestValue < 0) { currentSolution.moveProcess(bestMove.first, bestMove.second, bestDeltaObjValue); _pool->addSolution(currentSolution); numTries = 0; } else { numTries++; } boost::this_thread::interruption_point(); } while(bestValue < 0 || numTries < _numTriesMax); return currentSolution; }
void ParallelTabuSearch<TNeighborhood, TTabuList, TAspirationCriteria, TSolution>::run(const size_t numberOfSteps) { // initialize current solution TSolution currentSolution(bestSolution); for (size_t i = 0; i < numberOfSteps; ++i) { auto moves = neighborhood.getMoves(currentSolution); if (moves.empty()) { std::cout << "Moves exhaustion, return in bestSolution" << '\n'; currentSolution = bestSolution; continue; } auto task = [](const ITabuList& tabu_list, const IAspirationCriteria& aspiration_criteria, const TSolution solution, const std::vector< std::unique_ptr<IMove> >& moves, size_t first, size_t last) -> std::pair<int, double> { //std::cout << "Work " << std::this_thread::get_id() << " thread" << std::endl; std::vector<double> objectiveValues; std::vector<size_t> indexs; // generate all (!tabu or aspiration) solutions from the current for (size_t i = first; i < last; ++i) { if (!tabu_list.isTabu(*moves[i]) || aspiration_criteria.overrideTabu(solution, *moves[i])) { objectiveValues.push_back(solution.tryOnMove(*moves[i])); indexs.push_back(i); } } if (objectiveValues.empty()) { // -1 => nonexistent index return std::make_pair (-1, -1.0); } // choice of the local best solution auto local_best = std::min_element(objectiveValues.begin(), objectiveValues.end()); size_t idx = std::distance(objectiveValues.begin(), local_best); return std::make_pair (static_cast<int>(indexs[idx]), *local_best); }; // for each block we calculate an index of the best in the array of moves size_t number_of_block = ((moves.size() + blockSize) / blockSize); std::vector< std::future< std::pair<int, double> > > index_of_best_moves(number_of_block - 1); for (size_t index_of_block = 0; index_of_block + 1 < number_of_block; ++index_of_block) { index_of_best_moves[index_of_block] = scheduler->schedule(task, std::ref(tabuList), std::ref(aspirationCriteria), currentSolution, std::ref(moves), index_of_block * blockSize, (index_of_block + 1) * blockSize); } size_t last = (number_of_block - 1) * blockSize + blockSize; if (last > moves.size()) { last = moves.size(); } auto last_index = task(tabuList, aspirationCriteria, currentSolution, moves, (number_of_block - 1) * blockSize, last); // choice of the best solution int index_of_best = -1; double best_obj_val = -1.0; for (size_t j = 0; j < number_of_block - 1; ++j) { auto result_of_calculations = index_of_best_moves[j].get(); if (-1 != result_of_calculations.first) { if (-1 == index_of_best) { index_of_best = result_of_calculations.first; best_obj_val = result_of_calculations.second; } else if (best_obj_val > result_of_calculations.second) { index_of_best = result_of_calculations.first; best_obj_val = result_of_calculations.second; } } } // handle last index if (-1 != last_index.first) { if (-1 == index_of_best) { index_of_best = last_index.first; best_obj_val = last_index.second; } else if (best_obj_val > last_index.second) { index_of_best = last_index.first; best_obj_val = last_index.second; } } if (-1 == index_of_best) { std::cout << "all moves is tabu" << '\n'; break; } // apply move currentSolution.applyMove(*moves[index_of_best]); //std::cout << best_obj_val << '\n'; if (best_obj_val <= bestSolution.getObjectiveValue()) { bestSolution = currentSolution; } if (0 == bestSolution.getObjectiveValue()) { break; } // update tabuList and AspirationCriteria tabuList.update(*moves[index_of_best]); aspirationCriteria.update(currentSolution); } }