mardi 2 janvier 2018

Many combinations multi-line conditions of the 'if' in a loop

Here is a fragment of the code responsible for creating the graph and its edges, depending on whether the edge exists and the condition that validates the shortest paths:

for q in range(len(aaa_binary)):

    if len(added)!=i+1:
       g.add_nodes_from (aaa_binary[q])
       t1 = (aaa_binary[q][0],aaa_binary[q][1])
       t2 = (aaa_binary[q][1],aaa_binary[q][2])
       t3 = (aaa_binary[q][2],aaa_binary[q][3])
       if g.has_edge(*t1)==False and g.has_edge(*t2)==False and g.has_edge(*t3)==False:
          g.add_edge(*t1)
          g.add_edge(*t2)
          g.add_edge(*t3)
          added.append([aaa_binary[q],'p'+ str(i)])                                                
          for j in range(len(added)):
          if nx.shortest_path(g, added[j][0][0], added[j][0][3])!=added[j][0] or nx.shortest_path(g, aaa_binary[q][0], aaa_binary[q][3])!=aaa_binary[q]:
             g.remove_edge(*t1)
             g.remove_edge(*t2)   
             g.remove_edge(*t3)                                     
             added.remove([aaa_binary[q],'p'+ str(i)])
             break

       if g.has_edge(*t1)==False and g.has_edge(*t2)==False and g.has_edge(*t3)==True:
          g.add_edge(*t1)
          g.add_edge(*t2)
          added.append([aaa_binary[q],'p'+ str(i)])                                                
          for j in range(len(added)):
          if nx.shortest_path(g, added[j][0][0], added[j][0][3])!=added[j][0] or nx.shortest_path(g, aaa_binary[q][0], aaa_binary[q][3])!=aaa_binary[q]:
             g.remove_edge(*t1)
             g.remove_edge(*t2)                                           
             added.remove([aaa_binary[q],'p'+ str(i)])
             break                                            

# ... and then the rest of the False and True possibilities combinations in the `if g.has_edge()'condition.

added[] - list of currently valid paths in the form [[[0, 2, 4, 6], 'p0'], [[0, 2, 4, 1], 'p1'],...]

aaa_binary[] - list of path combinations to check in the form [[0, 2, 4, 6], [0, 2, 6, 4], [0, 4, 2, 6],...]

Loop operation:

The algorithm selects one sublist from the aaa_binary list, then adds nodes to the graph and creates edges. Then the algorithm checks if the given edge exists. If it does not exist, it adds it to the graph, if it exists, it does not add. Then, if the condition of the shortest path is not met, only the newly added edge is removed from the graph. And so until you find the right path from the aaa_binary list.

As you can see only with the four-element sublistors, there are 8 different combinations of False and True in the condition if g.has_edge () in the aaa_binary list, which already makes a technical problem. However, I would like to develop this to check, for example, eight-element paths, and then the combination will be 256! Which is obvious that I can not do it in the current way. And I care that the loop necessarily adds only non-existent edges, because then it is easier to control the creation of the optimal graph.

Hence my question, is it possible to write such a loop differently and automate it more? I will be very grateful for any comments.

Aucun commentaire:

Enregistrer un commentaire