<s> """<STR_LIT>""" <EOL> from . CartPoleBase import CartPoleBase , StateIndex <EOL> import numpy as np <EOL> from rlpy . Tools import plt <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 InfTrackCartPole ( CartPoleBase ) : <EOL> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> ANGLE_LIMITS = [ - np . pi / <NUM_LIT> , np . pi / <NUM_LIT> ] <EOL> ANGULAR_RATE_LIMITS = [ - <NUM_LIT> , <NUM_LIT> ] <EOL> POSITION_LIMITS = [ - np . inf , np . inf ] <EOL> VELOCITY_LIMITS = [ - np . inf , np . inf ] <EOL> AVAIL_FORCE = np . array ( [ - <NUM_LIT:50> , <NUM_LIT:0> , <NUM_LIT:50> ] ) <EOL> MASS_PEND = <NUM_LIT> <EOL> MASS_CART = <NUM_LIT> <EOL> LENGTH = <NUM_LIT:1.0> <EOL> MOMENT_ARM = LENGTH / <NUM_LIT> <EOL> _ALPHA_MASS = <NUM_LIT:1.0> / ( MASS_CART + MASS_PEND ) <EOL> dt = <NUM_LIT:0.1> <EOL> force_noise_max = <NUM_LIT:10> <EOL> int_type = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> self . statespace_limits = np . array ( <EOL> [ self . ANGLE_LIMITS , self . ANGULAR_RATE_LIMITS ] ) <EOL> self . continuous_dims = [ StateIndex . THETA , StateIndex . THETA_DOT ] <EOL> self . DimNames = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> super ( InfTrackCartPole , self ) . __init__ ( ) <EOL> def s0 ( self ) : <EOL> raise NotImplementedError <EOL> def step ( self , a ) : <EOL> """<STR_LIT>""" <EOL> s = np . append ( self . state , np . array ( <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> ] ) ) <EOL> ns = self . _stepFourState ( s , a ) <EOL> ns = ns [ <EOL> <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> self . state = ns . copy ( ) <EOL> terminal = self . isTerminal ( ) <EOL> reward = self . _getReward ( a ) <EOL> possibleActions = self . possibleActions ( ) <EOL> return reward , ns , terminal , possibleActions <EOL> def showDomain ( self , a = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> fourState = np . append ( self . state , np . array ( <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> ] ) ) <EOL> self . _plot_state ( fourState , a ) <EOL> def showLearning ( self , representation ) : <EOL> ( thetas , theta_dots ) = self . _setup_learning ( representation ) <EOL> pi = np . zeros ( ( len ( theta_dots ) , len ( thetas ) ) , '<STR_LIT>' ) <EOL> V = np . zeros ( ( len ( theta_dots ) , len ( thetas ) ) ) <EOL> for row , thetaDot in enumerate ( theta_dots ) : <EOL> for col , theta in enumerate ( thetas ) : <EOL> s = np . array ( [ theta , thetaDot ] ) <EOL> terminal = self . isTerminal ( s ) <EOL> Qs = representation . Qs ( s , terminal ) <EOL> As = self . possibleActions ( s = s ) <EOL> a = np . random . choice ( As [ Qs . max ( ) == Qs ] ) <EOL> pi [ row , col ] = a <EOL> V [ row , col ] = max ( Qs ) <EOL> self . _plot_policy ( pi ) <EOL> self . _plot_valfun ( V ) <EOL> class InfCartPoleBalance ( InfTrackCartPole ) : <EOL> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> FELL_REWARD = - <NUM_LIT:1> <EOL> ANGLE_LIMITS = [ - np . pi / <NUM_LIT> , np . pi / <NUM_LIT> ] <EOL> ANGULAR_RATE_LIMITS = [ <EOL> - <NUM_LIT> , <NUM_LIT> ] <EOL> def __init__ ( self , episodeCap = <NUM_LIT> ) : <EOL> self . episodeCap = episodeCap <EOL> super ( InfCartPoleBalance , self ) . __init__ ( ) <EOL> def s0 ( self ) : <EOL> self . state = ( self . random_state . rand ( <NUM_LIT:2> ) * <NUM_LIT:2> - <NUM_LIT:1> ) * <NUM_LIT> <EOL> return self . state . copy ( ) , self . isTerminal ( ) , self . possibleActions ( ) <EOL> def _getReward ( self , a , s = None ) : <EOL> if s is None : <EOL> s = self . state <EOL> return self . FELL_REWARD if self . isTerminal ( s = s ) else <NUM_LIT:0> <EOL> def isTerminal ( self , s = None ) : <EOL> if s is None : <EOL> s = self . state <EOL> return ( <EOL> not ( - np . pi / <NUM_LIT> < s [ StateIndex . THETA ] < np . pi / <NUM_LIT> ) <EOL> ) <EOL> class InfCartPoleSwingUp ( InfTrackCartPole ) : <EOL> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> GOAL_LIMITS = [ - np . pi / <NUM_LIT:6> , np . pi / <NUM_LIT:6> ] <EOL> ANGLE_LIMITS = [ - np . pi , np . pi ] <EOL> ANGULAR_RATE_LIMITS = [ - <NUM_LIT:3> * np . pi , <NUM_LIT:3> * np . pi ] <EOL> episodeCap = <NUM_LIT> <EOL> discount_factor = <NUM_LIT> <EOL> def __init__ ( self ) : <EOL> self . statespace_limits = np . array ( <EOL> [ self . ANGLE_LIMITS , self . ANGULAR_RATE_LIMITS ] ) <EOL> super ( InfCartPoleSwingUp , self ) . __init__ ( ) <EOL> def s0 ( self ) : <EOL> """<STR_LIT>""" <EOL> self . state = np . array ( [ np . pi , <NUM_LIT:0> ] ) <EOL> return self . state . copy ( ) , self . isTerminal ( ) , self . possibleActions ( ) <EOL> def _getReward ( self , a , s = None ) : <EOL> """<STR_LIT>""" <EOL> if s is None : <EOL> s = self . state <EOL> return ( <EOL> self . GOAL_REWARD if self . GOAL_LIMITS [ <EOL> <NUM_LIT:0> ] < s [ <EOL> StateIndex . THETA ] < self . GOAL_LIMITS [ <EOL> <NUM_LIT:1> ] else <NUM_LIT:0> <EOL> ) <EOL> def isTerminal ( self , s = None ) : <EOL> return False </s>
