@@ -46,66 +46,71 @@ let model = null
4646class Policy {
4747 constructor ( ) {
4848 this . step = 0
49- this . state = null
49+ this . states = null
5050 }
51- evaluate_step ( state ) {
52- state . observe ( )
51+ get_observation ( state , obs ) {
52+ let vehicle_state = null
5353 const full_observation = [ ...Array ( state . observation_dim ) . keys ( ) ] . map ( i => state . get_observation ( i ) )
5454 console . assert ( full_observation . length > 18 , "Observation is smaller than base observation" )
55- const get_obs = ( obs ) => {
56- let vehicle_state = null
57- const get_state = ( ) => {
58- if ( vehicle_state === null ) {
59- vehicle_state = JSON . parse ( state . get_state ( ) )
60- }
61- return vehicle_state
55+ const get_state = ( ) => {
56+ if ( vehicle_state === null ) {
57+ vehicle_state = JSON . parse ( state . get_state ( ) )
6258 }
63- switch ( true ) {
64- case obs === "Position" :
65- return full_observation . slice ( 0 , 3 )
66- case obs === "OrientationRotationMatrix" :
67- return full_observation . slice ( 3 , 12 )
68- case obs === "LinearVelocity" :
69- return full_observation . slice ( 12 , 15 )
70- case obs === "AngularVelocity" :
59+ return vehicle_state
60+ }
61+ switch ( true ) {
62+ case obs === "Position" :
63+ return full_observation . slice ( 0 , 3 )
64+ case obs === "OrientationRotationMatrix" :
65+ return full_observation . slice ( 3 , 12 )
66+ case obs === "LinearVelocity" :
67+ return full_observation . slice ( 12 , 15 )
68+ case obs === "AngularVelocity" :
69+ return full_observation . slice ( 15 , 18 )
70+ case obs . startsWith ( "AngularVelocityDelayed" ) :
71+ const delay_string = obs . split ( "(" ) [ 1 ] . split ( ")" ) [ 0 ]
72+ const delay = parseInt ( delay_string )
73+ if ( delay === 0 ) {
7174 return full_observation . slice ( 15 , 18 )
72- case obs . startsWith ( "AngularVelocityDelayed" ) :
73- const delay_string = obs . split ( "(" ) [ 1 ] . split ( ")" ) [ 0 ]
74- const delay = parseInt ( delay_string )
75- if ( delay === 0 ) {
76- return full_observation . slice ( 15 , 18 )
77- } else {
78- const s = get_state ( )
79- return s [ "angular_velocity_history" ] [ s [ "angular_velocity_history" ] . length - delay ]
80- }
81- case obs . startsWith ( "ActionHistory" ) :
82- const history_length_string = obs . split ( "(" ) [ 1 ] . split ( ")" ) [ 0 ]
83- const history_length = parseInt ( history_length_string )
84- return full_observation . slice ( 18 , 18 + history_length * 4 )
85- case obs === "RotorSpeeds" :
86- const parameters = JSON . parse ( state . get_parameters ( ) )
87- const min_action = parameters . dynamics . action_limit . min
88- const max_action = parameters . dynamics . action_limit . max
89- return get_state ( ) [ "rpm" ] . map ( x => ( x - min_action ) / ( max_action - min_action ) * 2 - 1 )
90- default :
91- console . error ( "Unknown observation: " , obs )
92- return null
93- }
75+ } else {
76+ const s = get_state ( )
77+ return s [ "angular_velocity_history" ] [ s [ "angular_velocity_history" ] . length - delay ]
78+ }
79+ case obs . startsWith ( "ActionHistory" ) :
80+ const history_length_string = obs . split ( "(" ) [ 1 ] . split ( ")" ) [ 0 ]
81+ const history_length = parseInt ( history_length_string )
82+ return full_observation . slice ( 18 , 18 + history_length * 4 )
83+ case obs === "RotorSpeeds" :
84+ const parameters = JSON . parse ( state . get_parameters ( ) )
85+ const min_action = parameters . dynamics . action_limit . min
86+ const max_action = parameters . dynamics . action_limit . max
87+ return get_state ( ) [ "rpm" ] . map ( x => ( x - min_action ) / ( max_action - min_action ) * 2 - 1 )
88+ default :
89+ console . error ( "Unknown observation: " , obs )
90+ return null
91+ }
92+ }
93+ evaluate_step ( states ) {
94+ if ( ! this . states || this . states . length !== states . length ) {
95+ this . states = states . map ( ( ) => null )
9496 }
95- const observation_description = document . getElementById ( "observations" ) . observation
96- let input = math . matrix ( [ observation_description . split ( "." ) . map ( x => get_obs ( x ) ) . flat ( ) ] )
97- const input_offset = default_trajectory ( this . step / 100 )
98- input_offset . forEach ( ( x , i ) => {
99- input . _data [ 0 ] [ i ] = input . _data [ 0 ] [ i ] - x
97+ return states . map ( ( state , i ) => {
98+ state . observe ( )
99+ const observation_description = document . getElementById ( "observations" ) . observation
100+ let input = math . matrix ( [ observation_description . split ( "." ) . map ( x => this . get_observation ( state , x ) ) . flat ( ) ] )
101+ const input_offset = default_trajectory ( this . step / 100 )
102+ input_offset . forEach ( ( x , i ) => {
103+ input . _data [ 0 ] [ i ] = input . _data [ 0 ] [ i ] - x
104+ } )
105+ const [ output , new_state ] = model . evaluate_step ( input , this . states [ i ] )
106+ this . states [ i ] = new_state
107+ this . step += 1
108+ return output . valueOf ( ) [ 0 ]
100109 } )
101- const [ output , new_state ] = model . evaluate_step ( input , this . state )
102- this . state = new_state
103- this . step += 1
104- return output . valueOf ( ) [ 0 ]
105110 }
106111 reset ( ) {
107112 this . step = 0
108- this . state = null
113+ this . states = null
109114 }
110115}
111116
@@ -168,6 +173,7 @@ async function main(){
168173 } ;
169174 reader . readAsArrayBuffer ( file ) ;
170175 }
176+ event . target . value = "" ;
171177 } )
172178 document . getElementById ( "observations" ) . addEventListener ( "keydown" , ( e ) => {
173179 if ( e . key === "Enter" ) {
0 commit comments