强化学习(多臂老虎机随笔)

import random

import numpy


def update_score(score, round_score, t):
    return (score + round_score) / (t + 2), t + 1


lr = 0.1
scoreT1 = scoreT2 = 998
TimeT1 = TimeT2 = 0
print('================================================')
for i in range(10):
    Tiger1 = numpy.random.normal(500, 50, 1)[0]
    Tiger2 = numpy.random.normal(550, 100, 1)[0]
    print('Epoch {} start: '.format(i + 1))
    print('Tiger1 : ', end='')
    print(Tiger1)
    print('Tiger2 : ', end='')
    print(Tiger2)
    p = random.random()
    print("The machine's choice is : ", end='')
    if p < lr:
        print('\nRandom! ', end='')
        tem = random.random()
        if tem < 0.5:
            print('TigerMachine1 ')
            scoreT1, TimeT1 = update_score(scoreT1, Tiger1, TimeT1)
        else:
            print('TigerMachine2 ')
            scoreT2, TimeT2 = update_score(scoreT2, Tiger2, TimeT2)
    else:
        if scoreT1 > scoreT2:
            print('TigerMachine1 ')
            scoreT1, TimeT1 = update_score(scoreT1, Tiger1, TimeT1)
        elif scoreT1 < scoreT2:
            print('TigerMachine2 ')
            scoreT2, TimeT2 = update_score(scoreT2, Tiger2, TimeT2)
        else:
            print("\nTigerMachine1 and 2 have the same score!")
            tem = random.random()
            if tem < 0.5:
                print('TigerMachine1 ')
                scoreT1, TimeT1 = update_score(scoreT1, Tiger1, TimeT1)
            else:
                print('TigerMachine2 ')
                scoreT2, TimeT2 = update_score(scoreT2, Tiger2, TimeT2)
    print('The score is: {} || {}'.format(scoreT1, scoreT2))
    print('================================================')
if scoreT1 > scoreT2:
    print("Machine's choice is : TigerMachine1!")
elif scoreT1 < scoreT2:
    print("Machine's choice is : TigerMachine2!")
else:
    print("Machine is not sure which to choose!")

结果:

================================================
Epoch 1 start: 
Tiger1 : 463.0101391643539
Tiger2 : 298.4885916641581
The machine's choice is : 
TigerMachine1 and 2 have the same score!
TigerMachine1 
The score is: 730.5050695821769 || 998
================================================
Epoch 2 start: 
Tiger1 : 551.4534188059761
Tiger2 : 519.840792191374
The machine's choice is : TigerMachine2 
The score is: 730.5050695821769 || 758.920396095687
================================================
Epoch 3 start: 
Tiger1 : 576.184385652595
Tiger2 : 504.77483655497775
The machine's choice is : TigerMachine2 
The score is: 730.5050695821769 || 421.2317442168883
================================================
Epoch 4 start: 
Tiger1 : 499.1808052984322
Tiger2 : 457.4179755424246
The machine's choice is : TigerMachine1 
The score is: 409.8952916268697 || 421.2317442168883
================================================
Epoch 5 start: 
Tiger1 : 525.7789113792235
Tiger2 : 436.6109507239315
The machine's choice is : TigerMachine2 
The score is: 409.8952916268697 || 214.46067373520495
================================================
Epoch 6 start: 
Tiger1 : 566.1834496378923
Tiger2 : 561.0098334950821
The machine's choice is : TigerMachine1 
The score is: 244.0196853161905 || 214.46067373520495
================================================
Epoch 7 start: 
Tiger1 : 419.8646885517717
Tiger2 : 583.9723500664198
The machine's choice is : Random! TigerMachine1 
The score is: 132.77687477359243 || 214.46067373520495
================================================
Epoch 8 start: 
Tiger1 : 366.3212662085439
Tiger2 : 813.7469293046312
The machine's choice is : TigerMachine2 
The score is: 132.77687477359243 || 205.64152060796724
================================================
Epoch 9 start: 
Tiger1 : 640.8566380301332
Tiger2 : 594.9562603783611
The machine's choice is : Random! TigerMachine1 
The score is: 128.93891880062094 || 205.64152060796724
================================================
Epoch 10 start: 
Tiger1 : 485.82866603037724
Tiger2 : 591.4838387998494
The machine's choice is : TigerMachine2 
The score is: 128.93891880062094 || 132.85422656796945
================================================
Machine's choice is : TigerMachine2!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赤赤赤赤赤赤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值