From ab73c9ebda35e89f0a5ed8abdb1dbf14660cec08 Mon Sep 17 00:00:00 2001 From: harrisj Date: Sat, 2 Apr 2011 23:59:14 -0500 Subject: [PATCH] Got the traverseNeighbor recursion to work. --- equitube/equitube.py | 20 +++++++++++++++----- equitube/field/field.py | 36 ++++++++++++++++++++---------------- equitube/tube/tube.py | 12 ++++++------ 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/equitube/equitube.py b/equitube/equitube.py index c87f5f8..81eebee 100644 --- a/equitube/equitube.py +++ b/equitube/equitube.py @@ -29,6 +29,7 @@ from field import Field from plot import Plot from tube import Tube +import time class Equitube: def __init__(self,argv): @@ -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: diff --git a/equitube/field/field.py b/equitube/field/field.py index 9946590..b3be5e6 100644 --- a/equitube/field/field.py +++ b/equitube/field/field.py @@ -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'] @@ -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 diff --git a/equitube/tube/tube.py b/equitube/tube/tube.py index d6f78e1..b492a69 100644 --- a/equitube/tube/tube.py +++ b/equitube/tube/tube.py @@ -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] @@ -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] @@ -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 @@ -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