python模擬生態系統

2021-09-24 19:44:59 字數 3949 閱讀 7133

基於matplotlib庫下animation、pyplot功能進行的乙個生態的模擬程式,參考了一些網上視覺化的教程和生態模擬的引數。在本程式中由4種東西構成生態系統,草、草食動物、肉食動物、空地,使用了搜尋的演算法、random函式來模擬動物進食關係,以及動物的捕食動向,最後通過times間隔每秒展示出animation動態圖的畫面。

import matplotlib

import matplotlib.pyplot as plt

import matplotlib.animation as animation

import numpy as np

def addgrass(_num):

for i in range(_num):

x = np.random.randint(0, size)

y = np.random.randint(0, size)

while grid[x][y] != 0:

x = np.random.randint(0, size)

y = np.random.randint(0, size)

grid[x][y] = 1 # 1代表草

def addgrasseater(_num):

for i in range(_num):

x = np.random.randint(0, size)

y = np.random.randint(0, size)

while grid[x][y] != 0:

x = np.random.randint(0, size)

y = np.random.randint(0, size)

grid[x][y] = 2 # 2代表食草動物

def addmeateater(_num):

for i in range(_num):

x = np.random.randint(0, size)

y = np.random.randint(0, size)

while grid[x][y] != 0 or growaround(x, y, 2) == [-1, -1]:

x = np.random.randint(0, size)

y = np.random.randint(0, size)

grid[x][y] = 3 # 3代表食肉動物

def growaround(_x, _y, _id):

field =

if _x-1 < 0:

x_begin = 0

else:

x_begin = _x-1

if _y-1 < 0:

y_begin = 0

else:

y_begin = _y-1

if _x+1 > size-1:

x_end = size-1

else:

x_end = _x+1

if _y+1 > size-1:

y_end = size-1

else:

y_end = _y+1

for i in range(x_begin, x_end+1):

for j in range(y_begin, y_end+1):

if grid[i][j] == _id or grid[i][j] == _id*10: # 2代表食草動物,1代表草,0代表空地

field += [[i, j]]

if len(field) == 0: # 沒有食物或者空地

return [-1, -1]

else:

count = np.random.randint(0, len(field))

return field[count]

def fieldupdate():

for i in range(size):

for j in range(size):

if grid[i][j] == 30:

grid[i][j] = 3

elif grid[i][j] == 20:

grid[i][j] = 2

elif grid[i][j] == 10:

grid[i][j] = 1

def data_gen():

for count in range(times):

timestext.set_text('times: %d' % (count+1))

for i in range(size):

for j in range(size):

if grid[i][j] == 3:

place = growaround(i, j, 2)

if place == [-1, -1]:

grid[i][j] = 0 # 食肉動物死亡

else:

grid[i][j] = 0

grid[place[0]][place[1]] = 30 # 食肉動物進食並移動

growth = growaround(i, j, 0)

if growth != [-1, -1]:

grid[growth[0]][growth[1]] = 30 # 食肉動物繁殖

if grid[i][j] == 2:

place = growaround(i, j, 1)

if place == [-1, -1]:

grid[i][j] = 0 # 食草動物死亡

else:

grid[i][j] = 0

grid[place[0]][place[1]] = 20 # 食草動物進食並移動

growth = growaround(i, j, 0)

if growth != [-1, -1]:

grid[growth[0]][growth[1]] = 20 # 食草動物繁殖

elif grid[i][j] == 1:

growth = growaround(i, j, 0)

if growth != [-1, -1]:

grid[growth[0]][growth[1]] = 10 # 草生長

fieldupdate()

yield grid

def update(_data):

ax.imshow(_data, interpolation='nearest', cmap='set3', norm=norm)

return ax

times = 100 # 迭代次數

size = 40

grid = np.zeros((size, size)) # 0代表空地

addgrass(1200)

addgrasseater(150)

addmeateater(30)

fig = plt.figure()

ax = plt.subplot(111)

norm = matplotlib.colors.normalize(vmin=0, vmax=3) # 固定數值對應的顏色對映

gci = ax.imshow(grid, interpolation='nearest', cmap='set3', norm=norm)

ax.set_xticks()

ax.set_yticks()

cbar = plt.colorbar(gci)

cbar.set_ticks(np.linspace(0, 3, 4))

cbar.set_ticklabels(('space', 'grass', 'grasseater', 'meateater'))

timestext = plt.text(-2, -2, 'times: 0')

ani = animation.funcanimation(fig, update, data_gen, interval=1000, repeat=false)

plt.show()

Hadoop生態系統

摘要 介紹hadoop生態系統,從hadoop生態系統有什麼成員,成員能做什麼和hadoop生態系統能夠提供大資料問題解決方案兩方面來認識。hadoop生態圖,通俗地說,就是hadoop核心模組和衍生的子專案。一幅hadoop生態圖,讓我想到了兩個問題。問題一 hadoop生態系統包括哪些成員?每個...

Docker生態系統

docker是以docker容器為資源分割和排程的基本單位,封裝軟體的執行時環境.用於快速構建,發布,執行分布式應用的平台。docker的執行時容器的本質是程序.在linux中,通過namespace進行資源隔離,cgroups進行資源限制,使docker容器看上去像是乙個執行在宿主機中的虛擬機器....

SOA生態系統

richard veryard在他的最近的一篇日誌裡提到 soa世界終於開始跟上一點生態系統的思想了.以生物性的方式來建立業務和軟體服務。根據richard的說法,這一方式與解決方案驅動的soa方式截然不同。在這種情況下,不是基於特定的解決方案來定義服務,而是基於它們所屬於的生態系統 可與 自頂向下...