<s> from __future__ import print_function <EOL> __author__ = '<STR_LIT>' <EOL> from pybrain . rl . environments import EpisodicTask <EOL> from pybrain . rl . environments . ode . sensors import SpecificBodyPositionSensor <EOL> from scipy import tanh , zeros , array , random , sqrt , asarray <EOL> class CCRLTask ( EpisodicTask ) : <EOL> def __init__ ( self , env ) : <EOL> EpisodicTask . __init__ ( self , env ) <EOL> self . maxPower = <NUM_LIT> <EOL> self . reward_history = [ ] <EOL> self . count = <NUM_LIT:0> <EOL> self . epiLen = <NUM_LIT> <EOL> self . incLearn = <NUM_LIT:0> <EOL> self . env . FricMu = <NUM_LIT> <EOL> self . env . dt = <NUM_LIT> <EOL> self . sensor_limits = [ ] <EOL> for i in range ( self . env . actLen ) : <EOL> self . sensor_limits . append ( ( self . env . cLowList [ i ] , self . env . cHighList [ i ] ) ) <EOL> for i in range ( self . env . actLen ) : <EOL> self . sensor_limits . append ( ( - <NUM_LIT:20> , <NUM_LIT:20> ) ) <EOL> self . actor_limits = [ ( - <NUM_LIT:1> , <NUM_LIT:1> ) ] * env . actLen <EOL> self . oldAction = zeros ( env . actLen , float ) <EOL> self . dist = zeros ( <NUM_LIT:9> , float ) <EOL> self . dif = array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) <EOL> self . target = array ( [ - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> ] ) <EOL> self . grepRew = <NUM_LIT:0.0> <EOL> self . tableFlag = <NUM_LIT:0.0> <EOL> self . env . addSensor ( SpecificBodyPositionSensor ( [ '<STR_LIT>' ] , "<STR_LIT>" ) ) <EOL> self . env . addSensor ( SpecificBodyPositionSensor ( [ '<STR_LIT>' ] , "<STR_LIT>" ) ) <EOL> self . env . addSensor ( SpecificBodyPositionSensor ( [ '<STR_LIT>' ] , "<STR_LIT>" ) ) <EOL> self . env . addSensor ( SpecificBodyPositionSensor ( [ '<STR_LIT>' ] , "<STR_LIT>" ) ) <EOL> self . env . obsLen = len ( self . env . getSensors ( ) ) <EOL> for i in range ( self . env . obsLen - <NUM_LIT:2> * self . env . actLen ) : <EOL> self . sensor_limits . append ( ( - <NUM_LIT:4> , <NUM_LIT:4> ) ) <EOL> self . actor_limits = None <EOL> def getObservation ( self ) : <EOL> """<STR_LIT>""" <EOL> sensors = self . env . getSensors ( ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> self . dist [ i ] = ( ( sensors [ self . env . obsLen - <NUM_LIT:9> + i ] + sensors [ self . env . obsLen - <NUM_LIT:6> + i ] + sensors [ self . env . obsLen - <NUM_LIT:3> + i ] ) / <NUM_LIT> - ( sensors [ self . env . obsLen - <NUM_LIT:12> + i ] + self . dif [ i ] ) ) * <NUM_LIT> <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> self . dist [ i + <NUM_LIT:3> ] = ( sensors [ self . env . obsLen - <NUM_LIT:3> + i ] - sensors [ self . env . obsLen - <NUM_LIT:6> + i ] ) * <NUM_LIT> <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> self . dist [ i + <NUM_LIT:6> ] = ( ( sensors [ self . env . obsLen - <NUM_LIT:3> + i ] + sensors [ self . env . obsLen - <NUM_LIT:6> + i ] ) / <NUM_LIT> - sensors [ self . env . obsLen - <NUM_LIT:9> + i ] ) * <NUM_LIT> <EOL> if self . sensor_limits : <EOL> sensors = self . normalize ( sensors ) <EOL> sens = [ ] <EOL> for i in range ( self . env . obsLen - <NUM_LIT:12> ) : <EOL> sens . append ( sensors [ i ] ) <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> sens . append ( self . dist [ i ] ) <EOL> for i in self . oldAction : <EOL> sens . append ( i ) <EOL> return sens <EOL> def performAction ( self , action ) : <EOL> self . oldAction = action <EOL> if abs ( abs ( self . dist [ : <NUM_LIT:3> ] ) . sum ( ) ) < <NUM_LIT> : action [ <NUM_LIT:15> ] = <NUM_LIT:1.0> <EOL> else : action [ <NUM_LIT:15> ] = - <NUM_LIT:1.0> <EOL> isJoints = array ( self . env . getSensorByName ( '<STR_LIT>' ) ) <EOL> isSpeeds = array ( self . env . getSensorByName ( '<STR_LIT>' ) ) <EOL> act = ( action + <NUM_LIT:1.0> ) / <NUM_LIT> * ( self . env . cHighList - self . env . cLowList ) + self . env . cLowList <EOL> action = tanh ( ( act - isJoints - <NUM_LIT> * isSpeeds * self . env . tourqueList ) * <NUM_LIT> ) * self . maxPower * self . env . tourqueList <EOL> EpisodicTask . performAction ( self , action ) <EOL> def isFinished ( self ) : <EOL> if self . count > self . epiLen : <EOL> self . res ( ) <EOL> return True <EOL> else : <EOL> self . count += <NUM_LIT:1> <EOL> return False <EOL> def res ( self ) : <EOL> self . count = <NUM_LIT:0> <EOL> self . incLearn += <NUM_LIT:1> <EOL> self . reward_history . append ( self . getTotalReward ( ) ) <EOL> self . tableFlag = <NUM_LIT:0.0> <EOL> def getReward ( self ) : <EOL> dis = sqrt ( ( self . dist [ <NUM_LIT:0> : <NUM_LIT:3> ] ** <NUM_LIT:2> ) . sum ( ) ) <EOL> return ( <NUM_LIT> - dis ) / float ( self . epiLen ) - float ( self . env . tableSum ) * <NUM_LIT:0.1> <EOL> class CCRLGlasTask ( CCRLTask ) : <EOL> def __init__ ( self , env ) : <EOL> CCRLTask . __init__ ( self , env ) <EOL> self . dif = array ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) <EOL> self . epiLen = <NUM_LIT:1000> <EOL> def isFinished ( self ) : <EOL> if self . count > self . epiLen : <EOL> self . res ( ) <EOL> return True <EOL> else : <EOL> self . count += <NUM_LIT:1> <EOL> return False <EOL> def getReward ( self ) : <EOL> if self . env . glasSum >= <NUM_LIT:2> : grip = <NUM_LIT> <EOL> else : grip = <NUM_LIT:0.0> <EOL> if self . env . tableSum > <NUM_LIT:0> : self . tableFlag = - <NUM_LIT:1.0> <EOL> else : tableFlag = <NUM_LIT:0.0> <EOL> self . dist [ <NUM_LIT:3> ] = <NUM_LIT:0.0> <EOL> self . dist [ <NUM_LIT:8> ] = <NUM_LIT:0.0> <EOL> dis = <NUM_LIT> / ( ( self . dist [ : <NUM_LIT:3> ] ** <NUM_LIT:2> ) . sum ( ) + <NUM_LIT:0.1> ) <EOL> nig = <NUM_LIT> / ( ( self . dist [ <NUM_LIT:3> : ] ** <NUM_LIT:2> ) . sum ( ) + <NUM_LIT:0.1> ) <EOL> if self . env . stepCounter == self . epiLen : print ( ( "<STR_LIT>" , grip , "<STR_LIT>" , dis , "<STR_LIT>" , nig , "<STR_LIT>" , self . tableFlag ) ) <EOL> return ( <NUM_LIT:10> + grip + nig + dis + self . tableFlag ) / float ( self . epiLen ) <EOL> class CCRLPlateTask ( CCRLTask ) : <EOL> def __init__ ( self , env ) : <EOL> CCRLTask . __init__ ( self , env ) <EOL> self . dif = array ( [ <NUM_LIT:0.0> , <NUM_LIT> , <NUM_LIT> ] ) <EOL> self . epiLen = <NUM_LIT:1000> <EOL> def isFinished ( self ) : <EOL> if self . count > self . epiLen : <EOL> self . res ( ) <EOL> return True <EOL> else : <EOL> if self . count == <NUM_LIT:1> : self . pertGlasPos ( <NUM_LIT:0> ) <EOL> self . count += <NUM_LIT:1> <EOL> return False <EOL> def pertGlasPos ( self , num ) : <EOL> if num == <NUM_LIT:0> : self . env . pert = asarray ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.5> ] ) <EOL> def getReward ( self ) : <EOL> if self . env . glasSum >= <NUM_LIT:2> : grip = <NUM_LIT:1.0> <EOL> else : grip = <NUM_LIT:0.0> <EOL> if self . env . tableSum > <NUM_LIT:0> : self . tableFlag = <NUM_LIT> <EOL> dis = sqrt ( ( self . dist [ <NUM_LIT:0> : <NUM_LIT:3> ] ** <NUM_LIT:2> ) . sum ( ) ) <EOL> if self . count == self . epiLen : <EOL> return <NUM_LIT> + grip - dis - self . tableFlag <EOL> else : <EOL> return ( <NUM_LIT> - dis ) / float ( self . epiLen ) + ( grip - float ( self . env . tableSum ) ) * <NUM_LIT:0.1> <EOL> class CCRLGlasVarTask ( CCRLGlasTask ) : <EOL> def __init__ ( self , env ) : <EOL> CCRLGlasTask . __init__ ( self , env ) <EOL> self . epiLen = <NUM_LIT> <EOL> def isFinished ( self ) : <EOL> if self . count > self . epiLen : <EOL> self . res ( ) <EOL> return True <EOL> else : <EOL> if self . count == <NUM_LIT:1> : <EOL> self . pertGlasPos ( <NUM_LIT:0> ) <EOL> if self . count == self . epiLen / <NUM_LIT:5> + <NUM_LIT:1> : <EOL> self . env . reset ( ) <EOL> self . pertGlasPos ( <NUM_LIT:1> ) <EOL> if self . count == <NUM_LIT:2> * self . epiLen / <NUM_LIT:5> + <NUM_LIT:1> : <EOL> self . env . reset ( ) <EOL> self . pertGlasPos ( <NUM_LIT:2> ) <EOL> if self . count == <NUM_LIT:3> * self . epiLen / <NUM_LIT:5> + <NUM_LIT:1> : <EOL> self . env . reset ( ) <EOL> self . pertGlasPos ( <NUM_LIT:3> ) <EOL> if self . count == <NUM_LIT:4> * self . epiLen / <NUM_LIT:5> + <NUM_LIT:1> : <EOL> self . env . reset ( ) <EOL> self . pertGlasPos ( <NUM_LIT:4> ) <EOL> self . count += <NUM_LIT:1> <EOL> return False <EOL> def pertGlasPos ( self , num ) : <EOL> if num == <NUM_LIT:0> : self . env . pert = asarray ( [ <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.5> ] ) <EOL> if num == <NUM_LIT:1> : self . env . pert = asarray ( [ - <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.5> ] ) <EOL> if num == <NUM_LIT:2> : self . env . pert = asarray ( [ <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) <EOL> if num == <NUM_LIT:3> : self . env . pert = asarray ( [ - <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) <EOL> if num == <NUM_LIT:4> : self . env . pert = asarray ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT> ] ) <EOL> def getReward ( self ) : <EOL> if self . env . glasSum >= <NUM_LIT:2> : grip = <NUM_LIT:1.0> <EOL> else : grip = <NUM_LIT:0.0> <EOL> if self . env . tableSum > <NUM_LIT:0> : self . tableFlag = <NUM_LIT> <EOL> self . dist [ <NUM_LIT:3> ] = <NUM_LIT:0.0> <EOL> self . dist [ <NUM_LIT:8> ] = <NUM_LIT:0.0> <EOL> dis = sqrt ( ( self . dist ** <NUM_LIT:2> ) . sum ( ) ) <EOL> nig = ( abs ( self . dist [ <NUM_LIT:4> ] ) + <NUM_LIT:1.0> ) <EOL> if self . count == self . epiLen or self . count == self . epiLen / <NUM_LIT:5> or self . count == <NUM_LIT:2> * self . epiLen / <NUM_LIT:5> or self . count == <NUM_LIT:3> * self . epiLen / <NUM_LIT:5> or self . count == <NUM_LIT:4> * self . epiLen / <NUM_LIT:5> : <EOL> return <NUM_LIT> + grip / nig - dis - self . tableFlag <EOL> else : <EOL> return ( <NUM_LIT> - dis ) / float ( self . epiLen ) + ( grip / nig - float ( self . env . tableSum ) ) * <NUM_LIT:0.1> <EOL> class CCRLGlasVarRandTask ( CCRLGlasVarTask ) : <EOL> def pertGlasPos ( self , num ) : <EOL> self . env . pert = asarray ( [ random . random ( ) * <NUM_LIT> - <NUM_LIT:1.0> , <NUM_LIT:0.0> , random . random ( ) * <NUM_LIT:0.5> + <NUM_LIT:0.5> ] ) <EOL> class CCRLPointTask ( CCRLGlasVarTask ) : <EOL> def __init__ ( self , env ) : <EOL> CCRLGlasVarTask . __init__ ( self , env ) <EOL> self . epiLen = <NUM_LIT:1000> <EOL> def isFinished ( self ) : <EOL> if self . count > self . epiLen : <EOL> self . res ( ) <EOL> return True <EOL> else : <EOL> if self . count == <NUM_LIT:1> : <EOL> self . pertGlasPos ( <NUM_LIT:0> ) <EOL> self . count += <NUM_LIT:1> <EOL> return False <EOL> def getObservation ( self ) : <EOL> """<STR_LIT>""" <EOL> sensors = self . env . getSensors ( ) <EOL> sensSort = [ ] <EOL> for i in range ( <NUM_LIT:32> ) : <EOL> sensSort . append ( sensors [ i ] ) <EOL> for i in self . oldAction : <EOL> sensSort . append ( i ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> sensSort . append ( ( sensors [ <NUM_LIT> + i ] + sensors [ <NUM_LIT> + i ] ) / <NUM_LIT:2> ) <EOL> sensSort . append ( ( sensors [ <NUM_LIT> ] - sensors [ <NUM_LIT> ] ) / <NUM_LIT:2> - sensors [ <NUM_LIT> ] ) <EOL> sensSort . append ( ( sensors [ <NUM_LIT> + <NUM_LIT:1> ] - sensors [ <NUM_LIT> + <NUM_LIT:1> ] ) / <NUM_LIT:2> - sensors [ <NUM_LIT> + <NUM_LIT:1> ] ) <EOL> sensSort . append ( ( sensors [ <NUM_LIT> + <NUM_LIT:1> ] - sensors [ <NUM_LIT> + <NUM_LIT:1> ] ) ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> sensSort . append ( self . target [ i ] ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> sensSort . append ( <NUM_LIT:0.0> ) <EOL> sensSort . append ( float ( random . randint ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) ) <EOL> if self . sensor_limits : <EOL> sensors = self . normalize ( sensors ) <EOL> sens = [ ] <EOL> for i in range ( <NUM_LIT:32> ) : <EOL> sens . append ( sensors [ i ] ) <EOL> for i in range ( <NUM_LIT> ) : <EOL> sens . append ( sensSort [ i + <NUM_LIT:32> ] ) <EOL> self . dist = array ( [ ( sens [ <NUM_LIT> ] - sens [ <NUM_LIT> ] ) , ( sens [ <NUM_LIT> ] - sens [ <NUM_LIT> ] ) , ( sens [ <NUM_LIT> ] - sens [ <NUM_LIT:50> ] ) , sens [ <NUM_LIT> ] , sens [ <NUM_LIT> ] , sens [ <NUM_LIT> ] , sens [ <NUM_LIT:15> ] ] ) <EOL> return sens <EOL> def pertGlasPos ( self , num ) : <EOL> if num == <NUM_LIT:0> : self . target = asarray ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:1.0> ] ) <EOL> self . env . pert = self . target . copy ( ) <EOL> self . target = self . target . copy ( ) + array ( [ - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> ] ) <EOL> def getReward ( self ) : <EOL> dis = sqrt ( ( self . dist ** <NUM_LIT:2> ) . sum ( ) ) <EOL> return ( <NUM_LIT> - dis ) / float ( self . epiLen ) - float ( self . env . tableSum ) * <NUM_LIT:0.1> <EOL> class CCRLPointVarTask ( CCRLPointTask ) : <EOL> def __init__ ( self , env ) : <EOL> CCRLPointTask . __init__ ( self , env ) <EOL> self . epiLen = <NUM_LIT> <EOL> def isFinished ( self ) : <EOL> if self . count > self . epiLen : <EOL> self . res ( ) <EOL> return True <EOL> else : <EOL> if self . count == <NUM_LIT:1> : <EOL> self . pertGlasPos ( <NUM_LIT:0> ) <EOL> if self . count == self . epiLen / <NUM_LIT:2> + <NUM_LIT:1> : <EOL> self . env . reset ( ) <EOL> self . pertGlasPos ( <NUM_LIT:1> ) <EOL> self . count += <NUM_LIT:1> <EOL> return False <EOL> def getObservation ( self ) : <EOL> """<STR_LIT>""" <EOL> sensors = self . env . getSensors ( ) <EOL> sensSort = [ ] <EOL> for i in range ( <NUM_LIT:32> ) : <EOL> sensSort . append ( sensors [ i ] ) <EOL> for i in self . oldAction : <EOL> sensSort . append ( i ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> sensSort . append ( ( sensors [ <NUM_LIT> + i ] + sensors [ <NUM_LIT> + i ] ) / <NUM_LIT:2> ) <EOL> sensSort . append ( ( sensors [ <NUM_LIT> ] - sensors [ <NUM_LIT> ] ) / <NUM_LIT:2> - sensors [ <NUM_LIT> ] ) <EOL> sensSort . append ( ( sensors [ <NUM_LIT> + <NUM_LIT:1> ] - sensors [ <NUM_LIT> + <NUM_LIT:1> ] ) / <NUM_LIT:2> - sensors [ <NUM_LIT> + <NUM_LIT:1> ] ) <EOL> sensSort . append ( ( sensors [ <NUM_LIT> + <NUM_LIT:1> ] - sensors [ <NUM_LIT> + <NUM_LIT:1> ] ) ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> sensSort . append ( self . target [ i ] ) <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> sensSort . append ( <NUM_LIT:0.0> ) <EOL> sensSort . append ( float ( random . randint ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) ) <EOL> if self . sensor_limits : <EOL> sensors = self . normalize ( sensors ) <EOL> sens = [ ] <EOL> for i in range ( <NUM_LIT:32> ) : <EOL> sens . append ( sensors [ i ] ) <EOL> for i in range ( <NUM_LIT> ) : <EOL> sens . append ( sensSort [ i + <NUM_LIT:32> ] ) <EOL> self . dist = array ( [ ( sens [ <NUM_LIT> ] - sens [ <NUM_LIT> ] ) * <NUM_LIT> , ( sens [ <NUM_LIT> ] - sens [ <NUM_LIT> ] ) * <NUM_LIT> , ( sens [ <NUM_LIT> ] - sens [ <NUM_LIT:50> ] ) * <NUM_LIT> , sens [ <NUM_LIT> ] , sens [ <NUM_LIT> ] , sens [ <NUM_LIT> ] , <NUM_LIT:1.0> + sens [ <NUM_LIT:15> ] ] ) <EOL> return sens <EOL> def pertGlasPos ( self , num ) : <EOL> if num == <NUM_LIT:0> : self . target = asarray ( [ <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:1.0> ] ) <EOL> if num == <NUM_LIT:1> : self . target = asarray ( [ - <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:1.0> ] ) <EOL> if num == <NUM_LIT:2> : self . target = asarray ( [ <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) <EOL> if num == <NUM_LIT:3> : self . target = asarray ( [ - <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) <EOL> if num == <NUM_LIT:4> : self . target = asarray ( [ <NUM_LIT:0.0> , <NUM_LIT:0.0> , <NUM_LIT:0.5> ] ) <EOL> self . env . pert = self . target . copy ( ) <EOL> self . target = self . target . copy ( ) + array ( [ - <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> ] ) <EOL> def getReward ( self ) : <EOL> dis = sqrt ( ( self . dist ** <NUM_LIT:2> ) . sum ( ) ) <EOL> subEpi = self . epiLen / <NUM_LIT:2> <EOL> if self . count == self . epiLen or self . count == subEpi : <EOL> return ( <NUM_LIT> - dis ) / <NUM_LIT> <EOL> else : <EOL> return ( <NUM_LIT> - dis ) / float ( self . epiLen ) - float ( self . env . tableSum ) * <NUM_LIT:0.1> </s>
