-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradial_rocket.py
144 lines (116 loc) · 4.06 KB
/
radial_rocket.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
from typing import (
Any,
Generic,
Iterable,
List,
Mapping,
Optional,
Sequence,
Tuple,
Type,
TypeVar,
Union,
Dict
)
import numpy as np
import matplotlib.pyplot as plt
import gym
from gym.spaces import Box
from gym.utils import seeding
class RadialBalance(gym.Env):
def __init__(self) -> None:
super().__init__()
self.G = 1
self.M = 1
self.rmin, self.rmax = 0.5, 1.5
self.rtarget = 1
self.ltarget = np.sqrt(self.G * self.M / self.rtarget)
self.simulation_steps = 3
self.max_iters = 200
self.dt = 0.05
self.max_thrust = 0.1
self.end_rmin, self.end_rmax = 0.98, 1.02
self.end_vmin, self.end_vmax = -.03, .03
self.end_steps = 20
self.end_energy_tolerance = 1e-4
self.end_energy = - 1 / self.rtarget + \
self.ltarget ** 2 / (2 * self.rtarget ** 2) + \
self.end_energy_tolerance
print(f'End Energy: {self.end_energy:.3f}')
self.action_space = Box(low=-1, high=1, shape=(1,))
self.observation_space = Box(low=np.array(
[self.rmin, -10]), high=np.array([self.rmax, 10]), shape=(2,))
def reset(self) -> Any:
r_init = np.random.uniform(0.5, 1.5)
rdt_init = np.random.uniform(-.5, .5)
self.state = np.array([r_init, rdt_init])
self.done = False
self.iters = 0
self.record = []
self.actions = []
self.last_action = [0]
self.end_counter = 0
return self.state
def _reward(self, state, action):
return - (state[0] - self.rtarget) ** 2 - 0.1 * state[1] ** 2 # - 0.01 * action[0] ** 2
def _total_energy(self, state):
r, v = state
pe = - 1 / r + self.ltarget ** 2 / (2 * r ** 2)
ke = v**2 / 2
return pe + ke
def step(self, action: np.ndarray) -> Tuple[np.ndarray, float, bool, bool, Dict[str, Any]]:
r, rdt = self.state
thrust = action[0] * self.max_thrust
for _ in range(self.simulation_steps):
acc = - self.G * self.M / r ** 2 + self.ltarget ** 2 / r ** 3 + thrust
rdt = rdt + acc * self.dt
r = r + rdt * self.dt
if r < self.rmin:
r = self.rmin
if rdt < 0:
rdt = 0
elif r > self.rmax:
r = self.rmax
if rdt > 0:
rdt = 0
self.state = np.array([r, rdt])
self.last_action = action
reward = self._reward(self.state, action)
self.iters += 1
if self.end_rmin < r < self.end_rmax and self.end_vmin < rdt < self.end_vmax:
# if self._total_energy(self.state) < self.end_energy:
self.end_counter += 1
if self.end_counter >= self.end_steps:
self.done = True
else:
self.end_counter = 0
if self.iters > self.max_iters:
self.done = True
return self.state, reward, self.done, False, dict()
def render(self):
self.record.append(self.state)
self.actions.append(self.last_action[0])
def show(self, summary, path):
record = np.array(self.record)
time = np.arange(0, len(record), dtype=np.float64)
time *= self.dt * self.simulation_steps
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
ax1.plot(time, record[:, 0], label='r')
ax1.plot(time, record[:, 1], label='$\dot{r}$')
ax1.axhline(y=self.end_rmax)
ax1.axhline(y=self.end_rmin)
ax1.axhline(y=self.end_vmax)
ax1.axhline(y=self.end_vmin)
ax2.plot(time, self.actions)
ax1.grid(True)
ax2.grid(True)
plt.legend()
plt.show()
class DiscretiseAction(gym.ActionWrapper):
def __init__(self, env: gym.Env):
super().__init__(env)
self.action_space = gym.spaces.Discrete(3)
# self.thrust_levels = [-1, -0.1, -.01, 0, 0.01, 0.1, 1]
def action(self, action):
return np.array([action - 1])
# return np.array([self.thrust_levels[action]])