本文整理汇总了Python中utils.distance函数的典型用法代码示例。如果您正苦于以下问题:Python distance函数的具体用法?Python distance怎么用?Python distance使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了distance函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: next_step
def next_step(self, things):
'''Zombies attack if in range, else move in direction of players.'''
action = None
# possible targets for movement and attack
humans = [thing for thing in things.values()
if isinstance(thing, Player)]
positions = possible_moves(self.position, things)
if humans:
# targets available
target = closest(self, humans)
if distance(self.position, target.position) < self.weapon.max_range:
# target in range, attack
action = 'attack', target
else:
# target not in range, _try_ to move
if positions:
by_distance = lambda position: distance(target.position,
position)
best_position = sorted(positions, key=by_distance)[0]
action = 'move', best_position
else:
# no targets, just wander around
if positions:
action = 'move', random.choice(positions)
return action
示例2: connect
def connect( self, pos ):
"""Search for any user-drawn platforms near pos. If one is found see if pos is
close to either of its endpoints. If it is near an endpoint, set pos to that endpoint."""
MAX_DIST = 30
shape = self.physics_interface.space.nearest_point_query_nearest( Vec2d( *pos ),
MAX_DIST,
COLLTYPE_USERPLAT | COLLTYPE_LAVA | COLLTYPE_USERCURVE )
if shape:
# Make sure magnets only connect two endpoints from different lines.
touching_line = self.physics_interface.smap[ shape ]
if touching_line != self.target_line and \
( shape.collision_type == COLLTYPE_USERPLAT or shape.collision_type == COLLTYPE_LAVA or shape.collision_type == COLLTYPE_USERCURVE ):
if shape.collision_type == COLLTYPE_LAVA:
start = touching_line.points[:2]
end = touching_line.points[2:]
else:
start = touching_line.get_start()
end = touching_line.get_end()
if distance( start, pos ) <= MAX_DIST:
pos = Vec2d( *start )
elif distance( end, pos ) <= MAX_DIST:
pos = Vec2d( *end )
return pos
示例3: work
def work(self):
if not self.config.catch_pokemon:
return
if 'catchable_pokemons' in self.cell and len(self.cell['catchable_pokemons']) > 0:
logger.log('Something rustles nearby!')
# Sort all by distance from current pos- eventually this should
# build graph & A* it
self.cell['catchable_pokemons'].sort(
key=
lambda x: distance(self.position[0], self.position[1], x['latitude'], x['longitude']))
user_web_catchable = 'web/catchable-%s.json' % (self.config.username)
for pokemon in self.cell['catchable_pokemons']:
with open(user_web_catchable, 'w') as outfile:
json.dump(pokemon, outfile)
return self.catch_pokemon(self.cell['catchable_pokemons'][0])
if 'wild_pokemons' in self.cell and len(self.cell['wild_pokemons']) > 0:
# Sort all by distance from current pos- eventually this should
# build graph & A* it
self.cell['wild_pokemons'].sort(
key=
lambda x: distance(self.position[0], self.position[1], x['latitude'], x['longitude']))
return self.catch_pokemon(self.cell['wild_pokemons'][0])
示例4: _get_nearest_fort_on_lure_way
def _get_nearest_fort_on_lure_way(self, forts):
if not self.lure_attraction:
return None, 0
lures = filter(lambda x: True if x.get("lure_info", None) != None else False, forts)
if len(lures):
dist_lure_me = distance(
self.bot.position[0], self.bot.position[1], lures[0]["latitude"], lures[0]["longitude"]
)
else:
dist_lure_me = 0
if dist_lure_me > 0 and dist_lure_me < self.lure_max_distance:
self.lure_distance = dist_lure_me
for fort in forts:
dist_lure_fort = distance(
fort["latitude"], fort["longitude"], lures[0]["latitude"], lures[0]["longitude"]
)
dist_fort_me = distance(fort["latitude"], fort["longitude"], self.bot.position[0], self.bot.position[1])
if dist_lure_fort < dist_lure_me and dist_lure_me > dist_fort_me:
return fort, dist_lure_me
if dist_fort_me > dist_lure_me:
break
return lures[0], dist_lure_me
else:
return None, 0
示例5: work
def work(self):
if 'catchable_pokemons' in self.bot.cell and len(self.bot.cell['catchable_pokemons']) > 0:
# Sort all by distance from current pos- eventually this should
# build graph & A* it
self.bot.cell['catchable_pokemons'].sort(
key=
lambda x: distance(self.bot.position[0], self.bot.position[1], x['latitude'], x['longitude'])
)
for pokemon in self.bot.cell['catchable_pokemons']:
with open(user_web_catchable, 'w') as outfile:
json.dump(pokemon, outfile)
self.emit_event(
'catchable_pokemon',
level='debug',
data={
'pokemon_id': pokemon['pokemon_id'],
'spawn_point_id': pokemon['spawn_point_id'],
'encounter_id': pokemon['encounter_id'],
'latitude': pokemon['latitude'],
'longitude': pokemon['longitude'],
'expiration_timestamp_ms': pokemon['expiration_timestamp_ms'],
}
)
return self.catch_pokemon(self.bot.cell['catchable_pokemons'].pop(0))
if 'wild_pokemons' in self.bot.cell and len(self.bot.cell['wild_pokemons']) > 0:
# Sort all by distance from current pos- eventually this should
# build graph & A* it
self.bot.cell['wild_pokemons'].sort(
key=
lambda x: distance(self.bot.position[0], self.bot.position[1], x['latitude'], x['longitude']))
return self.catch_pokemon(self.bot.cell['wild_pokemons'].pop(0))
示例6: find_closest
def find_closest(location, centroids):
"""Return the item in CENTROIDS that is closest to LOCATION. If two
centroids are equally close, return the first one.
>>> find_closest([3, 4], [[0, 0], [2, 3], [4, 3], [5, 5]])
[2, 3]
"""
return [x for x in centroids if distance(location,x) == min([distance(location,y) for y in centroids])][0]
示例7: findClosestAtom
def findClosestAtom( frame, coords ):
extendedCoords = ( coords[0], coords[1], 0 )
minDist = distance( extendedCoords , frame.atoms[0].x0 )
minName = frame.atoms[0].symbol
for atom in frame.atoms:
if distance( extendedCoords, atom.x0 ) < minDist:
minDist = distance( extendedCoords, atom.x0 )
minName = atom.symbol
return minName
示例8: best_parent
def best_parent(step):
ssga = world.ssga
[motherId, parentId] = ssga.getParents(world.nam_tsize)
population = ssga.population()
mother = population[motherId]
parent = population[parentId]
distances = [utils.distance(population[i], mother) for i in range(world.popsize)]
max_distances = np.array(distances).max()
distance = utils.distance(parent, mother)
assert distance == max_distances, "Distance from parent %f is different than maximum %f" % (distance, max_distances)
示例9: find_closest
def find_closest(location, centroids):
"""Return the item in CENTROIDS that is closest to LOCATION. If two
centroids are equally close, return the first one.
>>> find_closest([3, 4], [[0, 0], [2, 3], [4, 3], [5, 5]])
[2, 3]
"""
min_dist = lambda x: distance(location, x)
min_distance = min_dist(min(centroids, key = min_dist))
return [x for x in centroids if distance(location, x) == min_distance][0]
示例10: find_closest
def find_closest(location, centroids):
"""Return the centroid in centroids that is closest to location. If
multiple centroids are equally close, return the first one.
>>> find_closest([3.0, 4.0], [[0.0, 0.0], [2.0, 3.0], [4.0, 3.0], [5.0, 5.0]])
[2.0, 3.0]
"""
# BEGIN Question 3
dist = [distance(location, i) for i in centroids]
res = [item for item in centroids if distance(location, item) == min(dist)][0]
return res
示例11: find_closest
def find_closest(location, centroids):
"""Return the item in CENTROIDS that is closest to LOCATION. If two
centroids are equally close, return the first one.
>>> find_closest([3, 4], [[0, 0], [2, 3], [4, 3], [5, 5]])
[2, 3]
"""
"*** YOUR CODE HERE ***"
min_dis=min(distance(location, i) for i in centroids)
for i in range(len(centroids)):
if distance(location, centroids[i])==min_dis:
return centroids[i]
示例12: work
def work(self):
num_catchable_pokemon = 0
if 'catchable_pokemons' in self.bot.cell:
num_catchable_pokemon = len(self.bot.cell['catchable_pokemons'])
num_wild_pokemon = 0
if 'wild_pokemons' in self.bot.cell:
num_wild_pokemon = len(self.bot.cell['wild_pokemons'])
num_available_pokemon = num_catchable_pokemon + num_wild_pokemon
if num_catchable_pokemon > 0:
# Sort all by distance from current pos- eventually this should
# build graph & A* it
self.bot.cell['catchable_pokemons'].sort(
key=
lambda x: distance(self.bot.position[0], self.bot.position[1], x['latitude'], x['longitude'])
)
user_web_catchable = os.path.join(_base_dir, 'web', 'catchable-{}.json'.format(self.bot.config.username))
for pokemon in self.bot.cell['catchable_pokemons']:
with open(user_web_catchable, 'w') as outfile:
json.dump(pokemon, outfile)
self.emit_event(
'catchable_pokemon',
level='debug',
data={
'pokemon_id': pokemon['pokemon_id'],
'spawn_point_id': pokemon['spawn_point_id'],
'encounter_id': pokemon['encounter_id'],
'latitude': pokemon['latitude'],
'longitude': pokemon['longitude'],
'expiration_timestamp_ms': pokemon['expiration_timestamp_ms'],
}
)
self.catch_pokemon(self.bot.cell['catchable_pokemons'].pop(0))
if num_catchable_pokemon > 1:
return WorkerResult.RUNNING
else:
return WorkerResult.SUCCESS
if num_available_pokemon > 0:
# Sort all by distance from current pos- eventually this should
# build graph & A* it
self.bot.cell['wild_pokemons'].sort(
key=
lambda x: distance(self.bot.position[0], self.bot.position[1], x['latitude'], x['longitude']))
self.catch_pokemon(self.bot.cell['wild_pokemons'].pop(0))
if num_catchable_pokemon > 1:
return WorkerResult.RUNNING
else:
return WorkerResult.SUCCESS
示例13: similarity
def similarity(self, stone):
""" Computes similarity with another stone """
dc = distance(self.center, stone.center)
ds = distance(self.size, stone.size)
da = abs(self.angle - stone.angle)
if dc > 20:
return 0.0
if ds > 20:
return 0.0
if da > 20:
return 0.0
return 1.0 - max([dc / 20.0, ds / 20.0, da / 20.0])
示例14: calcHist
def calcHist(frame, extendedAtomsIter, binSize, boxSize, options):
bins = [0 for _ in range(int(boxSize / (2 * binSize)))]
extendedAtoms = [atom for atom in extendedAtomsIter]
for atom in frame.atoms:
if atom.symbol == options.name1 or options.name1 == 'all':
for atomLst in extendedAtoms:
for atom2 in atomLst:
if options.name2 == "all" or options.name2 == atom2.symbol:
if distance(atom.x0, atom2.x0) < boxSize / 2 and distance(atom.x0, atom2.x0) != 0:
for i in range(len(bins)):
if i * binSize > distance(atom.x0, atom2.x0):
bins[i] += 1
return bins
示例15: find_closest
def find_closest(location, centroids):
"""Return the item in CENTROIDS that is closest to LOCATION. If two
centroids are equally close, return the first one.
>>> find_closest([3, 4], [[0, 0], [2, 3], [4, 3], [5, 5]])
[2, 3]
"""
"*** YOUR CODE HERE ***"
min_dis = distance(location, centroids[0])
min_cen = centroids[0]
for li in centroids:
if distance(location, li) < min_dis:
min_cen, min_dis = li, distance(location, li)
return min_cen