Skip to main content

how to simply count number of nodes based of binary output variables

Answered

Comments

4 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.
  • Jaromił Najman
    • Gurobi Staff

    It looks like in your case, you have to retrieve the information you seek from variable names.

    You can split the variable names at the \(\texttt{'_'}\) character and use this information to do whatever you are trying to do,

    data = varX[j].VarName.split("_")
    print(data)

    For \(\texttt{"x_19_5"}\) the above will give you

    ['x', '19', '5']

    Best regards, 
    Jaromił

     

    0
  • Nahom Tsehaie
    • Gurobi-versary
    • First Comment

    Hi Jaromil,

    Thanks for your reply.

    However, it is not the solution I'm looking for. 

    In the example you gave (about x_19_5) this means node 19 has to be colored with ' number 5'. This is because the output value is only for this color 1.0 (and for the other 'colors/number it is 0.0). For eg. x_19_4 the output is 0.0 for 'number 4', so this means the node can not be colored with color 'number4'.  So for every node this means (x_XX_YY) where XX is the number of node and YY is the possible colors it can be, there is for every node just one output value 1. I want to store these values with the right color. So in my output example, I want to store the following: 

    node 1 (x_1_ YY) color 4 (since out of the colors 0 till 5, the output value is 1.0 at color 4 (YY = 4))

    node 2 (x_2_ YY)  color 4 (since out of the colors 0 till 5, the output value is 1.0 at color 4 (YY = 4))

    node 3  (x_3_YY) color 0 (since out of the colors 0 till 5, the output value is 1.0 at color 0 (YY = 0))

    node 4 (x_4_YY) color 5 (since out of the colors 0 till 5, the output value is 1.0 at color 5 (YY = 5))

    etc etc

    So in the end I would like to have an array with the number [4,4,0,5 etc etc] with shape [1,20] for this example. 

     

    Thank you very very much for your time and It would mean a lot if you could help me out! 

    kind regards,

    Nahom

     

     

    0
  • Jaromił Najman
    • Gurobi Staff

    Hi Nahom,

    I don't understand why you cannot use the information you have together with the string splitting.

    varX = m.getVars()
    nodecolordict = {}
    for j in range(n):
    varname = varX[j].varName
    if "x" not in varname:
    continue # avoid y

     data = varname.split("_")
    color = int(data[2])
    node = int(data[1])
    nodecolordict[color,node] = varX[j].X

    The above snippet constructs and dictionary which you can access via node-color indicies, e.g., \(\texttt{nodecolordict[19,5]}\), and will provide either 0 or 1 which means that the given node has a given color or not. I think with this dict you can construct whatever list or data structure you need.

    Best regards, 
    Jaromił

    0

Post is closed for comments.