理论物理研究:熵的模拟

下面是“万物匹配”的熵增定律:

S=\int \frac{dQ}{T}

意思就是说一切孤立系统的混乱度都不会减少。

这里的“孤立系统”可以理解为“没有外界干扰”。打个比方,如果不停把什么东西摆放整齐(也就是熵减),那它的混乱度就可能不会增加(甚至减少)。

在刘慈欣的《三体》中,有这么一段话:

“低熵体(也就是文明)存在的意义就是降低有序度。”


我们用Python模拟这种混乱过程:

#!/usr/bin/python
import random
import numpy
import matplotlib.pyplot as plt
import matplotlib
import time

plate = [[1, 1, 1, 3],
         [1, 3, 1, 1],
         [2, 2, 3, 2],
         [3, 2, 2, 2]]


def pprint(array: list) -> None:
    a = numpy.asarray(array)
    print(a)
    del a


def swap_list(pos_1: tuple, pos_2: tuple) -> None:
    global plate
    if type(pos_1) != tuple or type(pos_2) != tuple:
        raise TypeError("Function parameter must be <tuple>")
    temp = plate[pos_1[0]][pos_1[1]]
    plate[pos_1[0]][pos_1[1]] = plate[pos_2[0]][pos_2[1]]
    plate[pos_2[0]][pos_2[1]] = temp
    return None


def check_entropy() -> int:
    t_horizontal = []
    for i in plate:
        for j in i:
            t_horizontal.append(j)

    t_vertical = []
    for i in numpy.asarray(plate).transpose():
        for j in i:
            t_vertical.append(int(j))

    del i, j
    entropy = 0
    for i in range(16):
        try:
            if t_horizontal[i] == t_horizontal[i + 1]:
                entropy -= 0.5
            else:
                entropy += 0.75
        except IndexError:
            pass
    try:
        for j in range(16):
            if t_vertical[j] == t_horizontal[j + 1]:
                entropy -= 0.5
            else:
                entropy += 0.75
    except IndexError:
        pass
    del i, j
    return entropy


n = int(input("输入需要随机打乱的次数:"))
ns = []
entropy_ = []


for _count in range(n):
    print(f"第{str(_count+1)}次打乱:")
    p11 = random.randint(0, 3)
    p12 = random.randint(0, 3)
    p21 = random.randint(0, 3)
    p22 = random.randint(0, 3)
    position_1 = (p11, p12)
    position_2 = (p21, p22)
    swap_list(position_1, position_2)
    pprint(plate)
    print("混乱度", check_entropy())
    ns.append(_count + 1)
    entropy_.append(check_entropy())
    # time.sleep(0.2)

x_axis = numpy.asarray(ns)
y_axis = numpy.asarray(entropy_)
chinese_font = matplotlib.font_manager.FontProperties(fname="SourceHanSansSC-Bold.otf")
plt.title("")
plt.xlabel("打乱次数", fontproperties=chinese_font)
plt.ylabel("混乱度", fontproperties=chinese_font)
plt.plot(x_axis, y_axis)
plt.show()

还有一个字体文件“SourceHanSansSC-Bold.otf”:

https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChineseicon-default.png?t=M3K6https://github.com/adobe-fonts/source-han-sans/tree/release/OTF/SimplifiedChinese打乱算法是随机选取元素然后交换。

打乱的次数建议在5-50之间。

可能是列表太小的原因,熵增不明显,甚至减小。

 

 (但是总体看来还是在上面的!)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值