![](/img/trans.png)
[英]How can I reset a while loop in python so that all conditions are the same as when it started?
[英]How can I structure this while loop so that it checks for two conditions and also termaintes it when necessary
我這里有這段代碼
def step(self, joint_values):
"""
Step function which publishes actions and evaluates state/reward.
Args:
action: takes an action (Int) as input. Value from 0-6
Returns:
State after executed action, reward after executed action and done,
which is a bool, True or False depending on if episode is done.
"""
self.iterator += 1
self._coll = self.observation_callback_collision()
coll_force = self._coll.wrench.force.x
if coll_force >= 150 or coll_force <= -150:
self.reset_robot2.call()
self.reward_coll = -0.5
self.collided += 1
print("#############Collision##############")
rospy.sleep(1)
#print("ACTION", joint_values)
else:
i = 0
while i<=1000:
self.traj.header.stamp = rospy.Time.now()
action = JointTrajectoryPoint()
action.positions = joint_values
#print("W", action.positions)
action.time_from_start = rospy.Duration(0.7)
self.traj.points = []
self.traj.points.append(action)
#rospy.sleep(4.5)
self.action_pub.publish(self.traj)
i +=0.1
如果您在這里看到我想要實現的是,每當調用步驟 function 時,它都會檢查 if 條件(如果它為真,那么它會重置機器人,否則它會激活 while 循環)。 問題是當 RL 代理選擇一個動作“joint_values”時,當調用步驟 function 時,if con 只檢查一次,如果代理選擇的動作不好,機器人就會發生碰撞。
我想要的是,當步驟 function 被稱為 while 循環時 i<= 1000在 i += 1 處終止時,還會每次檢查碰撞,即coll_force >= 150 或 coll_force <= -150:以及如果碰撞超過了停止發布動作 (self.action_pub.publish(self.traj)) 並重置機器人 (self.reset_robot2.call()) 的限制。
我知道這有點令人困惑,但我想做的是這樣的,
def step(self, joint_values):
self.iterator += 1
self._coll = self.observation_callback_collision()
coll_force = self._coll.wrench.force.x
while i<=1000:
if coll_force >= 150 or coll_force <= -150:
self.reset_robot2.call()
self.reward_coll = -0.5
self.collided += 1
print("#############Collision##############")
rospy.sleep(1)
else:
self.traj.header.stamp = rospy.Time.now()
action = JointTrajectoryPoint()
action.positions = joint_values
#print("W", action.positions)
action.time_from_start = rospy.Duration(0.7)
self.traj.points = []
self.traj.points.append(action)
self.action_pub.publish(self.traj)
i +=0.1
但這似乎無法正常工作
通常,當您要檢查循環中的多個條件時,可以使用累積標志來指示循環是否應該終止。 還有其他選項,例如break
或將i
設置為范圍之外的值,但為了便於閱讀,最好使用該標志:
is_running = True
while is_running:
# do stuff here, calculate coll_force
i += 0.1
is_running = is_running and (coll_force > -150 and coll_force < 150)
is_running = is_running and i <= 1000
這樣,您可以使用任意數量的條件,每個條件都可以與其他條件分開處理,並且您有一個明顯的位置可以閱讀檢查條件的代碼,即在循環塊的末尾。
我喜歡艾蒂安的回答。 並建議您按照他的建議構建您的 function。 或者,這是對您的第一次嘗試的修改,它應該完全按照您的意願進行。
我添加了一個reset_one
state 作為if
語句要滿足的第二個條件。 如果if
被調用過一次,它將被設置為 true 並且不允許if
捕獲,只允許調用else
。
或者,如果您允許一定數量的重置,您可以使用我已注釋掉的reset_count
變量。
def step(self, joint_values):
self.iterator += 1
self._coll = self.observation_callback_collision()
coll_force = self._coll.wrench.force.x
# Add has_been_reset boolean state.
reset_once = False
# reset_count = 0 # Alternative.
while i <= 1000:
# Added second condition. Modify condition if you allow a certain
# amount of resets during the while loop. Here it allows the if
# condition to be triggered ONCE (Use reset_count <= n_allowed
# alternatively)
if (coll_force >= 150 or coll_force <= -150) and not reset_once:
self.reset_robot2.call()
self.reward_coll = -0.5
self.collided += 1
print("#############Collision##############")
rospy.sleep(1)
reset_once = True # <--------------
# reset_count += 1 # <-- Alternative
else:
self.traj.header.stamp = rospy.Time.now()
action = JointTrajectoryPoint()
action.positions = joint_values
# print("W", action.positions)
action.time_from_start = rospy.Duration(0.7)
self.traj.points = []
self.traj.points.append(action)
self.action_pub.publish(self.traj)
i += 0.1
編輯:錯字*
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.