获取二进制变量数量 / Drake 中的 Gurobi 输出

huangapple go评论91阅读模式
英文:

Get number of binary variables / Gurobi output in Drake

问题

我如何获取我的数学程序中的二进制变量数量?

我如何获取所有聚合二进制变量的向量引用?

此外,我如何获取Gurobi的标准输出(这也可以告诉我二进制变量的数量)?示例在这里:https://stackoverflow.com/questions/70826732/how-to-get-how-many-binary-variables-used-by-gurobi

非常感谢。

英文:

How do I get the number of binary variables to my mathematical program?

How do I get a reference to a vector of all the aggregate binary variables?

Also, how do I get the Gurobi std output (this could also tell the number of binary variables)? Example here: https://stackoverflow.com/questions/70826732/how-to-get-how-many-binary-variables-used-by-gurobi

Many thanks.

答案1

得分: 3

你需要循环遍历变量并检查哪些是二进制的:

from pydrake.solvers import MathematicalProgram
from pydrake.symbolic import Variable

prog = MathematicalProgram()
x = prog.NewContinuousVariables(2)
b = prog.NewBinaryVariables(3)

binaries = [
    x for x in prog.decision_variables() if x.get_type() == Variable.Type.BINARY
]
print(len(binaries))
英文:

You'll need to loop through the variables and check which are binary:

from pydrake.solvers import MathematicalProgram
from pydrake.symbolic import Variable

prog = MathematicalProgram()
x = prog.NewContinuousVariables(2)
b = prog.NewBinaryVariables(3)

binaries = [
    x for x in prog.decision_variables() if x.get_type() == Variable.Type.BINARY
]
print(len(binaries))

huangapple
  • 本文由 发表于 2023年6月26日 09:45:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定