-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
45 lines (33 loc) · 1.46 KB
/
Copy pathtrain.py
File metadata and controls
45 lines (33 loc) · 1.46 KB
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
import numpy as np
import gymnasium as gym
from model import ActorCriticAgent
def train(env, agent, episodes, N):
episode_rewards = []
# Pre-calculate scaling factors for state variables
state_high = env.observation_space.high
state_low = env.observation_space.low
# Handle infinite values if they exist
state_high[state_high == np.inf] = 1.0
state_low[state_low == -np.inf] = -1.0
state_range = state_high - state_low
for episode in range(episodes):
state, _ = env.reset()
state = (state - state_low) / state_range # Scale the state
total_reward = 0
done = False
transitions = []
while not done:
action = agent.choose_action(state)
next_state, reward, terminated, truncated, _ = env.step(action)
done = terminated or truncated
next_state = (next_state - state_low) / state_range # Scale the next state
transitions.append((state, action, reward, next_state, done))
if len(transitions) == N or done:
agent.update(transitions)
transitions = []
state = next_state
total_reward += reward
episode_rewards.append(total_reward)
if (episode + 1) % 100 == 0:
print(f"Episode {episode + 1}/{episodes}, Total Reward: {total_reward}")
return episode_rewards