def calcSegs(startNode, nodes): """Calculates distance from startNode to each node in nodes, and returns a list of network.Seg instances""" segs = [] for node in nodes: if node.getID() == startNode.getID(): continue dist = ((startNode.getX() - node.getX())**2 + (startNode.getY() - node.getY())**2)**(.5) segs.append(network.Seg(None, startNode, node, dist)) return segs
def generateSegments(centers, searchRadius): #centers=centerDict segments = [] #nodeCopy = centers.copy() nodeCopy = copy.deepcopy(centers) segID = 0 for startNode in centers.values(): del nodeCopy[startNode.getID()] for endNode in nodeCopy.values(): dist = ((startNode.getX() - endNode.getX())**2 + (startNode.getY() - endNode.getY())**2)**(.5) if dist < searchRadius: segments.append(network.Seg(segID, startNode, endNode, dist)) segID += 1 return segments
def addLVSeg(tree,centers,nodesByClusterID):#single points line from the root SegID=1000000 for centerID in centers.keys(): tree._nodesByNetID[centerID]=[] tree._network[centerID]=[] for node in nodesByClusterID[centerID]: length=((node.getX()-centers[centerID].getX())**2+ (node.getY()-centers[centerID].getY())**2)**(.5) newSeg=network.Seg(SegID, node, centers[centerID], length) tree._netIDByNode[node] = centerID tree._nodesByNetID[centerID].append(node) tree._network[centerID].append(newSeg) #print tree.getEdges() return tree
def addLVSeg(tree, centers, nodesByClusterID): #single points line from the root SegID = 1000000 meanSeg = {} for centerID in centers.keys(): tree._nodesByNetID[centerID] = [] tree._network[centerID] = [] sumLen = 0 count = 0 for node in nodesByClusterID[centerID]: length = ((node.getX() - centers[centerID].getX())**2 + (node.getY() - centers[centerID].getY())**2)**(.5) newSeg = network.Seg(SegID, node, centers[centerID], length) tree._netIDByNode[node] = centerID tree._nodesByNetID[centerID].append(node) tree._network[centerID].append(newSeg) sumLen += length count += 1 mean = sumLen / count meanSeg[centerID] = mean #print tree.getEdges() return tree, meanSeg
def CMST(households,capacity,root): #Connects hhs directly to the root first treeSegments={} distDict={} households_Copy=copy.deepcopy(households) SegID=10000000 root_Copy=copy.deepcopy(root) newRootID=root_Copy.getID()*(-1)-100 #### not to be confused with the same nodeID root_Copy.setID(newRootID) maxTvalue=0 branchNodeByNode={}# which branch is the node on? nodesByBranchNode=collections.defaultdict(list )# what are the nodes on a spesific branch? for node in households_Copy: length=((node.getX()-root_Copy.getX())**2+(node.getY()-root_Copy.getY())**2)**(.5) treeSegments[(node.getID(),newRootID)]=network.Seg(SegID, node, root_Copy, length) SegID+=1 node.setWeight(length) branchNodeByNode[node]=node nodesByBranchNode[node].append(node) distDict[(newRootID,node.getID())]=length distDict[(node.getID(),newRootID)]=length for node1 in households_Copy: for node2 in households_Copy: if node1==node2: continue else: distance=((node1.getX()-node2.getX())**2+(node1.getY()-node2.getY())**2)**(.5) distDict[(node1.getID(), node2.getID())]=distance Tvalue=treeSegments[(node1.getID(),newRootID)].getWeight()-distance if ((node2.getWeight()+distance)<=capacity): newT=T(node1,node1,node2,Tvalue) if (newT.getValue()>=maxTvalue): maxTObject=newT maxTvalue=newT.getValue() totalLVCost=0 for segment in treeSegments.values(): totalLVCost=totalLVCost+segment.getWeight() #print distDict.keys() while(maxTvalue>0): maxTvalue=0 SegID+=1 node1=maxTObject.getAddedSegmentNode1() # node1 and node2 of new segment #print "node1", node1 node1Weigth=node1.getWeight() node2=maxTObject.getAddedSegmentNode2() #print "node2", node2 #delete root segment of branch del treeSegments[(maxTObject.getRemovedSegmentNode().getID(),newRootID)] #if node1 is the first node in the branch if node1==branchNodeByNode[node1]: tempWeight=node1.getWeight() # I need this becouse node1 is updated first and it effects the others for node in nodesByBranchNode[branchNodeByNode[node1]]: node.setWeight(node.getWeight()-tempWeight+ node2.getWeight()+distDict[(node1.getID(),node2.getID())])# digerlerinin de set olmasi lazim #if not things get complicated and we need dfs else: segList=buildAssocDict(treeSegments.values()) # daha efficient yapmak icin sadece update edebilirim depthFirstSet(node1,node2,root_Copy,segList,distDict) # root (node1) hala icinde unutma #tree updated after weights are set treeSegments[(node1.getID(), node2.getID())]=network.Seg(SegID,node1, node2,distDict[(node1.getID(),node2.getID())]) # Update dictionaries nodesByBranchNode[branchNodeByNode[node2]].extend(nodesByBranchNode.pop(branchNodeByNode[node1])) for node in nodesByBranchNode[branchNodeByNode[node2]]: branchNodeByNode[node]=branchNodeByNode[node2] # Rebuilt TStore & select maxT object for node1 in households_Copy: # #print "node1", node1 for node2 in households_Copy: if node1==node2 or branchNodeByNode[node1]==branchNodeByNode[node2]: continue else: maxDistFromNode1=maxDistFromNode(node1,root_Copy,treeSegments,distDict) #print "maaxx",maxDistFromNode1 if (node2.getWeight()+distDict[node1.getID(),node2.getID()]+maxDistFromNode1<=capacity): #1 2ye baslansa ne olur? #print "TTTTT", Tvalue Tvalue=treeSegments[(branchNodeByNode[node1].getID(),newRootID)].getWeight()-distDict[(node1.getID(),node2.getID())] if Tvalue>=maxTvalue: maxTvalue=Tvalue maxTObject=T(branchNodeByNode[node1],node1,node2,Tvalue) #print maxTvalue #print maxTObject totalLVCost=0 for segment in treeSegments.values(): totalLVCost=totalLVCost+segment.getWeight() del root_Copy gc.collect() #print treeSegments.keys() #print households_Copy return treeSegments, totalLVCost