unsigned BasicHashTable::hashIndexFromKey(char const *key) const { unsigned result = 0; if (fKeyType == STRING_HASH_KEYS) { while (1) { char c = *key++; if (c == 0) break; result += (result << 3) + (unsigned)c; } result &= fMask; } else if (fKeyType == ONE_WORD_HASH_KEYS) { result = randomIndex((unsigned long)key); } else { unsigned *k = (unsigned *)key; unsigned long sum = 0; for (int i = 0; i < fKeyType; ++i) { sum += k[i]; } result = randomIndex(sum); } return result; }
size_t randomBit(const llvm::BitVector &Vector) { assert(Vector.any()); auto Itr = Vector.set_bits_begin(); for (size_t I = randomIndex(Vector.count()); I != 0; --I) ++Itr; return *Itr; }
static void randomFat(const vector<cv::Mat> &input, cv::Mat &fat) { cv::Mat_<int> randIdx(1, input.size()); randomIndex(randIdx); for (size_t i=0; i<input.size(); i++) { int idx = randIdx(i); fat.push_back(input[idx].reshape(1,1)); } }
/** * performs one sweep, which means Nx*Ny random updates */ double performSweep() { int overlap = 0; #pragma omp parallel for for(int n=0; n<Nx*Ny; n++) { int i = randomIndex(gen); disc_t temp = discs[i]; double dx = randomDisplacement(gen); double dy = randomDisplacement(gen); temp.x += dx; temp.y += dy; if(doesOverlap(temp, i)) overlap++; else discs[i] = temp; } return ((double) overlap)/(Nx*Ny); }
void MatrixGenerator::generateMatrixC(bool isPercent) { cout << "Generating matrixC... " << flush; int numbersNotNull = k; if (isPercent) numbersNotNull = (int)((double)n*((double)numbersNotNull/100.0)); for (int j=0 ; j<n ; j++) { matrix[j][j] = randomValue(minDiagValue, maxDiagValue); notNullElementsCount++; int numbersLeft = numbersNotNull - 1; while (numbersLeft) { int index = randomIndex(minIndex, maxIndex); if (matrix[index][j] == 0) { matrix[index][j] = randomValue(minValue, maxValue); notNullElementsCount++; numbersLeft--; } } } cout << "done" << endl; cout << "MatrixC test: " << ((testMatrixC(numbersNotNull) == true)?"passed":"failed") << endl; }
/** * @brief Perform the migrations between subpopulations * @param subpops The subpopulations * @param nSubpopulations The number of subpopulations involved in the migration * @param nIndsFronts0 The number of individuals in the front 0 of each subpopulation * @param conf The structure with all configuration parameters */ void migration(Individual *const subpops, const int nSubpopulations, const int *const nIndsFronts0, const Config *const conf) { // From subpopulations randomly choosen some individuals of the front 0 are copied to each subpopulation (the worst individuals are deleted) for (int subpop = 0; subpop < nSubpopulations; ++subpop) { // This vector contains the available subpopulations indexes which are randomly choosen for copy the individuals of the front 0 std::vector<int> randomIndex(nSubpopulations); std::iota(randomIndex.begin(), randomIndex.end(), 0); // The current subpopulation will not copy its own individuals randomIndex.erase(randomIndex.begin() + subpop); std::random_shuffle(randomIndex.begin(), randomIndex.end()); int maxCopy = conf -> subpopulationSize - nIndsFronts0[subpop]; Individual *ptrDest = subpops + (subpop * conf -> familySize) + conf -> subpopulationSize; for (int subpop2 = 0; subpop2 < nSubpopulations - 1 && maxCopy > 0; ++subpop2) { int toCopy = std::min(maxCopy, nIndsFronts0[randomIndex[subpop2]] >> 1); Individual *ptrOrig = subpops + (randomIndex[subpop2] * conf -> familySize); ptrDest -= toCopy; memcpy(ptrDest, ptrOrig, toCopy * sizeof(Individual)); maxCopy -= toCopy; } } }
/// evaluate best possible move up to a certain level /// this is done recursively by a form of the NegaMax algorithm with alpha-beta pruning Evaluation Game::_evaluateMoves(int level, float alpha, float beta, bool initialCall) { KBX::Logger log("evaluation"); if ((level == 0) || (this->getWinner() != NONE_OF_BOTH)) { return Evaluation(this->_rate(this->_nextPlayer)); } // get rating, either directly or by recursive call float rating; // container for best move candidates std::priority_queue< Evaluation, std::vector< Evaluation >, Evaluation::less > candidates; // limit indices to significant color size_t from, to; if (this->_nextPlayer == WHITE) { from = 0; to = 8; } else { from = 9; to = 17; } // iterate over all dice of a color for (size_t d = from; d <= to; d++) { // get value of current die size_t value = this->_dice[d].getValue(); // iterate over max number of moves for given dice value (stored in state-array) for (size_t i = 0; i < DieState::nPossibleMoves[value]; i++) { // abort evaluation if cancelled if (this->cancelled()) { return Evaluation(0.0f); } // check if this specific move is valid RelativeMove move = DieState::possibleMoves[value][i]; if (this->moveIsValid(Move(d, move))) { // store all data to undo move later RelativeMove moveBack = move.invert(); // kill the die lying on the target field int idDieOnTarget = this->_fields[this->_dice[d].x() + move.dx][this->_dice[d].y() + move.dy]; // perform move this->makeMove(Move(d, move), false); // recursive call for next step (negative weighting, since it is opponent's turn) rating = - this->_strategy.patience * this->_evaluateMoves(level - 1, -beta, -alpha, false).rating; // undo move this->makeMove(Move(d, moveBack), false); // revive killed die on target field if (idDieOnTarget != CLEAR) { this->reviveDie(idDieOnTarget); } // alpha-beta pruning if (rating >= beta) { return Evaluation(rating); } if (rating > alpha) { alpha = rating; if (initialCall == true) { // add move to candidate list candidates.push(Evaluation(rating, Move(d, move))); } } } } } if (initialCall == true) { if ( ! candidates.empty()) { float topRating = candidates.top().rating; std::vector<Evaluation> topCandidates; log.info(stringprintf("top rating: %0.4f", topRating)); while ((candidates.top().rating >= topRating) && ( ! candidates.empty())) { topCandidates.push_back(candidates.top()); candidates.pop(); } log.info(stringprintf("next rating: %0.4f", candidates.top().rating)); log.info(stringprintf("no. of top candidates: %d", topCandidates.size())); // randomly select from equally rated top candidates std::size_t iTop = randomIndex(0, topCandidates.size()-1); return topCandidates[iTop]; } } return Evaluation(alpha); }