如何为蚂蚁模拟更有效地存储和使用值



我正在做一个蚂蚁模拟,我需要存储所有的信息素(int值(

以下是它的工作原理:

  • 当蚂蚁进入睡眠时,它会沉积10个信息素。(已完成(
  • 每个蜱虫的信息素值都会降低一,并且不会低于0

我需要一些东西来做最后一件事
这是我的代码:

# world is the map on a 1000x1000 array of 0 at the start
def evaporate():
for x in range(len(world)):
for y in range(len(world[0])):
if world[x][y] > 0:
world[x][y] -= 1

我认为有更好的方法来存储映射值,但我找不到有效的方法
如果你不能使用任何库,那会更好,因为我想看看我的程序能做什么,但如果它更高效或更容易,那没关系

如果有错别字,告诉我我会改正的。(英语不是我的主要语言(
谢谢你抽出时间。

一个1000乘1000的世界有1.000.000个瓦片。一个是你的基地,几个是蚂蚁,有些里面有信息素

不存储完整的地图,只存储数据所在的位置。你可以用一本以(x,y(为关键字的字典。

由于信息素水平为10代表蚂蚁,你可以这样做:

class ant():
import random
# movement delta in 8 directions
_delta_move = [(dx,dy) for dx in range(-1,2) for dy in range(-1,2)]
def __init__(self, x, y, maxage = 5):
self.__x = x
self.__y = y
self.age = maxage
def getPos(self):
return (self.__x, self.__y)
def kill(self):
self.age = 0
def random_move(self):
# every move reduces ants age, when 0 it dies
self.age -= 1
if self.age > 0:
d = random.choice(ant._delta_move)
self.__x += d[0]
self.__y += d[1]
return self.getPos()
return None # dead

创建蚂蚁列表和地图并传播移动:

mapsize = 10
# helper 
def print_map(m):
"""prints the map, if a pheromon level is 10 the ant is in this field"""
for y in range(mapsize):
print("  #",end="")
for x in range(mapsize):
value = m.get( (x,y), " ")
if value == 10:
value = "a"
elif value == 0:
value = " "
print(value, end="")
print()

# list of ants
ants = [ ant(5, 5, 6) for _ in range(5 )]
# initial map, 10 pheromones is max per field
ant_map = { a.getPos():10 for a in ants}
while (ants):
print_map(ant_map)
input("Enter for next round")
# decay pheromones
done = [] # store keys to delete, avoid changing dict while iterating
for key,value in ant_map.items():
if value > 0:
ant_map[key] -= 1
else:
done.append(key)
for k in done:
del ant_map[k]
# remove dead ants
ants = [a for a in ants if a.age > 0]
# move ants and add new pheromones
for a in ants:
# move ant
new_pos = a.random_move()
if new_pos and 0 <= new_pos[0] < mapsize and 0 <= new_pos[1] < mapsize:
ant_map[new_pos] = 10
else:
a.kill()
print_map(ant_map)

输出:

#          
#          
#          
#          
#          
#     a    
#          
#          
#          
#          
Enter for next round 
#          
#          
#          
#          
#    a a   
#     aa   
#          
#          
#          
#          
Enter for next round
#          
#          
#          
#    a     
#    a 9a  
#     9a   
#      a   
#          
#          
#          
Enter for next round 
#          
#          
#          
#    9aa   
#   a9a89  
#     89   
#     a9   
#          
#          
#          
Enter for next round 
#          
#          
#    a a   
#    8a9   
#   a8978  
#     7a   
#     98   
#          
#          
#          
Enter for next round 
#          
#          
#    9a9   
#  a 7a8   
#   97a67  
#     6a   
#     87   
#          
#          
#          
Enter for next round 
#          
#          
#    898   
#  9 697   
#   86956  
#     59   
#     76   
#          
#          
#          
Enter for next round 
#          
#          
#    787   
#  8 586   
#   75845  
#     48   
#     65   
#          
#          
#          
Enter for next round
#          
#          
#    676   
#  7 475   
#   64734  
#     37   
#     54   
#          
#          
#          
Enter for next round
#          
#          
#    565   
#  6 364   
#   53623  
#     26   
#     43   
#          
#          
#          
Enter for next round 
#          
#          
#    454   
#  5 253   
#   42512  
#     15   
#     32   
#          
#          
#          
Enter for next round  
#          
#          
#    343   
#  4 142   
#   314 1  
#      4   
#     21   
#          
#          
#          
Enter for next round  
#          
#          
#    232   
#  3  31   
#   2 3    
#      3   
#     1    
#          
#          
#          
Enter for next round 
#          
#          
#    121   
#  2  2    
#   1 2    
#      2   
#          
#          
#          
#          
Enter for next round
#          
#          
#     1    
#  1  1    
#     1    
#      1   
#          
#          
#          
#          

试试这个:

import numpy as np  # <-- for efficient storage
import matplotlib.pyplot as plt # <-- for visualization
# Initialize a random map with values from 0 to 10.
pheromone_map = np.random.randint(low=0, high=11, size=(1000, 1000))
# Visualize the pheromones disappearing
for i in range(10):
# clip removes any negative value from the array that had 1 subtracted from it
pheromone_map = np.clip(pheromone_map - 1, a_min=0, a_max=11)
# Shows how the pheromones disappear
fig, ax = plt.subplots()
ax.imshow(pheromone_map)

最新更新