-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart_pendulum_measurement.py
90 lines (72 loc) · 3.73 KB
/
cart_pendulum_measurement.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
import torch
class CartPole_measurement():
def __init__(self):
"""
Initialize the CartPole_measurement class.
This class simulates a measurement system for a cart-pole system. It includes methods for simulating
accelerometer, statemeter, and adding white noise to the measurements.
Attributes:
device (torch.device): The device on which the computations will be performed. Default is 'cpu'.
mean (float): The mean value of the white noise. Default is 0.
std (float): The standard deviation of the white noise. Default is 0.01.
data_size (int): The size of the data. Default is 0.
bias (torch.Tensor): The bias added to the measurements. Initialized as a tensor of size data_size.
"""
self.device = torch.device('cpu')
self.mean = 0
self.std = 0.01
self.data_size = 0
self.bias = torch.tensor(self.data_size)
def accelerometer(self, acceleration, velocity, position, dt):
"""
This function takes the current acceleration, velocity, position, and time step (dt) as input,
simulates an accelerometer measurement by adding white noise to the acceleration, updating the velocity and position,#+
and adding a bias to the position. The white noise is generated using the `white_noise` method.
Parameters:
----------
acceleration : torch.Tensor
The current acceleration of the cart-pole system. It should be a 1D tensor of size (1,).#+
velocity : torch.Tensor
The current velocity of the cart-pole system. It should be a 1D tensor of size (1,).#+
position : torch.Tensor
The current position of the cart-pole system. It should be a 1D tensor of size (1,).#+
dt : float
The time step for the simulation. It should be a positive scalar value.#+
"""
acceleration = acceleration + self.white_noise(acceleration.size()).to(self.device)
velocity = velocity + acceleration * dt
position = position + velocity * dt + self.bias
return torch.row_stack([position, velocity, acceleration])
def statemeter(self, state):
"""
Simulate a statemeter measurement for the cart-pole system.
This function takes the true state of the cart-pole system as input, adds white noise to the state,
and returns the simulated measurement. The white noise is generated using the `white_noise` method.
Parameters:
----------
state : torch.Tensor
The true state of the cart-pole system. It should be a 1D tensor of size (n,) where n is the number of state variables.
Returns:
-------
measured_state : torch.Tensor
The simulated measurement of the cart-pole system state. It is a 1D tensor of size (n,) with white noise added.
"""
measured_state = (state + self.white_noise(state.size())).to(self.device)
return measured_state
def white_noise(self, size):
"""
Generate a tensor of white noise with the given size.
This function uses PyTorch's randn function to generate a tensor of random numbers
from a standard normal distribution. The generated noise is then scaled by the
model's standard deviation and shifted by the model's mean.
Parameters:
----------
size : tuple or torch.Size
The size of the output tensor.
Returns:
-------
noisy_data : torch.Tensor
A tensor of white noise with the given size.
"""
noisy_data = (self.mean + torch.randn(size)*self.std**2 ).to(self.device)
return noisy_data