how to simply count number of nodes based of binary output variables
AnsweredThis is probably a stupid question, but I can not fix it. Output of gurobi gives me zero and ones as output for the coloring problem.
I want to use the output of a gurobi optimizer to give nodes colors. So instead every nodes just get zeros and one, I wan to give nodes with value zero, zero (no colors. But output with value one, I want to give the first 'color' 1, the second color '2', the third color '3' etc.
I wrote a code, but I does not function:
varY = m.getAttr("x") #these are the values of output of gurobi, containing 0's and 1's`
varX = m.getVars()
def updateLabelBits(varY):
label_dict = {}
label_num = 0
for j in range(n):
if (varY[j] == 0):
varY[j] = 0 # so if value of out put is zero, than varY remains zero
else:
if (varY[j]) in label_dict:
varY[j] = label_dict[varY[j]]
else:
label_dict[varY[j]] = label_num #if the value is already in the label_dict, no new label/color has to be made
varY[j] = label_num
label_num = label_num + 1
return varY, label_num
updateLabelBits(varY)
so if we use the output from down below, we can see the graph (n =20 nodes) can be colored with 3 colors. node 0 has color 5, node 1 has color 4,nodes 2 has color 4 etc.
extra info: chromatic number of this graph is 3.0 (n (nodes) = 20, k (max degree) = 5)
output VarX :[
<gurobi.Var y_0 (value 1.0)>,
<gurobi.Var y_1 (value 0.0)>,
<gurobi.Var y_2 (value 0.0)>,
<gurobi.Var y_3 (value 0.0)>,
<gurobi.Var y_4 (value 1.0)>,
<gurobi.Var y_5 (value 1.0)>,
<gurobi.Var x_0_0 (value 0.0)>,
<gurobi.Var x_0_1 (value -0.0)>,
<gurobi.Var x_0_2 (value 0.0)>,
<gurobi.Var x_0_3 (value -0.0)>,
<gurobi.Var x_0_4 (value -0.0)>,
<gurobi.Var x_0_5 (value 1.0)>,
<gurobi.Var x_1_0 (value 0.0)>,
<gurobi.Var x_1_1 (value 0.0)>,
<gurobi.Var x_1_2 (value -0.0)>,
<gurobi.Var x_1_3 (value -0.0)>,
<gurobi.Var x_1_4 (value 1.0)>,
<gurobi.Var x_1_5 (value 0.0)>,
<gurobi.Var x_2_0 (value 0.0)>,
<gurobi.Var x_2_1 (value 0.0)>,
<gurobi.Var x_2_2 (value 0.0)>,
<gurobi.Var x_2_3 (value 0.0)>,
<gurobi.Var x_2_4 (value 1.0)>,
<gurobi.Var x_2_5 (value 0.0)>,
<gurobi.Var x_3_0 (value 1.0)>,
<gurobi.Var x_3_1 (value 0.0)>,
<gurobi.Var x_3_2 (value 0.0)>,
<gurobi.Var x_3_3 (value -0.0)>,
<gurobi.Var x_3_4 (value -0.0)>,
<gurobi.Var x_3_5 (value -0.0)>,
<gurobi.Var x_4_0 (value 0.0)>,
<gurobi.Var x_4_1 (value 0.0)>,
<gurobi.Var x_4_2 (value -0.0)>,
<gurobi.Var x_4_3 (value 0.0)>,
<gurobi.Var x_4_4 (value 0.0)>,
<gurobi.Var x_4_5 (value 1.0)>,
<gurobi.Var x_5_0 (value 0.0)>,
<gurobi.Var x_5_1 (value 0.0)>,
<gurobi.Var x_5_2 (value 0.0)>,
<gurobi.Var x_5_3 (value 0.0)>,
<gurobi.Var x_5_4 (value 1.0)>,
<gurobi.Var x_5_5 (value -0.0)>,
<gurobi.Var x_6_0 (value 1.0)>,
<gurobi.Var x_6_1 (value -0.0)>,
<gurobi.Var x_6_2 (value 0.0)>,
<gurobi.Var x_6_3 (value 0.0)>,
<gurobi.Var x_6_4 (value 0.0)>,
<gurobi.Var x_6_5 (value -0.0)>,
<gurobi.Var x_7_0 (value 0.0)>,
<gurobi.Var x_7_1 (value 0.0)>,
<gurobi.Var x_7_2 (value 0.0)>,
<gurobi.Var x_7_3 (value 0.0)>,
<gurobi.Var x_7_4 (value 0.0)>,
<gurobi.Var x_7_5 (value 1.0)>,
<gurobi.Var x_8_0 (value 0.0)>,
<gurobi.Var x_8_1 (value 0.0)>,
<gurobi.Var x_8_2 (value 0.0)>,
<gurobi.Var x_8_3 (value 0.0)>,
<gurobi.Var x_8_4 (value 1.0)>,
<gurobi.Var x_8_5 (value 0.0)>,
<gurobi.Var x_9_0 (value 1.0)>,
<gurobi.Var x_9_1 (value -0.0)>,
<gurobi.Var x_9_2 (value 0.0)>,
<gurobi.Var x_9_3 (value 0.0)>,
<gurobi.Var x_9_4 (value -0.0)>,
<gurobi.Var x_9_5 (value 0.0)>,
<gurobi.Var x_10_0 (value -0.0)>,
<gurobi.Var x_10_1 (value 0.0)>,
<gurobi.Var x_10_2 (value 0.0)>,
<gurobi.Var x_10_3 (value -0.0)>,
<gurobi.Var x_10_4 (value 0.0)>,
<gurobi.Var x_10_5 (value 1.0)>,
<gurobi.Var x_11_0 (value 0.0)>,
<gurobi.Var x_11_1 (value 0.0)>,
<gurobi.Var x_11_2 (value 0.0)>,
<gurobi.Var x_11_3 (value -0.0)>,
<gurobi.Var x_11_4 (value 1.0)>,
<gurobi.Var x_11_5 (value 0.0)>,
<gurobi.Var x_12_0 (value 1.0)>,
<gurobi.Var x_12_1 (value -0.0)>,
<gurobi.Var x_12_2 (value -0.0)>,
<gurobi.Var x_12_3 (value -0.0)>,
<gurobi.Var x_12_4 (value 0.0)>,
<gurobi.Var x_12_5 (value 0.0)>,
<gurobi.Var x_13_0 (value 0.0)>,
<gurobi.Var x_13_1 (value 0.0)>,
<gurobi.Var x_13_2 (value 0.0)>,
<gurobi.Var x_13_3 (value 0.0)>,
<gurobi.Var x_13_4 (value 0.0)>,
<gurobi.Var x_13_5 (value 1.0)>,
<gurobi.Var x_14_0 (value 1.0)>,
<gurobi.Var x_14_1 (value 0.0)>,
<gurobi.Var x_14_2 (value 0.0)>,
<gurobi.Var x_14_3 (value -0.0)>,
<gurobi.Var x_14_4 (value 0.0)>,
<gurobi.Var x_14_5 (value 0.0)>,
<gurobi.Var x_15_0 (value 0.0)>,
<gurobi.Var x_15_1 (value -0.0)>,
<gurobi.Var x_15_2 (value -0.0)>,
<gurobi.Var x_15_3 (value -0.0)>,
<gurobi.Var x_15_4 (value 1.0)>,
<gurobi.Var x_15_5 (value 0.0)>,
<gurobi.Var x_16_0 (value -0.0)>,
<gurobi.Var x_16_1 (value -0.0)>,
<gurobi.Var x_16_2 (value 0.0)>,
<gurobi.Var x_16_3 (value 0.0)>,
<gurobi.Var x_16_4 (value 0.0)>,
<gurobi.Var x_16_5 (value 1.0)>,
<gurobi.Var x_17_0 (value 1.0)>,
<gurobi.Var x_17_1 (value 0.0)>,
<gurobi.Var x_17_2 (value 0.0)>,
<gurobi.Var x_17_3 (value -0.0)>,
<gurobi.Var x_17_4 (value 0.0)>,
<gurobi.Var x_17_5 (value 0.0)>,
<gurobi.Var x_18_0 (value 0.0)>,
<gurobi.Var x_18_1 (value -0.0)>,
<gurobi.Var x_18_2 (value 0.0)>,
<gurobi.Var x_18_3 (value -0.0)>,
<gurobi.Var x_18_4 (value 1.0)>,
<gurobi.Var x_18_5 (value -0.0)>,
<gurobi.Var x_19_0 (value 0.0)>,
<gurobi.Var x_19_1 (value 0.0)>,
<gurobi.Var x_19_2 (value 0.0)>,
<gurobi.Var x_19_3 (value 0.0)>,
<gurobi.Var x_19_4 (value 0.0)>,
<gurobi.Var x_19_5 (value 1.0)>]
-
Official comment
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. -
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 -
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 -
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].XThe 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.
Comments
4 comments