<s> """<STR_LIT>""" <EOL> from gym import core , spaces <EOL> import numpy as np <EOL> import time <EOL> __copyright__ = "<STR_LIT>" <EOL> __credits__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> __license__ = "<STR_LIT>" <EOL> __author__ = "<STR_LIT>" <EOL> class AcrobotEnv ( core . Env ) : <EOL> """<STR_LIT>""" <EOL> metadata = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : <NUM_LIT:15> <EOL> } <EOL> dt = <NUM_LIT> <EOL> LINK_LENGTH_1 = <NUM_LIT:1.> <EOL> LINK_LENGTH_2 = <NUM_LIT:1.> <EOL> LINK_MASS_1 = <NUM_LIT:1.> <EOL> LINK_MASS_2 = <NUM_LIT:1.> <EOL> LINK_COM_POS_1 = <NUM_LIT:0.5> <EOL> LINK_COM_POS_2 = <NUM_LIT:0.5> <EOL> LINK_MOI = <NUM_LIT:1.> <EOL> MAX_VEL_1 = <NUM_LIT:4> * np . pi <EOL> MAX_VEL_2 = <NUM_LIT:9> * np . pi <EOL> AVAIL_TORQUE = [ - <NUM_LIT:1.> , <NUM_LIT:0.> , + <NUM_LIT:1> ] <EOL> torque_noise_max = <NUM_LIT:0.> <EOL> book_or_nips = "<STR_LIT>" <EOL> action_arrow = None <EOL> domain_fig = None <EOL> actions_num = <NUM_LIT:3> <EOL> def __init__ ( self ) : <EOL> high = np . array ( [ np . pi , np . pi , self . MAX_VEL_1 , self . MAX_VEL_2 ] ) <EOL> low = - high <EOL> self . observation_space = spaces . Box ( low , high ) <EOL> self . action_space = spaces . Discrete ( <NUM_LIT:3> ) <EOL> self . viewer = None <EOL> def _reset ( self ) : <EOL> self . state = np . random . uniform ( low = - <NUM_LIT:0.1> , high = <NUM_LIT:0.1> , size = ( <NUM_LIT:4> , ) ) <EOL> return self . state <EOL> def _step ( self , a ) : <EOL> s = self . state <EOL> torque = self . AVAIL_TORQUE [ a ] <EOL> if self . torque_noise_max > <NUM_LIT:0> : <EOL> torque += np . random . uniform ( - self . torque_noise_max , self . torque_noise_max ) <EOL> s_augmented = np . append ( s , torque ) <EOL> ns = rk4 ( self . _dsdt , s_augmented , [ <NUM_LIT:0> , self . dt ] ) <EOL> ns = ns [ - <NUM_LIT:1> ] <EOL> ns = ns [ : <NUM_LIT:4> ] <EOL> ns [ <NUM_LIT:0> ] = wrap ( ns [ <NUM_LIT:0> ] , - np . pi , np . pi ) <EOL> ns [ <NUM_LIT:1> ] = wrap ( ns [ <NUM_LIT:1> ] , - np . pi , np . pi ) <EOL> ns [ <NUM_LIT:2> ] = bound ( ns [ <NUM_LIT:2> ] , - self . MAX_VEL_1 , self . MAX_VEL_1 ) <EOL> ns [ <NUM_LIT:3> ] = bound ( ns [ <NUM_LIT:3> ] , - self . MAX_VEL_2 , self . MAX_VEL_2 ) <EOL> self . state = ns . copy ( ) <EOL> terminal = self . _terminal ( ) <EOL> reward = - <NUM_LIT:1.> if not terminal else <NUM_LIT:0.> <EOL> return ( np . array ( self . state ) , reward , terminal , { } ) <EOL> def _terminal ( self ) : <EOL> s = self . state <EOL> return bool ( - np . cos ( s [ <NUM_LIT:0> ] ) - np . cos ( s [ <NUM_LIT:1> ] + s [ <NUM_LIT:0> ] ) > <NUM_LIT:1.> ) <EOL> def _dsdt ( self , s_augmented , t ) : <EOL> m1 = self . LINK_MASS_1 <EOL> m2 = self . LINK_MASS_2 <EOL> l1 = self . LINK_LENGTH_1 <EOL> lc1 = self . LINK_COM_POS_1 <EOL> lc2 = self . LINK_COM_POS_2 <EOL> I1 = self . LINK_MOI <EOL> I2 = self . LINK_MOI <EOL> g = <NUM_LIT> <EOL> a = s_augmented [ - <NUM_LIT:1> ] <EOL> s = s_augmented [ : - <NUM_LIT:1> ] <EOL> theta1 = s [ <NUM_LIT:0> ] <EOL> theta2 = s [ <NUM_LIT:1> ] <EOL> dtheta1 = s [ <NUM_LIT:2> ] <EOL> dtheta2 = s [ <NUM_LIT:3> ] <EOL> d1 = m1 * lc1 ** <NUM_LIT:2> + m2 * ( l1 ** <NUM_LIT:2> + lc2 ** <NUM_LIT:2> + <NUM_LIT:2> * l1 * lc2 * np . cos ( theta2 ) ) + I1 + I2 <EOL> d2 = m2 * ( lc2 ** <NUM_LIT:2> + l1 * lc2 * np . cos ( theta2 ) ) + I2 <EOL> phi2 = m2 * lc2 * g * np . cos ( theta1 + theta2 - np . pi / <NUM_LIT> ) <EOL> phi1 = - m2 * l1 * lc2 * dtheta2 ** <NUM_LIT:2> * np . sin ( theta2 ) - <NUM_LIT:2> * m2 * l1 * lc2 * dtheta2 * dtheta1 * np . sin ( theta2 ) + ( m1 * lc1 + m2 * l1 ) * g * np . cos ( theta1 - np . pi / <NUM_LIT:2> ) + phi2 <EOL> if self . book_or_nips == "<STR_LIT>" : <EOL> ddtheta2 = ( a + d2 / d1 * phi1 - phi2 ) / ( m2 * lc2 ** <NUM_LIT:2> + I2 - d2 ** <NUM_LIT:2> / d1 ) <EOL> else : <EOL> ddtheta2 = ( a + d2 / d1 * phi1 - m2 * l1 * lc2 * dtheta1 ** <NUM_LIT:2> * np . sin ( theta2 ) - phi2 ) / ( m2 * lc2 ** <NUM_LIT:2> + I2 - d2 ** <NUM_LIT:2> / d1 ) <EOL> ddtheta1 = - ( d2 * ddtheta2 + phi1 ) / d1 <EOL> return ( dtheta1 , dtheta2 , ddtheta1 , ddtheta2 , <NUM_LIT:0.> ) <EOL> def _render ( self , mode = '<STR_LIT>' , close = False ) : <EOL> from gym . envs . classic_control import rendering <EOL> if close : <EOL> if self . viewer is not None : <EOL> self . viewer . close ( ) <EOL> return <EOL> s = self . state <EOL> if self . viewer is None : <EOL> self . viewer = rendering . Viewer ( <NUM_LIT> , <NUM_LIT> ) <EOL> self . viewer . set_bounds ( - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> ) <EOL> p1 = [ - self . LINK_LENGTH_1 * <EOL> np . cos ( s [ <NUM_LIT:0> ] ) , self . LINK_LENGTH_1 * np . sin ( s [ <NUM_LIT:0> ] ) ] <EOL> p2 = [ p1 [ <NUM_LIT:0> ] - self . LINK_LENGTH_2 * np . cos ( s [ <NUM_LIT:0> ] + s [ <NUM_LIT:1> ] ) , <EOL> p1 [ <NUM_LIT:1> ] + self . LINK_LENGTH_2 * np . sin ( s [ <NUM_LIT:0> ] + s [ <NUM_LIT:1> ] ) ] <EOL> xys = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:0> ] , p1 , p2 ] ) [ : , : : - <NUM_LIT:1> ] <EOL> thetas = [ s [ <NUM_LIT:0> ] - np . pi / <NUM_LIT:2> , s [ <NUM_LIT:0> ] + s [ <NUM_LIT:1> ] - np . pi / <NUM_LIT:2> ] <EOL> self . viewer . draw_line ( ( - <NUM_LIT> , <NUM_LIT:1> ) , ( <NUM_LIT> , <NUM_LIT:1> ) ) <EOL> for ( ( x , y ) , th ) in zip ( xys , thetas ) : <EOL> l , r , t , b = <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT> , - <NUM_LIT> <EOL> jtransform = rendering . Transform ( rotation = th , translation = ( x , y ) ) <EOL> link = self . viewer . draw_polygon ( [ ( l , b ) , ( l , t ) , ( r , t ) , ( r , b ) ] ) <EOL> link . add_attr ( jtransform ) <EOL> link . set_color ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> ) <EOL> circ = self . viewer . draw_circle ( <NUM_LIT> ) <EOL> circ . set_color ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> ) <EOL> circ . add_attr ( jtransform ) <EOL> self . viewer . render ( ) <EOL> if mode == '<STR_LIT>' : <EOL> return self . viewer . get_array ( ) <EOL> elif mode is '<STR_LIT>' : <EOL> pass <EOL> def wrap ( x , m , M ) : <EOL> """<STR_LIT>""" <EOL> diff = M - m <EOL> while x > M : <EOL> x = x - diff <EOL> while x < m : <EOL> x = x + diff <EOL> return x <EOL> def bound ( x , m , M = None ) : <EOL> """<STR_LIT>""" <EOL> if M is None : <EOL> M = m [ <NUM_LIT:1> ] <EOL> m = m [ <NUM_LIT:0> ] <EOL> return min ( max ( x , m ) , M ) <EOL> def rk4 ( derivs , y0 , t , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> Ny = len ( y0 ) <EOL> except TypeError : <EOL> yout = np . zeros ( ( len ( t ) , ) , np . float_ ) <EOL> else : <EOL> yout = np . zeros ( ( len ( t ) , Ny ) , np . float_ ) <EOL> yout [ <NUM_LIT:0> ] = y0 <EOL> i = <NUM_LIT:0> <EOL> for i in np . arange ( len ( t ) - <NUM_LIT:1> ) : <EOL> thist = t [ i ] <EOL> dt = t [ i + <NUM_LIT:1> ] - thist <EOL> dt2 = dt / <NUM_LIT> <EOL> y0 = yout [ i ] <EOL> k1 = np . asarray ( derivs ( y0 , thist , * args , ** kwargs ) ) <EOL> k2 = np . asarray ( derivs ( y0 + dt2 * k1 , thist + dt2 , * args , ** kwargs ) ) <EOL> k3 = np . asarray ( derivs ( y0 + dt2 * k2 , thist + dt2 , * args , ** kwargs ) ) <EOL> k4 = np . asarray ( derivs ( y0 + dt * k3 , thist + dt , * args , ** kwargs ) ) <EOL> yout [ i + <NUM_LIT:1> ] = y0 + dt / <NUM_LIT> * ( k1 + <NUM_LIT:2> * k2 + <NUM_LIT:2> * k3 + k4 ) <EOL> return yout </s>
