英文:
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论