1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WIN5の組み合わせ考えるコード

Posted at

WIN5がキャリーオーバ中ということで、打の組み合わせを買えばいいか考えるコード考えてみた(書いたのはAI)

考え方としては人気順、つまりオッズの低くなるような組み合わせを購入する。(無駄に邪念が入ると当たらないため)

データとしては以下のサイト様からコピペしてこれる以下のようなデータを使用する。

人気		馬番	馬名	単勝オッズ	複勝オッズ
1	8	10	サトノシャイニング	2.6	1.2 - 1.5
2	6	6	チェルヴィニア	4.2	1.4 - 1.9
3	7	8	ホウオウビスケッツ	5.4	1.5 - 2.1
4	2	2	エルトンバローズ	7.4	1.8 - 2.7
5	7	9	レーベンスティール	7.6	2.4 - 3.8

計算方法としては、すべての組み合わせのオッズの席を計算し、低いものから順番に表示する。

import csv
import itertools

def read_odds_from_tsv(filepath):
    """タブ区切りTSVから (馬番, 単勝オッズ) のリストを返す"""
    horses = []
    with open(filepath, encoding='utf-8') as f:
        reader = csv.DictReader(f, delimiter='\t')
        for row in reader:
            umaban = int(row["馬番"])
            odds = float(row["単勝オッズ"])
            horses.append((umaban, odds))
    return horses

def top_win5_combinations(race_files, top_n=20):
    """5つのTSVファイルから合成オッズの低い順に上位N通り出す"""
    odds_lists = [read_odds_from_tsv(f) for f in race_files]
    results = []
    for combo in itertools.product(*odds_lists):
        # combo = ((馬番,オッズ), (馬番,オッズ), ... 5レース分)
        total_odds = 1
        for (_, o) in combo:
            total_odds *= o
        results.append((total_odds, combo))
    results.sort(key=lambda x: x[0])
    return results[:top_n]

def print_results(results):
    print("順位\t合成オッズ\t組み合わせ")
    for i, (total, combo) in enumerate(results, start=1):
        combo_str = " | ".join(f"{umaban}({odds})" for umaban, odds in combo)
        print(f"{i}\t{total:.4f}\t{combo_str}")

if __name__ == "__main__":
    # 例:5レース分のTSVファイル
    race_files = [
        "data/race1.tsv",
        "data/race2.tsv",
        "data/race3.tsv",
        "data/race4.tsv",
        "data/race5.tsv",
    ]
    results = top_win5_combinations(race_files, top_n=20)
    print_results(results)

結果としてはこんな感じ。

順位    合成オッズ      組み合わせ
1       312.6152        5(4.3) | 9(2.2) | 2(4.1) | 4(3.1) | 10(2.6)
2       472.7351        5(4.3) | 9(2.2) | 15(6.2) | 4(3.1) | 10(2.6)
3       504.9937        5(4.3) | 9(2.2) | 2(4.1) | 4(3.1) | 6(4.2)
...
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?