1 from pendulum
import Pendulum
22 return int(round(q/DQ)) % NQ
25 v = np.clip(v,-VMAX+1e-3,VMAX-1e-3)
26 return int(np.floor((v+VMAX)/DV))
29 u = np.clip(u,-UMAX+1e-3,UMAX-1e-3)
30 return int(np.floor((u+UMAX)/DU))
33 '''From continuous to discrete.''' 38 iq = np.clip(iq,0,NQ-1)
42 iv = np.clip(iv,0,NV-1) - (NV-1)/2
46 iu = np.clip(iu,0,NU-1) - (NU-1)/2
50 '''From discrete to continuous''' 53 def x2i(x):
return x[0]+x[1]*NQ
54 def i2x(i):
return [ i%NQ, i/NQ ]
65 def nqv(self):
return [NQ,NV]
67 def nx(self):
return NQ*NV
69 def nu(self):
return NU
75 x = [ np.random.randint(0,NQ), np.random.randint(0,NV) ]
83 reward = 1
if x2i(self.
x)==self.
goal else 0
84 return x2i(self.
x),reward
88 self.pendulum.display(np.matrix([q,]))
89 time.sleep(self.pendulum.DT)
92 x = np.matrix(d2c (ix)).T
95 self.xc,_ = self.pendulum.dynamics(x,u)
96 return c2d(x.T.tolist()[0])
102 print env.reset(x2i([14,11])) 112 if d2cv(v)==0.0: u = MAXU-1 if u==0 else 0 113 hq.append( d2cq(env.x[0]) ) 114 hv.append( d2cv(env.x[1]) ) 115 hqc.append( env.xc[0,0] ) 116 hvc.append( env.xc[1,0] ) def dynamics(self, ix, iu)