def main():
    rankings = read_ranks_file('qvist_rankings.txt')
    ranker = Ranker([
            #(win_margin, 1.287),
            #(prob_win_margin, 1),
            #(win_given_no_gain, -.05),
            #(win_weighted_gain, 1.0),
            #(frequency_purchased, .1),
            #(frequency_weighted_win_margin, .01),
            (win_given_any_gain, 2.5),
            (log_odds_any_gained, .05),
            (num_plus_actions, -.03),
            (has_vp, -.1),
            (is_reaction, .1),
            ])
    rank_eval = RankEvaluator(ranker, rankings, ranking_accuracy)
    learned_weights = scipy.optimize.fmin(rank_eval, rank_eval.ranker.weights)
    ranker.weights = learned_weights
    
    grouped_by_cost = collections.defaultdict(list)
    for card in card_info.card_names():
        grouped_by_cost[card_info.cost(card)].append(card)
    for cost, card_list in grouped_by_cost.iteritems():
        if len(card_list) >= 4:
            card_list.sort(key=ranker.score)
            print cost, ','.join(card_list)
Exemple #2
0
def main():
    rankings = read_ranks_file('qvist_rankings.txt')
    ranker = Ranker([
        #(win_margin, 1.287),
        #(prob_win_margin, 1),
        #(win_given_no_gain, -.05),
        #(win_weighted_gain, 20),
        #(frequency_purchased, .1),
        #(frequency_weighted_win_margin, .01),
        (win_given_any_gain, 30),
        (log_odds_any_gained, 1.3),
        (num_plus_actions, -1),
        (has_vp, -1.5),
        (is_reaction, 1.5),
    ])
    # this should really be doing cross validation
    rank_eval = RankEvaluator(ranker, rankings, ranking_log_loss)
    learned_weights = [52.926, 1.358, -1.161, -1.712, 1.626]
    ranker.weights = learned_weights
    learned_weights = scipy.optimize.fmin_bfgs(rank_eval,
                                               rank_eval.ranker.weights,
                                               gtol=1e-3)
    ranker.weights = learned_weights
    print learned_weights
    display_large_errors(ranker, rankings)

    grouped_by_cost = collections.defaultdict(list)
    for card in card_info.card_names():
        grouped_by_cost[card_info.cost(card)].append(card)
    for cost, card_list in grouped_by_cost.iteritems():
        if len(card_list) >= 4:
            card_list.sort(key=ranker.score)
Exemple #3
0
def main():
    rankings = read_ranks_file('qvist_rankings.txt')
    ranker = Ranker([
            #(win_margin, 1.287),
            #(prob_win_margin, 1),
            #(win_given_no_gain, -.05),
            #(win_weighted_gain, 20),
            #(frequency_purchased, .1),
            #(frequency_weighted_win_margin, .01),
            (win_given_any_gain, 30),
            (log_odds_any_gained, 1.3),
            (num_plus_actions, -1),
            (has_vp, -1.5),
            (is_reaction, 1.5),
            ])
    # this should really be doing cross validation
    rank_eval = RankEvaluator(ranker, rankings, ranking_log_loss)
    learned_weights = [ 52.926,   1.358,  -1.161,  -1.712,   1.626]
    ranker.weights = learned_weights
    learned_weights = scipy.optimize.fmin_bfgs(
        rank_eval, rank_eval.ranker.weights, gtol=1e-3)
    ranker.weights = learned_weights 
    print learned_weights
    display_large_errors(ranker, rankings)
 
    grouped_by_cost = collections.defaultdict(list)
    for card in card_info.card_names():
        grouped_by_cost[card_info.cost(card)].append(card)
    for cost, card_list in grouped_by_cost.iteritems():
        if len(card_list) >= 4:
            card_list.sort(key=ranker.score)
    def GET(self):
        web.header("Content-Type", "text/html; charset=utf-8")  
        query_dict = dict(urlparse.parse_qsl(web.ctx.env['QUERY_STRING']))
        db = utils.get_mongo_database()
        selected_card = ''

        if 'card' in query_dict:
            selected_card = query_dict['card']

        results = db.trueskill_openings.find({'_id': {'$regex': '^open:'}})
        openings = list(results)
        card_list = card_info.OPENING_CARDS
        def split_opening(o):
            ret = o['_id'][len('open:'):].split('+')
            if ret == ['']: return []
            return ret

        if selected_card not in ('All cards', ''):
            openings = [o for o in openings if selected_card in 
                        split_opening(o)]
                        
        openings = [o for o in openings if split_opening(o)]
        for opening in openings:
            floor = opening['mu'] - opening['sigma'] * 3
            ceil = opening['mu'] + opening['sigma'] * 3
            opening['level_key'] = make_level_key(floor, ceil)
            opening['level_str'] = make_level_str(floor, ceil)
            opening['skill_str'] = skill_str(opening['mu'], opening['sigma'])
            opening['cards'] = split_opening(opening)
            opening['cards'].sort()
            opening['cards'].sort(key=lambda card: (card_info.cost(card)),
                reverse=True)
            costs = [str(card_info.cost(card)) for card in opening['cards']]
            while len(costs) < 2:
                costs.append('-')
            opening['cost'] = '/'.join(costs)

        openings.sort(key=lambda opening: opening['level_key'])
        openings.reverse()
        if selected_card == '':
            openings = [op for op in openings
                        if op['level_key'][0] != 0
                        or op['_id'] == ['Silver', 'Silver']]

        render = web.template.render('')
        return render.openings_template(openings, card_list, selected_card)
Exemple #5
0
    def GET(self):
        web.header("Content-Type", "text/html; charset=utf-8")  
        query_dict = dict(urlparse.parse_qsl(web.ctx.env['QUERY_STRING']))
        db = utils.get_mongo_database()
        selected_card = ''

        if 'card' in query_dict:
            selected_card = query_dict['card']

        results = db.trueskill_openings.find({'_id': {'$regex': '^open:'}})
        openings = list(results)
        card_list = card_info.OPENING_CARDS
        def split_opening(o):
            ret = o['_id'][len('open:'):].split('+')
            if ret == ['']: return []
            return ret

        if selected_card not in ('All cards', ''):
            openings = [o for o in openings if selected_card in 
                        split_opening(o)]
                        
        openings = [o for o in openings if split_opening(o)]
        for opening in openings:
            floor = opening['mu'] - opening['sigma'] * 3
            ceil = opening['mu'] + opening['sigma'] * 3
            opening['level_key'] = make_level_key(floor, ceil)
            opening['level_str'] = make_level_str(floor, ceil)
            opening['skill_str'] = skill_str(opening['mu'], opening['sigma'])
            opening['cards'] = split_opening(opening)
            opening['cards'].sort()
            opening['cards'].sort(key=lambda card: (card_info.cost(card)),
                reverse=True)
            costs = [str(card_info.cost(card)) for card in opening['cards']]
            while len(costs) < 2:
                costs.append('-')
            opening['cost'] = '/'.join(costs)

        openings.sort(key=lambda opening: opening['level_key'])
        openings.reverse()
        if selected_card == '':
            openings = [op for op in openings
                        if op['level_key'][0] != 0
                        or op['_id'] == ['Silver', 'Silver']]

        render = web.template.render('')
        return render.openings_template(openings, card_list, selected_card)
def main():
    ARCH = 'Archivist'
    card_data = json.load(open('card_conditional_data.json'))
    card_names = card_info.card_names()
    card_names.remove(ARCH)
    card_inds = {}
    for ind, card_name in enumerate(card_names):
        card_inds[card_name] = ind
    N = len(card_inds)
    
    # cluster based on gain prob, win rate given any gained, 
    # avg gained per game, and win rate per gain
    M = 4
    grouped_data = np.zeros((N, M, N))
    for card_row in card_data:
        card_name = card_row['card_name']
        condition = card_row['condition'][0]
        if card_name == ARCH or condition == ARCH:
            continue
        assert len(card_row['condition']) == 1
        if card_name == condition:
            continue
        i = card_inds[card_name]
        j = card_inds[condition]
        stats = card_row['stats']
        def parse(key):
            ret = MeanVarStat()
            ret.from_primitive_object(stats[key])
            return ret
        wgag = parse('win_given_any_gain')
        wgng = parse('win_given_no_gain')
        wwg = parse('win_weighted_gain')
        total_games = wgag.frequency() + wgng.frequency()
        grouped_data[i][0][j] = wgag.frequency() / total_games
        grouped_data[i][1][j] = wgag.mean()
        #grouped_data[i][2][j] = wwg.frequency() / total_games
        # grouped_data[i][3][j] = wwg.mean()

    for i in range(N):
        for j in range(M):
            s = sum(grouped_data[i][j])
            # make the self data == avg
            grouped_data[i][j][i] = s / (N - 1)

    for i in range(N):
        for j in range(M):
            grouped_data[i][j] = preprocessing.scale(grouped_data[i][j])

    flattened_normed_data = np.zeros((N, 
                                      2 * N * M + len(bonus_feature_funcs)))
    for i in range(N):
        bonus_vec = get_bonus_vec(card_names[i])
        v1, v2 = [], []
        for j in range(M):
            for k in range(N):
                v1.append(grouped_data[i][j][k])
                v2.append(grouped_data[k][j][i])
        v1, v2 = np.array(v1), np.array(v2)
        catted = np.concatenate((v1, v1, bonus_vec))
        flattened_normed_data[i] = catted

    flattened_normed_data, card_names = trim(
        lambda x: not (card_info.cost(x)[0] >= '5' or 
                   card_info.cost(x)[0] == '1' or 
                   card_info.cost(x)[0] == 'P') and not (
            x in card_info.EVERY_SET_CARDS or 
            card_info.cost(x)[0:2] == '*0'),
        flattened_normed_data, card_names)
    
    z = scipy.cluster.hierarchy.ward(flattened_normed_data)
    scipy.cluster.hierarchy.dendrogram(z, labels=card_names,
                                       orientation='left', leaf_font_size=4.5,
                                       )
    pylab.savefig('expensive_group_win_prob.png', 
                  dpi=len(card_names) * 2.5, bbox_inches='tight')