Last active
March 16, 2018 13:00
-
-
Save shiracamus/aae1d11bbcabff8bb485c22cf56b43bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import random | |
import matplotlib.pyplot as plt | |
NUM_TRIAL = 500 # 試行回数 | |
ODDS = 2 # オッズ | |
START_MONEY = 100 # 初期資金 | |
START_BET = 1 # 最初のベット額 | |
MARTIN_PARAM = 2 # マーチンゲール法で負けた場合のベットの増加額 | |
class Dealer: | |
def __init__(self): | |
self.losing_times = 0 | |
def deal(self, players): | |
if self.is_player_win(): | |
self.losing_times += 1 | |
for player in players: | |
player.win(player.betting_money * (ODDS - 1)) | |
else: | |
for player in players: | |
player.lose(player.betting_money) | |
def is_player_win(self): | |
dice = random.randint(1, 100) | |
return dice <= 100 / ODDS | |
class Player: | |
def __init__(self, strategy): | |
self.strategy = strategy | |
self.name = strategy.NAME_JP | |
self.label = strategy.NAME_EN | |
self.money = START_MONEY | |
self.money_history = [self.money] | |
self.betting_times = 0 | |
def bet(self): | |
self.betting_money = max(int(self.strategy.bet(self.money)), 0) | |
if self.is_betable(): | |
self.betting_times += 1 | |
else: | |
self.betting_money = 0 | |
def is_betable(self): | |
return 0 < self.betting_money <= self.money | |
def win(self, money): | |
self.money += money | |
self.money_history.append(self.money) | |
self.strategy.win() | |
def lose(self, money): | |
self.money -= money | |
self.money_history.append(self.money) | |
self.strategy.lose() | |
class Strategy: | |
def __init__(self): | |
self.betting_money = START_BET | |
def bet(self, money): | |
return self.betting_money | |
def win(self): | |
pass | |
def lose(self): | |
pass | |
class Martingale(Strategy): | |
NAME_JP = "マーチンゲール法" | |
NAME_EN = "Martin method" | |
def win(self): | |
self.betting_money = START_BET | |
def lose(self): | |
self.betting_money *= MARTIN_PARAM | |
class RevMartingale(Strategy): | |
NAME_JP = "逆マーチンゲール法" | |
NAME_EN = "Pare method" | |
def win(self): | |
if self.betting_money == START_BET * MARTIN_PARAM**3: | |
self.betting_money = START_BET | |
else: | |
self.betting_money *= MARTIN_PARAM | |
def lose(self): | |
self.betting_money = START_BET | |
class Dalembert(Strategy): | |
NAME_JP = "ダランベール法" | |
NAME_EN = "Dalembert method" | |
def win(self): | |
self.betting_money += 1 | |
def lose(self): | |
if self.betting_money > START_BET: | |
self.betting_money -= 1 | |
class EastCoast(Strategy): | |
NAME_JP = "イーストコースト法" | |
NAME_EN = "East coast method" | |
def win(self): | |
self.betting_money += (self.betting_money * ODDS) / 2 | |
def lose(self): | |
self.betting_money = START_BET | |
class TenPercent(Strategy): | |
NAME_JP = "テンパーセント法" | |
NAME_EN = "Ten percent method" | |
def bet(self, money): | |
return money * 0.1 | |
class MonteCarlo(Strategy): | |
NAME_JP = "モンテカルロ法" | |
NAME_EN = "Monte_carlo method" | |
def __init__(self): | |
self.quality = [1, 2, 3] | |
def bet(self, *args): | |
return (self.quality[0] + self.quality[-1]) * 0.5 | |
def win(self): | |
if len(self.quality) <= 5: | |
self.quality = [1, 2, 3] | |
else: | |
self.quality = self.quality[2:-2] | |
def lose(self): | |
self.quality.append(self.bet() * 2) | |
def play(players): | |
dealer = Dealer() | |
for trial in range(NUM_TRIAL): | |
for player in players: | |
player.bet() | |
dealer.deal(players) | |
for player in players: | |
print("{0.name}は{0.betting_times}回、{0.money}円で終了".format(player)) | |
print("勝率: %g" % (dealer.losing_times / NUM_TRIAL)) | |
def plot(players): | |
x = range(NUM_TRIAL + 1) | |
for player in players: | |
plt.plot(x, player.money_history, label=player.label) | |
plt.legend() | |
plt.show() | |
def main(): | |
players = (Player(Martingale()), | |
Player(RevMartingale()), | |
Player(Dalembert()), | |
Player(EastCoast()), | |
Player(TenPercent()), | |
Player(MonteCarlo())) | |
play(players) | |
plot(players) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment