<s> import numpy as np <EOL> from rllab . envs . box2d . parser import find_body <EOL> from rllab . core . serializable import Serializable <EOL> from rllab . envs . box2d . box2d_env import Box2DEnv <EOL> from rllab . misc import autoargs <EOL> from rllab . misc . overrides import overrides <EOL> class CartpoleEnv ( Box2DEnv , Serializable ) : <EOL> @ autoargs . inherit ( Box2DEnv . __init__ ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . max_pole_angle = <NUM_LIT> <EOL> self . max_cart_pos = <NUM_LIT> <EOL> self . max_cart_speed = <NUM_LIT> <EOL> self . max_pole_speed = <NUM_LIT> <EOL> super ( CartpoleEnv , self ) . __init__ ( <EOL> self . model_path ( "<STR_LIT>" ) , <EOL> * args , ** kwargs <EOL> ) <EOL> self . cart = find_body ( self . world , "<STR_LIT>" ) <EOL> self . pole = find_body ( self . world , "<STR_LIT>" ) <EOL> Serializable . __init__ ( self , * args , ** kwargs ) <EOL> @ overrides <EOL> def reset ( self ) : <EOL> self . _set_state ( self . initial_state ) <EOL> self . _invalidate_state_caches ( ) <EOL> bounds = np . array ( [ <EOL> self . max_cart_pos , <EOL> self . max_cart_speed , <EOL> self . max_pole_angle , <EOL> self . max_pole_speed <EOL> ] ) <EOL> low , high = - <NUM_LIT> * bounds , <NUM_LIT> * bounds <EOL> xpos , xvel , apos , avel = np . random . uniform ( low , high ) <EOL> self . cart . position = ( xpos , self . cart . position [ <NUM_LIT:1> ] ) <EOL> self . cart . linearVelocity = ( xvel , self . cart . linearVelocity [ <NUM_LIT:1> ] ) <EOL> self . pole . angle = apos <EOL> self . pole . angularVelocity = avel <EOL> return self . get_current_obs ( ) <EOL> @ overrides <EOL> def compute_reward ( self , action ) : <EOL> yield <EOL> notdone = <NUM_LIT:1> - int ( self . is_current_done ( ) ) <EOL> ucost = <NUM_LIT> * ( action ** <NUM_LIT:2> ) . sum ( ) <EOL> xcost = <NUM_LIT:1> - np . cos ( self . pole . angle ) <EOL> yield notdone * <NUM_LIT:10> - notdone * xcost - notdone * ucost <EOL> @ overrides <EOL> def is_current_done ( self ) : <EOL> return abs ( self . cart . position [ <NUM_LIT:0> ] ) > self . max_cart_pos or abs ( self . pole . angle ) > self . max_pole_angle </s>
