找到最小距离都值 pythonic

finding the minimum distance both value pythonic

我无法找到列表中每个值与另一个值的最小距离。每个值代表鱼,每条鱼都有视觉。

我可以计算距离,但问题已经开始鱼的价值增加了​​两倍:

例如,我有3条鱼的值,并检查值中的最小距离,结果与预期不符。值 3 更改为 6,乘以 2

这个算法类似于粒子群算法,这个方法的名字叫人工鱼群算法(AFSA)

我试过这样编码:

这个鱼对象:


class Fish(object):

  def __init__(self, weight, visual):
    self._weight = weight
    self._visual = visual

  def __iter__(self):
    return self

  def set_weight(self, weight):
    self._weight = weight

  def get_weight(self):
    return self._weight

  def set_visual(self, visual):
    self._visual = visual

  def get_visual(self):
    return self._visual

  def set_step(self, step):
    self._step = step

  def get_step(self):
    return self._step

  def set_fitness(self, fitness):
    self._fitness = fitness

  def get_fitness(self):
    return self._fitness

但是,我使用了鱼这个对象,计算了距离,然后比较这个距离,视觉小于另一个鱼的视觉:


import random

if __name__ == '__main__':

  agent_size = 2
  weight_length = 2
  fish = None
  fish_population = []

  visual = [random.uniform(0, 1) for _ in range(agent_size)]
  weight = [[random.uniform(0, 1) for _ in range(weight_length)] for _ in range(agent_size)]

  for i in range(agent_size):
    fish = Fish(weight[i], visual[i], step[i], fitness[i], None)
    fish_population.append(fish)

-------------> # duplicate position of fish 
  for current in fish_population:
    for target in fish_population:
      if current != target:
        distance = current.get_visual() - target.get_visual()

        if distance < current.get_visual():
           # follow
        else:
           # no follow
------------->

我预计可能

fish_population = [fish_1, fish_2]
....
if (fish_1.visual() - fish_2.visual()) < fish_2.visual():
   fish_2 follow fish_1
else:
   fish_1 not follow fish_2

但结果是fish followed more than population,真实情况是fish只有3条但是在循环中能够超过3条fish

拜托,我需要任何人对我的代码或算法提出建议或批评,

非常感谢

已经快4天了,我试图为自己的问题找到解决方案。

agent_size = 3
weight_length = 2
fish_population = []

visual = [random.uniform(0, 1) for _ in range(agent_size)]
step = [random.uniform(0, 1) for _ in range(agent_size)]
weight = [[random.uniform(0, 1) for _ in range(weight_length)] for _ in range(agent_size)]
fitness = [random.uniform(0, 1) for _ in range(agent_size)]

for i in range(agent_size):
  fish = Fish(weight[i], visual[i], step[i], fitness[i])
  fish_population.append(fish)

# collec fish with minimum distance
collect_fish = []
for index_current, fish_current in enumerate(fish_population):

collect_fish = []
for index_current, fish_current in enumerate(fish_population):
  bucket_fish = {"current": fish_current, "target": [], "distance": []}
  for index_target, fish_target in enumerate(fish_population):
    if index_current != index_target:
      result = abs(fish_current.get_visual() - fish_target.get_visual())
      bucket_fish["distance"].append(result)
      bucket_fish["target"].append(fish_target)
  collect_fish.append(bucket_fish)
print(collect_fish)

鱼是这样的

[{'current': <__main__.Fish object at 0x7f290ef14208>, 'target': [<__main__.Fish object at 0x7f290ef141d0>, <__main__.Fish object at 0x7f290ef14160>], 'distance': [0.5637158347185028, 0.5452865173391022]}, {'current': <__main__.Fish object at 0x7f290ef141d0>, 'target': [<__main__.Fish object at 0x7f290ef14208>, <__main__.Fish object at 0x7f290ef14160>], 'distance': [0.5637158347185028, 0.018429317379400678]}, {'current': <__main__.Fish object at 0x7f290ef14160>, 'target': [<__main__.Fish object at 0x7f290ef14208>, <__main__.Fish object at 0x7f290ef141d0>], 'distance': [0.5452865173391022, 0.018429317379400678]}]

鱼会像这样按最小距离收集当前鱼和目标鱼:

fish_follower = []
fish_preyer = []
for index, fish in enumerate(collect_fish):
  min_distance = min(fish["distance"])
  print(min_distance, fish["distance"], fish["current"].get_visual())
  if min_distance < fish["current"].get_visual(): 

    index_following = fish["distance"].index(min_distance)
    fish_follower.append({"current": fish["current"], "target": fish["target"][index_following]})
  else:
    fish_preyer.append({"current": fish["current"]})

  print("follower: {} {}".format(fish_follower, len(fish_follower)))
  print("preyer: {} {}".format(fish_preyer, len(fish_preyer)))

结果:

follower: [{'current': <__main__.Fish object at 0x7f57fa826dd8>, 'target': <__main__.Fish object at 0x7f57fa6d62e8>}, {'current': <__main__.Fish object at 0x7f57fa6d62e8>, 'target': <__main__.Fish object at 0x7f57fa6d6278>}] 2
preyer: [{'current': <__main__.Fish object at 0x7f57fa6d6278>}] 1

但是新出现的问题是鱼会跟着一只鱼和一只猎物。有时一条鱼可以不止一条鱼,但是捕食的鱼永远是一个捕食者不能超过一个捕食者

如果有其他最佳解决方案,我还是会来寻找其他答案,例如时间消耗或优化算法。

此代码是基本 AFSA 的一部分。