如何计算Python模拟中出现的次数?

如何计算Python模拟中出现的次数?,python,plot,count,simulation,Python,Plot,Count,Simulation,我正在Python中运行一个TASEP模拟,其中给定大小的晶格上的晶格站点可以是空的,也可以是被占用的(0或1) 模拟给出了给定模拟时间内晶格配置(状态是否被占用)的图,但没有给出被占用状态的数量 我无法让Python计算被占用状态的数量,因为图形来自模拟,而不是列表 TASEP代码: import random, pylab, math random.seed() L=100 # Number of lattice sites alpha=.2 # Rate of entry beta=.4

我正在Python中运行一个TASEP模拟,其中给定大小的晶格上的晶格站点可以是空的,也可以是被占用的(0或1)

模拟给出了给定模拟时间内晶格配置(状态是否被占用)的图,但没有给出被占用状态的数量

我无法让Python计算被占用状态的数量,因为图形来自模拟,而不是列表

TASEP代码:

import random, pylab, math
random.seed()
L=100 # Number of lattice sites
alpha=.2 # Rate of entry
beta=.4 # Rate of exit

Ntime=200000 # Simulation steps
state=[0 for k in range(L+1)]
for iter in range(Ntime):
   k=random.randint(0,L)
   if k==0:
      if random.random() < alpha: state[1]=1
   elif k==L:
         if random.random() < beta: state[L]=0
   elif state[k]==1 and state[k+1]==0: 
      state[k+1]=1
      state[k]=0
   if iter%2000 == 0: 
      yaxis=[]
      for i in range(L):
          if state[i+1]==1: yaxis.append(i)
      xaxis=[iter for k in range(len(yaxis))]
      pylab.plot(xaxis,yaxis,'r.')
pylab.xlabel('Number of steps')
pylab.ylabel('System configuration')
pylab.show()
导入随机、pylab、数学
random.seed()
L=100#格点数量
阿尔法=0.2#入学率
β=0.4#退出率
Ntime=200000模拟步骤
状态=[0表示范围(L+1)中的k]
对于范围内的iter(Ntime):
k=随机随机随机数(0,L)
如果k==0:
如果random.random()

好的,我基本上修复了你的代码,因为我没有冒犯你,但之前它有点乱(见注释)

随机导入
将matplotlib.pyplot作为plt导入
图,axs=plt.子批次(nrows=2,sharex=True)
L=100#格点数量
阿尔法=0.2#入学率
β=0.4#退出率
n_时间=200000#模拟步骤
每个记录=2000
状态=[0]*(L+1)
记录=[]#存储所占用状态总数的记录
对于范围内的itr(n_时间):
rand_int=random.randint(0,L)
如果rand_int==0:
如果为random.random()
这个输出


FHTMitchell提出的解决方案是正确的,但效率低下。
sum
操作要求在每次迭代时执行O(L)个功,使整个程序为O(L*n_时间)

请注意:

  • 开始时,占用状态计数为0
  • 只有当零状态切换为一时,
    占用状态\计数
    才应增加
  • 只有当一个状态切换为零时,
    占用状态计数才应减少
    
  • 如果您已经处于目标状态,则无需更改,短路可以避免对
    random()
    进行不必要的调用
  • 最后,当两个状态在相反方向上切换时(您的最终
    elif
    ),无需更改
    占用状态\计数
应用以上所有方法可以产生以下O(n_时间)实现,这要快得多:

import random
import matplotlib.pyplot as plt

fig, axs = plt.subplots(nrows=2, sharex=True)

L = 100  # Number of lattice sites
alpha = 0.2  # Rate of entry
beta = 0.4  # Rate of exit

n_time = 200000  # Simulation steps
record_each = 2000
state = [0]*(L + 1)
occupied_state_count = 0
record = []  # store a record of the total number of states occupied

for itr in range(n_time):

    rand_int = random.randint(0, L)

    if rand_int == 0:
        if state[1] == 0 and random.random() < alpha:
            state[1] = 1
            occupied_state_count += 1
    elif rand_int == L:
        if state[L] == 1 and random.random() < beta:
            state[L] = 0
            occupied_state_count -= 1
    elif state[rand_int] == 1 and state[rand_int + 1] == 0:
        state[rand_int + 1] = 1
        state[rand_int] = 0

    if itr % record_each == 0:
        yaxis = [i for i in range(L) if state[i + 1] == 1]
        axs[1].plot([itr]*len(yaxis), yaxis, 'r.')
        record.append(occupied_state_count)  # add the total number of states to the record

axs[0].plot(range(0, n_time, record_each), record)  # plot the record
axs[1].set_xlabel('Number of steps')
axs[1].set_ylabel('System configuration')
axs[0].set_ylabel('Number of states occupied')
plt.show()
随机导入
将matplotlib.pyplot作为plt导入
图,axs=plt.子批次(nrows=2,sharex=True)
L=100#格点数量
阿尔法=0.2#入学率
β=0.4#退出率
n_时间=200000#模拟步骤
每个记录=2000
状态=[0]*(L+1)
占用状态计数=0
记录=[]#存储所占用状态总数的记录
对于范围内的itr(n_时间):
rand_int=random.randint(0,L)
如果rand_int==0:
如果状态[1]==0且为random.random()
状态存储在名为
状态的列表中。您是否尝试过求和(状态)
?这将为您提供给定时间步的占用状态数…不要使用
iter
作为变量,它会影响内置函数。而
pylab
已被弃用。改用
导入matplotlib.pyplot作为plt
。除非你传递一个变量,请看一下(例如,把空格放在操作符的两边),否则实际上什么都不会做。我会试试你的建议。谢谢!我试试你的建议。
import random
import matplotlib.pyplot as plt

fig, axs = plt.subplots(nrows=2, sharex=True)

L = 100  # Number of lattice sites
alpha = 0.2  # Rate of entry
beta = 0.4  # Rate of exit

n_time = 200000  # Simulation steps
record_each = 2000
state = [0]*(L + 1)
occupied_state_count = 0
record = []  # store a record of the total number of states occupied

for itr in range(n_time):

    rand_int = random.randint(0, L)

    if rand_int == 0:
        if state[1] == 0 and random.random() < alpha:
            state[1] = 1
            occupied_state_count += 1
    elif rand_int == L:
        if state[L] == 1 and random.random() < beta:
            state[L] = 0
            occupied_state_count -= 1
    elif state[rand_int] == 1 and state[rand_int + 1] == 0:
        state[rand_int + 1] = 1
        state[rand_int] = 0

    if itr % record_each == 0:
        yaxis = [i for i in range(L) if state[i + 1] == 1]
        axs[1].plot([itr]*len(yaxis), yaxis, 'r.')
        record.append(occupied_state_count)  # add the total number of states to the record

axs[0].plot(range(0, n_time, record_each), record)  # plot the record
axs[1].set_xlabel('Number of steps')
axs[1].set_ylabel('System configuration')
axs[0].set_ylabel('Number of states occupied')
plt.show()