当前位置: 首页>>代码示例>>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;未经允许,请勿转载。