首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PyDrake中使用autodiff时无法检查不可行的约束

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

Stack Overflow用户
提问于 2022-01-14 21:05:48
回答 1查看 69关注 0票数 1

我正在用SNOPT解决PyDrake中的一个问题,我得到的解决方案看起来很合理,但是当我做result.is_success()时,它又返回了False,所以我希望研究为什么它认为这个问题没有解决。我假设我在某个地方有一个糟糕的约束,所以我使用以下代码来完成这个任务:

代码语言:javascript
复制
result = mp.Solve(prog)
if not result.is_success():
    print("INFEASIBLE:")
    infeasible = result.GetInfeasibleConstraints(prog)
    for c in infeasible:
        print(c)

但是,它退出对result.GetInfeasibleConstraints(prog)的调用时会出现以下错误:

代码语言:javascript
复制
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.

它说的是真的,因为我的约束函数正在利用Drake中的autodiff功能,所以它们使用dtype=AutoDiffXd返回数组。如果是这样的话,这是否意味着我不能使用约束不可行性检查器?当我使用autodiff时,对于检查不可行的约束有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-14 21:21:56

我想您应该使用python函数来编写约束。我建议编写这个python函数来处理float和autodiffxd,所以类似这样

代码语言:javascript
复制
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 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70716513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档