Friday, January 4, 2013

Branch identification - III

Another round of changes.

When a spreadsheet is saved as a csv file, a couple of things could be done depending on the settings:

1. The contents of the cells could be padded with an additional set of quotes.

2. The last element of every row contains a carriage return "\n".


An absolute requirement is that the column separator is a comma and not a semicolon.

With respect to the above, added the following block of code:


# Remove any leading or trailing quotes and also carriage returns (\n) that may have been added while generating the csv file.
for c1 in range(0, conn_rows):
for c2 in range(0, conn_columns):
if conn_matrix[c1][c2]:
if "\n" in conn_matrix[c1][c2]:
conn_matrix[c1][c2]=conn_matrix[c1][c2][:-1]
if conn_matrix[c1][c2]:
while (conn_matrix[c1][c2][0]=='"' or conn_matrix[c1][c2][0]=="'"):
conn_matrix[c1][c2]=conn_matrix[c1][c2][1:]
while (conn_matrix[c1][c2][-1]=='"' or conn_matrix[c1][c2][-1]=="'"):
conn_matrix[c1][c2]=conn_matrix[c1][c2][:-1]
view raw branch2.py hosted with ❤ by GitHub

So this ensures, that every element of the array (list of lists actually) conn_matrix is a regular string without an additional set of quotes or a carriage return at the end.

The next change is a huge one. In any circuit, it is always possible that wires may cross each other without forming a connection. So a jumper has to be included. The jumper also performs another task. In a large circuit where smaller networks may be included conveniently as modules with electrical connections, the jumper connections could make a huge difference because the modules can be conveniently lined up maybe at the top of the spreadsheet and the main circuit could be below.

So this means we need to add jumper connections to the circuit. A few rules devised:

1. A jumper connection has to be the end-point. It can't be a node and it can't have elements (wires or devices) both before and after it.
2. Also, jumper can be adjacent to a node. Bring the branch out from the node explicitly with a "wire" or a device and then connect a jumper. This is to make sure that we don't have to jump directly from one node to another. The jumps are between two branches. So there has to be at least one separator from a node.
3. The jumper name has to start with "jump". Case does not matter so "jump_a" is the same as "Jump_a" but the need to be sensible about labeling remains.
4. Can't have more than two jumpers of the same label. If there is a need t join more than two separate branches at a give node, split that terminating node up into two receiving branches and add two separate receiving jumper labels.
5. Every jumper must have another jumper.
6. Two jumps can't be next to each other - in the adjacent cells that is.

So with this, here is the code:

#! /usr/bin/env python
import sys, math, matrix
new_file = open("testckt.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.
for c1 in range(0, conn_rows):
for c2 in range(0, conn_columns):
if conn_matrix[c1][c2]:
if "\n" in conn_matrix[c1][c2]:
conn_matrix[c1][c2]=conn_matrix[c1][c2][:-1]
if conn_matrix[c1][c2]:
while (conn_matrix[c1][c2][0]=='"' or conn_matrix[c1][c2][0]=="'"):
conn_matrix[c1][c2]=conn_matrix[c1][c2][1:]
while (conn_matrix[c1][c2][-1]=='"' or conn_matrix[c1][c2][-1]=="'"):
conn_matrix[c1][c2]=conn_matrix[c1][c2][:-1]
jump_list=[]
# Check for jump label sanity and add a list of elements where jumps exist.
for c1 in range(0, conn_rows):
for c2 in range(0, conn_columns):
curr_element = {"exist":0, "jump":1}
if (conn_matrix[c1][c2]==''):
del curr_element["exist"]
del curr_element["jump"]
elif (len(conn_matrix[c1][c2])>3):
if (conn_matrix[c1][c2].lower()[0:4] == "jump"):
del curr_element["exist"]
else:
del curr_element["jump"]
else:
del curr_element["jump"]
if ("jump" in curr_element):
if (c1<conn_rows-1):
next_row_element = {"exist":0, "jump":1}
if (conn_matrix[c1+1][c2]==''):
del next_row_element["exist"]
del next_row_element["jump"]
elif (len(conn_matrix[c1+1][c2])>3):
if (conn_matrix[c1+1][c2].lower()[0:4] == "jump"):
del next_row_element["exist"]
else:
del next_row_element["jump"]
else:
del next_row_element["jump"]
else:
next_row_element = {}
if (c1>0):
prev_row_element = {"exist":0, "jump":1}
if (conn_matrix[c1-1][c2]==''):
del prev_row_element["exist"]
del prev_row_element["jump"]
elif (len(conn_matrix[c1-1][c2])>3):
if (conn_matrix[c1-1][c2].lower()[0:4] == "jump"):
del prev_row_element["exist"]
else:
del prev_row_element["jump"]
else:
del prev_row_element["jump"]
else:
prev_row_element = {}
if (c2<conn_columns-1):
next_col_element = {"exist":0, "jump":1}
if (conn_matrix[c1][c2+1]==''):
del next_col_element["exist"]
del next_col_element["jump"]
elif (len(conn_matrix[c1][c2+1])>3):
if (conn_matrix[c1][c2+1].lower()[0:4] == "jump"):
del next_col_element["exist"]
else:
del next_col_element["jump"]
else:
del next_col_element["jump"]
else:
next_col_element = {}
if (c2>0):
prev_col_element = {"exist":0, "jump":1}
if (conn_matrix[c1][c2-1]==''):
del prev_col_element["exist"]
del prev_col_element["jump"]
elif (len(conn_matrix[c1][c2-1])>3):
if (conn_matrix[c1][c2-1].lower()[0:4] == "jump"):
del prev_col_element["exist"]
else:
del prev_col_element["jump"]
else:
del prev_col_element["jump"]
else:
prev_col_element = {}
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" %(c1, c2)
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" %(c1, c2)
else:
jump_list.append([c1, c2, conn_matrix[c1][c2], "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" %(c1, c2)
else:
jump_list.append([c1, c2, conn_matrix[c1][c2], "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" %(c1, c2)
else:
jump_list.append([c1, c2, conn_matrix[c1][c2], "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" %(c1, c2)
else:
jump_list.append([c1, c2, conn_matrix[c1][c2], "left"])
# Create a dictionary of jumps - for each jump label - there is a list with two elements.
jump_matrix={}
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]:
jump_matrix[jump_list[c1][2]]=[[[jump_list[c1][0], jump_list[c1][1]], jump_list[c1][3]], [[jump_list[c2][0], jump_list[c2][1]], jump_list[c2][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.
node_list=[]
for c1 in range(0, conn_rows):
for c2 in range(0, conn_columns):
curr_element = {"exist":0, "jump":1}
if (conn_matrix[c1][c2]==''):
del curr_element["exist"]
del curr_element["jump"]
elif (len(conn_matrix[c1][c2])>3):
if (conn_matrix[c1][c2].lower()[0:4] == "jump"):
del curr_element["exist"]
else:
del curr_element["jump"]
if ("exist" in curr_element):
if ((c1==0 and c2==0) or (c1==conn_rows-1 and c2==conn_columns-1) or (c1==0 and c2==conn_columns-1) or (c1==conn_rows-1 and c2==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 (c1==0):
# If it is the first row,
# check if the element in the next and previous columns and same row are connected.
if not (conn_matrix[c1][c2+1]=='' or conn_matrix[c1][c2-1]==''):
# Then check if the element in next row and same column is connected. Look for a T junction.
if not (conn_matrix[c1+1][c2]==''):
node_list.append([c1, c2])
if (c1==conn_rows-1):
# If it is the last row,
# check if the elements in the next and previous columns and same row are connected.
if not (conn_matrix[c1][c2+1]=='' or conn_matrix[c1][c2-1]==''):
if not (conn_matrix[c1-1][c2]==''):
# Then check if element in the previous row and same column is connected. Look for a T junction.
node_list.append([c1, c2])
if (c2==0):
# If it is the first column,
# check if the element in the next column and same row is connected.
if not (conn_matrix[c1][c2+1]==''):
# Then check if the elements in next and previous row and same column are connected. Look for a T junction.
if not (conn_matrix[c1+1][c2]=='' or conn_matrix[c1-1][c2]==''):
node_list.append([c1, c2])
if (c2==conn_columns-1):
# If it is the last column,
# check if the element in the previous column and same row is connected.
if not (conn_matrix[c1][c2-1]==''):
# Then check if the elements in next and previous row and same column are connected. Look for a T junction.
if not (conn_matrix[c1+1][c2]=='' or conn_matrix[c1-1][c2]==''):
node_list.append([c1, c2])
if (c1>0 and c1<conn_rows-1 and c2>0 and c2<conn_columns-1):
# If the element is not on the outer boundary
if (conn_matrix[c1][c2+1]!='' and conn_matrix[c1][c2-1]!=''):
# Check if the elements in next and previous columns and same row are connected
if (conn_matrix[c1+1][c2]!='' or conn_matrix[c1-1][c2]!=''):
# Then check if elements in either the next and previous row and same column are connected
node_list.append([c1, c2])
elif (conn_matrix[c1+1][c2]!='' and conn_matrix[c1-1][c2]!=''):
if (conn_matrix[c1][c2+1]!='' or conn_matrix[c1][c2-1]!=''):
node_list.append([c1, c2])
else:
pass
# This list contains all the nodes that are T or + junctions.
print "*"*60
print node_list
print "*"*60
print "*"*60
print "*"*60
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)
# 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.
for c1 in range(len(node_list)):
node_row=node_list[c1][0]
node_column=node_list[c1][1]
for branch_dir in node_iter_rule[c1].keys():
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
print branch_dir
# 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.
if (len(conn_matrix[next_node_row][next_node_column])>3):
if (conn_matrix[next_node_row][next_node_column].lower()[0:4] == "jump"):
jump_trace=conn_matrix[next_node_row][next_node_column]
if (jump_matrix[jump_trace][0][0] == [next_node_row, next_node_column]):
if (jump_matrix[jump_trace][1][1] == "left"):
next_node_row=jump_matrix[jump_trace][1][0][0]
next_node_column=jump_matrix[jump_trace][1][0][1] - 1
jump_executed="left"
elif (jump_matrix[jump_trace][1][1] == "right"):
next_node_row=jump_matrix[jump_trace][1][0][0]
next_node_column=jump_matrix[jump_trace][1][0][1] + 1
jump_executed="right"
elif (jump_matrix[jump_trace][1][1] == "up"):
next_node_row=jump_matrix[jump_trace][1][0][0] - 1
next_node_column=jump_matrix[jump_trace][1][0][1]
jump_executed="up"
elif (jump_matrix[jump_trace][1][1] == "down"):
next_node_row=jump_matrix[jump_trace][1][0][0] + 1
next_node_column=jump_matrix[jump_trace][1][0][1]
jump_executed="down"
elif (jump_matrix[jump_trace][1][0] == [next_node_row, next_node_column]):
if (jump_matrix[jump_trace][0][1] == "left"):
next_node_row=jump_matrix[jump_trace][0][0][0]
next_node_column=jump_matrix[jump_trace][0][0][1] - 1
jump_executed="left"
elif (jump_matrix[jump_trace][0][1] == "right"):
next_node_row=jump_matrix[jump_trace][0][0][0]
next_node_column=jump_matrix[jump_trace][0][0][1] + 1
jump_executed="right"
elif (jump_matrix[jump_trace][0][1] == "up"):
next_node_row=jump_matrix[jump_trace][0][0][0] - 1
next_node_column=jump_matrix[jump_trace][0][0][1]
jump_executed="up"
elif (jump_matrix[jump_trace][0][1] == "down"):
next_node_row=jump_matrix[jump_trace][0][0][0] + 1
next_node_column=jump_matrix[jump_trace][0][0][1]
jump_executed="down"
branch_iter.append([next_node_row, next_node_column])
# This temporary variable is to ensure, two advancements don't take place in one loop.
branch_proceed=0
if ((next_node_column>0) and branch_proceed==0):
# We are trying to go left, so check if we didn't jump right.
if (conn_matrix[next_node_row][next_node_column-1] != '' and jump_executed!="right"):
# Next check is if we are going backwards.
if not ([next_node_row, next_node_column-1] in branch_iter):
next_node_column=next_node_column-1
branch_proceed=1
# Set jump to null after a movement. We can't go back anyway.
jump_executed=""
if ((next_node_row>0) and branch_proceed==0):
# We are trying to go up, so check if we didn't jump down.
if (conn_matrix[next_node_row-1][next_node_column] != '' and jump_executed!="down"):
if not ([next_node_row-1, next_node_column] in branch_iter):
next_node_row=next_node_row-1
branch_proceed=1
# Set jump to null after a movement. We can't go back anyway.
jump_executed=""
if ((next_node_column<conn_columns-1) and branch_proceed==0):
# We are trying to go right, so check if we didn't jump left.
if (conn_matrix[next_node_row][next_node_column+1] != '' and jump_executed!="left"):
if not ([next_node_row, next_node_column+1] in branch_iter):
next_node_column=next_node_column+1
branch_proceed=1
# Set jump to null after a movement. We can't go back anyway.
jump_executed=""
if ((next_node_row<conn_rows-1) and branch_proceed==0):
# We are trying to go down, so check if we didn't jump up.
if (conn_matrix[next_node_row+1][next_node_column] != '' and jump_executed!="up"):
if not ([next_node_row+1, next_node_column] in branch_iter):
next_node_row=next_node_row+1
branch_proceed=1
# Set jump to null after a movement. We can't go back anyway.
jump_executed=""
# 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."
print [next_node_row, next_node_column]
break
else:
branch_iter.append([next_node_row, next_node_column])
branch_map[c1][node_list.index([next_node_row, next_node_column])].append(branch_iter)
print branch_iter
print "*"*70
view raw netw_inter.py hosted with ❤ by GitHub

The logic is as follows:

1. First create a list of jump labels, their co-ordinates, and the direction to move in to reach the adjacent element.
2. So, here when an jump is encountered, the checks performed are:
-Is it a node?
-Is it next to another jump?
-Is it a terminal element?



jump_list=[]
# Check for jump label sanity and add a list of elements where jumps exist.
for c1 in range(0, conn_rows):
for c2 in range(0, conn_columns):
curr_element = {"exist":0, "jump":1}
if (conn_matrix[c1][c2]==''):
del curr_element["exist"]
del curr_element["jump"]
elif (len(conn_matrix[c1][c2])>3):
if (conn_matrix[c1][c2].lower()[0:4] == "jump"):
del curr_element["exist"]
else:
del curr_element["jump"]
else:
del curr_element["jump"]
if ("jump" in curr_element):
if (c1<conn_rows-1):
next_row_element = {"exist":0, "jump":1}
if (conn_matrix[c1+1][c2]==''):
del next_row_element["exist"]
del next_row_element["jump"]
elif (len(conn_matrix[c1+1][c2])>3):
if (conn_matrix[c1+1][c2].lower()[0:4] == "jump"):
del next_row_element["exist"]
else:
del next_row_element["jump"]
else:
del next_row_element["jump"]
else:
next_row_element = {}
if (c1>0):
prev_row_element = {"exist":0, "jump":1}
if (conn_matrix[c1-1][c2]==''):
del prev_row_element["exist"]
del prev_row_element["jump"]
elif (len(conn_matrix[c1-1][c2])>3):
if (conn_matrix[c1-1][c2].lower()[0:4] == "jump"):
del prev_row_element["exist"]
else:
del prev_row_element["jump"]
else:
del prev_row_element["jump"]
else:
prev_row_element = {}
if (c2<conn_columns-1):
next_col_element = {"exist":0, "jump":1}
if (conn_matrix[c1][c2+1]==''):
del next_col_element["exist"]
del next_col_element["jump"]
elif (len(conn_matrix[c1][c2+1])>3):
if (conn_matrix[c1][c2+1].lower()[0:4] == "jump"):
del next_col_element["exist"]
else:
del next_col_element["jump"]
else:
del next_col_element["jump"]
else:
next_col_element = {}
if (c2>0):
prev_col_element = {"exist":0, "jump":1}
if (conn_matrix[c1][c2-1]==''):
del prev_col_element["exist"]
del prev_col_element["jump"]
elif (len(conn_matrix[c1][c2-1])>3):
if (conn_matrix[c1][c2-1].lower()[0:4] == "jump"):
del prev_col_element["exist"]
else:
del prev_col_element["jump"]
else:
del prev_col_element["jump"]
else:
prev_col_element = {}
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" %(c1, c2)
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" %(c1, c2)
else:
jump_list.append([c1, c2, conn_matrix[c1][c2], "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" %(c1, c2)
else:
jump_list.append([c1, c2, conn_matrix[c1][c2], "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" %(c1, c2)
else:
jump_list.append([c1, c2, conn_matrix[c1][c2], "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" %(c1, c2)
else:
jump_list.append([c1, c2, conn_matrix[c1][c2], "left"])
view raw branch3.py hosted with ❤ by GitHub


3. Next is jump_matrix is created where the jump labels are keys in a dictionary and their corresponding values are the two co-ordinates and the direction to move in to reach the next element.



# Create a dictionary of jumps - for each jump label - there is a list with two elements.
jump_matrix={}
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]:
jump_matrix[jump_list[c1][2]]=[[[jump_list[c1][0], jump_list[c1][1]], jump_list[c1][3]], [[jump_list[c2][0], jump_list[c2][1]], jump_list[c2][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]]
view raw branch4.py hosted with ❤ by GitHub

 4. Finding the node is almost the same except that it can't be a jump so a small bit of code to check that:


for c1 in range(0, conn_rows):
for c2 in range(0, conn_columns):
curr_element = {"exist":0, "jump":1}
if (conn_matrix[c1][c2]==''):
del curr_element["exist"]
del curr_element["jump"]
elif (len(conn_matrix[c1][c2])>3):
if (conn_matrix[c1][c2].lower()[0:4] == "jump"):
del curr_element["exist"]
else:
del curr_element["jump"]
view raw branch5.py hosted with ❤ by GitHub

 5. Identifying a branch.
- Start from a node as usual and move in one of the valid directions.
- Then check if the next element is a jump.
- If it is go to the jump matrix and find out which co-ordinates, the jump tag links.
- Find out which co-ordinate we are currently at a pick the other.
- Move in the direction indicated by the other co-ordinate.
- This brings us to the next valid element after the jump.
- Store this direction of jumping.

Check which direction we can move in from the new element and check if that is not opposite to the direction that a jump was made.
If OK, move in that direction and make the jump direction a null string.
Continue until a node is reached.



for c1 in range(len(node_list)):
node_row=node_list[c1][0]
node_column=node_list[c1][1]
for branch_dir in node_iter_rule[c1].keys():
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
print branch_dir
# 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.
if (len(conn_matrix[next_node_row][next_node_column])>3):
if (conn_matrix[next_node_row][next_node_column].lower()[0:4] == "jump"):
jump_trace=conn_matrix[next_node_row][next_node_column]
if (jump_matrix[jump_trace][0][0] == [next_node_row, next_node_column]):
if (jump_matrix[jump_trace][1][1] == "left"):
next_node_row=jump_matrix[jump_trace][1][0][0]
next_node_column=jump_matrix[jump_trace][1][0][1] - 1
jump_executed="left"
elif (jump_matrix[jump_trace][1][1] == "right"):
next_node_row=jump_matrix[jump_trace][1][0][0]
next_node_column=jump_matrix[jump_trace][1][0][1] + 1
jump_executed="right"
elif (jump_matrix[jump_trace][1][1] == "up"):
next_node_row=jump_matrix[jump_trace][1][0][0] - 1
next_node_column=jump_matrix[jump_trace][1][0][1]
jump_executed="up"
elif (jump_matrix[jump_trace][1][1] == "down"):
next_node_row=jump_matrix[jump_trace][1][0][0] + 1
next_node_column=jump_matrix[jump_trace][1][0][1]
jump_executed="down"
elif (jump_matrix[jump_trace][1][0] == [next_node_row, next_node_column]):
if (jump_matrix[jump_trace][0][1] == "left"):
next_node_row=jump_matrix[jump_trace][0][0][0]
next_node_column=jump_matrix[jump_trace][0][0][1] - 1
jump_executed="left"
elif (jump_matrix[jump_trace][0][1] == "right"):
next_node_row=jump_matrix[jump_trace][0][0][0]
next_node_column=jump_matrix[jump_trace][0][0][1] + 1
jump_executed="right"
elif (jump_matrix[jump_trace][0][1] == "up"):
next_node_row=jump_matrix[jump_trace][0][0][0] - 1
next_node_column=jump_matrix[jump_trace][0][0][1]
jump_executed="up"
elif (jump_matrix[jump_trace][0][1] == "down"):
next_node_row=jump_matrix[jump_trace][0][0][0] + 1
next_node_column=jump_matrix[jump_trace][0][0][1]
jump_executed="down"
branch_iter.append([next_node_row, next_node_column])
# This temporary variable is to ensure, two advancements don't take place in one loop.
branch_proceed=0
if ((next_node_column>0) and branch_proceed==0):
# We are trying to go left, so check if we didn't jump right.
if (conn_matrix[next_node_row][next_node_column-1] != '' and jump_executed!="right"):
# Next check is if we are going backwards.
if not ([next_node_row, next_node_column-1] in branch_iter):
next_node_column=next_node_column-1
branch_proceed=1
# Set jump to null after a movement. We can't go back anyway.
jump_executed=""
if ((next_node_row>0) and branch_proceed==0):
# We are trying to go up, so check if we didn't jump down.
if (conn_matrix[next_node_row-1][next_node_column] != '' and jump_executed!="down"):
if not ([next_node_row-1, next_node_column] in branch_iter):
next_node_row=next_node_row-1
branch_proceed=1
# Set jump to null after a movement. We can't go back anyway.
jump_executed=""
if ((next_node_column<conn_columns-1) and branch_proceed==0):
# We are trying to go right, so check if we didn't jump left.
if (conn_matrix[next_node_row][next_node_column+1] != '' and jump_executed!="left"):
if not ([next_node_row, next_node_column+1] in branch_iter):
next_node_column=next_node_column+1
branch_proceed=1
# Set jump to null after a movement. We can't go back anyway.
jump_executed=""
if ((next_node_row<conn_rows-1) and branch_proceed==0):
# We are trying to go down, so check if we didn't jump up.
if (conn_matrix[next_node_row+1][next_node_column] != '' and jump_executed!="up"):
if not ([next_node_row+1, next_node_column] in branch_iter):
next_node_row=next_node_row+1
branch_proceed=1
# Set jump to null after a movement. We can't go back anyway.
jump_executed=""
# 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."
print [next_node_row, next_node_column]
break
else:
branch_iter.append([next_node_row, next_node_column])
branch_map[c1][node_list.index([next_node_row, next_node_column])].append(branch_iter)
print branch_iter
print "*"*70
view raw branch6.py hosted with ❤ by GitHub




Chose another test circuit.

Here two jumps are defined.

The output of the program is:
----------------------------------------------------------------------------------------------------
************************************************************
[[0, 5], [0, 8], [2, 1], [6, 0], [6, 3], [6, 5], [6, 7], [6, 12], [8, 12], [9, 5]]
************************************************************
************************************************************
************************************************************
************************************************************
down
[[0, 5], [1, 5], [2, 5], [3, 5], [4, 5], [5, 5], [6, 5]]
**********************************************************************
right
[[0, 5], [0, 6], [0, 7], [0, 8]]
**********************************************************************
left
[[0, 5], [0, 4], [0, 3], [0, 2], [0, 1], [1, 1], [2, 1]]
**********************************************************************
down
[[0, 8], [1, 8], [2, 8], [2, 2], [2, 1]]
**********************************************************************
right
[[0, 8], [0, 9], [0, 10], [0, 11], [0, 12], [1, 12], [2, 12], [3, 12], [4, 12], [5, 12], [6, 12]]
**********************************************************************
left
[[0, 8], [0, 7], [0, 6], [0, 5]]
**********************************************************************
right
[[2, 1], [2, 2], [2, 8], [1, 8], [0, 8]]
**********************************************************************
up
[[2, 1], [1, 1], [0, 1], [0, 2], [0, 3], [0, 4], [0, 5]]
**********************************************************************
left
[[2, 1], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0]]
**********************************************************************
down
[[6, 0], [7, 0], [8, 0], [9, 0], [9, 1], [9, 2], [9, 3], [9, 4], [9, 5]]
**********************************************************************
right
[[6, 0], [6, 1], [6, 2], [6, 3]]
**********************************************************************
up
[[6, 0], [5, 0], [4, 0], [3, 0], [2, 0], [2, 1]]
**********************************************************************
right
[[6, 3], [6, 4], [6, 5]]
**********************************************************************
up
[[6, 3], [5, 3], [5, 7], [6, 7]]
**********************************************************************
left
[[6, 3], [6, 2], [6, 1], [6, 0]]
**********************************************************************
down
[[6, 5], [7, 5], [8, 5], [9, 5]]
**********************************************************************
right
[[6, 5], [6, 6], [6, 7]]
**********************************************************************
up
[[6, 5], [5, 5], [4, 5], [3, 5], [2, 5], [1, 5], [0, 5]]
**********************************************************************
left
[[6, 5], [6, 4], [6, 3]]
**********************************************************************
right
[[6, 7], [6, 8], [6, 9], [6, 10], [6, 11], [6, 12]]
**********************************************************************
up
[[6, 7], [5, 7], [5, 3], [6, 3]]
**********************************************************************
left
[[6, 7], [6, 6], [6, 5]]
**********************************************************************
down
[[6, 12], [7, 12], [8, 12]]
**********************************************************************
up
[[6, 12], [5, 12], [4, 12], [3, 12], [2, 12], [1, 12], [0, 12], [0, 11], [0, 10], [0, 9], [0, 8]]
**********************************************************************
left
[[6, 12], [6, 11], [6, 10], [6, 9], [6, 8], [6, 7]]
**********************************************************************
down
[[8, 12], [9, 12], [10, 12], [11, 12], [11, 11], [11, 10], [11, 9], [11, 8], [11, 7], [11, 6], [11, 5], [10, 5], [9, 5]]
**********************************************************************
up
[[8, 12], [7, 12], [6, 12]]
**********************************************************************
left
[[8, 12], [8, 11], [8, 10], [8, 9], [8, 8], [9, 8], [9, 7], [9, 6], [9, 5]]
**********************************************************************
down
[[9, 5], [10, 5], [11, 5], [11, 6], [11, 7], [11, 8], [11, 9], [11, 10], [11, 11], [11, 12], [10, 12], [9, 12], [8, 12]]
**********************************************************************
right
[[9, 5], [9, 6], [9, 7], [9, 8], [8, 8], [8, 9], [8, 10], [8, 11], [8, 12]]
**********************************************************************
up
[[9, 5], [8, 5], [7, 5], [6, 5]]
**********************************************************************
left
[[9, 5], [9, 4], [9, 3], [9, 2], [9, 1], [9, 0], [8, 0], [7, 0], [6, 0]]
**********************************************************************
16


----------------------------------------------------------------------------------------------------

No comments:

Post a Comment