微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

RuntimeError: 你不应该在 `Formula` 上调用 `__bool__` / `__nonzero__`

如何解决RuntimeError: 你不应该在 `Formula` 上调用 `__bool__` / `__nonzero__`

我正在研究 Russ Tedrake 机器人操作课程中的练习 3.9(虚拟墙),并且在尝试添加末端执行器位置约束时收到上述运行时错误。我几乎整天都被困在这个问题上,所以我屈服并决定发帖。另一个 post 也有类似的错误,但我还不足以看出哪里出了问题。我会尽量提供尽可能少的信息,以避免展示我对问题的解决方案。

这是缺少几行的代码

prog = MathematicalProgram()
      v = prog.NewContinuousVariables(7,'joint_veLocities')
      v_max = 3.0 # do not modify
      h = 4e-3 # do not modify
      lower_bound = np.array([-0.3,-1.0,-1.0]) # do not modify
      upper_bound = np.array([0.3,1.0,1.0]) # do not modify

在下文中,'p' 是依赖于 'v' 元素的符号表达式数组。

  for i in range(p.shape[0]):
    prog.AddConstraint(le(lower_bound[i],p[i]))
    prog.AddConstraint(ge(upper_bound[i],p[i]))

  solver = SnoptSolver()
  result = solver.solve(prog)
    
  if not (result.is_success()):
    raise ValueError("Could not find the optimal solution.")

  v_solution = result.GetSolution(v)
  return v_solution

这是错误信息。 for 循环中的第一行是发生错误的地方。

RuntimeError                              Traceback (most recent call last)
<ipython-input-108-b73c5373ad6c> in <module>()
      1 V_d = np.array([0.0,0.,0.1,0.05,0])
----> 2 simulator = BuildAndSimulate(DiffIKQP_Wall,V_d)

5 frames
/usr/local/lib/python3.7/dist-packages/numpy/lib/function_base.py in _get_ufunc_and_otypes(self,func,args)
   2144 
   2145             inputs = [arg.flat[0] for arg in args]
-> 2146             outputs = func(*inputs)
   2147 
   2148             # Performance note: profiling indicates that -- for simple

RuntimeError: You should not call `__bool__` / `__nonzero__` on `Formula`. If you are trying to make a map with `Variable`,`Expression`,or `polynomial` as keys (and then access the map in Python),please use pydrake.common.containers.EqualToDict`.

如果有任何建议或需要更多信息,请告诉我。

谢谢!

编辑:

我认为这些信息可能会有所帮助。运行

print(type(lower_bound[i]))
print(type(p[i]))

返回

<class 'numpy.float64'>
<class 'pydrake.symbolic.Expression'>

解决方法

顺便说一句,我建议不要在这里使用约束的符号版本。在我看来,您的符号表达式 p 很复杂(即它不是决策变量的线性表达式)。添加约束的这种符号公式会减慢求解器的计算速度,因为求解器需要在每次迭代中评估符号表达式。如果p是机器人链接的位置,那么我建议使用https://drake.mit.edu/pydrake/pydrake.multibody.inverse_kinematics.html?highlight=positionconstraint#pydrake.multibody.inverse_kinematics.PositionConstraint

中描述的PositionConstraint ,

嗯……我很尴尬。但我认为您在我们的 Python 绑定中发现了一个错误。

import dotenv from 'dotenv/config';

我已打开:https://github.com/RobotLocomotion/drake/issues/15549

但现在,请选择任何其他方式来编写相同的约束?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。