临时云剪切板

import matplotlib.pyplot as plt
import numpy as np

# 创建一个01矩阵
matrix = np.array([[0, 1, 0], 
                    [1, 0, 1], 
                    [0, 1, 0]])

# 使用matshow()函数可视化矩阵
plt.matshow(matrix, cmap=plt.cm.gray)  # 这里使用灰度颜色图
plt.title('01 Matrix Visualization')  # 添加标题
plt.colorbar()  # 显示颜色条
plt.show()
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation

# 读取文本文件并返回矩阵列表
def read_matrices_from_file(file_path):
    with open(file_path, 'r') as file:
        matrices = []
        matrix = []
        for line in file:
            line = line.strip()
            if line:
                # 添加当前行到矩阵列表
                matrix.append([int(num) for num in line.split()])
            elif matrix:
                # 当读到空行时,添加完成的矩阵到列表,并重置矩阵列表
                matrices.append(np.array(matrix))
                matrix = []
        if matrix:
            # 如果文件末尾没有空行,添加最后一个矩阵
            matrices.append(np.array(matrix))
    return matrices

# 初始化绘图
def init_plot():
    plt.matshow(np.zeros((3, 3)), cmap=plt.cm.gray)  # 假设矩阵是3x3
    plt.colorbar()
    return fig,

# 动画更新函数
def update(frame):
    plt.matshow(frame, cmap=plt.cm.gray)
    plt.title(f'Frame {idx+1}')
    return fig,

# 主函数
def main():
    file_path = 'matrices.txt'  # 假设文本文件名为matrices.txt
    matrices = read_matrices_from_file(file_path)

    fig = plt.figure()
    idx = 0
    ani = FuncAnimation(fig, update, frames=matrices, init_func=init_plot,
                        interval=1000, save_count=50)  # 1000ms显示一个矩阵

    plt.show()

if __name__ == '__main__':
    main()
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>

const int GRID_SIZE = 5; // 10x10网格
const double GRID_DISTANCE = 0.5; // 网格中每个个体之间的距离(m)
const int MAX_TIME_STEPS = 10; // 最大时间步数

// 计算声压级衰减
double calculateSoundLevelAttenuation(double initialLevel, double distance) {
    return initialLevel - 20.0 * log10(distance / GRID_DISTANCE);
}

// 计算个体鼓掌的概率
double calculateClapProbability(double soundPressureLevel) {
    // 假设声压级每超过参考声压级1dB,鼓掌概率增加0.01
    return 1.0/(1.0+exp(-(0.8*(soundPressureLevel-76))));
}

double calculateTotalSoundLevel(double soundLevel1, double soundLevel2) {
    return 10.0 * log10(pow(10.0, soundLevel1 / 10.0) + pow(10.0, soundLevel2 / 10.0));
}

bool clapping[GRID_SIZE][GRID_SIZE] = {0};
double soundSources[GRID_SIZE][GRID_SIZE];

int main() {
    freopen("input.txt", "r", stdin);
    freopen("clapstatus.txt", "w", stdout);
    srand(static_cast<unsigned int>(time(0)));

    // 初始化声源强度
    for (int i = 0; i < GRID_SIZE; ++i) 
        for (int j = 0; j < GRID_SIZE; ++j) 
            std::cin>>soundSources[i][j];// 模拟10-110dB的声源强度

    // 模拟时间步进
    for (int t = 0; t < MAX_TIME_STEPS; ++t) {
        double soundPressure[GRID_SIZE][GRID_SIZE] = {0};

        // 计算每个个体接收到的声压级
        for (int i = 0; i < GRID_SIZE; ++i) {
            for (int j = 0; j < GRID_SIZE; ++j) {
                for (int di = -1; di <= 1; ++di) {
                    for (int dj = -1; dj <= 1; ++dj) {
                        if (di == 0 && dj == 0) continue; // 忽略个体自身对其自己的影响

                        int ni = (i + di + GRID_SIZE) % GRID_SIZE; // 循环边界条件
                        int nj = (j + dj + GRID_SIZE) % GRID_SIZE;
                        double distance = sqrt(di * di + dj * dj) * GRID_DISTANCE; // 计算距离

                        double initialSoundLevel = soundSources[ni][nj];
                        double attenuatedSoundLevel = calculateSoundLevelAttenuation(initialSoundLevel, distance);
                        soundPressure[i][j] = calculateTotalSoundLevel(soundPressure[i][j], attenuatedSoundLevel);
                    }
                }
            }
        }

        // 根据声压级更新人群鼓掌状态
        for (int i = 0; i < GRID_SIZE; ++i) {
            for (int j = 0; j < GRID_SIZE; ++j) {
                double probability = calculateClapProbability(soundPressure[i][j]);
                //printf("%d %d %lf %lf\n", i, j, probability, soundPressure[i][j]);
                clapping[i][j] = (rand() % 100) < (probability * 100);
            }
        }

        // 打印当前步进的人群状态
        //std::cout << "Time step " << t << std::endl;
        
        for (int i = 0; i < GRID_SIZE; ++i) {
            for (int j = 0; j < GRID_SIZE; ++j) {
                std::cout << (clapping[i][j] ? "1 " : "0 ");
            }
            std::cout << std::endl;
        }
        puts("");
    }

    return 0;
}
posted @ 2024-08-02 20:30  xzllll07  阅读(23)  评论(0)    收藏  举报