def updateNewMonkeyToChosenSetDistanceVector(self, graph=None, oldShortestDistanceVectorData=None, \ newlyChosenMonkeyID=None, minShortestDistance=0.4): """ 2012.11.26 supplement to constructNewMonkeyToChosenSetDistanceVector() """ oldShortestDistanceToChosenSet_monkeyID_ls = oldShortestDistanceVectorData.shortestDistanceToChosenSet_monkeyID_ls sys.stderr.write("Updating the shortest-distance to chosen set vector (%s elements) because monkeys %s has been added into the chosen set ..."%\ (len(oldShortestDistanceToChosenSet_monkeyID_ls), newlyChosenMonkeyID)) returnData = PassingData(shortestDistanceToChosenSet_monkeyID_ls = []) counter = 0 real_counter = 0 spanStartPos = 0 monkey2ID = newlyChosenMonkeyID for element in oldShortestDistanceToChosenSet_monkeyID_ls: shortestDistance, monkey1ID, shortestDistanceToThisChosenMonkey = element[:3] if monkey1ID!=monkey2ID: #skip the chosen monkey counter += 1 #get the shortest path #short path in graph, sum all the edge weights, A* star algorithm seems to be much faster (>100%) than default shortest_path. #distance = nx.shortest_path_length(graph, source=monkey1ID, target=monkey2ID, weight='weight') distance = nx.astar_path_length(graph, source=monkey1ID, target=monkey2ID, weight='weight') if distance<shortestDistance: shortestDistance = distance shortestDistanceToThisChosenMonkey = monkey2ID element = (shortestDistance, monkey1ID, shortestDistanceToThisChosenMonkey) real_counter += 1 if shortestDistance>=minShortestDistance: returnData.shortestDistanceToChosenSet_monkeyID_ls.append(element) spanStartPos += shortestDistance #increase the distance. returnData.totalDistance = spanStartPos sys.stderr.write("%s (out of %s) monkeys changed their shortest distance to the chosen set (%s elements). \n\ total distance to the chosen set is %s.\n"%\ (real_counter, counter, len(returnData.shortestDistanceToChosenSet_monkeyID_ls), spanStartPos)) return returnData
def constructNewMonkeyToChosenSetDistanceVector(self, graph=None, preChosenMonkeyIDSet=None, minShortestDistance=0.4): """ 2012.11.26 each monkey is assigned a probability mass based on its geographic distance to the closest monkey in the preChosenMonkeyIDSet. """ sys.stderr.write("Constructing distance vector from new monkey to %s chosen monkeys (%s total monkeys) ..."%\ (len(preChosenMonkeyIDSet), len(graph))) counter = 0 real_counter = 0 spanStartPos = 0 unChosenMonkeyIDSet = set(graph.nodes()) - preChosenMonkeyIDSet preChosenAndInGraphMonkeyIDSet = set(graph.nodes()) - unChosenMonkeyIDSet returnData = PassingData(shortestDistanceToChosenSet_monkeyID_ls = []) for monkey1ID in unChosenMonkeyIDSet: counter += 1 shortestDistance = None shortestDistanceToThisChosenMonkey = None for monkey2ID in preChosenAndInGraphMonkeyIDSet: #get the shortest path #distance = nx.shortest_path_length(graph, source=monkey1ID, target=monkey2ID, weight='weight') distance = nx.astar_path_length(graph, source=monkey1ID, target=monkey2ID, weight='weight') #short path in graph, sum all the edge weights if shortestDistance is None or distance<shortestDistance: shortestDistance = distance shortestDistanceToThisChosenMonkey = monkey2ID if shortestDistance is not None and shortestDistance>=minShortestDistance: #ignore monkeys from same sites returnData.shortestDistanceToChosenSet_monkeyID_ls.append((shortestDistance, monkey1ID, shortestDistanceToThisChosenMonkey)) real_counter += 1 spanStartPos += shortestDistance #increase the distance. returnData.totalDistance = spanStartPos sys.stderr.write("%s (out of %s candidate) monkeys added into candidate vector, total distance to the chosen set is %s.\n"%\ (real_counter, counter, spanStartPos)) return returnData