@@ -87,6 +87,7 @@ def get_context_features() -> dict[str, ContextFeature]:
8787
8888 def _update_context (self ) -> None :
8989 self .env : bipedal_walker .BipedalWalker
90+ self .context = CARLBipedalWalker .get_context_space ().insert_defaults (self .context )
9091 bpw .FPS = self .context ["FPS" ]
9192 bpw .SCALE = self .context ["SCALE" ]
9293 bpw .FRICTION = self .context ["FRICTION" ]
@@ -153,94 +154,3 @@ def _update_context(self) -> None:
153154 )
154155
155156 self .env .world .gravity = gravity
156-
157-
158- def demo_heuristic (env : CARLBipedalWalker | bipedal_walker .BipedalWalker ) -> None :
159- env .reset ()
160- steps = 0
161- total_reward = 0
162- a = np .array ([0.0 , 0.0 , 0.0 , 0.0 ])
163- STAY_ON_ONE_LEG , PUT_OTHER_DOWN , PUSH_OFF = 1 , 2 , 3
164- SPEED = 0.29 # Will fall forward on higher speed
165- state = STAY_ON_ONE_LEG
166- moving_leg = 0
167- supporting_leg = 1 - moving_leg
168- SUPPORT_KNEE_ANGLE = + 0.1
169- supporting_knee_angle = SUPPORT_KNEE_ANGLE
170- while True :
171- s , r , terminated , truncated , info = env .step (a )
172- s = s ["state" ]
173- total_reward += r
174- if steps % 20 == 0 or terminated or truncated :
175- print ("\n action " + str (["{:+0.2f}" .format (x ) for x in a ]))
176- print ("step {} total_reward {:+0.2f}" .format (steps , total_reward ))
177- print ("hull " + str (["{:+0.2f}" .format (x ) for x in s [0 :4 ]]))
178- print ("leg0 " + str (["{:+0.2f}" .format (x ) for x in s [4 :9 ]]))
179- print ("leg1 " + str (["{:+0.2f}" .format (x ) for x in s [9 :14 ]]))
180- steps += 1
181-
182- contact0 = s [8 ] # noqa: F841
183- contact1 = s [13 ] # noqa: F841
184- moving_s_base = 4 + 5 * moving_leg
185- supporting_s_base = 4 + 5 * supporting_leg
186-
187- hip_targ = np .array ([None , None ]) # -0.8 .. +1.1
188- knee_targ = np .array ([None , None ]) # -0.6 .. +0.9
189- hip_todo = np .array ([0.0 , 0.0 ])
190- knee_todo = np .array ([0.0 , 0.0 ])
191-
192- if state == STAY_ON_ONE_LEG :
193- hip_targ [moving_leg ] = 1.1
194- knee_targ [moving_leg ] = - 0.6
195- supporting_knee_angle += 0.03
196- if s [2 ] > SPEED :
197- supporting_knee_angle += 0.03
198- supporting_knee_angle = min (supporting_knee_angle , SUPPORT_KNEE_ANGLE )
199- knee_targ [supporting_leg ] = supporting_knee_angle
200- if s [supporting_s_base + 0 ] < 0.10 : # supporting leg is behind
201- state = PUT_OTHER_DOWN
202- if state == PUT_OTHER_DOWN :
203- hip_targ [moving_leg ] = + 0.1
204- knee_targ [moving_leg ] = SUPPORT_KNEE_ANGLE
205- knee_targ [supporting_leg ] = supporting_knee_angle
206- if s [moving_s_base + 4 ]:
207- state = PUSH_OFF
208- supporting_knee_angle = min (s [moving_s_base + 2 ], SUPPORT_KNEE_ANGLE )
209- if state == PUSH_OFF :
210- knee_targ [moving_leg ] = supporting_knee_angle
211- knee_targ [supporting_leg ] = + 1.0
212- if s [supporting_s_base + 2 ] > 0.88 or s [2 ] > 1.2 * SPEED :
213- state = STAY_ON_ONE_LEG
214- moving_leg = 1 - moving_leg
215- supporting_leg = 1 - moving_leg
216-
217- if hip_targ [0 ]:
218- hip_todo [0 ] = 0.9 * (hip_targ [0 ] - s [4 ]) - 0.25 * s [5 ]
219- if hip_targ [1 ]:
220- hip_todo [1 ] = 0.9 * (hip_targ [1 ] - s [9 ]) - 0.25 * s [10 ]
221- if knee_targ [0 ]:
222- knee_todo [0 ] = 4.0 * (knee_targ [0 ] - s [6 ]) - 0.25 * s [7 ]
223- if knee_targ [1 ]:
224- knee_todo [1 ] = 4.0 * (knee_targ [1 ] - s [11 ]) - 0.25 * s [12 ]
225-
226- hip_todo [0 ] -= 0.9 * (0 - s [0 ]) - 1.5 * s [1 ] # PID to keep head strait
227- hip_todo [1 ] -= 0.9 * (0 - s [0 ]) - 1.5 * s [1 ]
228- knee_todo [0 ] -= 15.0 * s [3 ] # vertical speed, to damp oscillations
229- knee_todo [1 ] -= 15.0 * s [3 ]
230-
231- a [0 ] = hip_todo [0 ]
232- a [1 ] = knee_todo [0 ]
233- a [2 ] = hip_todo [1 ]
234- a [3 ] = knee_todo [1 ]
235- a = np .clip (0.5 * a , - 1.0 , 1.0 )
236-
237- env .render ()
238- if terminated or truncated :
239- break
240-
241-
242- if __name__ == "__main__" :
243- # Heurisic: suboptimal, have no notion of balance.
244- env = CARLBipedalWalker ()
245- demo_heuristic (env )
246- env .close ()
0 commit comments