Here is the loop finder part (click on "view raw" below the code box to see it in a new window):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def loop_copy(loop_inp): | |
""" Will return a copy of a loop list | |
Used when a change needs to be made.""" | |
loop_op=[] | |
for c1 in range(len(loop_inp)): | |
row_vector=[] | |
for c2 in range(len(loop_inp[c1])): | |
row_vector.append(loop_inp[c1][c2]) | |
loop_op.append(row_vector) | |
return loop_op | |
def check_loop_repeat(lp_iter, lp_list): | |
""" Will return 1 if the loop already | |
exists if the loop_list found so far.""" | |
# Make a copy of the entire loop list | |
# found so far. Just in case. | |
list_cmp=loop_copy(lp_list) | |
# As a default, loop is not repeated | |
# A single instance of repitition will | |
# set it to 1. | |
lp_repeat=0 | |
# Go through every loop found | |
# so far | |
for c1 in range(len(list_cmp)): | |
# Make a copy of the loop being checked | |
iter_cmp=loop_copy(lp_iter) | |
# Move in the reverse direction of the loop | |
# This is because elements are deleted | |
# as they are found. | |
for c2 in range(len(iter_cmp)-1, -1, -1): | |
# Check if the element is found or if the | |
# symmetrical element is found in any of | |
# the loops found so far. | |
# Because they are the same. | |
if ([iter_cmp[c2][0], iter_cmp[c2][1]] in list_cmp[c1]) or \ | |
([iter_cmp[c2][1], iter_cmp[c2][0]] in list_cmp[c1]): | |
# If so, remove the element | |
iter_cmp.remove(iter_cmp[c2]) | |
# If all element have been deleted | |
# it means the loop exists | |
if not iter_cmp: | |
lp_repeat=1 | |
return lp_repeat | |
def loop_addition(br_map, nd_list, lp_list, curr_lp_iter, lp_update, curr_elem, all_loops, main_loops): | |
""" Take a new element of br_map in any direction. | |
Check for a parallel branch at that element. | |
Add that element if not already there in the temporary loop.""" | |
row=curr_elem[0] | |
col=curr_elem[1] | |
# Check if there is an element | |
if br_map[row][col]: | |
# Check for parallel branches | |
if len(br_map[row][col])>1: | |
if (br_map[row][col][0][0] in nd_list) or \ | |
(br_map[row][col][0][-1] in nd_list): | |
del nd_list[nd_list.index(br_map[row][col][0][-1])] | |
del nd_list[nd_list.index(br_map[row][col][0][0])] | |
# Check if the parallel branch exists already | |
# in lp_list | |
if not check_loop_repeat([[row, col], [row, col]], lp_list): | |
lp_list.append([[row, col], [row, col]]) | |
# Update the all_loops counter | |
all_loops+=len(br_map[row][col])-1 | |
# Temp list to make copies | |
# of loops found | |
row_vec=[] | |
for item in curr_lp_iter: | |
row_vec.append(item) | |
# Check if an element has been | |
# encountered before | |
# not going back and forth that is | |
if not (([row, col] in row_vec) or [col, row] in row_vec): | |
# Add the element found | |
lp_update.append(row_vec) | |
lp_update[main_loops].append([row, col]) | |
# Update the main loops counter | |
main_loops+=1 | |
# If an element has not been found | |
# or is a duplicate, lp_update | |
# won't contain it. | |
return [all_loops, main_loops] | |
def loop_closing(br_map, lp_list, nd_list, lp_update, c1, lp_count): | |
""" After a loop has been indentified. That is | |
the first node and the last node are the same. | |
It is checked whether the loop passes through | |
nodes that have not been passed through. If not, | |
loop is directly added. If it does, another | |
additional check is is the loop has the same elements | |
as any other loops already found in lp_list. """ | |
# Default is that all nodes have been "found" | |
# or passed through. A single exception will | |
# produce a new loop. | |
nd_exist="found" | |
for c5 in range(len(lp_update[c1])): | |
# Go through every branch in the loop | |
# Take the first and last node of every branch | |
st_node=br_map[lp_update[c1][c5][0]][lp_update[c1][c5][1]][0][0] | |
end_node=br_map[lp_update[c1][c5][0]][lp_update[c1][c5][1]][0][-1] | |
# Check if either are there in nd_list | |
# Nodes are deleted from nd_list as they are | |
# found. So if the node is in nd_list | |
# it means it has not been passed through | |
if (st_node in nd_list): | |
nd_exist="not_found" | |
nd_idx=nd_list.index(st_node) | |
del nd_list[nd_idx] | |
if (end_node in nd_list): | |
nd_exist="not_found" | |
nd_idx=nd_list.index(end_node) | |
del nd_list[nd_idx] | |
if nd_exist=="not_found": | |
# Add that loop to loop list | |
lp_list.append(lp_update[c1]) | |
# Remove the loop from the temp | |
# variable. | |
del lp_update[c1] | |
lp_count+=1 | |
else: | |
# Check if the loop is new even | |
# if all nodes have been passed through. | |
lp_exist="found" | |
if not check_loop_repeat(lp_update[c1], lp_list): | |
lp_exist="not_found" | |
if lp_exist=="not_found": | |
# Add that loop to loop list | |
lp_list.append(lp_update[c1]) | |
# Remove the loop from the temp | |
# variable. | |
del lp_update[c1] | |
lp_count+=1 | |
return lp_count | |
def loop_horiz(br_map, nd_list, lp_list, lp_iter, elem, lp_count): | |
""" Moves horizontally in a loop find. | |
Looks for every element in a particular column of br_map. | |
Each element is added onto the exiting loops. """ | |
# lp_list is the loops found. | |
# lp_iter is the list of loops as they are being identified. | |
# Those that are loops will be added to lp_list. | |
no_nodes=len(br_map) | |
start_row=elem[0] | |
start_col=elem[1] | |
# Temp list to make copies | |
# of exisiting lists | |
# if elements exist on that row | |
lp_update=[] | |
# Counter for number | |
# of elements found in the row | |
c2=0 | |
for c1 in range(len(lp_iter)): | |
# Set loop row and counter | |
# to the latest element | |
# in lp_iter | |
loop_row=lp_iter[c1][-1][0] | |
loop_column=lp_iter[c1][-1][1] | |
# Start from element in next column | |
# to end of matrix | |
#for c3 in range(loop_column+1, no_nodes): | |
for c3 in range(0, no_nodes): | |
curr_elem=[loop_row, c3] | |
lp_count,c2=loop_addition(br_map, nd_list, lp_list, \ | |
lp_iter[c1], lp_update, curr_elem, lp_count, c2) | |
for c1 in range(len(lp_update)-1, -1, -1): | |
# End condition | |
# Latest element has ending or starting node | |
# Same as last element of first branch | |
last_elem_frst=br_map[start_row][start_col][0][-1] | |
frst_elem_curr=br_map[lp_update[c1][-1][0]][lp_update[c1][-1][1]][0][0] | |
last_elem_curr=br_map[lp_update[c1][-1][0]][lp_update[c1][-1][1]][0][-1] | |
if (frst_elem_curr==last_elem_frst or \ | |
last_elem_curr==last_elem_frst): | |
lp_count=loop_closing(br_map, lp_list, nd_list, lp_update, c1, lp_count) | |
# lp_iter will be the same as lp_update | |
lp_iter=[] | |
for c1 in range(len(lp_update)): | |
lp_iter.append(lp_update[c1]) | |
# lp_iter contains ongoing loops | |
# Closed loops are moved to lp_list | |
# Broken loops are dropped | |
#print lp_update, "Check" | |
#print lp_iter, "End" | |
return [lp_count, lp_iter] | |
def loop_vert(br_map, nd_list, lp_list, lp_iter, elem, lp_count): | |
""" Moves vertically in a loop find. | |
Looks for every element in a particular column of br_map. | |
Each element is added onto the exiting loops. """ | |
# lp_list is the loops found. | |
# lp_iter is the list of loops as they are being identified. | |
# Those that are loops will be added to lp_list. | |
no_nodes=len(br_map) | |
start_row=elem[0] | |
start_col=elem[1] | |
# Temp list to make copies | |
# of exisiting lists | |
# if elements exist on that column | |
lp_update=[] | |
# Counter for number | |
# of elements found in the column | |
c2=0 | |
for c1 in range(len(lp_iter)): | |
# Set loop row and counter | |
# to the latest element | |
# in lp_iter | |
loop_row=lp_iter[c1][-1][0] | |
loop_column=lp_iter[c1][-1][1] | |
# Start from element from first row | |
# to end of column | |
for c3 in range(0, no_nodes): | |
curr_elem=[c3, loop_column] | |
lp_count,c2=loop_addition(br_map, nd_list, lp_list, \ | |
lp_iter[c1], lp_update, curr_elem, lp_count, c2) | |
for c1 in range(len(lp_update)-1, -1, -1): | |
# End condition | |
# Latest element has ending or starting node | |
# Same as last element of first branch | |
last_elem_frst=br_map[start_row][start_col][0][-1] | |
frst_elem_curr=br_map[lp_update[c1][-1][0]][lp_update[c1][-1][1]][0][0] | |
last_elem_curr=br_map[lp_update[c1][-1][0]][lp_update[c1][-1][1]][0][-1] | |
if (frst_elem_curr==last_elem_frst or \ | |
last_elem_curr==last_elem_frst): | |
lp_count=loop_closing(br_map, lp_list, nd_list, lp_update, c1, lp_count) | |
# lp_iter will be the same as lp_update | |
lp_iter=[] | |
for c1 in range(len(lp_update)): | |
lp_iter.append(lp_update[c1]) | |
# lp_iter contains ongoing loops | |
# Closed loops are moved to lp_list | |
# Broken loops are dropped | |
return [lp_count, lp_iter] | |
def find_loop(br_map, nd_list, lp_list, lp_iter, elem, lp_count, lp_limit): | |
"""Find the loops from info on branches and | |
nodes. The starting point is the first branch in br_map. | |
The loops found need not be independent loops.""" | |
no_nodes=len(br_map) | |
# First branch | |
start_row=elem[0] | |
start_col=elem[1] | |
# Move right from that element | |
# This is the first element | |
# In a general sense, the direction is horiz | |
loop_dir="horiz" | |
# The termination condition is | |
# that there should not be any element | |
# in the nd_list. The nodes are deleted | |
# as a completed loop contains them. | |
# This is to ensure that all the nodes | |
# are included in the loops found. | |
# To ensure that parallel loops between | |
# a few pair of nodes, do not cause | |
# loops to be left out, additionally, | |
# it is checked whether | |
# Loops < Branches - Nodes + 1 | |
while (nd_list or lp_count<lp_limit): | |
# Will be executed if we are moving horizontally | |
if (loop_dir == "horiz"): | |
lp_count, lp_iter=loop_horiz(br_map, nd_list, lp_list, lp_iter, elem, lp_count) | |
# Change direction to vertical | |
loop_dir="vert" | |
# Will be executed if we are moving vertically | |
if (loop_dir == "vert"): | |
lp_count, lp_iter=loop_vert(br_map, nd_list, lp_list, lp_iter, elem, lp_count) | |
# Change direction to horizontal | |
loop_dir="horiz" | |
return lp_count | |
# Determining the loops | |
loop_list=[] | |
loop_count=0 | |
loop_nodes=[] | |
# Making a copy of node_list | |
# for finding the loops | |
for c1 in range(len(node_list)): | |
loop_nodes.append(node_list[c1]) | |
# Pick a row | |
for c1 in range(number_of_nodes-1): | |
# Diagonal elements are null | |
# So choose the column next to diagonal | |
for c2 in range(c1+1, number_of_nodes): | |
#check if it exists | |
if branch_map[c1][c2]: | |
# Starting branch found | |
if len(branch_map[c1][c2])>1: | |
if not check_loop_repeat([[c1, c2], [c1, c2]], loop_list): | |
loop_list.append([[c1, c2], [c1, c2]]) | |
# Check for parallel branches again. | |
if (branch_map[c1][c2][0][0] in loop_nodes): | |
del loop_nodes[loop_nodes.index(branch_map[c1][c2][0][0])] | |
if (branch_map[c1][c2][0][-1] in loop_nodes): | |
del loop_nodes[loop_nodes.index(branch_map[c1][c2][0][-1])] | |
# Check is there are parallel branches between the nodes | |
loop_list.append([[c1, c2], [c1, c2]]) | |
loop_count+=len(branch_map[c1][c2])-1 | |
# Initialize the loop iterator list with the first element. | |
loop_iter=[] | |
loop_iter.append([[c1, c2]]) | |
loop_count=find_loop(branch_map, loop_nodes, loop_list, loop_iter, \ | |
[c1, c2], loop_count, number_of_branches-number_of_nodes+1) | |
# Remove any repitions in loop_list | |
for c1 in range(len(loop_list)-1): | |
for c2 in range(len(loop_list)-1, c1, -1): | |
if loop_list[c1]==loop_list[c2]: | |
del loop_list[c2] | |
# The actual elements from the branches | |
# to be entered into the loops | |
loop_branches=[] | |
# Go through every element in loop_list | |
for c1 in range(len(loop_list)): | |
# If the loop has two elements | |
# it means it is a group of | |
# parallel branches between nodes | |
if len(loop_list[c1])==2: | |
curr_br=loop_list[c1][0] | |
# Get every permutation of branch pairs possible | |
for c2 in range(len(branch_map[curr_br[0]][curr_br[1]])-1): | |
for c3 in range(c2+1, len(branch_map[curr_br[0]][curr_br[1]])): | |
loop_updt=[] | |
# Iterate in the forward direction | |
for c4 in range(len(branch_map[curr_br[0]][curr_br[1]][c2])): | |
loop_updt.append(branch_map[curr_br[0]][curr_br[1]][c2][c4]) | |
# Iterate in the reverse direction | |
for c4 in range(len(branch_map[curr_br[0]][curr_br[1]][c3])-2, -1, -1): | |
loop_updt.append(branch_map[curr_br[0]][curr_br[1]][c3][c4]) | |
loop_branches.append(loop_updt) | |
else: | |
loop_updt=[] | |
# Go through all elements in the loop | |
for c2 in range(0, len(loop_list[c1])-1): | |
# Mark two elements in the loop | |
# The current and the next element | |
curr_br=loop_list[c1][c2] | |
curr_br_beg=branch_map[curr_br[0]][curr_br[1]][0][0] | |
curr_br_end=branch_map[curr_br[0]][curr_br[1]][0][-1] | |
next_br=loop_list[c1][c2+1] | |
next_br_beg=branch_map[next_br[0]][next_br[1]][0][0] | |
next_br_end=branch_map[next_br[0]][next_br[1]][0][-1] | |
curr_dir="forward" | |
# Start stringing the branches together | |
# So if it is the first branch | |
# Check if the beginning element of the branch | |
# is the same as the beginning or ending element | |
# if the next branch | |
# In that case, the first/current branch | |
# is to be reversed | |
if not loop_updt: | |
if curr_br_beg==next_br_beg or curr_br_beg==next_br_end: | |
curr_dir="reverse" | |
# If the loop update is in progress | |
# check how the current element is linked to | |
# the last element on the updated loop | |
else: | |
if curr_br_end==loop_updt[-1]: | |
curr_dir="reverse" | |
# Depending on the direction in which | |
# an element is to be added to | |
# the loop. | |
if curr_dir=="forward": | |
for c3 in range(len(branch_map[curr_br[0]][curr_br[1]][0])): | |
loop_updt.append(branch_map[curr_br[0]][curr_br[1]][0][c3]) | |
else: | |
for c3 in range(len(branch_map[curr_br[0]][curr_br[1]][0])-1, -1, -1): | |
loop_updt.append(branch_map[curr_br[0]][curr_br[1]][0][c3]) | |
# Repeat for the last element | |
next_dir="forward" | |
if next_br_end==loop_updt[-1]: | |
next_dir="reverse" | |
if next_dir=="forward": | |
for c3 in range(len(branch_map[next_br[0]][next_br[1]][0])): | |
loop_updt.append(branch_map[next_br[0]][next_br[1]][0][c3]) | |
else: | |
for c3 in range(len(branch_map[next_br[0]][next_br[1]][0])-1, -1, -1): | |
loop_updt.append(branch_map[next_br[0]][next_br[1]][0][c3]) | |
# Remove any repitions in the elements | |
# in consecutive elements only | |
for c3 in range(len(loop_updt)-1, 0, -1): | |
if loop_updt[c3]==loop_updt[c3-1]: | |
del loop_updt[c3] | |
loop_branches.append(loop_updt) | |
#loop_count=len(loop_branches) | |
print "Number of nodes", | |
print number_of_nodes | |
print "Number of branches", | |
print number_of_branches | |
print "Number of loops", | |
print loop_count | |
print "*"*50 | |
excel_col="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" | |
excel_dict={} | |
excel_col_list=excel_col.split(" ") | |
for c1 in range(26): | |
excel_dict[c1]=excel_col_list[c1] | |
for item in loop_branches: | |
for c1 in range(len(item)): | |
print str(item[c1][0]+1) + excel_dict[item[c1][1]], | |
And here is the entire code (click on "view raw" below the code box to see it in a new window):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import sys, math, matrix | |
new_file = open("powerckt3.csv","r") | |
# This matrix will read the .csv file | |
# The .csv file will contain the string "wire" | |
# where a zero impedance direct connection exists. | |
# Where no connection nor device | |
#(resitor, inductor etc) exists, a '' will be found. | |
# The devices will be strings. | |
# Maybe later will be actual object constructor calls. | |
conn_matrix=[] | |
for line in new_file: | |
conn_matrix.append(line.split(",")) | |
conn_rows=len(conn_matrix) | |
conn_columns=len(conn_matrix[0]) | |
# Remove any leading or trailing quotes | |
# and also carriage returns (\n) that | |
# may have been added while generating the csv file. | |
def scrub_elements(x, row, col): | |
if x[row][col]: | |
if "\n" in x[row][col]: | |
x[row][col]=x[row][col][:-1] | |
if x[row][col]: | |
while (x[row][col][0]=='"' or x[row][col][0]=="'"): | |
x[row][col]=x[row][col][1:] | |
while (x[row][col][-1]=='"' or x[row][col][-1]=="'"): | |
x[row][col]=x[row][col][:-1] | |
for c1 in range(0, conn_rows): | |
for c2 in range(0, conn_columns): | |
scrub_elements(conn_matrix, c1, c2) | |
# List of jumps labels | |
jump_list=[] | |
# Structure of jump_list | |
# row, column, jump_label, direction | |
# Check is it a jump, an element | |
# or simply no connection | |
def jump_sanity(x, x_jump, row, column): | |
x_elem = x[row][column] | |
if (x_elem ==''): | |
del x_jump["exist"] | |
del x_jump["jump"] | |
elif (len(x_elem)>3): | |
if (x_elem.lower()[0:4] == "jump"): | |
del x_jump["exist"] | |
else: | |
del x_jump["jump"] | |
else: | |
del x_jump["jump"] | |
# Check for jump label sanity and | |
# add a list of elements where jumps exist. | |
# Basically examines whether element (c1, c2) | |
# is a jump and what are the elements | |
# around it. | |
def jump_checking(x_matrix, x_jump, row, col, no_rows, no_cols): | |
# Current element | |
curr_element = {"exist":0, "jump":1} | |
# Determine if it is a jump label | |
jump_sanity(x_matrix, curr_element, row, col) | |
if ("jump" in curr_element): | |
# If so, what is the element in the same column | |
# and next row | |
if (row<no_rows-1): | |
next_row_element = {"exist":0, "jump":1} | |
jump_sanity(x_matrix, next_row_element, row+1, col) | |
else: | |
next_row_element = {} | |
# If so, what is the element in the same column | |
# and previous row | |
if (row>0): | |
prev_row_element = {"exist":0, "jump":1} | |
jump_sanity(x_matrix, prev_row_element, row-1, col) | |
else: | |
prev_row_element = {} | |
# If so, what is the element in the same row | |
# and next column | |
if (col<no_cols-1): | |
next_col_element = {"exist":0, "jump":1} | |
jump_sanity(x_matrix, next_col_element, row, col+1) | |
else: | |
next_col_element = {} | |
# If so, what is the element in the same row | |
# and previous column | |
if (col>0): | |
prev_col_element = {"exist":0, "jump":1} | |
jump_sanity(x_matrix, prev_col_element, row, col-1) | |
else: | |
prev_col_element = {} | |
# Check if two jumps are next to each other | |
if ("jump" in next_row_element or "jump" in next_col_element or \ | |
"jump" in prev_row_element or "jump" in prev_col_element): | |
print "Two jumps can't be adjacent to each other." | |
print "Check jump at row %d, column %d" %(row, col) | |
# Jump must have only one element adjacent to it. | |
if ("exist" in next_row_element): | |
if ("exist" in next_col_element or "exist" in prev_row_element or \ | |
"exist" in prev_col_element): | |
print "Jump has to be the extreme connector on a branch segment." | |
print "Check jump at row %d, column %d" %(row, col) | |
else: | |
x_jump.append([row, col, x_matrix[row][col], "down"]) | |
elif ("exist" in next_col_element): | |
if ("exist" in next_row_element or "exist" in prev_row_element or \ | |
"exist" in prev_col_element): | |
print "Jump has to be the extreme connector on a branch segment." | |
print "Check jump at row %d, column %d" %(row, col) | |
else: | |
x_jump.append([row, col, x_matrix[row][col], "right"]) | |
elif ("exist" in prev_row_element): | |
if ("exist" in next_row_element or "exist" in next_col_element or \ | |
"exist" in prev_col_element): | |
print "Jump has to be the extreme connector on a branch segment." | |
print "Check jump at row %d, column %d" %(row, col) | |
else: | |
x_jump.append([row, col, x_matrix[row][col], "up"]) | |
elif ("exist" in prev_col_element): | |
if ("exist" in next_row_element or "exist" in next_col_element or \ | |
"exist" in prev_row_element): | |
print "Jump has to be the extreme connector on a branch segment." | |
print "Check jump at row %d, column %d" %(row, col) | |
else: | |
x_jump.append([row, col, x_matrix[row][col], "left"]) | |
for c1 in range(0, conn_rows): | |
for c2 in range(0, conn_columns): | |
jump_checking(conn_matrix, jump_list, c1, c2, conn_rows, conn_columns) | |
# Create a dictionary of jumps - | |
# for each jump label - there is a list with two elements. | |
jump_matrix={} | |
# Structure of jump_matrix | |
# label: [[[row, col], "dir"], [[row, col], "dir"]] | |
for c1 in range(len(jump_list)): | |
jump_count=1 | |
for c2 in range(len(jump_list)): | |
if not c1==c2: | |
if jump_list[c1][2]==jump_list[c2][2]: | |
frst_jmp = jump_list[c1] | |
scd_jmp = jump_list[c2] | |
jump_matrix[frst_jmp[2]]=[[[frst_jmp[0], frst_jmp[1]], frst_jmp[3]],\ | |
[[scd_jmp[0], scd_jmp[1]], scd_jmp[3]]] | |
jump_count=jump_count+1 | |
if (jump_count<2): | |
print "Error. Corresponding jump label for %s does not exist" %(jump_list[c1][2]) | |
elif (jump_count>2): | |
print "Error. More than two jump labels for %s present" %(jump_list[c1][2]) | |
del jump_matrix[jump_list[c1][2]] | |
# A node is defined as a junction of 3 or more branches. | |
def node_checking(x_mat, x_list, row, col, x_row, x_col): | |
if ((row==0 and col==0) or (row==x_row-1 and col==x_col-1) or \ | |
(row==0 and col==x_col-1) or (row==x_row-1 and col==0)): | |
# If its a corner point it can't be a node. | |
# This prevents array index going out of range. | |
pass | |
# The next cases, can't be outer edges or corner points. | |
else: | |
if (row==0): | |
# If it is the first row, | |
# check if the element in the next and | |
# previous columns and same row are connected. | |
if not (x_mat[row][col+1]=='' or x_mat[row][col-1]==''): | |
# Then check if the element in next row and | |
# same column is connected. Look for a T junction. | |
if not (x_mat[row+1][col]==''): | |
x_list.append([row, col]) | |
if (row==x_row-1): | |
# If it is the last row, | |
# check if the elements in the next and | |
# previous columns and same row are connected. | |
if not (x_mat[row][col+1]=='' or x_mat[row][col-1]==''): | |
if not (x_mat[row-1][col]==''): | |
# Then check if element in the previous row and | |
# same column is connected. Look for a T junction. | |
x_list.append([row, col]) | |
if (col==0): | |
# If it is the first column, | |
# check if the element in the next column and | |
# same row is connected. | |
if not (x_mat[row][col+1]==''): | |
# Then check if the elements in next and | |
# previous row and same column are connected. | |
# Look for a T junction. | |
if not (x_mat[row+1][col]=='' or x_mat[row-1][col]==''): | |
x_list.append([row, col]) | |
if (col==x_col-1): | |
# If it is the last column, | |
# check if the element in the previous column and | |
# same row is connected. | |
if not (x_mat[row][col-1]==''): | |
# Then check if the elements in next and | |
# previous row and same column are connected. | |
# Look for a T junction. | |
if not (x_mat[row+1][col]=='' or x_mat[row-1][col]==''): | |
x_list.append([row, col]) | |
if (row>0 and row<x_row-1 and col>0 and col<x_col-1): | |
# If the element is not on the outer boundary | |
if (x_mat[row][col+1]!='' and x_mat[row][col-1]!=''): | |
# Check if the elements in next and | |
# previous columns and same row are connected | |
if (x_mat[row+1][col]!='' or x_mat[row-1][col]!=''): | |
# Then check if elements in either the next and | |
# previous row and same column are connected | |
x_list.append([row, col]) | |
elif (x_mat[row+1][col]!='' and x_mat[row-1][col]!=''): | |
# Check if the elements in next and | |
# previous rows and same column are connected | |
if (x_mat[row][col+1]!='' or x_mat[row][col-1]!=''): | |
# Then check if elements in either the next and | |
# previous column and same row are connected | |
x_list.append([row, col]) | |
node_list=[] | |
# Structure of node_list | |
# [row, column] | |
for c1 in range(0, conn_rows): | |
for c2 in range(0, conn_columns): | |
curr_element = {"exist":0, "jump":1} | |
jump_sanity(conn_matrix, curr_element, c1, c2) | |
if ("exist" in curr_element): | |
node_checking(conn_matrix, node_list, c1, c2, conn_rows, conn_columns) | |
else: | |
pass | |
# This list contains all the nodes that are T or + junctions. | |
#print "*"*60 | |
#print node_list | |
#print "*"*60 | |
# Map of branches between nodes in node_list | |
branch_map=[] | |
# Creating an square of the dimension of node_list. | |
# Each element will be a list of the | |
# series connection of branches between the nodes. | |
for c1 in range(len(node_list)): | |
branch_rows=[] | |
for c2 in range(len(node_list)): | |
branch_rows.append([]) | |
branch_map.append(branch_rows) | |
# Generate a search rule for each node. | |
# The concept is to start at a node and | |
# search until another node is reached. | |
node_iter_rule=[] | |
for c1 in range(len(node_list)): | |
node_row=node_list[c1][0] | |
node_column=node_list[c1][1] | |
iter_rule={"left":0, "down":1, "right":2, "up":3} | |
# For nodes in the outer edges, | |
# the rules going outwards will be removed. | |
if (node_row==0): | |
del(iter_rule["up"]) | |
if (node_row==conn_rows-1): | |
del(iter_rule["down"]) | |
if (node_column==0): | |
del(iter_rule["left"]) | |
if (node_column==conn_columns-1): | |
del(iter_rule["right"]) | |
# Depending on the non-existence of elements | |
# in a direction, those rules will be removed. | |
if (node_row>0) and (node_row<conn_rows-1) and \ | |
(node_column>0) and (node_column<conn_columns-1): | |
if (conn_matrix[node_row-1][node_column]==''): | |
del(iter_rule["up"]) | |
if (conn_matrix[node_row+1][node_column]==''): | |
del(iter_rule["down"]) | |
if (conn_matrix[node_row][node_column+1]==''): | |
del(iter_rule["right"]) | |
if (conn_matrix[node_row][node_column-1]==''): | |
del(iter_rule["left"]) | |
node_iter_rule.append(iter_rule) | |
def jump_node_check(x_mat, x_list, jdir, row): | |
n_row=x_list[c1][0] | |
n_col=x_list[c1][1] | |
if (jdir=="up"): | |
if (len(x_mat[n_row-1][n_col])>3): | |
if (x_mat[n_row-1][n_col].lower()[0:4]=="jump"): | |
print "Error. Jump can't be next to a node. \ | |
Check jump at row %d column %d." %(n_row-1, n_col) | |
if (jdir=="down"): | |
if (len(x_mat[n_row+1][n_col])>3): | |
if (x_mat[n_row+1][n_col].lower()[0:4]=="jump"): | |
print "Error. Jump can't be next to a node. \ | |
Check jump at row %d column %d." %(n_row+1, n_col) | |
if (jdir=="left"): | |
if (len(x_mat[n_row][n_col-1])>3): | |
if (x_mat[n_row][n_col-1].lower()[0:4]=="jump"): | |
print "Error. Jump can't be next to a node. \ | |
Check jump at row %d column %d." %(n_row, n_col-1) | |
if (jdir=="right"): | |
if (len(x_mat[n_row][n_col+1])>3): | |
if (x_mat[n_row][n_col+1].lower()[0:4]=="jump"): | |
print "Error. Jump can't be next to a node. \ | |
Check jump at row %d column %d." %(n_row, n_col+1) | |
# Check if a jump is not next to a node. | |
for c1 in range(len(node_list)): | |
for jump_check_dir in node_iter_rule[c1].keys(): | |
jump_node_check(conn_matrix, node_list, jump_check_dir, c1) | |
# For each node in node_list perform the search operation. | |
# Each node has a list of possible search rules. | |
# Perform a search for each rule. | |
# From the starting node, advance in the direction of the rule. | |
# After advancing, check if the next element is a node. | |
# If it is a node, stop. | |
# If it is not a node, there can be only two directions of movement. | |
# Move in a direction and check is an element exists. | |
# If it exists, check if it is not already an element encountered - | |
# shouldn't be moving backwards. | |
# If a new element is encountered, | |
# update the element is branch iter and continue. | |
def jump_move(x_mat, x_jump, x_element, pos): | |
jump_trace=x_mat[x_element[0]][x_element[1]] | |
if (x_jump[jump_trace][pos][1] == "left"): | |
nxt_row=x_jump[jump_trace][pos][0][0] | |
nxt_col=x_jump[jump_trace][pos][0][1] - 1 | |
jmp_exec="left" | |
elif (x_jump[jump_trace][pos][1] == "right"): | |
nxt_row=x_jump[jump_trace][pos][0][0] | |
nxt_col=x_jump[jump_trace][pos][0][1] + 1 | |
jmp_exec="right" | |
elif (x_jump[jump_trace][pos][1] == "up"): | |
nxt_row=x_jump[jump_trace][pos][0][0] - 1 | |
nxt_col=x_jump[jump_trace][pos][0][1] | |
jmp_exec="up" | |
elif (x_jump[jump_trace][pos][1] == "down"): | |
nxt_row=x_jump[jump_trace][pos][0][0] + 1 | |
nxt_col=x_jump[jump_trace][pos][0][1] | |
jmp_exec="down" | |
return [jmp_exec, nxt_row, nxt_col] | |
def branch_jump(x_mat, x_jump, x_element): | |
# If a jump is encountered. | |
# Look for the label in the jump_matrix dictionary | |
# Check which element has been encountered. | |
# Check the co-ordinates of the other element and | |
# the sense of movement. | |
# Depending on the sense of movement, update | |
# the new co-ordinates with respect | |
# to the other element | |
# Add a flag to show which direction movement | |
# has taken place | |
# To ensure that we don't go back | |
# from the next element after the jump. | |
nxt_row=x_element[0] | |
nxt_col=x_element[1] | |
jump_exec="" | |
if (len(x_mat[nxt_row][nxt_col])>3): | |
if (x_mat[nxt_row][nxt_col].lower()[0:4] == "jump"): | |
jump_trace=x_mat[nxt_row][nxt_col] | |
if (x_jump[jump_trace][0][0] == [nxt_row, nxt_col]): | |
jump_exec, nxt_row, nxt_col = \ | |
jump_move(x_mat, x_jump, x_element, 1) | |
elif (x_jump[jump_trace][1][0] == [nxt_row, nxt_col]): | |
jump_exec, nxt_row, nxt_col = \ | |
jump_move(x_mat, x_jump, x_element, 0) | |
return [jump_exec, nxt_row, nxt_col] | |
# Advancing one element in a branch | |
# Checking for jump direction | |
# to make sure we don't go back | |
def branch_advance(x_mat, x_iter, nxt_elem, jmp_exec, x_rows, x_cols): | |
nxt_row=nxt_elem[0] | |
nxt_col=nxt_elem[1] | |
# This temporary variable is to ensure, | |
# two advancements don't take place in one loop. | |
branch_proceed=0 | |
if ((nxt_col>0) and branch_proceed==0): | |
# We are trying to go left, so check if we didn't jump right. | |
if (x_mat[nxt_row][nxt_col-1] != '' and jmp_exec!="right"): | |
# Next check is if we are going backwards. | |
if not ([nxt_row, nxt_col-1] in x_iter): | |
nxt_col=nxt_col-1 | |
branch_proceed=1 | |
# Set jump to null after a movement. We can't go back anyway. | |
jmp_exec="" | |
if ((nxt_row>0) and branch_proceed==0): | |
# We are trying to go up, so check if we didn't jump down. | |
if (x_mat[nxt_row-1][nxt_col] != '' and jmp_exec!="down"): | |
if not ([nxt_row-1, nxt_col] in x_iter): | |
nxt_row=nxt_row-1 | |
branch_proceed=1 | |
# Set jump to null after a movement. We can't go back anyway. | |
jmp_exec="" | |
if ((nxt_col<x_cols-1) and branch_proceed==0): | |
# We are trying to go right, so check if we didn't jump left. | |
if (x_mat[nxt_row][nxt_col+1] != '' and jmp_exec!="left"): | |
if not ([nxt_row, nxt_col+1] in x_iter): | |
nxt_col=nxt_col+1 | |
branch_proceed=1 | |
# Set jump to null after a movement. We can't go back anyway. | |
jmp_exec="" | |
if ((nxt_row<x_rows-1) and branch_proceed==0): | |
# We are trying to go down, so check if we didn't jump up. | |
if (x_mat[nxt_row+1][nxt_col] != '' and jmp_exec!="up"): | |
if not ([nxt_row+1, nxt_col] in x_iter): | |
nxt_row=nxt_row+1 | |
branch_proceed=1 | |
# Set jump to null after a movement. We can't go back anyway. | |
jmp_exec="" | |
return [jmp_exec, nxt_row, nxt_col] | |
for c1 in range(len(node_list)): | |
# Iterate through every node found | |
node_row=node_list[c1][0] | |
node_column=node_list[c1][1] | |
for branch_dir in node_iter_rule[c1].keys(): | |
# Move in every direction possible | |
# for every node | |
branch_iter=[] | |
branch_iter.append([node_row, node_column]) | |
# Initial advancement. | |
if (branch_dir=="left"): | |
if (node_column>0): | |
next_node_row=node_row | |
next_node_column=node_column-1 | |
if (branch_dir=="down"): | |
if (node_row<conn_rows-1): | |
next_node_row=node_row+1 | |
next_node_column=node_column | |
if (branch_dir=="right"): | |
if (node_column<conn_columns-1): | |
next_node_row=node_row | |
next_node_column=node_column+1 | |
if (branch_dir=="up"): | |
if (node_row>0): | |
next_node_row=node_row-1 | |
next_node_column=node_column | |
# This variable is used when jumps are encountered. | |
jump_executed="" | |
# Termination condition - next element is a node. | |
while not ([next_node_row, next_node_column] in node_list): | |
# If a jump is encountered. | |
# Look for the label in the jump_matrix dictionary | |
# Check which element has been encountered. | |
# Check the co-ordinates of the other element and | |
# the sense of movement. | |
# Depending on the sense of movement, update | |
# the new co-ordinates with respect | |
# to the other element | |
# Add a flag to show which direction movement | |
# has taken place | |
# To ensure that we don't go back | |
# from the next element after the jump. | |
next_element = [next_node_row, next_node_column] | |
jump_executed, next_node_row, next_node_column = \ | |
branch_jump(conn_matrix, jump_matrix, next_element) | |
branch_iter.append([next_node_row, next_node_column]) | |
next_element = [next_node_row, next_node_column] | |
jump_executed, next_node_row, next_node_column = \ | |
branch_advance(conn_matrix, branch_iter, next_element, \ | |
jump_executed, conn_rows, conn_columns) | |
# If no advancement is possible, it means circuit is broken. | |
# Can improve on this error message later. | |
if ([next_node_row, next_node_column] in branch_iter): | |
print "Error. Circuit broken! Close all branches. \ | |
Check at row %d column %d" %(next_node_row, next_node_column) | |
break | |
else: | |
branch_iter.append([next_node_row, next_node_column]) | |
next_elem_index=node_list.index([next_node_row, next_node_column]) | |
branch_map[c1][next_elem_index].append(branch_iter) | |
nw_look=open("nw_check.csv","w") | |
for c1 in range(len(node_list)): | |
for c2 in range(len(node_list)-1): | |
if (branch_map[c1][c2]): | |
for c3 in range(len(branch_map[c1][c2])): | |
nw_look.write("(") | |
for c4 in range(len(branch_map[c1][c2][c3])): | |
nw_look.write("[%d %d] ;" \ | |
%(branch_map[c1][c2][c3][c4][0], branch_map[c1][c2][c3][c4][1])) | |
nw_look.write(")") | |
nw_look.write(", ") | |
else: | |
nw_look.write("[], ") | |
if (branch_map[c1][c2+1]): | |
for c3 in range(len(branch_map[c1][c2+1])): | |
nw_look.write("(") | |
for c4 in range(len(branch_map[c1][c2+1][c3])): | |
nw_look.write("[%d %d] ;" \ | |
%(branch_map[c1][c2+1][c3][c4][0], branch_map[c1][c2+1][c3][c4][1])) | |
nw_look.write(")") | |
nw_look.write("\n") | |
else: | |
nw_look.write("[] \n") | |
##for c1 in range(len(node_list)): | |
## for c2 in range(len(node_list)-1): | |
## nw_look.write("%s ;" %(branch_map[c1][c2])) | |
## nw_look.write("%s \n" %(branch_map[c1][c2+1])) | |
nw_look.close() | |
number_of_nodes=len(node_list) | |
number_of_branches=0 | |
# Counting the number of branches | |
for c1 in range(len(node_list)): | |
for c2 in range(c1+1, len(node_list)): | |
for parallel_branches in branch_map[c1][c2]: | |
number_of_branches+=1 | |
def loop_copy(loop_inp): | |
""" Will return a copy of a loop list | |
Used when a change needs to be made.""" | |
loop_op=[] | |
for c1 in range(len(loop_inp)): | |
row_vector=[] | |
for c2 in range(len(loop_inp[c1])): | |
row_vector.append(loop_inp[c1][c2]) | |
loop_op.append(row_vector) | |
return loop_op | |
def check_loop_repeat(lp_iter, lp_list): | |
""" Will return 1 if the loop already | |
exists if the loop_list found so far.""" | |
# Make a copy of the entire loop list | |
# found so far. Just in case. | |
list_cmp=loop_copy(lp_list) | |
# As a default, loop is not repeated | |
# A single instance of repitition will | |
# set it to 1. | |
lp_repeat=0 | |
# Go through every loop found | |
# so far | |
for c1 in range(len(list_cmp)): | |
# Make a copy of the loop being checked | |
iter_cmp=loop_copy(lp_iter) | |
# Move in the reverse direction of the loop | |
# This is because elements are deleted | |
# as they are found. | |
for c2 in range(len(iter_cmp)-1, -1, -1): | |
# Check if the element is found or if the | |
# symmetrical element is found in any of | |
# the loops found so far. | |
# Because they are the same. | |
if ([iter_cmp[c2][0], iter_cmp[c2][1]] in list_cmp[c1]) or \ | |
([iter_cmp[c2][1], iter_cmp[c2][0]] in list_cmp[c1]): | |
# If so, remove the element | |
iter_cmp.remove(iter_cmp[c2]) | |
# If all element have been deleted | |
# it means the loop exists | |
if not iter_cmp: | |
lp_repeat=1 | |
return lp_repeat | |
def loop_addition(br_map, nd_list, lp_list, curr_lp_iter, lp_update, curr_elem, all_loops, main_loops): | |
""" Take a new element of br_map in any direction. | |
Check for a parallel branch at that element. | |
Add that element if not already there in the temporary loop.""" | |
row=curr_elem[0] | |
col=curr_elem[1] | |
# Check if there is an element | |
if br_map[row][col]: | |
# Check for parallel branches | |
if len(br_map[row][col])>1: | |
if (br_map[row][col][0][0] in nd_list) or \ | |
(br_map[row][col][0][-1] in nd_list): | |
del nd_list[nd_list.index(br_map[row][col][0][-1])] | |
del nd_list[nd_list.index(br_map[row][col][0][0])] | |
# Check if the parallel branch exists already | |
# in lp_list | |
if not check_loop_repeat([[row, col], [row, col]], lp_list): | |
lp_list.append([[row, col], [row, col]]) | |
# Update the all_loops counter | |
all_loops+=len(br_map[row][col])-1 | |
# Temp list to make copies | |
# of loops found | |
row_vec=[] | |
for item in curr_lp_iter: | |
row_vec.append(item) | |
# Check if an element has been | |
# encountered before | |
# not going back and forth that is | |
if not (([row, col] in row_vec) or [col, row] in row_vec): | |
# Add the element found | |
lp_update.append(row_vec) | |
lp_update[main_loops].append([row, col]) | |
# Update the main loops counter | |
main_loops+=1 | |
# If an element has not been found | |
# or is a duplicate, lp_update | |
# won't contain it. | |
return [all_loops, main_loops] | |
def loop_closing(br_map, lp_list, nd_list, lp_update, c1, lp_count): | |
""" After a loop has been indentified. That is | |
the first node and the last node are the same. | |
It is checked whether the loop passes through | |
nodes that have not been passed through. If not, | |
loop is directly added. If it does, another | |
additional check is is the loop has the same elements | |
as any other loops already found in lp_list. """ | |
# Default is that all nodes have been "found" | |
# or passed through. A single exception will | |
# produce a new loop. | |
nd_exist="found" | |
for c5 in range(len(lp_update[c1])): | |
# Go through every branch in the loop | |
# Take the first and last node of every branch | |
st_node=br_map[lp_update[c1][c5][0]][lp_update[c1][c5][1]][0][0] | |
end_node=br_map[lp_update[c1][c5][0]][lp_update[c1][c5][1]][0][-1] | |
# Check if either are there in nd_list | |
# Nodes are deleted from nd_list as they are | |
# found. So if the node is in nd_list | |
# it means it has not been passed through | |
if (st_node in nd_list): | |
nd_exist="not_found" | |
nd_idx=nd_list.index(st_node) | |
del nd_list[nd_idx] | |
if (end_node in nd_list): | |
nd_exist="not_found" | |
nd_idx=nd_list.index(end_node) | |
del nd_list[nd_idx] | |
if nd_exist=="not_found": | |
# Add that loop to loop list | |
lp_list.append(lp_update[c1]) | |
# Remove the loop from the temp | |
# variable. | |
del lp_update[c1] | |
lp_count+=1 | |
else: | |
# Check if the loop is new even | |
# if all nodes have been passed through. | |
lp_exist="found" | |
if not check_loop_repeat(lp_update[c1], lp_list): | |
lp_exist="not_found" | |
if lp_exist=="not_found": | |
# Add that loop to loop list | |
lp_list.append(lp_update[c1]) | |
# Remove the loop from the temp | |
# variable. | |
del lp_update[c1] | |
lp_count+=1 | |
return lp_count | |
def loop_horiz(br_map, nd_list, lp_list, lp_iter, elem, lp_count): | |
""" Moves horizontally in a loop find. | |
Looks for every element in a particular column of br_map. | |
Each element is added onto the exiting loops. """ | |
# lp_list is the loops found. | |
# lp_iter is the list of loops as they are being identified. | |
# Those that are loops will be added to lp_list. | |
no_nodes=len(br_map) | |
start_row=elem[0] | |
start_col=elem[1] | |
# Temp list to make copies | |
# of exisiting lists | |
# if elements exist on that row | |
lp_update=[] | |
# Counter for number | |
# of elements found in the row | |
c2=0 | |
for c1 in range(len(lp_iter)): | |
# Set loop row and counter | |
# to the latest element | |
# in lp_iter | |
loop_row=lp_iter[c1][-1][0] | |
loop_column=lp_iter[c1][-1][1] | |
# Start from element in next column | |
# to end of matrix | |
#for c3 in range(loop_column+1, no_nodes): | |
for c3 in range(0, no_nodes): | |
curr_elem=[loop_row, c3] | |
lp_count,c2=loop_addition(br_map, nd_list, lp_list, \ | |
lp_iter[c1], lp_update, curr_elem, lp_count, c2) | |
for c1 in range(len(lp_update)-1, -1, -1): | |
# End condition | |
# Latest element has ending or starting node | |
# Same as last element of first branch | |
last_elem_frst=br_map[start_row][start_col][0][-1] | |
frst_elem_curr=br_map[lp_update[c1][-1][0]][lp_update[c1][-1][1]][0][0] | |
last_elem_curr=br_map[lp_update[c1][-1][0]][lp_update[c1][-1][1]][0][-1] | |
if (frst_elem_curr==last_elem_frst or \ | |
last_elem_curr==last_elem_frst): | |
lp_count=loop_closing(br_map, lp_list, nd_list, lp_update, c1, lp_count) | |
# lp_iter will be the same as lp_update | |
lp_iter=[] | |
for c1 in range(len(lp_update)): | |
lp_iter.append(lp_update[c1]) | |
# lp_iter contains ongoing loops | |
# Closed loops are moved to lp_list | |
# Broken loops are dropped | |
#print lp_update, "Check" | |
#print lp_iter, "End" | |
return [lp_count, lp_iter] | |
def loop_vert(br_map, nd_list, lp_list, lp_iter, elem, lp_count): | |
""" Moves vertically in a loop find. | |
Looks for every element in a particular column of br_map. | |
Each element is added onto the exiting loops. """ | |
# lp_list is the loops found. | |
# lp_iter is the list of loops as they are being identified. | |
# Those that are loops will be added to lp_list. | |
no_nodes=len(br_map) | |
start_row=elem[0] | |
start_col=elem[1] | |
# Temp list to make copies | |
# of exisiting lists | |
# if elements exist on that column | |
lp_update=[] | |
# Counter for number | |
# of elements found in the column | |
c2=0 | |
for c1 in range(len(lp_iter)): | |
# Set loop row and counter | |
# to the latest element | |
# in lp_iter | |
loop_row=lp_iter[c1][-1][0] | |
loop_column=lp_iter[c1][-1][1] | |
# Start from element from first row | |
# to end of column | |
for c3 in range(0, no_nodes): | |
curr_elem=[c3, loop_column] | |
lp_count,c2=loop_addition(br_map, nd_list, lp_list, \ | |
lp_iter[c1], lp_update, curr_elem, lp_count, c2) | |
for c1 in range(len(lp_update)-1, -1, -1): | |
# End condition | |
# Latest element has ending or starting node | |
# Same as last element of first branch | |
last_elem_frst=br_map[start_row][start_col][0][-1] | |
frst_elem_curr=br_map[lp_update[c1][-1][0]][lp_update[c1][-1][1]][0][0] | |
last_elem_curr=br_map[lp_update[c1][-1][0]][lp_update[c1][-1][1]][0][-1] | |
if (frst_elem_curr==last_elem_frst or \ | |
last_elem_curr==last_elem_frst): | |
lp_count=loop_closing(br_map, lp_list, nd_list, lp_update, c1, lp_count) | |
# lp_iter will be the same as lp_update | |
lp_iter=[] | |
for c1 in range(len(lp_update)): | |
lp_iter.append(lp_update[c1]) | |
# lp_iter contains ongoing loops | |
# Closed loops are moved to lp_list | |
# Broken loops are dropped | |
return [lp_count, lp_iter] | |
def find_loop(br_map, nd_list, lp_list, lp_iter, elem, lp_count, lp_limit): | |
"""Find the loops from info on branches and | |
nodes. The starting point is the first branch in br_map. | |
The loops found need not be independent loops.""" | |
no_nodes=len(br_map) | |
# First branch | |
start_row=elem[0] | |
start_col=elem[1] | |
# Move right from that element | |
# This is the first element | |
# In a general sense, the direction is horiz | |
loop_dir="horiz" | |
# The termination condition is | |
# that there should not be any element | |
# in the nd_list. The nodes are deleted | |
# as a completed loop contains them. | |
# This is to ensure that all the nodes | |
# are included in the loops found. | |
# To ensure that parallel loops between | |
# a few pair of nodes, do not cause | |
# loops to be left out, additionally, | |
# it is checked whether | |
# Loops < Branches - Nodes + 1 | |
while (nd_list or lp_count<lp_limit): | |
# Will be executed if we are moving horizontally | |
if (loop_dir == "horiz"): | |
lp_count, lp_iter=loop_horiz(br_map, nd_list, lp_list, lp_iter, elem, lp_count) | |
# Change direction to vertical | |
loop_dir="vert" | |
# Will be executed if we are moving vertically | |
if (loop_dir == "vert"): | |
lp_count, lp_iter=loop_vert(br_map, nd_list, lp_list, lp_iter, elem, lp_count) | |
# Change direction to horizontal | |
loop_dir="horiz" | |
return lp_count | |
# Determining the loops | |
loop_list=[] | |
loop_count=0 | |
loop_nodes=[] | |
# Making a copy of node_list | |
# for finding the loops | |
for c1 in range(len(node_list)): | |
loop_nodes.append(node_list[c1]) | |
# Pick a row | |
for c1 in range(number_of_nodes-1): | |
# Diagonal elements are null | |
# So choose the column next to diagonal | |
for c2 in range(c1+1, number_of_nodes): | |
#check if it exists | |
if branch_map[c1][c2]: | |
# Starting branch found | |
if len(branch_map[c1][c2])>1: | |
if not check_loop_repeat([[c1, c2], [c1, c2]], loop_list): | |
loop_list.append([[c1, c2], [c1, c2]]) | |
# Check for parallel branches again. | |
if (branch_map[c1][c2][0][0] in loop_nodes): | |
del loop_nodes[loop_nodes.index(branch_map[c1][c2][0][0])] | |
if (branch_map[c1][c2][0][-1] in loop_nodes): | |
del loop_nodes[loop_nodes.index(branch_map[c1][c2][0][-1])] | |
# Check is there are parallel branches between the nodes | |
loop_list.append([[c1, c2], [c1, c2]]) | |
loop_count+=len(branch_map[c1][c2])-1 | |
# Initialize the loop iterator list with the first element. | |
loop_iter=[] | |
loop_iter.append([[c1, c2]]) | |
loop_count=find_loop(branch_map, loop_nodes, loop_list, loop_iter, \ | |
[c1, c2], loop_count, number_of_branches-number_of_nodes+1) | |
# Remove any repitions in loop_list | |
for c1 in range(len(loop_list)-1): | |
for c2 in range(len(loop_list)-1, c1, -1): | |
if loop_list[c1]==loop_list[c2]: | |
del loop_list[c2] | |
# The actual elements from the branches | |
# to be entered into the loops | |
loop_branches=[] | |
# Go through every element in loop_list | |
for c1 in range(len(loop_list)): | |
# If the loop has two elements | |
# it means it is a group of | |
# parallel branches between nodes | |
if len(loop_list[c1])==2: | |
curr_br=loop_list[c1][0] | |
# Get every permutation of branch pairs possible | |
for c2 in range(len(branch_map[curr_br[0]][curr_br[1]])-1): | |
for c3 in range(c2+1, len(branch_map[curr_br[0]][curr_br[1]])): | |
loop_updt=[] | |
# Iterate in the forward direction | |
for c4 in range(len(branch_map[curr_br[0]][curr_br[1]][c2])): | |
loop_updt.append(branch_map[curr_br[0]][curr_br[1]][c2][c4]) | |
# Iterate in the reverse direction | |
for c4 in range(len(branch_map[curr_br[0]][curr_br[1]][c3])-2, -1, -1): | |
loop_updt.append(branch_map[curr_br[0]][curr_br[1]][c3][c4]) | |
loop_branches.append(loop_updt) | |
else: | |
loop_updt=[] | |
# Go through all elements in the loop | |
for c2 in range(0, len(loop_list[c1])-1): | |
# Mark two elements in the loop | |
# The current and the next element | |
curr_br=loop_list[c1][c2] | |
curr_br_beg=branch_map[curr_br[0]][curr_br[1]][0][0] | |
curr_br_end=branch_map[curr_br[0]][curr_br[1]][0][-1] | |
next_br=loop_list[c1][c2+1] | |
next_br_beg=branch_map[next_br[0]][next_br[1]][0][0] | |
next_br_end=branch_map[next_br[0]][next_br[1]][0][-1] | |
curr_dir="forward" | |
# Start stringing the branches together | |
# So if it is the first branch | |
# Check if the beginning element of the branch | |
# is the same as the beginning or ending element | |
# if the next branch | |
# In that case, the first/current branch | |
# is to be reversed | |
if not loop_updt: | |
if curr_br_beg==next_br_beg or curr_br_beg==next_br_end: | |
curr_dir="reverse" | |
# If the loop update is in progress | |
# check how the current element is linked to | |
# the last element on the updated loop | |
else: | |
if curr_br_end==loop_updt[-1]: | |
curr_dir="reverse" | |
# Depending on the direction in which | |
# an element is to be added to | |
# the loop. | |
if curr_dir=="forward": | |
for c3 in range(len(branch_map[curr_br[0]][curr_br[1]][0])): | |
loop_updt.append(branch_map[curr_br[0]][curr_br[1]][0][c3]) | |
else: | |
for c3 in range(len(branch_map[curr_br[0]][curr_br[1]][0])-1, -1, -1): | |
loop_updt.append(branch_map[curr_br[0]][curr_br[1]][0][c3]) | |
# Repeat for the last element | |
next_dir="forward" | |
if next_br_end==loop_updt[-1]: | |
next_dir="reverse" | |
if next_dir=="forward": | |
for c3 in range(len(branch_map[next_br[0]][next_br[1]][0])): | |
loop_updt.append(branch_map[next_br[0]][next_br[1]][0][c3]) | |
else: | |
for c3 in range(len(branch_map[next_br[0]][next_br[1]][0])-1, -1, -1): | |
loop_updt.append(branch_map[next_br[0]][next_br[1]][0][c3]) | |
# Remove any repitions in the elements | |
# in consecutive elements only | |
for c3 in range(len(loop_updt)-1, 0, -1): | |
if loop_updt[c3]==loop_updt[c3-1]: | |
del loop_updt[c3] | |
loop_branches.append(loop_updt) | |
#loop_count=len(loop_branches) | |
print "Number of nodes", | |
print number_of_nodes | |
print "Number of branches", | |
print number_of_branches | |
print "Number of loops", | |
print loop_count | |
print "*"*50 | |
excel_col="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" | |
excel_dict={} | |
excel_col_list=excel_col.split(" ") | |
for c1 in range(26): | |
excel_dict[c1]=excel_col_list[c1] | |
for item in loop_branches: | |
for c1 in range(len(item)): | |
print str(item[c1][0]+1) + excel_dict[item[c1][1]], | |
No comments:
Post a Comment