首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在考虑尺寸的情况下选择最近的对象

如何在考虑尺寸的情况下选择最近的对象
EN

Stack Overflow用户
提问于 2020-09-27 11:47:24
回答 1查看 48关注 0票数 1

我正在做一个基于智能体的模拟。我想让每个智能体选择关注哪个其他智能体。有很多不同的输入,但这里我要问的是vision one。因此,考虑到大小,智能体必须看看哪个其他智能体最接近。他们应该关心更大的事情而不是更小的事情,一个离得很近的小代理不像一个离得很远的大代理那么大的问题。到目前为止,它看起来还不错,除了考虑到谁是最近的和大的也考虑了其他人在视觉范围之外。

到目前为止,我有这样的想法:

代码语言:javascript
复制
# both seen and viewer are two different groups
def sight(seen, viewer)
    for entity in viewer:
# This is needed later to decide which way to go, not used here
        currentDir = entity.Direction
# Figure out where the viewer is
        pos = pygame.math.Vector2(entity.rect.centerx, entity.rect.centery)
# Find out who is closest. 
# Uses viewer location and location of each agent in the other group to see who is closest. 
# At the end, divided by size*size of the agent in the other group (for height / width).
# Otherwise they'd pay attention to a tiny agent that's close by 
# instead of a huge agent that's just a bit further away.
        closeby = min([e for e in seen.sprites()], key=lambda e: (pos.distance_to(pygame.math.Vector2(e.rect.centerx, e.rect.centery)) / (e.size * e.size)))
        vector = pygame.math.Vector2((entity.rect.centerx - closeby.rect.centerx), (entity.rect.centery - closeby.rect.centery))
# Get the distance to that other agent
        distance = math.hypot(entity.rect.centerx - closeby.rect.centerx, entity.rect.centery - closeby.rect.centery)
# They can't see forever, so they have a sight limit
            if distance < entity.sight:
                Blah blah, rest of the code here

问题是:在他们选择" closeby“之后出现的"if distance < entity.sight”意味着他们可能会“选择”一个大代理作为近距离代理,然后因为距离太远而被淘汰。

想象一下:我是代理X。有一个我应该担心的小的人(代理Y)在我的视线范围内。然而,有个人(代理Z)真的很大,超出了我的视线范围。特工Z太大了,我选择他作为“近亲”。然后他在可视范围之外,所以没有人会被"if distance < entity.sight“选中。现在没有人被选中,即使代理Y足够接近,他也应该被选中。

我觉得要么是搜索

代码语言:javascript
复制
closeby = min([e for e in etc etc etc

应该限制在可视范围内的人(但我不知道如何做到这一点),或者如果被选为“近距离”的代理不在可视范围内,则应该选择第二接近的代理。如果它们再次超出可视范围,则应该选择第三最近的,依此类推,直到有人进入可视范围。但我也不确定该怎么做。

任何人都可以提供任何帮助来限制“min”([e for e in blah blah]),或者在第一个不意味着"entity.sight“标准的情况下选择该组中的下一个min,我们将非常感谢。我觉得第二种选择(对组进行迭代)更有可能实现,但我对python还很陌生,所以我不知道。

非常感谢大家!

EN

回答 1

Stack Overflow用户

发布于 2020-12-21 16:26:21

使用循环查找最近且也可见的元素:

代码语言:javascript
复制
import math
代码语言:javascript
复制
def sight(seen, viewer)
    for entity in viewer:
        currentDir = entity.Direction
        entityPos = pygame.math.Vector2(entity.rect.center)
    
        minE = None
        minCloseByDist = math.inf
        for e for e in seen.sprites():
            ePos = pygame.math.Vector2(e.rect.center)
            distance = entityPos.distance_to(ePos)
            if distance < entity.sight:
                closeByDist = distance / (e.size * e.size)
                if minE == None or closeByDist < minCloseByDist:
                    minE = e
                    minCloseByDist = minE

        if minE != None:
            # [...]

或者,您可以先查找视图中的所有元素,然后查找最近的元素:

代码语言:javascript
复制
def sight(seen, viewer)
    for entity in viewer:
        currentDir = entity.Direction
        entityPos = pygame.math.Vector2(entity.rect.center)
    
        inSight = [e for e in seen.sprites() if entityPos.distance_to(pygame.math.Vector2(e.rect.center)) < entity.sight]
        if inSight:
            closeby = min([e for e in inSight], key=lambda e: (pos.distance_to(pygame.math.Vector2(e.rect.center)) / (e.size * e.size)))

            # [...]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64084593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档