Skip to content

Commit

Permalink
Got the traverseNeighbor recursion to work.
Browse files Browse the repository at this point in the history
  • Loading branch information
harrisj committed Apr 3, 2011
1 parent 478f331 commit ab73c9e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
20 changes: 15 additions & 5 deletions equitube/equitube.py
Expand Up @@ -29,6 +29,7 @@
from field import Field
from plot import Plot
from tube import Tube
import time

class Equitube:
def __init__(self,argv):
Expand Down Expand Up @@ -86,25 +87,34 @@ def Run(self):
for key in field.getTubes().keys():
if field.getTubes()[key].getParams()['P'][0] <= 0:
start.append(key),","
if field.getTubes()[key].getParams()['Q'][0] >= 10:
if field.getTubes()[key].getParams()['Q'][0] >= self._length:
stop.append(key)
print "Starting Tubes:",start
print "Stopping Tubes:",stop
print "------------------------------------------"

#time.sleep(10)
end = 0
while end < 1:
field.calculateIntercepts()
point_forces = field.getPointForces()

for key in field.getTubes().keys():
print key,":",field.getTubes()[key].getParams()['neighbours'].keys()
print key,":",field.getTubes()[key].getParams()['neighbors'].keys()
print "=================================="
traverses = 0
neighbor_dict = {}
roots = []
leaves = []
for index in tubes.keys():
neighbor_dict[index] = list()
neighbor_dict[index] = tubes[index].getParams()['neighbors'].keys()
if tubes[index].getParams()['P'][0] <= 0:
print index
traverses += field.traverseNeighbours(index,[])
roots.append(index)
if tubes[index].getParams()['Q'][0] >= self._length:
leaves.append(index)
#print index
for index in roots:
traverses += field.traverseNeighbors(index,neighbor_dict,leaves,())
print traverses

#if end % 1 == 0:
Expand Down
36 changes: 20 additions & 16 deletions equitube/field/field.py
Expand Up @@ -108,11 +108,11 @@ def getPointForces(self):
else:
Fq += -1*self._k*q_dist

neighbour_dict = self._tubes[tube_id].getParams()['neighbours']
for index in neighbour_dict.keys():
xint = neighbour_dict[index][1]
neighbor_dict = self._tubes[tube_id].getParams()['neighbors']
for index in neighbor_dict.keys():
xint = neighbor_dict[index][1]
yint = slope*xint + self._tubes[tube_id].getParams()['b']
phi = neighbour_dict[index][0]
phi = neighbor_dict[index][0]
torque = 1/np.sin(phi)-1
slope2 = self._tubes[index].getParams()['m']

Expand Down Expand Up @@ -323,30 +323,34 @@ def calculateIntercepts(self):
angle = tube1['theta'] - tube2['theta']
else:
angle = tube2['theta'] - tube1['theta']
self._tubes[dex1].addNeighbours(dex2,angle,x)
self._tubes[dex1].addNeighbors(dex2,angle,x)
return None

def traverseNeighbours(self, index, prev_tubes):
def traverseNeighbors(self,index,neighbor_dict,leaves,prev_tubes):
"""Calculates the total number of paths from left to right.
All tubes with ( P[0] <= 0 ) intersect the left side of
the field. These are the tubes initially passed to this
function. This function then gathers neighbours and pass
function. This function then gathers neighbors and pass
them recursively. prev_tubes is a list of tube ID #'s.
It keeps track of the previous tubes along a given path of
recursion. It prevents backtracking.
"""
prev_tubes.append(index)
print prev_tubes

neighbours = self._tubes[index].getParams()['neighbours']
neighbour_key_list = neighbours.keys()
me = (index,)
test = prev_tubes + me
#print test
#prev_tubes.append(index)
#print prev_tubes
##neighbors = self._tubes[index].getParams()['neighbors']
##neighbor_key_list = neighbors.keys()
count = 0
if self._tubes[index].getParams()['Q'][0] >= self._length:
##if self._tubes[index].getParams()['Q'][0] >= self._length:
if leaves.count(index) == 1:
count += 1
print prev_tubes
for key in neighbour_key_list:
print "HIT!",test
##for key in neighbor_key_list:
for key in neighbor_dict[index]:
if prev_tubes.count(key) > 0:
continue
count += self.traverseNeighbours(key,prev_tubes)
count += self.traverseNeighbors(key,neighbor_dict,leaves,test)
return count
12 changes: 6 additions & 6 deletions equitube/tube/tube.py
Expand Up @@ -38,7 +38,7 @@ def __init__(self):
self._l = None
self._b = None
self._theta = None
self._neighbours = {}
self._neighbors = {}
self._cm = [] #[Xcm,Ycm]
self._P = [] #[Xp,Yp]
self._Q = [] #[Xq,Yq]
Expand All @@ -55,7 +55,7 @@ def createLine(self,slope,center):
self._l = random.uniform(2,5)
self._m = slope
self._cm = center
self._neighbours = {}
self._neighbors = {}
self._theta = np.arctan(self._m)
self._b = self._cm[1] - self._m*self._cm[0]

Expand All @@ -72,14 +72,14 @@ def createLine(self,slope,center):

return None

def addNeighbours(self, key, angle, x_intercept):
def addNeighbors(self, key, angle, x_intercept):
""" Function for adding neighbors
The Field class keeps track of the intersecting
tubes via a dict'd list. {key:[angle,x_intecept]}
"""
self._neighbours[key] = list()
self._neighbours[key] = [angle,x_intercept]
self._neighbors[key] = list()
self._neighbors[key] = [angle,x_intercept]

return None

Expand All @@ -90,7 +90,7 @@ def getParams(self):
attributes, and returns them as a dict.
"""

params = {'m':self._m,'l':self._l,'b':self._b,'cm':self._cm,'theta':self._theta,'P':self._P,'Q':self._Q,'neighbours':self._neighbours}
params = {'m':self._m,'l':self._l,'b':self._b,'cm':self._cm,'theta':self._theta,'P':self._P,'Q':self._Q,'neighbors':self._neighbors}

return params

0 comments on commit ab73c9e

Please sign in to comment.