找到最小距离两个值pythonic

时间:2019-08-13 20:10:27

标签: arrays python-3.x algorithm dictionary swarm

我在查找每个值到列表内另一个值的最小距离时遇到问题。每个代表鱼类的价值,每个鱼类都有视觉。

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

例如,我有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

但是结果是鱼类跟随的数量超过了种群数量,实际情况是只有3条鱼类,但能够循环3条以上的鱼类

请,我需要任何人对我的代码或算法的建议或批评,

非常感谢

1 个答案:

答案 0 :(得分:0)

已经快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的一部分。