-
Notifications
You must be signed in to change notification settings - Fork 0
/
q_table.py
53 lines (44 loc) · 1.8 KB
/
q_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
@Description: This file contains some useful functions for the project.
@Author : Erfan Fathi
@Date : 25 May 2023
"""
import numpy as np
def Qtable(state_space, action_space, bin_size=100):
"""
Description : This function defines the q-table for the cartpole-v1 environment.
Args:
state_space : The state space of the environment.
action_space : The action space of the environment.
bin_size : The number of bins for discretizing the state space.
Returns:
q_table : The q-table for the cartpole-v1 environment.
bins : The bins for discretizing the state space.
Info:
state_space Shape : (4,)
state_space High : [ 4.8 inf 0.42 inf]
state_space Low : [-4.8 - inf -0.42 -inf]
"""
# define the bins
bins = np.zeros((state_space.shape[0], bin_size))
# initialize the bins
bins[0] = np.linspace(-4.8, 4.8, bin_size)
bins[1] = np.linspace(-4, 4, bin_size)
bins[2] = np.linspace(-0.42, 0.42, bin_size)
bins[3] = np.linspace(-4, 4, bin_size)
# define the q-table
q_table = np.zeros((bin_size, bin_size, bin_size, bin_size, action_space.n))
return q_table, bins
def discretize_state(state_space, bins):
"""
Description : This function discretizes the state space.
Args:
state_space : The state space of the environment.
bins : The bins for discretizing the state space.
Returns:
state_discrete : The discretized state space.
"""
state_discrete = np.zeros(state_space.shape)
for i in range(state_space.shape[0]):
state_discrete[i] = np.digitize(state_space[i], bins[i])
return state_discrete.astype(np.int32)