dpendulum.py
Go to the documentation of this file.
1 import time
2 
3 import numpy as np
4 from numpy import pi
5 from pendulum import Pendulum
6 
7 p = Pendulum(1)
8 
9 NQ = 20 # Discretization steps for position
10 NV = 19 # Discretization steps for velocity
11 VMAX = 5 # Max velocity (v in [-vmax,vmax])
12 NU = 9 # Discretization steps for torque
13 UMAX = 5 # Max torque (u in [-umax,umax])
14 DT = 3e-1
15 
16 DQ = 2 * pi / NQ
17 DV = 2.0 * (VMAX) / NV
18 DU = 2.0 * (UMAX) / NU
19 
20 
21 # Continuous to discrete
22 def c2dq(q):
23  q = (q + pi) % (2 * pi)
24  return int(round(q / DQ)) % NQ
25 
26 
27 def c2dv(v):
28  v = np.clip(v, -VMAX + 1e-3, VMAX - 1e-3)
29  return int(np.floor((v + VMAX) / DV))
30 
31 
32 def c2du(u):
33  u = np.clip(u, -UMAX + 1e-3, UMAX - 1e-3)
34  return int(np.floor((u + UMAX) / DU))
35 
36 
37 def c2d(qv):
38  """From continuous to discrete."""
39  return c2dq(qv[0]), c2dv(qv[1])
40 
41 
42 # Discrete to continuous
43 def d2cq(iq):
44  iq = np.clip(iq, 0, NQ - 1)
45  return iq * DQ - pi
46 
47 
48 def d2cv(iv):
49  iv = np.clip(iv, 0, NV - 1) - (NV - 1) / 2
50  return iv * DV
51 
52 
53 def d2cu(iu):
54  iu = np.clip(iu, 0, NU - 1) - (NU - 1) / 2
55  return iu * DU
56 
57 
58 def d2c(iqv):
59  """From discrete to continuous"""
60  return d2cq(iqv[0]), d2cv(iqv[1])
61 
62 
63 def x2i(x):
64  return x[0] + x[1] * NQ
65 
66 
67 def i2x(i):
68  return [i % NQ, i / NQ]
69 
70 
71 # --- PENDULUM
72 
73 
74 class DPendulum:
75  def __init__(self):
76  self.pendulum = Pendulum(1)
77  self.pendulum.DT = DT
78  self.pendulum.NDT = 5
79 
80  @property
81  def nqv(self):
82  return [NQ, NV]
83 
84  @property
85  def nx(self):
86  return NQ * NV
87 
88  @property
89  def nu(self):
90  return NU
91 
92  @property
93  def goal(self):
94  return x2i(c2d([0.0, 0.0]))
95 
96  def reset(self, x=None):
97  if x is None:
98  x = [np.random.randint(0, NQ), np.random.randint(0, NV)]
99  else:
100  x = i2x(x)
101  assert len(x) == 2
102  self.x = x
103  return x2i(self.x)
104 
105  def step(self, iu):
106  self.x = self.dynamics(self.x, iu)
107  reward = 1 if x2i(self.x) == self.goal else 0
108  return x2i(self.x), reward
109 
110  def render(self):
111  q = d2cq(self.x[0])
112  self.pendulum.display(
113  np.array(
114  [
115  q,
116  ]
117  )
118  )
119  time.sleep(self.pendulum.DT)
120 
121  def dynamics(self, ix, iu):
122  x = np.array(d2c(ix))
123  u = d2cu(iu)
124 
125  self.xc, _ = self.pendulum.dynamics(x, u)
126  return c2d(x.T.tolist()[0])
127 
128 
129 """
130 env = DPendulum()
131 
132 print env.reset(x2i([14,11]))
133 hq = []
134 hv = []
135 hqc = []
136 hvc = []
137 u = 0
138 for i in range(100):
139  ix,r=env.step(u)
140  q,v = i2x(ix)
141  env.render()
142  if d2cv(v)==0.0: u = MAXU-1 if u==0 else 0
143  hq.append( d2cq(env.x[0]) )
144  hv.append( d2cv(env.x[1]) )
145  hqc.append( env.xc[0,0] )
146  hvc.append( env.xc[1,0] )
147 
148 """
149 
150 """
151 
152 
153 EPS = 1e-3
154 q = 0.0
155 v = -VMAX
156 hq = []
157 hv = []
158 hiq = []
159 hiv = []
160 hqa = []
161 hva = []
162 
163 while q<2*pi:
164  hq.append(q)
165  iq = c2dq(q)
166  hiq.append(iq)
167  hqa.append(d2cq(iq))
168  q += EPS
169 while v<VMAX:
170  iv = c2dv(v)
171  hv.append(v)
172  hiv.append(iv)
173  hva.append(d2cv(iv))
174  v += EPS
175 
176 
177 
178 
179 """
dpendulum.DPendulum.__init__
def __init__(self)
Definition: dpendulum.py:75
dpendulum.DPendulum.nqv
def nqv(self)
Definition: dpendulum.py:81
dpendulum.DPendulum
Definition: dpendulum.py:74
dpendulum.DPendulum.pendulum
pendulum
Definition: dpendulum.py:76
dpendulum.d2c
def d2c(iqv)
Definition: dpendulum.py:58
dpendulum.x2i
def x2i(x)
Definition: dpendulum.py:63
dpendulum.DPendulum.x
x
Definition: dpendulum.py:102
dpendulum.c2dv
def c2dv(v)
Definition: dpendulum.py:27
dpendulum.DPendulum.step
def step(self, iu)
Definition: dpendulum.py:105
dpendulum.d2cu
def d2cu(iu)
Definition: dpendulum.py:53
dpendulum.i2x
def i2x(i)
Definition: dpendulum.py:67
dpendulum.DPendulum.reset
def reset(self, x=None)
Definition: dpendulum.py:96
dpendulum.c2d
def c2d(qv)
Definition: dpendulum.py:37
dpendulum.DPendulum.nx
def nx(self)
Definition: dpendulum.py:85
dpendulum.DPendulum.nu
def nu(self)
Definition: dpendulum.py:89
pendulum.Pendulum
Definition: pendulum.py:43
dpendulum.c2du
def c2du(u)
Definition: dpendulum.py:32
dpendulum.d2cq
def d2cq(iq)
Definition: dpendulum.py:43
display
Definition: display.py:1
dpendulum.DPendulum.render
def render(self)
Definition: dpendulum.py:110
dpendulum.d2cv
def d2cv(iv)
Definition: dpendulum.py:48
dpendulum.DPendulum.dynamics
def dynamics(self, ix, iu)
Definition: dpendulum.py:121
dpendulum.c2dq
def c2dq(q)
Definition: dpendulum.py:22
dpendulum.DPendulum.goal
def goal(self)
Definition: dpendulum.py:93


pinocchio
Author(s):
autogenerated on Mon Dec 16 2024 03:41:01