bitset::bitset(const bitset& b) { bits.resize(b.size()); for (unsigned int i = 0; i < b.size(); i++) bits[i] = b[i]; }
/** * 计算基因型概率 * @param geneBit 基因型的二进制表示,其中1表示大写,0表示小写 * @param r1 左侧重组率 * @param r2 右侧重组率 * @param r 整体重组率,默认为0,通过r1和r2计算 * @param zeta 符合系数,默认为1 * @return 基因型概率 */ double calcCross(const bitset<GENE_CP_BIT> geneBit, const double r1, const double r2, const double r=0, const double zeta=1) { if (geneBit.size()!=GENE_CP_BIT) return 0; double r12 = r1*r2*zeta; double r0 = r; if (r==0) { r0 = r1+r2-2*r12; } // 配子产生的概率,见书105页 switch(geneBit.to_ulong()) { case 0: //M1QM2 000 case 7: //m1qm2 111 return (1.0-r1-r2+r12);// `=(1-r1)(1-r2); case 1: //M1Qm2 001 case 6: //m1qM2 110 return (r2-r12);// `=r2(1-r1); case 4: //M1qm2 100 case 3: //m1QM2 011 return (r1-r12);// `=r1(1-r2); case 5: //M1qM2 101 case 2: //m1Qm2 010 return r12;// `=r1*r2; } return 0; }
bool covered(bitset<10> digits){ for(int i=0;i<digits.size();i++){ if(digits[i] == false) return false; } return true; }
// restituisce la posizione del nodo i-esimo in preordine inline unsigned int preorder_node ( unsigned int n ) const { unsigned int i = 0; for ( i = 0; i < tree.size() && n != 0; i++ ) { n -= tree[i]; } return i - 1; }
bool isPrimeUtil(ll N,bitset<100007>&_bitset,vector<int>&primes){ if(N < (int)_bitset.size()) return _bitset.test(N); for(ll i=0; i<primes.size();i++) if(N%primes[i]==0) return false; return true; }
void print(bitset<16> &b) { int i = 0; int bsize = b.size(); for (i = 0; i < bsize; ++i) { cout << b[i]; } cout << " :" << "the size of bitset:" << bsize << endl; }
// restituisco la fine del primo sottoalbero che parte da pos inline unsigned int first_subtree ( unsigned int pos ) const { unsigned int z; unsigned int o; for ( z = o = 0, pos++; pos < tree.size(); pos++ ) { z += !tree[pos]; o += tree[pos]; if ( z > o ) break; } return pos; }
bool cart::should_terminate(const vector<const sample*>& samples, const bitset& splitted_features, size_t min_num_samples) { if (splitted_features.num_bits_set() == splitted_features.size()) { return true; } if (samples.size() < min_num_samples) { return true; } return false; }
void logger::setmask(bitset<8>_mask) { mask = _mask; int bits = 0; for (size_t i = 0; i < _mask.size(); i++) { if (_mask.test(i)) { bits |= logmasks[i]; } } // cout << "setlogmask(" << hex << bits << dec << ")" << endl; #ifdef _WIN32 WinLog::SetMask(bits); #else setlogmask(bits); #endif }
/* Decodes Huffman code to original string. * * @param code Huffman code. * @param root Root of Huffman tree. * @return Original string. */ string decode_huffman(const bitset& code, HuffmanTree* root) { string origin; HuffmanTree* curr_node = root; for (uint i = 0; i <= code.size(); i++) { if (curr_node->symbol != 0) { origin += curr_node->symbol; curr_node = root; i--; } else { if (code[i] == 0) { curr_node = curr_node->left; } else { curr_node = curr_node->right; } } } return origin; }
void cart::find_best_split(vector<const sample*>& samples, const bitset& splitted_features, tree_node_split& best_split) { /*for each feature f not set in splitted_features for each splittable value s of feature f R1 = {samples whose feature f's value is smaller than s} R2 = {samples whose feature f's value is not smaller than s} calculate the cost C = sum{(y1 - c1)^2} + sum{(y2 - c2)^2} c1 = mean(y) in R1, c2 = mean(y) in R2 find (f,s) with mininum C */ //for each feature f // sort samples by f's value // scan samples in ascending order of f's value // for splittable value s // C = sum(y1 ^ 2) + sum(c1 ^ 2) - 2 * c1 * sum(y1) + sum(y2 ^ 2) + sum(c2 ^ 2) - 2 * c2 * sum(y2) // C = CL + CR // c1 = sum(y1) / |CL| // c2 = sum(y2) / |CR| // CL = CLY2 + CLC2 - 2 * c1 * CLY // CR = CRY2 + CRC2 - 2 * c2 * CRY double min_ds = -1; best_split.fid = splitted_features.size(); best_split.val = FLT_MIN; for (size_t fid = 0; fid < splitted_features.size(); fid++) { if (splitted_features.test(fid)) { continue; } //sort samples by feature fid feature_comp fcmp(fid); std::sort(samples.begin(), samples.end(), fcmp); double square_cy1, square_cy2, sum_y1, sum_y2, my1, my2, ds1, ds2; size_t ny1, ny2; square_cy1 = square_cy2 = sum_y1 = sum_y2 = ny1 = ny2 = my1 = my2 = ds1 = ds2 = 0; for (vector<const sample*>::iterator it = samples.begin(); it != samples.end(); it++) { const sample* sa = *it; square_cy2 += sa->y * sa->y; sum_y2 += sa->y; ny2++; } for (vector<const sample*>::iterator it = samples.begin(); it != samples.end(); it++) { const sample* sa = *it; //each sample's value is a split value square_cy1 += sa->y * sa->y; ny1++; sum_y1 += sa->y; my1 = sum_y1 / ny1; ds1 = square_cy1 + my1 * my1 * ny1 - 2 * my1 * sum_y1; square_cy2 -= sa->y * sa->y; ny2--; sum_y2 -= sa->y; my2 = sum_y2 / ny2; ds2 = square_cy2 + my2 * my2 * ny2 - 2 * my2 * sum_y2; if (min_ds < 0 || min_ds > ds1 + ds2) { min_ds = ds1 + ds2; best_split.fid = fid; best_split.val = sa->x[fid]; } } } }