void Individual::initChromo(){ int cCnt = mPopulation->mDStream->getCharsNum(); int wCnt = mPopulation->mDStream->getWordsNum(); int *charSet = new int[cCnt]; int *wordSet = new int[wCnt]; int i; // initialize random array for(i = 0; i < cCnt && i < wCnt; i++){ charSet[i] = wordSet[i] = i; } while(i < cCnt){ charSet[i] = i; i++; } while(i < wCnt){ wordSet[i] = i; i++; } // initialize mCharGenes for(i = 0; i < CHAR_NUM; i++){ mCharGenes[i] = pickRandom(charSet, cCnt--); //cout << "mWordGenes[" << i << "]: " << mWordGenes[i] << endl; } // initialize mWordGenes for(i = 0; i < WORD_NUM; i++){ mWordGenes[i] = pickRandom(wordSet, wCnt--); //cout << "mWordGenes[" << i << "]: " << mWordGenes[i] << endl; } delete []charSet; delete []wordSet; }//void Individual::init()
void PlayerState::drawCards(int cards) { // For each card to be drawn for (int handCounter = 0; handCounter < cards; handCounter ++) { // If there are any top cards, draw them first if (topOfDeckAsIndex.size() > 0) { deck[topOfDeckAsIndex.top()]--; hand[topOfDeckAsIndex.top()]++; topOfDeckAsIndex.pop(); } else { // If deck is empty, shuffle if (countCards(deck) == 0) { // If discard too is empty, then don't draw more cards. if (countCards(discard) == 0) return; else shuffle(); } // Pick a random card int cardIndex = pickRandom(deck); hand[cardIndex] += 1; deck[cardIndex] -= 1; } } }
Move BookReader::pick(const Board &b) { #ifdef _TRACE cout << "BookReader::pick - hash=" << (hex) << b.hashCode() << (dec) << endl; #endif vector < pair<Move,int> > results; if (book_moves(b, results) <= 0) return NullMove; else return pickRandom(b,results); }
struct wordTree *predictNext(struct wordStore *store, struct dlList *past) /* Given input data store and what is known from the past, predict the next word. */ { struct dlNode *recent = nodesFromTail(past, store->maxChainSize); struct wordTree *pick = predictFromWordTree(store->markovChains, recent); if (pick == NULL) pick = predictFromPreviousTypes(store, past); if (pick == NULL) { pick = pickRandom(store->markovChains->children); verbose(2, "in predictNext() last resort pick of %s\n", pick->info->word); } return pick; }
struct wordTree *predictNextFromAllPredecessors(struct wordTree *wt, struct dlNode *list) /* Predict next word given tree and recently used word list. If tree doesn't * have statistics for what comes next given the words in list, then it returns * NULL. */ { verbose(2, " predictNextFromAllPredecessors(%s, %s)\n", wordTreeString(wt), dlListFragWords(list)); struct dlNode *node; for (node = list; !dlEnd(node); node = node->next) { struct wordInfo *info = node->val; wt = wordTreeFindInList(wt->children, info); verbose(2, " wordTreeFindInList(%s) = %p %s\n", info->word, wt, wordTreeString(wt)); if (wt == NULL || wt->children == NULL) break; } struct wordTree *result = NULL; if (wt != NULL && wt->children != NULL) result = pickRandom(wt->children); return result; }
int PlayerState::flipThiefCards(int& absoluteCardId, int& extraCardId) { // For each card to be flipped for (int index = 0; index < 2; index++) { // If there are any top cards, flip them first if (topOfDeckAsIndex.size() > 0) { deck[topOfDeckAsIndex.top()]--; discard[topOfDeckAsIndex.top()]++; if (index == 0) absoluteCardId = CardManager::cardLookupByIndex[topOfDeckAsIndex.top()].id; else extraCardId = CardManager::cardLookupByIndex[topOfDeckAsIndex.top()].id; topOfDeckAsIndex.pop(); } else { // If deck is empty, shuffle if (countCards(deck) == 0) { // If discard too is empty, then don't draw more cards. if (countCards(discard) == 0) return index; else shuffle(); } int cardIndex = pickRandom(deck); if (index == 0) absoluteCardId = CardManager::cardLookupByIndex[cardIndex].id; else extraCardId = CardManager::cardLookupByIndex[cardIndex].id; deck[cardIndex]--; discard[cardIndex]++; } } return 2; }
void alphaChain(char *readsFile, char *monomerOrderFile, char *outFile) /* alphaChain - Create Markov chain of words and optionally output chain in two formats. */ { struct wordStore *store = wordStoreForChainsInFile(readsFile, maxChainSize); struct wordTree *wt = store->markovChains; wordStoreLoadMonomerOrder(store, readsFile, monomerOrderFile); wordStoreNormalize(store, outSize); if (optionExists("chain")) { char *fileName = optionVal("chain", NULL); wordTreeWrite(wt, store->maxChainSize, fileName); } wordTreeGenerateFile(store, store->maxChainSize, pickRandom(wt->children), outSize, outFile); if (optionExists("afterChain")) { char *fileName = optionVal("afterChain", NULL); wordTreeWrite(wt, store->maxChainSize, fileName); } }
/** * @function solveQuestion3 */ void solveQuestion3( int k, int t, int N, std::vector<Eigen::VectorXi> _points2D, std::vector<Eigen::VectorXd> _points3D, Eigen::MatrixXd _T2, Eigen::MatrixXd _T3, double &_sum_res, Eigen::MatrixXd &_M ) { std::vector<Eigen::VectorXi> pointsK2D; std::vector<Eigen::VectorXd> pointsK3D; std::vector<Eigen::VectorXi> pointsTest2D; std::vector<Eigen::VectorXd> pointsTest3D; std::vector<Eigen::VectorXd> normPointsK2D; std::vector<Eigen::VectorXd> normPointsK3D; std::vector<Eigen::VectorXd> normPointsTest2D; std::vector<Eigen::VectorXd> normPointsTest3D; Eigen::MatrixXd M_SVD; /** Pick random points */ std::vector<int> randomK; std::vector<int> test; pickRandom( k, randomK, t, test, N ); printf(" K: "); for( unsigned int i = 0; i < k; i++ ) { printf(" %d ", randomK[i] ); } printf("\n test: "); for( unsigned int i = 0; i < t; i++ ) { printf(" %d ", test[i] ); } printf("\n"); pointsK2D.resize(0); pointsK3D.resize(0); pointsTest2D; pointsTest3D; normPointsK2D.resize(0); normPointsK3D.resize(0); normPointsTest2D.resize(0); normPointsTest3D.resize(0); /** Separate the sets */ for( unsigned int i = 0; i < k; i++ ) { pointsK2D.push_back( _points2D[ randomK[i] ] ); pointsK3D.push_back( _points3D[ randomK[i] ] ); } for( unsigned int i = 0; i < t; i++ ) { pointsTest2D.push_back( _points2D[ test[i] ] ); pointsTest3D.push_back( _points3D[ test[i] ] ); } /** Normalize the 2D points with T2 */ applyNorm2D( pointsK2D, _T2, normPointsK2D ); applyNorm3D( pointsK3D, _T3, normPointsK3D ); /** Calculate M from k points */ calculateM_SVD( normPointsK2D, normPointsK3D, M_SVD ); /// Residual applyNorm2D( pointsTest2D, _T2, normPointsTest2D ); applyNorm3D( pointsTest3D, _T3, normPointsTest3D ); std::vector<Eigen::VectorXd> residual; Residual( M_SVD, pointsTest2D, pointsTest3D, normPointsTest2D, normPointsTest3D, T2, residual ); /** Output: M and Residual */ _sum_res = 0; for( unsigned int i = 0; i < residual.size(); i++ ) { _sum_res += residual[i](2); } _sum_res /= residual.size(); _M = M_SVD; }