-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
147 lines (126 loc) · 6.5 KB
/
utils.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import numpy as np
import random
from collections import deque, namedtuple
import torch
class ReplayBuffer:
def __init__(self, buffer_size=int(1e5), batch_size=64):
# Initialize a ReplayBuffer object (for ddpg).
self.memory = deque(maxlen=buffer_size)
self.batch_size = batch_size
self.experience = namedtuple("Experience", field_names=["state", "action", "action_safe", "reward", "next_state", "done"])
def add(self, state, action, action_safe, reward, next_state, done):
# Add a new experience to memory.
e = self.experience(state, action, action_safe, reward, next_state, done)
self.memory.append(e)
def sample(self, batch_size=None):
# Randomly sample a batch of experiences from memory.
if batch_size is None:
batch_size = self.batch_size
experiences = random.sample(self.memory, k=batch_size)
states = torch.from_numpy(np.vstack([e.state for e in experiences])).float()
actions = torch.from_numpy(np.vstack([e.action for e in experiences])).float()
actions_safe = torch.from_numpy(np.vstack([e.action_safe for e in experiences])).float()
rewards = torch.from_numpy(np.vstack([e.reward for e in experiences])).float()
next_states = torch.from_numpy(np.vstack([e.next_state for e in experiences])).float()
dones = torch.from_numpy(np.vstack([e.done for e in experiences]).astype(np.uint8)).float()
return (states, actions, actions_safe, rewards, next_states, dones)
def __len__(self):
return len(self.memory)
class ReplayBuffer_Compensator:
def __init__(self, buffer_size=int(1e5), batch_size=64):
# Initialize a ReplayBuffer object (for Compensator).
self.memory = deque(maxlen=buffer_size)
self.batch_size = batch_size
self.experience = namedtuple("Experience", field_names=["state", "control_input", "h_derivative_true", "Lf"])
def add(self, state, control_input, h_derivative_true, Lf):
# Add a new experience to memory.
e = self.experience(state, control_input, h_derivative_true, Lf)
self.memory.append(e)
def sample(self, batch_size=None):
# Randomly sample a batch of experiences from memory.
if batch_size is None:
batch_size = self.batch_size
experiences = random.sample(self.memory, k=batch_size)
states = torch.from_numpy(np.vstack([e.state for e in experiences])).float()
control_input = torch.from_numpy(np.vstack([e.control_input for e in experiences])).float()
h_derivative_true = torch.from_numpy(np.vstack([e.h_derivative_true for e in experiences])).float()
Lf = torch.from_numpy(np.vstack([e.Lf for e in experiences])).float()
return (states, control_input, h_derivative_true, Lf)
def __len__(self):
return len(self.memory)
class ReplayBuffer_PPO:
def __init__(self, args):
# Initialize a ReplayBuffer_PPO object.
self.state = np.zeros((args.batch_size, args.state_dim))
self.action = np.zeros((args.batch_size, args.action_dim))
self.action_logprob = np.zeros((args.batch_size, args.action_dim))
self.reward = np.zeros((args.batch_size, 1))
self.state_ = np.zeros((args.batch_size, args.state_dim))
self.done = np.zeros((args.batch_size, 1))
self.acceleration = np.zeros((args.batch_size, args.vehicle_num))
self.count = 0
def store(self, state, action, action_logprob, reward, state_, done, acceleration):
# Store the transition in the replay buffer
self.state[self.count] = state
self.action[self.count] = action
self.action_logprob[self.count] = action_logprob
self.reward[self.count] = reward
self.state_[self.count] = state_
self.done[self.count] = done
self.acceleration[self.count] = acceleration
self.count += 1
def numpy_to_tensor(self):
# Convert numpy array to torch tensor and return
state = torch.tensor(self.state, dtype=torch.float)
action = torch.tensor(self.action, dtype=torch.float)
action_logprob = torch.tensor(self.action_logprob, dtype=torch.float)
reward = torch.tensor(self.reward, dtype=torch.float)
state_ = torch.tensor(self.state_, dtype=torch.float)
done = torch.tensor(self.done, dtype=torch.float)
acceleration = torch.tensor(self.acceleration, dtype=torch.float)
return state, action, action_logprob, reward, state_, done, acceleration
class ReplayBuffer_SIDE:
def __init__(self, buffer_size=int(1e5), batch_size=64):
# Initialize a ReplayBuffer object (for SIDE).
self.memory = deque(maxlen=buffer_size)
self.batch_size = batch_size
self.experience = namedtuple("Experience", field_names=["state", "action", "next_state"])
self.count = 0
def store(self, state, action, next_state):
# Add a new experience to memory.
e = self.experience(state, action, next_state)
self.memory.append(e)
self.count += 1
def sample(self, batch_size=None):
# Randomly sample a batch of experiences from memory.
if batch_size is None:
batch_size = self.batch_size
experiences = random.sample(self.memory, k=batch_size)
states = torch.from_numpy(np.vstack([e.state for e in experiences])).float()
actions = torch.from_numpy(np.vstack([e.action for e in experiences])).float()
next_states = torch.from_numpy(np.vstack([e.next_state for e in experiences])).float()
return (states, actions, next_states)
class OUNoise:
def __init__(self, action_size, mu=0.0, theta=0.15, sigma=0.2):
# Initialize parameters and noise process.
self.action_size = action_size
self.mu = mu
self.theta = theta
self.sigma = sigma
self.state = np.ones(self.action_size) * self.mu
self.reset()
def sample(self):
# Update internal state and return it as a noise sample.
x = self.state
dx = self.theta * (self.mu - x) + self.sigma * np.random.randn(len(x))
self.state = x + dx
return self.state
def reset(self):
# Reset the internal state (= noise) to mean (mu).
self.state = np.ones(self.action_size) * self.mu
def to_numpy(x):
# convert torch tensor to numpy array
return x.cpu().detach().double().numpy()
def to_tensor(x, dtype, device, requires_grad=False):
# convert numpy array to torch tensor
return torch.from_numpy(x).type(dtype).to(device).requires_grad_(requires_grad)