當前位置: 首頁>>代碼示例>>Python>>正文


Python PassingData.totalDistance方法代碼示例

本文整理匯總了Python中pymodule.PassingData.totalDistance方法的典型用法代碼示例。如果您正苦於以下問題:Python PassingData.totalDistance方法的具體用法?Python PassingData.totalDistance怎麽用?Python PassingData.totalDistance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pymodule.PassingData的用法示例。


在下文中一共展示了PassingData.totalDistance方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: updateNewMonkeyToChosenSetDistanceVector

# 需要導入模塊: from pymodule import PassingData [as 別名]
# 或者: from pymodule.PassingData import totalDistance [as 別名]
	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
開發者ID:mjmontague,項目名稱:vervet-web,代碼行數:42,代碼來源:SampleStKittsMonkeysByGeography.py

示例2: constructNewMonkeyToChosenSetDistanceVector

# 需要導入模塊: from pymodule import PassingData [as 別名]
# 或者: from pymodule.PassingData import totalDistance [as 別名]
	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
開發者ID:mjmontague,項目名稱:vervet-web,代碼行數:38,代碼來源:SampleStKittsMonkeysByGeography.py


注:本文中的pymodule.PassingData.totalDistance方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。