python模拟生态系统

基于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()

 

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个非常有趣的问题。首先,我们需要了解生态保护模拟系统的基本原理和需求。通常,这类系统需要模拟生态系统的各种生物、植物和环境因素的相互作用,还需要记录和分析这些因素的变化情况,以便帮助生态学家和环保工作者进行科学研究和决策。 为了实现这个系统,我们可以采用面向对象编程的方法,将不同的生物、植物和环境因素抽象为不同的类,然后通过这些类的相互组合和交互来模拟整个生态系统的运行。 以下是一个简单的示例代码,包括了生物、植物和环境因素的基本类定义及其相应的属性和方法: ```python class Organism: def __init__(self, age, size, habitat): self.age = age self.size = size self.habitat = habitat def move(self): pass def eat(self): pass def reproduce(self): pass class Plant: def __init__(self, age, size, habitat, nutrient): self.age = age self.size = size self.habitat = habitat self.nutrient = nutrient def grow(self): pass def photosynthesize(self): pass class Environment: def __init__(self, temperature, humidity, light, nutrient): self.temperature = temperature self.humidity = humidity self.light = light self.nutrient = nutrient def change_temperature(self): pass def change_humidity(self): pass def change_light(self): pass ``` 上面的代码只是一个简单的示例,实际的生态保护模拟系统要更复杂。我们还需要定义更多的类和方法,例如不同类型的生物、植物和环境因素的特殊属性和行为,以及对它们进行模拟和分析的算法和数据结构。 通过采用面向对象编程的方法,我们可以更加清晰地组织和管理整个系统的代码,使其更易于维护和扩展。同时,我们还可以利用Python的一些高级特性,例如继承、多态和装饰器等,来进一步优化代码的结构和性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值