简体   繁体   English

在 PyDrake 中使用 autodiff 时无法检查不可行的约束

[英]Unable to check infeasible constraints when using autodiff in PyDrake

I am solving a problem in PyDrake with SNOPT and I get solutions that look reasonable, but when I do result.is_success() it comes back with False , so I am hoping to investigate why it thinks the problem wasn't solved.我正在使用 SNOPT 解决 PyDrake 中的一个问题,我得到了看起来合理的解决方案,但是当我执行result.is_success()时,它会返回False ,所以我希望调查为什么它认为问题没有解决。 I assume I have a bad constraint somewhere, so I'm doing this with the following code:我假设我在某处有一个不好的约束,所以我使用以下代码执行此操作:

result = mp.Solve(prog)
if not result.is_success():
    print("INFEASIBLE:")
    infeasible = result.GetInfeasibleConstraints(prog)
    for c in infeasible:
        print(c)

However, it exits on the call to result.GetInfeasibleConstraints(prog) with the following error:但是,它在调用result.GetInfeasibleConstraints(prog)时退出,并出现以下错误:

Traceback (most recent call last):
  File "test_drake_distribution.py", line 250, in <module>
    infeasible = result.GetInfeasibleConstraints(prog)
  File "/home/adam/.miniconda3/envs/drake/lib/python3.6/site-packages/pydrake/solvers/_mathematicalprogram_extra.py", line 34, in _check_array_type
    f"{var_name} must be of scalar type {expected_name}, but unable "
RuntimeError: PyFunctionConstraint: Output must be of scalar type float, but unable to infer scalar type.

What it says is true, because my constraint functions are utilizing the autodiff functionality in Drake, so they are returning arrays with dtype=AutoDiffXd .它说的是真的,因为我的约束函数正在利用 Drake 中的 autodiff 功能,所以它们返回 arrays 和dtype=AutoDiffXd If this is the case, does that mean I cannot use the constraint infeasibility checker?如果是这种情况,这是否意味着我不能使用约束不可行性检查器? Any advice on being able to check infeasible constraints when I'm using autodiff?关于在使用 autodiff 时能够检查不可行约束的任何建议?

I suppose you write your constraint using a python function.我想您使用 python function 编写约束。 I would suggest to write this python function to handle both float and autodiffxd, so something like this我建议写这个 python function 来处理浮动和 autodiffxd,所以像这样

def my_evaluator(x: np.ndarray):
    if x.dtype == np.object:
        # This is the autodiff case
    elif x.dtype == np.float:
        # This is the float case 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM