import numpy as np
import random

# 状態数と行動数
n_state = 60
n_action = 50

# Qテーブル
Q = np.zeros((n_state, n_action))

def get_reward(state):
    # 例: 最後の状態に近づけば +1
    return 1 if state == 100 else -1

for episode in range(100):
    state = 0
    for step in range(100):
        action = random.randint(0, n_action - 1)
        reward = get_reward(state)

        next_state = min(18, state + 1)

        # Q学習ルール
        Q[state, action] += 0.1 * (
            reward + 0.9 * np.max(Q[next_state]) - Q[state, action]
        )

        state = next_state

print(Q)

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: