Skip to main content

How can I count the zero number in my decsion variable?

Awaiting user input

Comments

5 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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.
  • Jonasz Staszek
    • Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    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
    Jonasz

    0
  • Tangzhe Chen
    • Gurobi-versary
    • First Comment
    • First Question

    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
  • Tangzhe Chen
    • Gurobi-versary
    • First Comment
    • First Question
    Here is how I define the decision variable Hire[j]  Besides, J is the month in a year 
    0
  • Jonasz Staszek
    • Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    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 += 1

    If 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
    Jonasz

     

    0

Post is closed for comments.