更改

第363行: 第363行:     
因此,具体的粗粒化规则为:将变量D去掉,从而得到内生元素A,B,C构成的宏观系统,而宏观动力学为每个变量的状态由指向它的系统内变量的状态拷贝而来。
 
因此,具体的粗粒化规则为:将变量D去掉,从而得到内生元素A,B,C构成的宏观系统,而宏观动力学为每个变量的状态由指向它的系统内变量的状态拷贝而来。
−
  −
== 由布尔网络生成TPM的代码 ==
  −
使用有向图生成布尔网络,然后根据给定的微观机制计算网络的TPM,以下给出空间因果涌现中带噪声的AND门实例的python实现代码:
  −
  −
<syntaxhighlight lang="python3">
  −
import numpy as np
  −
import networkx as nx
  −
  −
def bool2tpm(G, noise_prob=0.3):
  −
def update_state(node, state, G, noise_prob):
  −
"""
  −
布尔机制可修改,这里使用的是空间因果涌现实例中带噪声的AND门
  −
result:该节点下一时刻状态返回为1的概率
  −
"""
  −
  −
predecessors = list(G.predecessors(node))
  −
pred_state = [state[nodes.index(pred)] for pred in predecessors]
  −
  −
result = 1
  −
for val in pred_state:
  −
result &= val  # AND
  −
  −
if result < 1:
  −
result += noise_prob  # 带噪音的AND门
  −
  −
return result
  −
  −
# 构建布尔网络的状态空间
  −
nodes = list(G.nodes())
  −
num_nodes = len(nodes)
  −
state_space = list(np.array(np.meshgrid(*[[0, 1]] * num_nodes)).T.reshape(-1, num_nodes))
  −
  −
# 计算TPM
  −
num_states = len(state_space)
  −
TPM = np.zeros((num_states, num_states))
  −
for idx, state in enumerate(state_space):
  −
p1 = np.zeros((1, num_nodes))
  −
  −
# 为每个节点更新下一时刻为1的概率p1
  −
for node in nodes:
  −
p1[0, nodes.index(node)] = update_state(node, state, G, noise_prob)
  −
  −
for next_idx, next_state in enumerate(state_space):
  −
TrueP = p1 * next_state + (1-p1) * (1-next_state)
  −
TPM[idx, next_idx] = np.prod(TrueP)
  −
  −
# 归一化TPM
  −
TPM /= TPM.sum(axis=1, keepdims=True)
  −
return TPM
  −
  −
  −
# 创建布尔网络
  −
G = nx.DiGraph()
  −
G.add_nodes_from(['A', 'B', 'C', 'D'])
  −
G.add_edges_from([('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'A'), ('C', 'B'), ('D', 'A'), ('D', 'B')])
  −
TPM = bool2tpm(G, noise_prob=0.3)
  −
</syntaxhighlight>
  −
  −
后续可以使用[[有效信息]]词条中的源代码,计算TPM的有效信息。
      
== 应用 ==
 
== 应用 ==