Message
Message
pygame.init()
class Fish:
def __init__(self, dimension, bounds):
self.position = np.random.uniform(bounds[0], bounds[1], dimension)
self.velocity = np.random.uniform(-1, 1, dimension)
self.best_position = np.copy(self.position)
self.best_value = float('inf')
def food_function(x):
return np.sum((x - np.array([5, 5])) ** 2)
history = []
positions = []
value = food_function(fish.position)
if value < fish.best_value:
fish.best_value = value
fish.best_position = fish.position
current_positions.append(fish.position)
positions.append(np.array(current_positions))
history.append(global_best_value)
print(f"Iteration {iteration+1}: Best Value = {global_best_value}")
if all_reached:
print("All fish have reached the food!")
break
return global_best_position, global_best_value, history, positions
# Draw fish
for fish in pos:
x = int((fish[0] - bounds[0]) / (bounds[1] - bounds[0]) * screen_size)
y = int((fish[1] - bounds[0]) / (bounds[1] - bounds[0]) * screen_size)
pygame.draw.circle(screen, (0, 0, 255), (x, y), 7) # Blue fish
# Draw food
food_x = int((5 - bounds[0]) / (bounds[1] - bounds[0]) * screen_size)
food_y = int((5 - bounds[0]) / (bounds[1] - bounds[0]) * screen_size)
pygame.draw.circle(screen, (255, 0, 0), (food_x, food_y), 10) # Red food
pygame.display.flip()
clock.tick(5)
pygame.quit()
if __name__ == "__main__":
num_fish = 30
dimension = 2
bounds = (-10, 10)
max_iterations = 1000
visualize_combined(positions, history)