线性不等式约束在Drake中不起作用

2024-09-27 17:34:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习如何使用Drake来解决优化问题。 这个问题是为了找到栅栏的最佳长度和宽度,栅栏的周长必须小于或等于40。以下代码仅在周长约束为相等约束时有效。它应该作为一个不等式约束,但我的最优解是x=[nan]。有人知道为什么会这样吗

from pydrake.solvers.mathematicalprogram import MathematicalProgram, Solve
import numpy as np
import matplotlib.pyplot as plt

prog = MathematicalProgram()

#add two decision variables
x = prog.NewContinuousVariables(2, "x")

#adds objective function where
#
# min 0.5 xt * Q * x + bt * x
#
# Q = [0,-1
#      -1,0]
#
# bt = [0,
#      0]
#
Q = [[0,-1],[-1,0]]
b = [[0],[0]]
prog.AddQuadraticCost(Q , b, vars=[x[0],x[1]])

# Adds the linear constraints.
prog.AddLinearEqualityConstraint(2*x[0] + 2*x[1] == 40)
#prog.AddLinearConstraint(2*x[0] + 2*x[1] <= 40)
prog.AddLinearConstraint(0*x[0] + -1*x[1] <= 0)
prog.AddLinearConstraint(-1*x[0] + 0*x[1] <= 0)

# Solve the program.
result = Solve(prog)
print(f"optimal solution x: {result.GetSolution(x)}")

Tags: the代码import宽度asresultnandrake
1条回答
网友
1楼 · 发布于 2024-09-27 17:34:35

对于不等式约束和等式约束,我得到[nan,nan]

正如Russ提到的,问题在于成本是非凸的,Drake使用了错误的解算器。目前,我建议明确指定一个解算器。你可以

from pydrake.solvers.ipopt_solver import IpoptSolver
from pydrake.solvers.mathematicalprogram import MathematicalProgram, Solve
import numpy as np
import matplotlib.pyplot as plt

prog = MathematicalProgram()

#add two decision variables
x = prog.NewContinuousVariables(2, "x")

#adds objective function where
#
# min 0.5 xt * Q * x + bt * x
#
# Q = [0,-1
#      -1,0]
#
# bt = [0,
#      0]
#
Q = [[0,-1],[-1,0]]
b = [[0],[0]]
prog.AddQuadraticCost(Q , b, vars=[x[0],x[1]])

# Adds the linear constraints.
prog.AddLinearEqualityConstraint(2*x[0] + 2*x[1] == 40)
#prog.AddLinearConstraint(2*x[0] + 2*x[1] <= 40)
prog.AddLinearConstraint(0*x[0] + -1*x[1] <= 0)
prog.AddLinearConstraint(-1*x[0] + 0*x[1] <= 0)

# Solve the program.
solver = IpoptSolver()
result = solver.Solve(prog)
print(f"optimal solution x: {result.GetSolution(x)}")

我将在Drake方面进行修复,以确保在具有非凸二次成本时,它会产生正确的解算器

相关问题 更多 >

    热门问题