GetInfeasibleConstraints runtime error: Output must be of scalar type float

GetInfeasibleConstraints runtime error: Output must be of scalar type float

我正在尝试使用以下命令调试我的 MP

solver = IpoptSolver()
result = solver.Solve(prog)
result.GetInfeasibleConstraints(prog)

但是,我收到以下错误:

PyFunctionConstraint: Output must be of scalar type float. Got AutoDiffXd instead.

我添加了与 compass gait example.

中类似的方式的 autodiff 约束

例如

plant_autodiff = plant.ToAutoDiffXd()
def eq7h(q_v_r):
    q, v, r = np.split(q_v_r, [
        plant.num_positions(),
        plant.num_positions() + plant.num_velocities()])
    context = plant_autodiff.CreateDefaultContext()
    plant_autodiff.SetPositions(context, q)
    plant_autodiff.SetVelocities(context, v)
    return plant_autodiff.CalcCenterOfMassPosition(context) - r
prog.AddConstraint(eq7h, lb=[0]*3, ub=[0]*3, vars=np.concatenate([q[k], v[k], r[k]]))

如何正确使用GetInfeasibleConstraints

GetInfeasibleConstraints 要求使用双精度向量评估约束。所以我会re-write你的约束为

def eq7h(q_v_r):
    q, v, r = np.split(q_v_r, [
        plant.num_positions(),
        plant.num_positions() + plant.num_velocities()])
    # Select plant based on the data type of q_v_r. If the data type is autodiff (np.object in this case), then use plant_autodiff, otherwise use plant
    plant_eval = plant_autodiff if q_v_r.dtype == np.object else plant
    context = plant_eval.CreateDefaultContext()
    plant_eval.SetPositions(context, q)
    plant_eval.SetVelocities(context, v)
    return plant_eval.CalcCenterOfMassPosition(context) - r