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