aboutsummaryrefslogtreecommitdiffstats
path: root/System/Tests/system_swingup_test.py
blob: e13c7caf7c026f6741c46625f4a75b1293ad118a (plain) (blame)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import numpy as np
import numpy.random as rnd
import torch as pt

import math
from gym import spaces, logger
from gym.utils import seeding

from system import System
import time
from sys import exit

class SwingUpEnv():
    """
    Description:
        A pole is attached by an un-actuated joint to a cart, which moves along a frictionless track. The pendulum starts upright, and the goal is to prevent it from falling over by increasing and reducing the cart's velocity.

    Source:
        This environment corresponds to the version of the cart-pole problem described by Barto, Sutton, and Anderson

    Observation: 
        Type: Box(4)
        Num	Observation                 Min         Max
        0	Cart Position             -4.8            4.8
        1	Cart Velocity             -Inf            Inf
        2	Pole Angle                 -Inf           Inf
        3	Pole Velocity At Tip      -Inf            Inf
        
    Actions:
        Type: Box(1)
        Num	Action                      Min         Max
        0	Push cart                   -1          1
        
        Note: The amount the velocity that is reduced or increased is not fixed; it depends on the angle the pole is pointing. This is because the center of gravity of the pole increases the amount of energy needed to move the cart underneath it

    Reward:
        Reward is 1 for every step taken, including the termination step

    Starting State:
        All observations are assigned a uniform random value in [-0.05..0.05]

    Episode Termination:
        Pole Angle is more than 12 degrees
        Cart Position is more than 2.4 (center of the cart reaches the edge of the display)
        Episode length is greater than 200
        Solved Requirements
        Considered solved when the average reward is greater than or equal to 195.0 over 100 consecutive trials.
    """
    
    metadata = {
        'render.modes': ['human', 'rgb_array'],
        'video.frames_per_second' : 50
    }

    def __init__(self):
        self.sys = System(angular_units='Radians')
		
        self.force_mag = 10.
        self.last_time = time.time()  # time for seconds between updates

        # Angle at which to fail the episode
        self.x_threshold = 10.
        self.x_dot_threshold = 10.
        self.theta_dot_threshold = 3*np.pi

        # Angle limit set to 2 * theta_threshold_radians so failing observation is still within bounds
        high = np.array([self.x_threshold*2, self.x_dot_threshold, np.finfo(np.float32).max, np.finfo(np.float32).max])

        
        self.action_space = spaces.Box(-np.ones(1), np.ones(1), dtype = np.float32)

        self.seed()
        self.state = None

        self.steps_beyond_done = None

    def seed(self, seed=None):
        self.np_random, seed = seeding.np_random(seed)
        return [seed]

    def step(self, action):
        assert self.action_space.contains(action), "%r (%s) invalid"%(action, type(action))
        state = self.state
        x, x_dot, theta, theta_dot = state
        force = self.force_mag * action[0]
        self.sys.adjust(force)

        costheta = math.cos(theta)
        sintheta = math.sin(theta)

        if costheta > 0:
            self.up_time += 1
            self.max_up_time = np.max([self.up_time, self.max_up_time])

        else:
            self.up_time = 0

        current_time = time.time()
        tau = current_time - self.last_time
        self.last_time = current_time
        
        new_theta, new_x = self.sys.measure()
        theta_dot = (new_theta - theta) / tau
        x_dot = (new_x - x) / tau
        self.state = (new_x, x_dot, new_theta, theta_dot)
        self.sys.add_results(new_theta, new_x, force)

        done =  x < -self.x_threshold \
                or x > self.x_threshold \
                or theta_dot < -self.theta_dot_threshold \
                or theta_dot > self.theta_dot_threshold
        done = bool(done)

        if not done:
            reward = np.ceil(costheta)
        elif self.steps_beyond_done is None:
            # Pole just fell!
            self.steps_beyond_done = 0
            reward = -( 100 * (np.abs(x_dot) + np.abs(theta_dot)) )
        else:
            if self.steps_beyond_done == 0:
                logger.warn("You are calling 'step()' even though this environment has already returned done = True. You should always call 'reset()' once you receive 'done = True' -- any further steps are undefined behavior.")
            self.steps_beyond_done += 1
            reward = 0.0

        return np.array(self.state), reward, done, {'max_up_time' : self.max_up_time}

    def reset(self, home = True):
        if home == True:
            self.sys.return_home()
        time.sleep(1)
        init_ang, lin = self.sys.measure()
        time.sleep(0.05)
        ang, lin = self.sys.measure()
        self.state = (0, 0, ang, (ang-init_ang)/0.05)

        self.up_time = 0
        self.max_up_time = 0
        self.up = False
        self.steps_beyond_done = None
        return np.array(self.state)
        
    def end(self):
        self.sys.deinitialize()


class nnQ(pt.nn.Module):
    """
    Here is a basic neural network with for representing a policy 
    """
    
    def __init__(self, stateDim, numActions, numHiddenUnits, numLayers):
        super().__init__()
        
        InputLayer = [pt.nn.Linear(stateDim + numActions, numHiddenUnits),
                      pt.nn.ReLU()]
        
        HiddenLayers = []
        for _ in range(numLayers - 1):
            HiddenLayers.append(pt.nn.Linear(numHiddenUnits, numHiddenUnits))
            HiddenLayers.append(pt.nn.ReLU())
            
        
        OutputLayer = [pt.nn.Linear(numHiddenUnits, 1)]
        
        AllLayers = InputLayer + HiddenLayers + OutputLayer
        self.net = pt.nn.Sequential(*AllLayers)
        
        self.numActions = numActions
        
    def forward(self,x,a):
        x = pt.tensor(x, dtype = pt.float32)

        b = pt.nn.functional.one_hot(pt.tensor(a).long(), self.numActions)
        
        c = b.float().detach()
        y = pt.cat([x, c])
        
        return self.net(y)
        
    
class sarsaAgent:
    def __init__(self, stateDim, numActions, numHiddenUnits, numLayers,
                 epsilon = .1, gamma = .9, alpha = .1):
        self.Q = nnQ(stateDim, numActions, numHiddenUnits, numLayers)
        self.gamma = gamma
        self.epsilon = epsilon
        self.alpha = alpha
        self.numActions = numActions
        self.s_last = None

    def action(self, x):
        # This is an epsilon greedy selection
        a = 0
        if rnd.rand() < self.epsilon:
            a = rnd.randint(0, numActions)
        else:
            qBest = -np.inf
            for aTest in range(self.numActions):
                qTest = self.Q(x, aTest).detach().numpy()[0]
                if qTest > qBest:
                    qBest = qTest
                    a = aTest
        return a
    
    def update(self, s, a, r, s_next,done):
        # Compute the TD error, if there is enough data
        update = True
        if done:
            Q_cur = self.Q(s, a).detach().numpy()[0]
            delta = r - Q_cur
            self.s_last = None
            Q_diff = self.Q(s, a)
        elif self.s_last is not None:
            Q_next = self.Q(s, a).detach().numpy()[0]
            Q_cur = self.Q(self.s_last, self.a_last).detach().numpy()[0]
            delta = self.r_last + self.gamma * Q_next - Q_cur
            Q_diff = self.Q(self.s_last, self.a_last)
        else:
            update = False
            
        # Update the parameter via the semi-gradient method
        if update:
            self.Q.zero_grad()
            Q_diff.backward()
            for p in self.Q.parameters():
                p.data.add_(self.alpha * delta, p.grad.data)

        if not done:
            self.s_last = np.copy(s)
            self.a_last = np.copy(a)
            self.r_last = np.copy(r)

# This is the environment
env = SwingUpEnv()

# For simplicity, we only consider forces of -1 and 1
numActions = 5
Actions = np.linspace(-1, 1, numActions)

# This is our learning agent
gamma = .95
agent = sarsaAgent(5, numActions, 20, 1, epsilon = 5e-2, gamma = gamma, alpha = 1e-5)

maxSteps = 5e4

# This is a helper to deal with the fact that x[2] is actually an angle
x_to_y = lambda x : np.array([x[0], x[1], np.cos(x[2]), np.sin(x[2]), x[3]])

R = []
UpTime = []

step = 0
ep = 0
try:
    while step < maxSteps:
        ep += 1
        x = env.reset(home = ep > 1)
        C = 0.
        
        done = False
        t = 1
        while not done:
            t += 1
            step += 1
            y = x_to_y(x)
            a = agent.action(y)
            u = Actions[a:a+1]
            x_next, c, done, info = env.step(u)
            
            max_up_time = info['max_up_time']
            y_next = x_to_y(x_next)

            C += (1./t) * (c - C)
            agent.update(y, a, c, y_next, done)
            x = x_next
            if done:
                break
                
            if step >= maxSteps:
                break
                
            
            R.append(C)
        UpTime.append(max_up_time)
        #print('t:',ep+1,', R:',C,', L:',t-1,', G:',G,', Q:', Q_est, 'U:', max_up_time)
        print('Episode:',ep, 'Total Steps:',step, ', Ave. Reward:',C, ', Episode Length:',t-1, 'Max Up-Time:',max_up_time)
except:
    env.end()
    exit(-1)
finally:
    env.end()
    exit(0)