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); }
Solution AlgorithmVorace::concreteSolve(const Problem& problem) { Solution bestSolution(problem); std::uniform_real_distribution<float> randomGenerator = std::uniform_real_distribution<float>(0.f, 1.f); for (int i = 0; i < 10; i++) { Solution tempSolution(problem); std::random_device rd; std::mt19937 gen(rd()); std::vector<Location> possibleLocationLeft(problem.locations); std::vector<float> locationProbability; while (possibleLocationLeft.size() > 0) { float totalCost = 0.0f; for (auto&& location : possibleLocationLeft) { float cost = location.cost(); totalCost += cost; locationProbability.push_back(cost); } for (size_t i = 0; i < locationProbability.size(); ++i) { locationProbability[i] /= totalCost; } float chosenLocation = randomGenerator(gen); float totalProbability = 0.f; for (size_t i = 0; i < locationProbability.size(); ++i) { totalProbability += locationProbability[i]; if (totalProbability > chosenLocation) { if (possibleLocationLeft[i].chickenConsommation + tempSolution.totalConsommation() < tempSolution.totalChickenProduction) { tempSolution.locations.push_back(possibleLocationLeft[i]); } possibleLocationLeft.erase(possibleLocationLeft.begin() + i); break; } } locationProbability.clear(); } if (bestSolution.totalIncome() < tempSolution.totalIncome()) { bestSolution = tempSolution; } } return bestSolution; }
BSONObjSet PlanExecutor::getOutputSorts() const { if (_qs && _qs->root) { _qs->root->computeProperties(); return _qs->root->getSort(); } if (_root->stageType() == STAGE_MULTI_PLAN) { // If we needed a MultiPlanStage, the PlanExecutor does not own the QuerySolution. We // must go through the MultiPlanStage to access the output sort. auto multiPlanStage = static_cast<MultiPlanStage*>(_root.get()); if (multiPlanStage->bestSolution()) { multiPlanStage->bestSolution()->root->computeProperties(); return multiPlanStage->bestSolution()->root->getSort(); } } else if (_root->stageType() == STAGE_SUBPLAN) { auto subplanStage = static_cast<SubplanStage*>(_root.get()); if (subplanStage->compositeSolution()) { subplanStage->compositeSolution()->root->computeProperties(); return subplanStage->compositeSolution()->root->getSort(); } } return BSONObjSet(); }
void CNonLinearSolver::solve() { m_IGUState = m_IGU.getState(); std::vector<double> initialState(m_IGUState); std::vector<double> bestSolution(m_IGUState.size()); auto achievedTolerance = 1000.0; m_SolutionTolerance = achievedTolerance; m_Iterations = 0; bool iterate = true; while(iterate) { ++m_Iterations; std::vector<double> aSolution = m_QBalance.calcBalanceMatrix(); achievedTolerance = calculateTolerance(aSolution); estimateNewState(aSolution); m_IGU.setState(m_IGUState); if(achievedTolerance < m_SolutionTolerance) { initialState = m_IGUState; m_SolutionTolerance = std::min(achievedTolerance, m_SolutionTolerance); bestSolution = m_IGUState; } if(m_Iterations > IterationConstants::NUMBER_OF_STEPS) { m_Iterations = 0; m_RelaxParam -= IterationConstants::RELAXATION_PARAMETER_STEP; m_IGU.setState(initialState); m_IGUState = initialState; } iterate = achievedTolerance > m_Tolerance; if(m_RelaxParam < IterationConstants::RELAXATION_PARAMETER_MIN) { iterate = false; } } m_IGUState = bestSolution; }
void operator()() { // Memory leak at the end... inst::Instance* instance = createInstance(_param); sol::Solution initialSolution(instance); sol::ObjValue initObjValue = initialSolution.computeObjValue(); initialSolution.applyDelta(initObjValue); _pool.addSolution(initialSolution); RandomMoves randomMoves( _dist(_gen), *instance, instance->numProcesses() * _param["a"].as<double>()); HillClimbing hillClimbing(_dist(_gen), *instance, &_pool, _param["b"].as<int>(), _param["e"].as<int>(), _param["f"].as<int>()); IteratedLocalSearch<HillClimbing, RandomMoves> ils(_param["c"].as<int>(), &hillClimbing, &randomMoves, &_pool); do { sol::Solution solution(bestSolution()); ils.apply(solution); boost::this_thread::interruption_point(); } while(true); }
void MakeBid( std::shared_ptr<Packet_ServerBeginRound> roundInfo, // Information about this particular round const std::shared_ptr<Packet_ServerRequestBid> request, // The specific request we received double period, // How long this bidding period will last double skewEstimate, // An estimate of the time difference between us and the server (positive -> we are ahead) std::vector<uint32_t> &solution, // Our vector of indices describing the solution uint32_t *pProof // Will contain the "proof", which is just the value ) { double tSafetyMargin = 0.5; // accounts for uncertainty in network conditions /* This is when the server has said all bids must be produced by, plus the adjustment for clock skew, and the safety margin */ double tFinish = request->timeStampReceiveBids * 1e-9 + skewEstimate - tSafetyMargin; Log(Log_Verbose, "MakeBid - start, total period=%lg.", period); /* We will use this to track the best solution we have created so far. */ roundInfo->maxIndices = 4; std::vector<uint32_t> bestSolution(roundInfo->maxIndices); std::vector<uint32_t> gpuBestSolution(roundInfo->maxIndices); bigint_t bestProof, gpuBestProof; wide_ones(BIGINT_WORDS, bestProof.limbs); // Incorporate the existing block chain data - in a real system this is the // list of transactions we are signing. This is the FNV hash: // http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function hash::fnv<64> hasher; uint64_t chainHash = hasher((const char *)&roundInfo->chainData[0], roundInfo->chainData.size()); bigint_t x; wide_x_init(&x.limbs[0], uint32_t(0), roundInfo->roundId, roundInfo->roundSalt, chainHash); std::vector<uint32_t> indices(roundInfo->maxIndices); //Define TBB arrays uint32_t *parallel_Indices = (uint32_t *)malloc(sizeof(uint32_t) * TBB_PARALLEL_COUNT); uint32_t *parallel_BestSolutions = (uint32_t *)malloc(sizeof(uint32_t) * TBB_PARALLEL_COUNT * roundInfo->maxIndices); uint32_t *parallel_Proofs = (uint32_t *)malloc(sizeof(uint32_t) * 8 * TBB_PARALLEL_COUNT); uint32_t *parallel_BestProofs = (uint32_t *)malloc(sizeof(uint32_t) * 8 * TBB_PARALLEL_COUNT); //Define GPU arrays uint32_t *d_ParallelBestSolutions; checkCudaErrors(cudaMalloc((void **)&d_ParallelBestSolutions, sizeof(uint32_t) * CUDA_DIM * CUDA_DIM * roundInfo->maxIndices)); checkCudaErrors(cudaMemcpy(d_hashConstant, &roundInfo->c[0], sizeof(uint32_t) * 4, cudaMemcpyHostToDevice)); unsigned gpuTrials = 0; unsigned cpuTrials = 0; unsigned maxNum = uint32_t(0xFFFFFFFF); auto runGPU = [ = , &gpuTrials] { cudaInit(CUDA_DIM, d_ParallelBestProofs); do { cudaIteration(d_ParallelIndices, d_ParallelProofs, d_ParallelBestProofs, d_ParallelBestSolutions, x, d_hashConstant, roundInfo->hashSteps, CUDA_DIM, gpuTrials, CUDA_TRIALS, roundInfo->maxIndices); gpuTrials += CUDA_TRIALS; } while ((tFinish - now() * 1e-9) > 0); }; std::thread runGPUThread(runGPU); auto tbbInitial = [ = ](unsigned i) { bigint_t ones; wide_ones(8, ones.limbs); wide_copy(8, ¶llel_BestProofs[i * 8], ones.limbs); }; tbb::parallel_for<unsigned>(0, TBB_PARALLEL_COUNT, tbbInitial); do { auto tbbIteration = [ = ](unsigned i) { uint32_t index = maxNum - (TBB_PARALLEL_COUNT<<2) - cpuTrials + (i<<1); bigint_t proof = tbbHash(roundInfo.get(), index, x); wide_copy(8, ¶llel_Proofs[i * 8], proof.limbs); parallel_Indices[i] = index; }; tbb::parallel_for<unsigned>(0, TBB_PARALLEL_COUNT, tbbIteration); auto tbbCrossHash = [ = ](unsigned i) { for (unsigned xorStride = 1; xorStride < TBB_PARALLEL_COUNT >> 2; xorStride++) { if (i + (roundInfo->maxIndices * xorStride) < TBB_PARALLEL_COUNT) { bigint_t candidateBestProof; wide_copy(8, candidateBestProof.limbs, ¶llel_Proofs[i * 8]); for (unsigned indexNum = 1; indexNum < roundInfo->maxIndices; indexNum++) { wide_xor(8, candidateBestProof.limbs, candidateBestProof.limbs, ¶llel_Proofs[(i + (indexNum * xorStride)) * 8]); } if (wide_compare(8, candidateBestProof.limbs, ¶llel_BestProofs[i * 8]) < 0) { wide_copy(8, ¶llel_BestProofs[i * 8], candidateBestProof.limbs); for (unsigned ID = 0; ID < roundInfo->maxIndices; ID++) { parallel_BestSolutions[(i * roundInfo->maxIndices) + ID] = parallel_Indices[i + (ID * xorStride)]; } } } } }; tbb::parallel_for<unsigned>(0, TBB_PARALLEL_COUNT, tbbCrossHash); cpuTrials += TBB_PARALLEL_COUNT; } while ((tFinish - now() * 1e-9) > 0); runGPUThread.join(); auto reduceGPU = [ = , &gpuBestSolution, &gpuBestProof] { cudaParallelReduce(CUDA_DIM, roundInfo->maxIndices, d_ParallelBestProofs, d_ParallelBestSolutions, &gpuBestSolution[0], gpuBestProof.limbs); }; std::thread reduceThread(reduceGPU); //TBB for (int toDo = TBB_PARALLEL_COUNT / 2; toDo >= 1; toDo >>= 1) { auto tbbReduce = [ = ](unsigned i) { if (wide_compare(BIGINT_WORDS, ¶llel_BestProofs[(i + toDo) * 8], ¶llel_BestProofs[i * 8]) < 0) { wide_copy(8, ¶llel_BestProofs[i * 8], ¶llel_BestProofs[(i + toDo) * 8]); wide_copy(roundInfo->maxIndices, ¶llel_BestSolutions[i * roundInfo->maxIndices], ¶llel_BestSolutions[(i + toDo) * roundInfo->maxIndices]); } }; tbb::parallel_for<unsigned>(0, toDo, tbbReduce); } wide_copy(BIGINT_WORDS, bestProof.limbs, ¶llel_BestProofs[0]); wide_copy(roundInfo->maxIndices, &bestSolution[0], ¶llel_BestSolutions[0]); reduceThread.join(); if (wide_compare(BIGINT_WORDS, gpuBestProof.limbs, bestProof.limbs) < 0) { Log(Log_Verbose, "Accepting GPU Solution"); wide_copy(8, bestProof.limbs, gpuBestProof.limbs); wide_copy(roundInfo->maxIndices, &bestSolution[0], &gpuBestSolution[0]); } solution = bestSolution; wide_copy(BIGINT_WORDS, pProof, bestProof.limbs); free(parallel_Indices); free(parallel_BestSolutions); free(parallel_Proofs); free(parallel_BestProofs); checkCudaErrors(cudaFree(d_ParallelBestSolutions)); Log(Log_Verbose, "MakeBid - finish. Total trials %d, cpu: %d, gpu %d", cpuTrials + gpuTrials, cpuTrials, gpuTrials); }