def build_score_dict(reads_dict, combined_dict, number_of_haplotypes, min_HF): """ Returns a dicitonary lists read_IDs and gives their score. The scoring algorithm scores each read according to the number of differet haplotypes that the reference panel supports at the chromosomal region that overlaps with the read. Only bialleic SNP with a minor allele frequeancy above 0.01 are considered for the calculation, since they are unlikely to affect the score. In addition, only haplotypes with a frequnecy between min_HF and 1-min_HF add to the score of a read. """ N = number_of_haplotypes b = (1 << number_of_haplotypes ) - 1 #### equivalent to int('1'*number_of_haplotypes,2) score_dict = dict() for read_id in reads_dict: haplotypes = ( (combined_dict[pos][-1], combined_dict[pos][-1] ^ b) for pos, base in reads_dict[read_id] if 0.01 <= popcount(combined_dict[pos][-1]) / N <= 0.99 ) #Include only biallelic SNPs with MAF of at least 0.01. Also, ^b flips all bits of the binary number, hap_tab[ind] using bitwise xor operator. score_dict[read_id] = sum( min_HF <= popcount(reduce(and_, hap)) / N <= (1 - min_HF) for hap in product(*haplotypes) if len(hap) != 0) return score_dict
def checkGroup(self, state=None, player=None): if state is None: st = self.state else: st = state if player is None: p = self.currentPlayer else: p = player areGrouped = True if gmpy2.popcount(st[p]) == 1: areGrouped = True else: unvisited = st[p] checklist = 2**gmpy2.bit_scan1(unvisited) unvisited ^= checklist while gmpy2.popcount(checklist) > 0: current = gmpy2.bit_scan1(checklist) checklist ^= 2**current currentNeighbours = self.neighbourhood[current] & unvisited if currentNeighbours: unvisited ^= currentNeighbours checklist |= currentNeighbours if gmpy2.popcount(unvisited) > 0: areGrouped = False return areGrouped
def joint_frequencies_combo(self, *alleles, normalize): """ Based on the reference panel, it calculates joint frequencies of observed alleles. The function arguments are alleles, that is, tuples of position and base, e.g., (100,'T'), (123, 'A') and (386, 'C'). Each allele is enumerated according to the order it was received by the function. The function returns a dictionary that lists all the possible subgroups of the given alleles. Each key in the dictionary is a tuple of intergers that are lexicographically sorted. Moreover, each integer within the keys corresponds to an enumerated allele. For each subgroup of alleles the dictionary gives the joint frequencies in a given population. The function arguments can also include haplotypes, that is, tuples of alleles; Haplotypes are treated in the same manner as alleles. """ hap = self.intrenal_hap_dict(*alleles) result = {c: popcount(A) for c,A in hap.items() } for C in combinations(hap, 2): result[C[0]|C[1]] = popcount(hap[C[0]]&hap[C[1]]) for C in combinations(hap, 3): result[C[0]|C[1]|C[2]] = popcount(hap[C[0]]&hap[C[1]]&hap[C[2]]) for r in range(4,len(alleles)): for C in combinations(hap, r): result[sum(C)] = popcount(reduce(and_,itemgetter(*C)(hap))) if len(alleles)>=4: result[sum(hap.keys())] = popcount(reduce(and_,hap.values())) if normalize: result = {k: v/self.total_number_of_haplotypes_in_reference_panel for k,v in result.items()} return result
def get_ground_state(self,verbose=False): ''' :return: The ground state of the Hamiltonian ''' e,v = scipy.linalg.eigh(self.H) if(verbose): print("energies : ") print(np.around(e,2)) index=np.argsort(e) n_degenerate = 1 continuer = True gs=[v[:,0]] #we also want to fix the number of electrons (avoid degeneracy with different numbers of electrons index_el = np.where(np.abs(v[:,0])>0.0001)[0] n_el = gmpy2.popcount(int(index_el[0])) while(continuer): if(np.abs(e[index[n_degenerate]]-e[index[0]])<0.00000001): index_bis = np.where(np.abs(v[:, index[n_degenerate]]) > 0.00001)[0] n_el_bis=0 for runner in index_bis: n_el_bis += gmpy2.popcount(int(runner)) n_el_bis/=len(index_bis) if np.abs(n_el_bis-n_el)<0.001: gs.append(v[:,index[n_degenerate]]) n_degenerate+=1 else: continuer=False return gs
def _add_subgroup2list(self, subgroup2add): self.bitset_covered = self.bitset_covered | subgroup2add.bitarray self.support_covered = popcount(self.bitset_covered) self.bitset_uncovered = self.bitset_uncovered & ~subgroup2add.bitarray self.support_uncovered = popcount(self.bitset_uncovered) self.bitset_rules.append(subgroup2add.bitarray) self.subgroups.append(deepcopy(subgroup2add)) return self
def is_insufficient_material(self): if popcount(self.occupancy) == 2: return True elif popcount(self.occupancy) == 3: if (self.piece_bb[W_KNIGHT] or self.piece_bb[B_KNIGHT] or self.piece_bb[W_BISHOP] or self.piece_bb[B_BISHOP]): return True return False
def bit_solver(shift: int, player: Bitmap, not_player: Bitmap): """ Calculates the heuristic score for a particular orientation of pieces (i.e. vertically aligned, horizontally aligned, right diagonal or left diagonal). Each direction has a unique shift value in the bitmap. :param shift: bit shift of the current direction being calculated :param player: bitmap representing positions of the current player :param not_player: bitmap representing positions of the other player :return: the score associated with this particular direction """ # Initialize the score and point values score = 0 pt2 = 1 pt3 = 10 s1_right = (player >> shift) s2_right = player >> (2 * shift) s3_right = player >> (3 * shift) s1_left = (player << shift) s2_left = player << (2 * shift) s3_left = player << (3 * shift) s1_right_n1 = s1_right & player s1_left_n1 = s1_left & player # Check for 3 in 4 # XXX- score += pt3 * popcount(((s1_left_n1 & s2_left) << shift) & not_player) # -XXX score += pt3 * popcount(((s1_right_n1 & s2_right) >> shift) & not_player) # XX-X score += pt3 * popcount((s1_right_n1 & s3_right) << (2 * shift) & not_player) # X-XX score += pt3 * popcount((s1_left_n1 & s3_left) >> (2 * shift) & not_player) # Check for 2 in 4 # XX-- score += pt2 * popcount(s1_left_n1 << shift & (not_player & (not_player >> shift))) # --XX score += pt2 * popcount(s1_right_n1 >> shift & (not_player & (not_player << shift))) # X-X- score += pt2 * popcount((player & s2_left) << shift & (not_player & (not_player << (2 * shift)))) # -X-X score += pt2 * popcount((player & s2_right) >> shift & (not_player & (not_player >> (2 * shift)))) # X--X score += pt2 * popcount((player & s3_right) << shift & (not_player & (not_player >> shift))) # -XX- score += pt2 * popcount(s1_right_n1 >> shift & (not_player & (not_player >> (3 * shift)))) return score
def bit_solver(shift: int, player: Bitmap, not_player: Bitmap): """ """ # Initialize the score and point values score = 0 pt2 = 1 pt3 = 10 # win_pts = 100 s1_right = (player >> shift) s2_right = player >> (2 * shift) s3_right = player >> (3 * shift) s1_left = (player << shift) s2_left = player << (2 * shift) s3_left = player << (3 * shift) s1_right_n1 = s1_right & player s1_left_n1 = s1_left & player # Check for wins # score += win_pts * popcount(s1_left_n1 & (s1_left_n1 >> (2 * shift))) # Check for 3 in 4 # XXX- score += pt3 * popcount(((s1_left_n1 & s2_left) << shift) & not_player) # -XXX score += pt3 * popcount(((s1_right_n1 & s2_right) >> shift) & not_player) # XX-X score += pt3 * popcount((s1_right_n1 & s3_right) << (2 * shift) & not_player) # X-XX score += pt3 * popcount((s1_left_n1 & s3_left) >> (2 * shift) & not_player) # Check for 2 in 4 # XX-- score += pt2 * popcount(s1_left_n1 << shift & (not_player & (not_player >> shift))) # --XX score += pt2 * popcount(s1_right_n1 >> shift & (not_player & (not_player << shift))) # X-X- score += pt2 * popcount((player & s2_left) << shift & (not_player & (not_player << (2 * shift)))) # -X-X score += pt2 * popcount((player & s2_right) >> shift & (not_player & (not_player >> (2 * shift)))) # X--X score += pt2 * popcount((player & s3_right) << shift & (not_player & (not_player >> shift))) # -XX- score += pt2 * popcount(s1_right_n1 >> shift & (not_player & (not_player >> (3 * shift)))) return score
def evaluate_pawns(self, position): pawn_table_index = position.pawn_key & 0xFFFF pawn_entry = self.pawn_table[pawn_table_index] if pawn_entry and pawn_entry.key == position.pawn_key: return pawn_entry.score_mg, pawn_entry.score_eg king_sqr = None score_mg = [0, 0] score_eg = [0, 0] for colour in COLOURS: player_pawns = position.piece_bb[(colour << 3) | PAWN] enemy_pawns = position.piece_bb[((colour ^ 1) << 3) | PAWN] enemy_pawn_attacks = (pawn_shift[colour ^ 1](enemy_pawns, NORTHEAST) | pawn_shift[colour ^ 1](enemy_pawns, NORTHWEST)) for sq in gen_bitboard_indices(player_pawns): pawn_bb = 1 << sq adjacent_squares = adjacent_files[sq] & rank_of[sq] adjacent_pawns = adjacent_files[sq] & player_pawns aligned = True if adjacent_squares & player_pawns else False defenders = adjacent_pawns & pawn_shift[colour ^ 1](rank_of[sq], NORTH) opposed = True if forward_fill[colour](pawn_bb) & enemy_pawns else False # Doubled pawn if (player_pawns & pawn_shift[colour ^ 1](pawn_bb, NORTH)) and not defenders: score_mg[colour] -= DOUBLED[MIDGAME] score_eg[colour] -= DOUBLED[ENDGAME] # Connected pawn if aligned or defenders: score_mg[colour] += connected_bonus[opposed][aligned][popcount(defenders)][(sq >> 3) ^ (colour * 7)][MIDGAME] score_eg[colour] += connected_bonus[opposed][aligned][popcount(defenders)][(sq >> 3) ^ (colour * 7)][ENDGAME] # Isolated pawn elif not adjacent_pawns: score_mg[colour] -= ISOLATED[MIDGAME] score_eg[colour] -= ISOLATED[ENDGAME] # Backward pawn elif (pawn_shift[colour](pawn_bb, NORTH) & (enemy_pawns | enemy_pawn_attacks) and not forward_fill[colour ^ 1](adjacent_squares) & player_pawns): score_mg[colour] -= BACKWARD[MIDGAME] score_eg[colour] -= BACKWARD[ENDGAME] score_mg = score_mg[WHITE] - score_mg[BLACK] score_eg = score_eg[WHITE] - score_eg[BLACK] self.pawn_table[pawn_table_index] = PawnEntry(position.pawn_key, score_mg, score_eg) return score_mg, score_eg
def test_delete(self): for flags in xrange(16): pn = MemoryPatriciaDict() pn.update((k,v) for k,v in six.iteritems(SCENARIOS[flags]) if k != 'hash_') for idx,scenario in enumerate(SCENARIOS): items = [(k,v) for k,v in six.iteritems(scenario) if k != 'hash_'] pn2 = MemoryPatriciaDict() pn2.update(pn) self.assertEqual(pn2.hash, SCENARIOS[flags]['hash_']) self.assertEqual(pn2.size, gmpy2.popcount(flags)) self.assertEqual(pn2.length, gmpy2.popcount(flags)) pn3 = MemoryPatriciaDict() pn3.update(pn) self.assertEqual(pn3.hash, SCENARIOS[flags]['hash_']) self.assertEqual(pn3.size, gmpy2.popcount(flags)) self.assertEqual(pn3.length, gmpy2.popcount(flags)) for (k,v) in items: if k in SCENARIOS[flags]: pn2.delete([k]) pn3.delete(iter([k])) else: with self.assertRaises(KeyError): pn2.delete([k]) with self.assertRaises(KeyError): pn3.delete(iter([k])) self.assertEqual(pn2.hash, SCENARIOS[flags & ~idx]['hash_']) self.assertEqual(pn2.size, gmpy2.popcount(flags & ~idx)) self.assertEqual(pn2.length, gmpy2.popcount(flags & ~idx)) self.assertEqual(pn3.hash, SCENARIOS[flags & ~idx]['hash_']) self.assertEqual(pn3.size, gmpy2.popcount(flags & ~idx)) self.assertEqual(pn3.length, gmpy2.popcount(flags & ~idx))
def test_delete(self): for flags in range(16): pn = MemoryPatriciaAuthTree() pn.update((k,v) for k,v in six.iteritems(SCENARIOS[flags]) if k != 'hash_') for idx,scenario in enumerate(SCENARIOS): items = [(k,v) for k,v in six.iteritems(scenario) if k != 'hash_'] pn2 = MemoryPatriciaAuthTree() pn2.update(pn) self.assertEqual(pn2.hash, SCENARIOS[flags]['hash_']) self.assertEqual(pn2.size, gmpy2.popcount(flags)) self.assertEqual(pn2.length, gmpy2.popcount(flags)) pn3 = MemoryPatriciaAuthTree() pn3.update(pn) self.assertEqual(pn3.hash, SCENARIOS[flags]['hash_']) self.assertEqual(pn3.size, gmpy2.popcount(flags)) self.assertEqual(pn3.length, gmpy2.popcount(flags)) for (k,v) in items: if k in SCENARIOS[flags]: pn2.delete([k]) pn3.delete(iter([k])) else: with self.assertRaises(KeyError): pn2.delete([k]) with self.assertRaises(KeyError): pn3.delete(iter([k])) self.assertEqual(pn2.hash, SCENARIOS[flags & ~idx]['hash_']) self.assertEqual(pn2.size, gmpy2.popcount(flags & ~idx)) self.assertEqual(pn2.length, gmpy2.popcount(flags & ~idx)) self.assertEqual(pn3.hash, SCENARIOS[flags & ~idx]['hash_']) self.assertEqual(pn3.size, gmpy2.popcount(flags & ~idx)) self.assertEqual(pn3.length, gmpy2.popcount(flags & ~idx))
def cvc_kifu(self, count): ban = Ban().init_ban() color = 0 pb = ban.bb ob = ban.wb kifulist_ = [] while True: if not Play().legal_board(pb, ob): mv = 64 vb = (ban.bb | ban.wb) ^ 0xffffffffffffffff kifu = np.array([ban.bb, ban.wb, vb, color, mv], dtype='uint64') kifulist_.append(kifu) color ^= 1 pb, ob = ob, pb if not Play().legal_board(pb, ob): mv = 64 vb = (ban.bb | ban.wb) ^ 0xffffffffffffffff kifu = np.array([ban.bb, ban.wb, vb, color, mv], dtype='uint64') kifulist_.append(kifu) break mp = Move().rand_move(pb, ob) # move_positionをmove_valueに変換 mv = 63 - popcount(mp - 1) vb = (ban.bb | ban.wb) ^ 0xffffffffffffffff # void_board kifu = np.array([ban.bb, ban.wb, vb, color, mv], dtype='uint64') kifulist_.append(kifu) # player_board, opponent_board更新 pb, ob = Play().put_turn(pb, ob, mp) # board更新 ban = Play().set_board(pb, ob, color) # 手番とplayer_board, opponent_board入れ替え color ^= 1 pb, ob = ob, pb kifulist = np.array(kifulist_, dtype='uint64') bs = popcount(ban.bb) ws = popcount(ban.wb) row_num = kifulist.shape[0] if bs >= ws: r = np.zeros((row_num, 1)) kifulist = np.append(kifulist, r, axis=1) elif bs < ws: r = np.ones((row_num, 1)) kifulist = np.append(kifulist, r, axis=1) np.save( './kifu/{0}-{1}'.format( datetime.now().strftime('%y_%m_%d_%H_%M_%S_%f'), count), kifulist)
def refine_subgroup(rulelist,data,candidate2refine,beam,subgroup2add): """ Expands a subgroup by adding an item from all other variables not included in the subgroup. """ bitarray_candidate = reduce((lambda x, y: x & y), (item.bitarray for item in candidate2refine)) \ if candidate2refine != [] else bit_mask(data.number_instances) #TODO: move this computation depending if it is a rule list or a rule set bitarray_candidate = bitarray_candidate & rulelist.bitset_uncovered variable_list = [item.parent_variable for item in candidate2refine] for attribute in filter(lambda x: x.name not in variable_list, data.attributes): #for item in attribute.generate_items(candidate2refine): for item in attribute.items: bitarray_newcandidate = bitarray_candidate & item.bitarray usage = popcount(bitarray_newcandidate) if usage >= rulelist.min_support: new_subgroup_statistics, new_default_rule_statistics = \ compute_statistics_newrules(rulelist, data,bitarray_newcandidate) new_candidate = candidate2refine + [item] score, gain_data, gain_model = compute_delta_score(rulelist, new_candidate, new_subgroup_statistics, new_default_rule_statistics) else: score = np.NINF if score > subgroup2add.score: subgroup2add.update(new_candidate, new_subgroup_statistics, gain_data, gain_model, score) if score > beam.min_score and set([item.description for item in new_candidate]) not in beam.set_patterns: beam.replace(new_candidate, score, usage) #print("Subgroup: {} ; score : {}".format([pat.parent_variable for pat in new_candidate],score)) return beam, subgroup2add
def calc_ber(src, dst, m): """Calculate bit error rate (BER) Parameters ---------- src: array_like dst: array_like, whose shape should be same to `src`. m: modulation level, such as 2, 4, 8, ... This is required to calculate number of bits for each symbol. Returns ------- SER """ if src.shape != dst.shape: raise ValueError("Shape consistency error.") if (dst < 0).any(): raise ValueError("Negative value(s) are found.") if (src < 0).any(): raise ValueError("Negative value(s) are found.") x = src ^ dst # pylint: disable=no-member if __gmpy2: f = np.vectorize(lambda i: gmpy2.popcount(int(i))) else: f = np.vectorize(lambda i: bin(i).count('1')) x = x[np.where(x != 0)] c = np.sum(f(x)) if np.size(x == 0) else 0 return c / (np.log2(m) * np.size(src))
def generate_full_board(player, empty_spaces=0): # Generate an empty board arr_board = cm.initialize_game_state() # Convert board to bitmap bit_board, bit_mask = cm.board_to_bitmap(arr_board, player) # Calculate the board shape bd_shp = arr_board.shape # While the board is not full, continue placing pieces while popcount(bit_mask) != bd_shp[0] * bd_shp[1] - empty_spaces: # Select a random move in a column that is not full move = -1 while not (0 <= move < bd_shp[1]): move = np.random.choice(bd_shp[1]) try: move = cm.PlayerAction(move) cm.top_row(arr_board, move) except IndexError: move = -1 # Apply the move to both boards cm.apply_action(arr_board, move, player) bit_board, bit_mask = cm.apply_action_cp(bit_board, bit_mask, move, bd_shp) # Switch to the next player player = cm.BoardPiece(player % 2 + 1) return arr_board, bit_board, bit_mask, player
def gnubg_id_str_to_board_id(config, pos_id_str): """Convert a Base64 endcoded position ID from gnubg to a board ID. See https://www.gnu.org/software/gnubg/manual/html_node/A-technical-description-of-the-Position-ID.html for a description of their position id encoding. Args: config: board.GameConfiguration pos_id_str: Base64 encoded position ID from gnubg Returns: int """ pos_id = int.from_bytes(base64.b64decode(pos_id_str + '=='), byteorder='little') # pos_id is just like our encoding except they don't explictly # have the bits for markers off the board. We'll just count how many # markers there are and add those 1s in. missing_markers = config.num_markers - gmpy2.popcount(pos_id) modified_pos_id = ((pos_id << (missing_markers + 1)) | ~(~0 << missing_markers)) return modified_pos_id
def evaluate_mobility(self, position, mobility_bb): mobility_area = [0, 0] mobility_score_mg = [0, 0] mobility_score_eg = [0, 0] mobility_area[WHITE] = self.get_mobility_area(position, WHITE) mobility_area[BLACK] = self.get_mobility_area(position, BLACK) for sq in gen_bitboard_indices(mobility_bb): piece = position.squares[sq] piece_type = piece & 7 piece_colour = piece >> 3 if piece_type == KNIGHT: moves = pseudo_attacks[KNIGHT][sq] elif piece_type == BISHOP: moves = batk_table[sq][(position.occupancy ^ position.piece_bb[(piece_colour << 3) | QUEEN]) & bishop_masks[sq]] elif piece_type == ROOK: moves = ratk_table[sq][(position.occupancy ^ position.piece_bb[(piece_colour << 3) | ROOK] ^ position.piece_bb[(piece_colour << 3) | QUEEN]) & rook_masks[sq]] elif piece_type == QUEEN: moves = ratk_table[sq][position.occupancy & rook_masks[sq]] | batk_table[sq][position.occupancy & bishop_masks[sq]] moves &= mobility_area[piece_colour] move_count = popcount(moves) mobility_score_mg[piece_colour] += mobility[piece_type][move_count][MIDGAME] mobility_score_eg[piece_colour] += mobility[piece_type][move_count][ENDGAME] score_mg = mobility_score_mg[WHITE] - mobility_score_mg[BLACK] score_eg = mobility_score_eg[WHITE] - mobility_score_eg[BLACK] return score_mg, score_eg
def bitset(self,tid_bitsets): if self.pattern: tid_pattern = tid_bitsets[self.pattern[0]] for item in self.pattern: tid_pattern &= tid_bitsets[item] self.bitset = tid_pattern self.support_total = popcount(self.bitset) return self
def replace_stats(self, data, index_bitarray): self.usage = self.update_usage(index_bitarray) for varname, bit_arrays_class in data.targets_info.bit_arrays_var_class.items( ): for category in data.targets_info.categories[varname]: self.usage_per_class[varname][category] = popcount( index_bitarray & bit_arrays_class[category]) return self
def evaluate(self, position): knights = position.piece_bb[W_KNIGHT] | position.piece_bb[B_KNIGHT] bishops = position.piece_bb[W_BISHOP] | position.piece_bb[B_BISHOP] rooks = position.piece_bb[W_ROOK] | position.piece_bb[B_ROOK] queens = position.piece_bb[W_QUEEN] | position.piece_bb[B_QUEEN] phase = 24 - (popcount(knights | bishops) + (popcount(rooks) * 2) + (popcount(queens) * 4)) phase = ((phase * 256) + 12) // 24 score_mg = 0 score_eg = 0 # Piece-square tables score_mg += position.psq_score_mg[WHITE] - position.psq_score_mg[BLACK] score_eg += position.psq_score_eg[WHITE] - position.psq_score_eg[BLACK] # Evaluate material material_score_mg, material_score_eg = self.evaluate_material(position) score_mg += material_score_mg score_eg += material_score_eg # Evaluate pawns pawn_score_mg, pawn_score_eg = self.evaluate_pawns(position) score_mg += pawn_score_mg score_eg += pawn_score_eg # Mobility mobility_bb = knights | bishops | rooks | queens mobility_score_mg, mobility_score_eg = self.evaluate_mobility(position, mobility_bb) score_mg += mobility_score_mg score_eg += mobility_score_eg # King safety w_king_score_mg, w_king_score_eg = self.get_king_safety(position, WHITE) b_king_score_mg, b_king_score_eg = self.get_king_safety(position, BLACK) score_mg += w_king_score_mg - b_king_score_mg score_eg += w_king_score_eg - b_king_score_eg # Interpolate between midgame and endgame score based on phase score = ((score_mg * (256 - phase)) + (score_eg * phase)) // 256 # Return score from current player's perspective + bonus for the side to move return score + 28 if position.colour == WHITE else -score + 28
def test_trim(self): for flags in xrange(16): pn = MemoryPatriciaAuthTree() pn.update((k, v) for k, v in six.iteritems(SCENARIOS[flags]) if k != 'hash_') pn2 = MemoryPatriciaAuthTree() pn2.update(pn) res = pn2.trim(['abc']) self.assertEqual(res, gmpy2.popcount(flags >> 1)) del pn2.hash self.assertEqual(pn2.hash, SCENARIOS[flags]['hash_'], flags) self.assertEqual(pn2.size, gmpy2.popcount(flags)) self.assertEqual(pn2.length, gmpy2.popcount(flags & 1)) if pn2.children: self.assertEqual(pn2.children[0].size, gmpy2.popcount(flags) - (flags & 1)) self.assertEqual(pn2.children[0].length, 0) self.assertTrue(pn2.children[0].pruned) pn3 = MemoryPatriciaAuthTree() pn3.update(pn) res = pn3.trim([Bits(bytes='abc\x00', length=25)]) self.assertEqual(res, gmpy2.popcount(flags >> 2)) del pn3.hash self.assertEqual(pn3.hash, SCENARIOS[flags]['hash_']) self.assertEqual(pn3.size, gmpy2.popcount(flags)) self.assertEqual(pn3.length, gmpy2.popcount(flags & 3))
def count(number: int) -> int: """Count 1s in a binary number Args: number (int): binary number Returns: int: count """ return gmpy2.popcount(number)
def test_add_rule_2items(self, search_parameters, generate_input_dataframe_two_target_normal, generate_subgroup_2subgroups): data = generate_input_dataframe_two_target_normal input_target_model, input_max_depth, input_beam_width, input_minsupp, input_max_rules, input_alpha_gain = search_parameters subgroup2add1, subgroup2add2 = generate_subgroup_2subgroups input_task = "discovery" output_ruleset = GaussianRuleList(data, input_task, input_max_depth, input_beam_width, input_minsupp, input_max_rules, input_alpha_gain) output_ruleset.add_rule(subgroup2add1, data) output_ruleset.add_rule(subgroup2add2, data) expected_number_instances = data.number_instances expected_bitset_uncovered = mpz() expected_bitset_covered = bit_mask(100000) expected_number_rules = 2 expected_length_model = universal_code_integers(2) + \ universal_code_integers(1) +uniform_combination_code(1, 2) +\ universal_code_integers_maximum(1, 2) + uniform_code(10)+ \ universal_code_integers(1) + uniform_combination_code(1, 2) + \ universal_code_integers_maximum(1, 1) + uniform_code(2) actual_numberinstances1 = popcount(output_ruleset.subgroups[0].bitarray) + \ popcount(output_ruleset.subgroups[1].bitarray &~ output_ruleset.subgroups[0].bitarray) + \ popcount(output_ruleset.bitset_uncovered) actual_numberinstances2 = output_ruleset.support_covered + output_ruleset.support_uncovered actual_numberinstances3 = popcount(output_ruleset.bitset_covered) + \ popcount(output_ruleset.bitset_uncovered) actual_numberinstances4 = output_ruleset.subgroups[0].usage + output_ruleset.subgroups[1].usage +\ output_ruleset.default_rule_statistics.usage assert expected_number_instances == actual_numberinstances1 assert expected_number_instances == actual_numberinstances2 assert expected_number_instances == actual_numberinstances3 assert expected_number_instances == actual_numberinstances4 assert expected_bitset_uncovered == output_ruleset.bitset_uncovered assert expected_bitset_covered == output_ruleset.bitset_covered assert expected_number_rules == output_ruleset.number_rules assert expected_length_model == pytest.approx( output_ruleset.length_model)
def generateTotients(n): print "Generating Totients" memory = Memory(n) while gmpy2.popcount(memory.unseen)!=0: # print bin(memory.unseen) memory.updateUnseen() exponentiated_list = buildExponents(memory) seen_times_exponentiated_list = buildSeenTimesExponent(memory,exponentiated_list) memory.seen |= (exponentiated_list|seen_times_exponentiated_list) # print bin(memory.seen) return memory.totients
def test_add_rule_itemnumeric(self, search_parameters, generate_input_dataframe_two_target_normal, generate_subgroup_oneitem_numeric): data = generate_input_dataframe_two_target_normal input_target_model, input_max_depth, input_beam_width, input_minsupp, input_max_rules, input_alpha_gain = search_parameters subgroup2add = generate_subgroup_oneitem_numeric input_task = "discovery" output_ruleset = GaussianRuleList(data, input_task, input_max_depth, input_beam_width, input_minsupp, input_max_rules, input_alpha_gain) output_ruleset.add_rule(subgroup2add, data) expected_number_instances = data.number_instances expected_bitset_uncovered = indexes2bitset( [i for i in range(expected_number_instances) if i > 16666]) expected_bitset_covered = indexes2bitset( [i for i in range(expected_number_instances) if i < 16666 + 1]) expected_number_rules = 1 expected_length_model = universal_code_integers(1) + universal_code_integers(1)+\ uniform_combination_code(1, 2) + universal_code_integers_maximum(1,2)+ \ uniform_code(10) actual_numberinstances1 = popcount(output_ruleset.subgroups[0].bitarray) +\ popcount(output_ruleset.bitset_uncovered) actual_numberinstances2 = output_ruleset.support_covered + output_ruleset.support_uncovered actual_numberinstances3 = popcount(output_ruleset.bitset_covered) +\ popcount(output_ruleset.bitset_uncovered) actual_numberinstances4 = output_ruleset.subgroups[ 0].usage + output_ruleset.default_rule_statistics.usage assert expected_number_instances == actual_numberinstances1 assert expected_number_instances == actual_numberinstances2 assert expected_number_instances == actual_numberinstances3 assert expected_number_instances == actual_numberinstances4 assert expected_bitset_uncovered == output_ruleset.bitset_uncovered assert expected_bitset_covered == output_ruleset.bitset_covered assert expected_number_rules == output_ruleset.number_rules assert expected_length_model == pytest.approx( output_ruleset.length_model)
def __init__(self, name, num, truthtable): self.num = num self.name = name self.tt = truthtable # subtract 1 to remove leading 1 self.cardinality = truthtable.num_digits(2) - 1 self.captured = gmpy2.popcount(truthtable) - 1 # if more than half the samples are captured if labels and self.captured > 0: self.corr = gmpy2.popcount(self.tt & labels[1].tt) - 1 if self.corr >= (self.captured / 2): self.predict = 1 self.correct = self.corr / float(self.captured) else: self.predict = 0 self.correct = (self.captured - self.corr) / \ float(self.captured) else: self.predict = 1 self.correct = 0 self.corr = 0
def set_mu(self,new_mu): ''' update the value of mu in the Hamiltonian :param new_mu: new value of mu ''' # Compute the modification on the diagonal of H delta_mu = self.mu-new_mu #compute the dimension of the hamiltonian n=2*len(self.impurity_index) fock_dim=2**(2*n) for i in np.arange(fock_dim): # update each diagonal element of H according to the number of electrons self.H[i,i]+=delta_mu*gmpy2.popcount(int(i)) self.mu=new_mu
def compare_bin15(large, small, mism): import gmpy2 mycode = {'A': b'1000', 'C': b'0100', 'G': b'0010', 'T': b'0001'} large_str = b''.join(mycode[c] for c in large) small_str = b''.join(mycode[c] for c in small) small_num = int(small_str, 2) S = len(small) out = [] for i in range(len(large) - S + 1): large_num = int(large_str[4 * i : 4 * (i + S)], 2) and_num = small_num & large_num if gmpy2.popcount(and_num) >= S - mism: out.append(i) return out
def popcount(x): """ Efficient methods for counting the on-bits in an integer It uses 17 arithmetic operations. """ # x -= (x >> 1) & m1 # put count of each 2 bits into those 2 bits # x = (x & m2) + ((x >> 2) & m2) # put count of each 4 bits into those 4 bits # x = (x + (x >> 4)) & m4 # put count of each 8 bits into those 8 bits # x += x >> 8 # put count of each 16 bits into their lowest 8 bits # x += x >> 16 # put count of each 32 bits into their lowest 8 bits # x += x >> 32 # put count of each 64 bits into their lowest 8 bits # return x & 0x7f # Using library return gmpy2.popcount(x)
def build_lookuptable(self): """ Build a clever basis """ j_down = 0 for n_down in range(0, self.nb_electrons + 1): nb_states = special.binom(self.nb_flavors / 2 * self.nb_sites, self.nb_electrons - n_down) for down_part in range( 0, int(pow(2, self.nb_flavors / 2 * self.nb_sites))): j_up = 0 if gmpy2.popcount(down_part) == n_down: for up_part in range( 0, int(pow(2, self.nb_flavors / 2 * self.nb_sites))): if gmpy2.popcount( up_part) == self.nb_electrons - n_down: self.J_up[up_part] = j_up self.J_down[down_part] = j_down self.J[int(j_up + j_down)] = pow( 2, self.nb_flavors / 2 * self.nb_sites) * down_part + up_part j_up += 1 j_up = 0 j_down += nb_states
def build_look_up_table(self,nb_sites): ''' Build a look up tables to index the states in occupation representation J(i) is the binary representation of the i-th state Starting with the i-th state, its index can be found by computing (J_up(int(i_up))+J_down(int(i_down))) :return: J J_up and J_down ''' L = nb_sites N = nb_sites # todo : custum number of electrons n_states_per_spin = 2 ** (L) J_up = np.zeros([n_states_per_spin],dtype="int") J_down = np.zeros([n_states_per_spin],dtype="int") J = np.zeros([int(scipy.misc.comb(2 * L, N))],dtype="int") j_down = int(0) # First runner for n_down in np.arange(N+1): n_up=N-n_down n_states_up=scipy.misc.comb(L,n_up) j_up=int(0) for down_part in np.arange (n_states_per_spin): down_part=int(down_part) n_down_electrons = gmpy2.popcount(down_part) if n_down_electrons == n_down: for upper_part in np.arange(n_states_per_spin): upper_part=int(upper_part) n_up_electrons = gmpy2.popcount(upper_part) if (n_up_electrons==n_up): J_up[upper_part]=j_up J_down[down_part]=j_down J[j_down+j_up]=n_states_per_spin*upper_part+down_part j_up+=1 j_up=int(0) j_down+=int(n_states_up) return J,J_up,J_down
def add_rule(self, subgroup2add, tid_bitsets, attributes): self.number_rules += 1 tid_cand = subgroup2add.bitset self.bitset_covered = self.bitset_covered | tid_cand self.support_covered = popcount(self.bitset_covered) self.bitset_uncovered = self.bitset_uncovered & ~tid_cand self.support_uncovered = popcount(self.bitset_uncovered) self.bitset_rules.append(tid_cand) self.antecedent_raw.append(subgroup2add.pattern) self.statistic_rules.append(subgroup2add.statistic) # IN CLASSIFICATION CASE EVERYTHING HAS TO BE UPDATED! self.default_statistic["usage"] = self.support_uncovered if self.task == "classification": aux_tid = xmpz(self.bitset_uncovered) idx_bitsdef = list(aux_tid.iter_set()) values_default = self.target_values[idx_bitsdef] self.default_mean["mean"] = np.mean(values_default) self.default_variance["variance"] = np.var(values_default) # SHOULD BE REMOVED LATER ON support = popcount(tid_cand) self.support_rules.append(support) self.add_pattern4prediction(subgroup2add.pattern, attributes) self.consequent_description.append( self.add_description_consequent(subgroup2add)) self.consequent_lastrule_description = self.add_consequent_lastrule() self.add_description_antecedent(subgroup2add.pattern, attributes) self.length_model = compute_length_model(self) self.length_data = compute_length_data[self.target_type](self) self.constant = delta_data_const[self.target_type](self) if self.length_original > 0: self.length_ratio = (self.length_data + self.length_model) / self.length_original elif self.length_original < 0: self.length_ratio = self.length_original / (self.length_data + self.length_model) return self
def play_out(self, pb, ob, mp): # pb,obが返されるのでob,pbに代入して同時に入れ替える ob, pb = Play().put_turn(pb, ob, mp) teban = 0 while True: if not Play().legal_board(pb, ob): pb, ob = ob, pb teban ^= 1 if not Play().legal_board(pb, ob): ps = popcount(pb) # player_stone os = popcount(ob) # opponent_stone if teban: # 手番が渡されたときと同じ if ps >= os: return 1 elif ps < os: return 0 else: # 手番が渡されたときと異なる if os >= ps: return 1 elif os < ps: return 0 mp = Move().rand_move(pb, ob) ob, pb = Play().put_turn(pb, ob, mp) teban ^= 1
def test_prune(self): for flags in xrange(16): pn = MemoryPatriciaDict() old_items = set((k,v) for k,v in six.iteritems(SCENARIOS[flags]) if k != 'hash_') pn.update(old_items) for idx,scenario in enumerate(SCENARIOS): if idx|flags != flags or idx&flags != idx: continue new_items = set((k,v) for k,v in six.iteritems(scenario) if k != 'hash_') pn2 = MemoryPatriciaDict() pn2.update(pn) pn3 = MemoryPatriciaDict() pn3.update(pn) pn4 = MemoryPatriciaDict() pn4.update(pn) for d in (pn, pn2, pn3, pn4): self.assertEqual(d.hash, SCENARIOS[flags]['hash_']) self.assertEqual(d.size, gmpy2.popcount(flags)) self.assertEqual(d.length, gmpy2.popcount(flags)) keys = set() for (k,v) in new_items: pn2.prune([k]) pn3.prune(iter([k])) keys.add(k) pn4.prune(keys) self.assertEqual(pn.hash, SCENARIOS[flags]['hash_']) self.assertEqual(pn.size, gmpy2.popcount(flags)) self.assertEqual(pn.length, gmpy2.popcount(flags)) for d in (pn2, pn3, pn4): del d.hash self.assertEqual(d.hash, SCENARIOS[flags]['hash_']) self.assertEqual(d.size, gmpy2.popcount(flags)) self.assertEqual(d.length, gmpy2.popcount(flags) - len(new_items)) self.assertEqual(set(d.items()), old_items - new_items) for key,value in old_items: if key in keys: with self.assertRaises(KeyError): d.prune([key]) else: self.assertEqual(d[key], value) for item in old_items: if item in new_items: self.assertEqual(d[item[0]], item[1]) else: with self.assertRaises(KeyError): d.prune([item[0]])
def test_trim(self): for flags in xrange(16): pn = MemoryPatriciaDict() pn.update((k,v) for k,v in six.iteritems(SCENARIOS[flags]) if k != 'hash_') pn2 = MemoryPatriciaDict() pn2.update(pn) res = pn2.trim(['abc']) self.assertEqual(res, gmpy2.popcount(flags>>1)) del pn2.hash self.assertEqual(pn2.hash, SCENARIOS[flags]['hash_'], flags) self.assertEqual(pn2.size, gmpy2.popcount(flags)) self.assertEqual(pn2.length, gmpy2.popcount(flags&1)) if pn2.children: self.assertEqual(pn2.children[0].size, gmpy2.popcount(flags) - (flags&1)) self.assertEqual(pn2.children[0].length, 0) self.assertTrue(pn2.children[0].pruned) pn3 = MemoryPatriciaDict() pn3.update(pn) res = pn3.trim([Bits(bytes='abc\x00', length=25)]) self.assertEqual(res, gmpy2.popcount(flags>>2)) del pn3.hash self.assertEqual(pn3.hash, SCENARIOS[flags]['hash_']) self.assertEqual(pn3.size, gmpy2.popcount(flags)) self.assertEqual(pn3.length, gmpy2.popcount(flags&3))
neighbors = [list(set([i for nbhd in neighborhoods[s] for i in nbhd]) - {s}) for s in squares] setneighbors = [set(neighbors[s]) for s in squares] coordinates = dict((s, [9,9,9]) for s in squares) for x in range(9): for y in the_rows[x]: coordinates[y][0] = x for y in the_columns[x]: coordinates[y][1] = x for y in the_boxes[x]: coordinates[y][2] = x # TODO This is a mess. Fix it. tuples = [[] for n in range(10)] for x in range(2**9): p = gmpy2.popcount(x) tuples[p].append(x) singles = tuples[1] values = dict((v, list({s & v for s in singles} - {0})) for v in range(2**9)) valuesets = dict((v, {s & v for s in singles} - {0}) for v in range(2**9)) methodnames = ['naked singles', 'naked doubles', 'hidden singles', 'hidden doubles', 'hidden triples', 'hidden quadruples', 'intersectionremoval', 'x-wing', 'given clue'] methodcounter = [0 for dummy in methodnames] # </editor-fold> def initialize(v): """ :param v: String representation of a sudoku board.
def eval_board(board, gameover=False): white_mask = board.occupied_co[chess.WHITE] black_mask = board.occupied_co[chess.BLACK] material = ((gmpy2.popcount((board.pawns & white_mask)) - gmpy2.popcount((board.pawns & black_mask)))*1 + (gmpy2.popcount((board.knights & white_mask)) - gmpy2.popcount((board.knights & black_mask)))*3 + (gmpy2.popcount((board.bishops & white_mask)) - gmpy2.popcount((board.bishops & black_mask)))*3 + (gmpy2.popcount((board.rooks & white_mask)) - gmpy2.popcount((board.rooks & black_mask)))*5 + (gmpy2.popcount((board.queens & white_mask)) - gmpy2.popcount((board.queens & black_mask)))*9 + (gmpy2.popcount((board.kings & white_mask)) - gmpy2.popcount((board.kings & black_mask)))*1000) if gameover or abs(material) >= 9 or (gmpy2.popcount(white_mask) + gmpy2.popcount(black_mask)) < 9: if board.is_checkmate(): if board.turn: return -10000 else: return 10000 return material
def test_update(self): for flags in xrange(16): pn = MemoryPatriciaDict() pn.update((k,v) for k,v in six.iteritems(SCENARIOS[flags]) if k != 'hash_') for idx,scenario in enumerate(SCENARIOS): items = [(k,v) for k,v in six.iteritems(scenario) if k != 'hash_'] pn2 = MemoryPatriciaDict() pn2.update(pn) self.assertEqual(pn2.hash, SCENARIOS[flags]['hash_']) self.assertEqual(pn2.size, gmpy2.popcount(flags)) self.assertEqual(pn2.length, gmpy2.popcount(flags)) pn2.update(items) self.assertEqual(pn2.hash, SCENARIOS[flags|idx]['hash_']) self.assertEqual(pn2.size, gmpy2.popcount(flags|idx)) self.assertEqual(pn2.length, gmpy2.popcount(flags|idx)) pn3 = MemoryPatriciaDict() pn3.update(pn) self.assertEqual(pn3.hash, SCENARIOS[flags]['hash_']) self.assertEqual(pn3.size, gmpy2.popcount(flags)) self.assertEqual(pn3.length, gmpy2.popcount(flags)) pn3.update(iter(items)) self.assertEqual(pn3.hash, SCENARIOS[flags|idx]['hash_']) self.assertEqual(pn3.size, gmpy2.popcount(flags|idx)) self.assertEqual(pn3.length, gmpy2.popcount(flags|idx)) pn4 = MemoryPatriciaDict() pn4.update(pn) self.assertEqual(pn4.hash, SCENARIOS[flags]['hash_']) self.assertEqual(pn4.size, gmpy2.popcount(flags)) self.assertEqual(pn4.length, gmpy2.popcount(flags)) pn4.update(**dict(items)) self.assertEqual(pn4.hash, SCENARIOS[flags|idx]['hash_']) self.assertEqual(pn4.size, gmpy2.popcount(flags|idx)) self.assertEqual(pn4.length, gmpy2.popcount(flags|idx))
def get_mobility(self, player): valid_moves = self.__valid_moves(player) move_in_all_dir = 0 for vm in valid_moves.values(): move_in_all_dir |= vm return gmpy2.popcount(move_in_all_dir)