How can I count the zero number in my decsion variable?
ユーザーの入力を待っています。I want to count how many zeros in hire[], and get num_hire for the numbers of zero, so I tried to use the following code. But bug come out. How can I count them?

-
正式なコメント
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Dear Tangzhe,
based on the description you provided it is difficult to give specific advice. Could you please share a minimal reproducible example of your problem?
Best regards
Jonasz0 -
hire[j] is a decision variable for how many workers are hired in j month in a year in my code. and num_hire, I would like to treat it as a label, counting how many times for the company hire worker in a year. For example, if hire[j]=[0,0,0,0,0,1,2,3,4,5,6,7] in a year (j represents the month) (the company hire 1 person in June and 7 people in Dec. etc.) then the variable num_hire should be 7,cause the company hire people 7 times in this year.
0 -
Here is how I define the decision variable Hire[j] Besides, J is the month in a year
0 -
Thanks for these clarifications. In the future please paste the code rather than the screenshot - you can't really copy anything out from a picture...
My understanding is that you would be interested in knowing how many variables were assigned a value of 0 in the optimal solution to your problem. This can be done by checking the parameter x of the respective variable.
In your case, the simplified code (not accounting for integer tolerances) would look something like:
num_hire = 0
for j in J:
if hire[j].X == 0:
num_hire += 1If that's not what you're looking for, we would require a complete description of the model you are trying to implement to give further guidance.
Best regards
Jonasz0
投稿コメントは受け付けていません。
コメント
5件のコメント