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



我正在从Russ Tedrake的机器人操纵课程中学习练习3.9(虚拟墙),当我试图添加末端执行器位置约束时,我正在接收上面的运行时错误。我几乎一整天都在纠结这个问题,所以我屈服了,决定发布一下。另一个帖子也有类似的错误,但这还不足以让我看到哪里出了问题。我会尽可能少地提供必要的信息,以避免展示我对问题的解决方案。

下面的代码缺少了一些行。

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., 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'>

嗯…我窘迫。但是我想你在我们的python绑定中发现了一个bug。

from pydrake.all import Variable, le, ge
x = Variable("x")
print(2 <= x**2)
print(x**2 <= 2)
print(le(x**2, 2))
#print(le(2, x**2))  <== this crashes
print(ge(x**2, 2))
#print(ge(2, x**2))  <== this too.

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

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

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

中描述的PositionConstraint

最新更新