0

I have a problem with finding the minimum distance for each value to another value inside the list. Each value representative to fish and each fish has visual.

I can calculate distances, but the problem has started the value of fish is increased two times:

For example, I have 3 value of fish and check distance minimum among the value and the result is not equal with expected. the value 3 is changed to 6, multiply by 2

This algorithm is similar to the particle swarm algorithm, the name of this method is the artificial fish swarm algorithm (AFSA)

I have tried to code like this:

this object of fish:


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

However, I have used the object of fish and calculate distance then compare the distance with the visual is less than another fish visual:


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
------------->

I have expected likely

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

but the result is fish followed more than population, the real case is fish only three but is able to more than three fish in loop

Please, I need anyone your advice or criticism about my code or algorithm,

Thanks a lot

Subhanshuja
  • 390
  • 1
  • 3
  • 20

1 Answers1

0

It is has been almost 4 days, I tried to find a solution for own question.

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)

the fish is like this

[{'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]}]

The fish will collect by minimum distance both current fish and target fish like this:

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)))

The result:

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

But the new problem showed is the fish will there are one will follow one fish and one preyer. Sometimes one fish can be more than one fish, but the fish who do preyer is always one preyer cannot be more than one preyer

if there is another best solution, I'm still well come to find another answer, for example for time consume or optimization algorithm.

This code is part of basic AFSA.

Subhanshuja
  • 390
  • 1
  • 3
  • 20