<s> __author__ = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> from scipy import pi , array , cos , sin <EOL> from pybrain . rl . environments . episodic import EpisodicTask <EOL> class AcrobotTask ( EpisodicTask ) : <EOL> """<STR_LIT>""" <EOL> input_ranges = [ [ - pi , pi ] , [ - pi , pi ] , [ - <NUM_LIT:4> * pi , <NUM_LIT:4> * pi ] , [ - <NUM_LIT:9> * pi , <NUM_LIT:9> * pi ] ] <EOL> reward_ranges = [ [ - <NUM_LIT:1.0> , <NUM_LIT> ] ] <EOL> maxSpeed1 = <NUM_LIT:4> * pi <EOL> maxSpeed2 = <NUM_LIT:9> * pi <EOL> m1 = <NUM_LIT:1.0> <EOL> m2 = <NUM_LIT:1.0> <EOL> l1 = <NUM_LIT:1.0> <EOL> l2 = <NUM_LIT:1.0> <EOL> l1Square = l1 * l1 <EOL> l2Square = l2 * l2 <EOL> lc1 = <NUM_LIT:0.5> <EOL> lc2 = <NUM_LIT:0.5> <EOL> lc1Square = lc1 * lc1 <EOL> lc2Square = lc2 * lc2 <EOL> I1 = <NUM_LIT:1.0> <EOL> I2 = <NUM_LIT:1.0> <EOL> g = <NUM_LIT> <EOL> delta_t = <NUM_LIT> <EOL> action_list = ( - <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:1.0> ) <EOL> nactions = len ( action_list ) <EOL> nsenses = <NUM_LIT:5> <EOL> steps = <NUM_LIT:0> <EOL> maxSteps = <NUM_LIT> <EOL> episode = <NUM_LIT:0> <EOL> target = <NUM_LIT> <EOL> easy_rewards = False <EOL> resetOnSuccess = False <EOL> def __init__ ( self ) : <EOL> self . reset ( ) <EOL> self . cumreward = <NUM_LIT:0> <EOL> def getObservation ( self ) : <EOL> return array ( self . state + [ pi ] ) / ( pi ) <EOL> def performAction ( self , action ) : <EOL> if self . done > <NUM_LIT:0> : <EOL> self . done += <NUM_LIT:1> <EOL> else : <EOL> self . state = self . DoAction ( action , self . state ) <EOL> self . r , self . done = self . GetReward ( self . state ) <EOL> self . cumreward += self . r <EOL> def reset ( self ) : <EOL> self . state = self . GetInitialState ( ) <EOL> def getReward ( self ) : <EOL> return self . r <EOL> def isFinished ( self ) : <EOL> if self . done >= <NUM_LIT:3> and self . resetOnSuccess : <EOL> self . reset ( ) <EOL> return False <EOL> else : <EOL> return self . done >= <NUM_LIT:3> <EOL> def GetInitialState ( self ) : <EOL> s = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> self . StartEpisode ( ) <EOL> return s <EOL> def StartEpisode ( self ) : <EOL> self . steps = <NUM_LIT:0> <EOL> self . episode = self . episode + <NUM_LIT:1> <EOL> self . done = <NUM_LIT:0> <EOL> def GetReward ( self , x ) : <EOL> y_acrobot = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> theta1 = x [ <NUM_LIT:0> ] <EOL> theta2 = x [ <NUM_LIT:1> ] <EOL> y_acrobot [ <NUM_LIT:1> ] = y_acrobot [ <NUM_LIT:0> ] - cos ( theta1 ) <EOL> y_acrobot [ <NUM_LIT:2> ] = y_acrobot [ <NUM_LIT:1> ] - cos ( theta2 ) <EOL> goal = y_acrobot [ <NUM_LIT:0> ] + self . target <EOL> if self . easy_rewards : <EOL> r = y_acrobot [ <NUM_LIT:2> ] <EOL> else : <EOL> r = <NUM_LIT:0> <EOL> f = <NUM_LIT:0> <EOL> if y_acrobot [ <NUM_LIT:2> ] >= goal : <EOL> if self . easy_rewards : <EOL> r = <NUM_LIT:10> * y_acrobot [ <NUM_LIT:2> ] <EOL> else : <EOL> r = <NUM_LIT:1> <EOL> f = <NUM_LIT:1> <EOL> if self . steps >= self . maxSteps : <EOL> f = <NUM_LIT:5> <EOL> return r , f <EOL> def DoAction ( self , a , x ) : <EOL> self . steps = self . steps + <NUM_LIT:1> <EOL> torque = self . action_list [ a ] <EOL> theta1 , theta2 , theta1_dot , theta2_dot = x <EOL> for _ in range ( <NUM_LIT:4> ) : <EOL> d1 = self . m1 * self . lc1Square + self . m2 * ( self . l1Square + self . lc2Square + <NUM_LIT:2> * self . l1 * self . lc2 * cos ( theta2 ) ) + self . I1 + self . I2 <EOL> d2 = self . m2 * ( self . lc2Square + self . l1 * self . lc2 * cos ( theta2 ) ) + self . I2 <EOL> phi2 = self . m2 * self . lc2 * self . g * cos ( theta1 + theta2 - pi / <NUM_LIT> ) <EOL> phi1 = - self . m2 * self . l1 * self . lc2 * theta2_dot * sin ( theta2 ) * ( theta2_dot - <NUM_LIT:2> * theta1_dot ) + ( self . m1 * self . lc1 + self . m2 * self . l1 ) * self . g * cos ( theta1 - ( pi / <NUM_LIT> ) ) + phi2 <EOL> accel2 = ( torque + phi1 * ( d2 / d1 ) - self . m2 * self . l1 * self . lc2 * theta1_dot * theta1_dot * sin ( theta2 ) - phi2 ) <EOL> accel2 = accel2 / ( self . m2 * self . lc2Square + self . I2 - ( d2 * d2 / d1 ) ) <EOL> accel1 = - ( d2 * accel2 + phi1 ) / d1 <EOL> theta1_dot = theta1_dot + accel1 * self . delta_t <EOL> if theta1_dot < - self . maxSpeed1 : <EOL> theta1_dot = - self . maxSpeed1 <EOL> if theta1_dot > self . maxSpeed1 : <EOL> theta1_dot = self . maxSpeed1 <EOL> theta1 = theta1 + theta1_dot * self . delta_t <EOL> theta2_dot = theta2_dot + accel2 * self . delta_t <EOL> if theta2_dot < - self . maxSpeed2 : <EOL> theta2_dot = - self . maxSpeed2 <EOL> if theta2_dot > self . maxSpeed2 : <EOL> theta2_dot = self . maxSpeed2 <EOL> theta2 = theta2 + theta2_dot * self . delta_t <EOL> if theta1 < - pi : <EOL> theta1 += <NUM_LIT:2> * pi <EOL> elif theta1 > pi : <EOL> theta1 -= <NUM_LIT:2> * pi <EOL> if theta2 < - pi : <EOL> theta2 += <NUM_LIT:2> * pi <EOL> elif theta2 > pi : <EOL> theta2 -= <NUM_LIT:2> * pi <EOL> xp = [ theta1 , theta2 , theta1_dot , theta2_dot ] <EOL> return xp <EOL> class SimpleAcrobot ( AcrobotTask ) : <EOL> target = - <NUM_LIT:0.5> <EOL> class VerySimpleAcrobot ( AcrobotTask ) : <EOL> target = - <NUM_LIT:1.> <EOL> class SingleArmSwinger ( AcrobotTask ) : <EOL> """<STR_LIT>""" <EOL> nsenses = <NUM_LIT:3> <EOL> resetOnSuccess = False <EOL> target = <NUM_LIT> <EOL> maxSteps = <NUM_LIT> <EOL> def GetInitialState ( self ) : <EOL> s = [ pi , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> self . StartEpisode ( ) <EOL> return s <EOL> def getObservation ( self ) : <EOL> return array ( self . state [ <NUM_LIT:2> : ] + [ pi ] ) / ( pi ) <EOL> def performAction ( self , action ) : <EOL> AcrobotTask . performAction ( self , action ) <EOL> _ , theta2 , _ , theta2_dot = self . state <EOL> self . state = [ pi , theta2 , <NUM_LIT:0> , theta2_dot ] </s>
