我正在学习如何使用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)}")
对于不等式约束和等式约束,我得到[nan,nan]
正如Russ提到的,问题在于成本是非凸的,Drake使用了错误的解算器。目前,我建议明确指定一个解算器。你可以
我将在Drake方面进行修复,以确保在具有非凸二次成本时,它会产生正确的解算器
相关问题 更多 >
编程相关推荐