Organizing model.printAttr('x') output.
回答済みI'm reading in a model and I wish to see the non-zero solutions with:
model.printAttr('x')
This gives me the code below, which is great, but for models with many more solutions, I'd like it to be more organized.
Variable x ------------------------- LN1 1 LN19 1 LN25 1 LN27 1 LN35 1 LN38 1
#So I'm wondering if it is possible to organize the following output as a python list:
[1,19,25,27,35,38] #desired output
0
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
You can get a list of all the x-values with model.getAttr('x'). However, the output produced by printAttr only contains the variables with non-zero values, the result of getAttr will contain all values including the zeroes.
If all your variables are binaries, you could get the indices of all variables with value 1 as follows:
output = [i for i, x in enumerate(m.getAttr("x")) if x > 0.5]
1
投稿コメントは受け付けていません。
コメント
2件のコメント