-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlspi_cartpole.py
37 lines (27 loc) · 1.04 KB
/
lspi_cartpole.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
import gymnasium as gym
from lspi_modules import LSPI, BlockBasis, RadialBasisFunction, PolynomialBasisFunction
env = gym.make("CartPole-v1")
env.action_space.seed(42)
observation, info = env.reset(seed=42)
basis_function = BlockBasis(2, 4)
# basis_function = RadialBasisFunction(num_centers=4, state_dim=4)
# basis_function = PolynomialBasisFunction(2)
lspi = LSPI(basis_function, discount=0.9)
num_episodes = 10000
for i_episode in range(num_episodes):
terminated = False
observation, info = env.reset()
reward_sum = 0
while not terminated:
if i_episode < 1000:
action = env.action_space.sample()
else:
action = lspi.policy(observation)
next_observation, reward, terminated, truncated, info = env.step(action)
# Update the policy
lspi.update(observation, action, reward, next_observation)
observation = next_observation
reward_sum += reward
if i_episode % 100 == 0:
print("Episode: {}, reward: {}".format(i_episode, reward_sum))
env.close()