修改我的代码，使其逻辑完整，我想模拟一个时间编码系统，每次在buffer里面存入inital个数据bit，然后开始模拟，当没有overflow 也没有underflow时，我认为全部信息（16或32位）传递完成 success，然后在每种参数下实验500次，统计成功的概率，overflow、underflow的概率。我在代码中 搞不清楚Arrival_ProcessServer_Process 应该怎么分别模拟这个过程，然后判断overflow、underflow，也不知道应该怎么和Sim_var、 Generator、Packet 类交互。我要实现的效果是： 一旦发生overflow、underflow，本次实验就结束，此时我要退出实验，保证我的buffer清空，我的下一次实验开始时，重新按照init_packets的i_values = [2, 6, 10, 14, 18] 来生成预先存在buffer中的数据。 请修改我的代码，给出正确的代码，不要省略，一行一行解释import simpyimport simpy
import random
import math

class Sim_var(object):
    RANDOM_SEED = 33
    SIM_TIME = 1000
    LONG_SLEEP_TIMER = 1000000
    MAX_DELAY = 5
    MIN_DELAY = 0
    MU = 1
    MEDIAN_DELAY = 1/MU

    def __init__(self, encode_message, init_packets):
        self.B = 20
        self.current_packets = len(init_packets)
        self.buffer = init_packets
        self.encode_message = encode_message

        self.overflow = 0
        self.underflow = 0
        self.success = 0
        self.simulation_continue = True

    def reset(self):
        self.simulation_continue = False
        self.current_packets = len(self.initial_packets)
        self.buffer = self.initial_packets


class Packet:
    def __init__(self, identifier, delay_time):
        self.identifier = identifier
        self.delay_time = delay_time


class Generator:
    def __init__(self, m, encode_distribution='exponential'):
        self.m = m
        self.secret_message = [random.choice(['0', '1']) for _ in range(m)]
        self.encode_distribution = encode_distribution
        self.packet_delays = [self.encode_packet(bit) for bit in self.secret_message]

    def encode_packet(self, bit):
        delay = 0
        if self.encode_distribution == 'exponential':
            return random.uniform(0, Sim_var.MEDIAN_DELAY) if bit == '0' else random.uniform(Sim_var.MEDIAN_DELAY, Sim_var.MAX_DELAY)
        print(f"Packet delay: {delay} for bit: {bit}")
        return delay


class StatObject:
    def __init__(self):
        self.dataset =[]

    def addNumber(self,x):
        self.dataset.append(x)
    def sum(self):
        n = len(self.dataset)
        sum = 0
        for i in self.dataset:
            sum = sum + i
        return sum
    def mean(self):
        n = len(self.dataset)
        sum = 0
        for i in self.dataset:
            sum = sum + i
        return sum/n
    def maximum(self):
        return max(self.dataset)
    def minimum(self):
        return min(self.dataset)
    def count(self):
        return len(self.dataset)
    def median(self):
        self.dataset.sort()
        n = len(self.dataset)
        if n//2 != 0: # get the middle number
            return self.dataset[n//2]
        else: # find the average of the middle two numbers
            return ((self.dataset[n//2] + self.dataset[n//2 + 1])/2)
    def standarddeviation(self):
        temp = self.mean()
        sum = 0
        for i in self.dataset:
            sum = sum + (i - temp)**2
        sum = sum/(len(self.dataset) - 1)
        return math.sqrt(sum)

class Server_Process(object):
    def __init__(self, env, sim_var):
        self.env = env
        self.sim_var = sim_var
        self.action = env.process(self.run())

    def run(self):
        # print("TX-process started")
        idx_bit = 0
        while True:
            if self.sim_var.simulation_continue == False:
                return
            if self.sim_var.current_packets == 0:
                self.sim_var.underflow += 1 # Underflow occurred
                self.sim_var.simulation_continue = False
                return

            packet = self.sim_var.buffer.pop(0)
            self.sim_var.current_packets -= 1
            idx_bit += 1

            if idx_bit == len(self.sim_var.encode_message):
                self.sim_var.success += 1
                self.sim_var.simulation_continue = False
                return
            yield self.env.timeout(packet.delay_time)



class Arrival_Process(object):
    def __init__(self, env, sim_var, arrival_rate, generator):
        self.env = env
        self.sim_var = sim_var
        self.arrival_rate = arrival_rate
        self.generator = generator
        # self.server_process = Server_Process(env, self.sim_var.packets_buffer)
        self.action = env.process(self.run())

    def run(self):
        # packet arrivals
        # print("Arrival Process Started")
        while True:
            if self.sim_var.simulation_continue == False:
                return

            yield self.env.timeout(random.expovariate(self.arrival_rate))
            if self.sim_var.simulation_continue == False:
                return
            delay = self.generator.packet_delays[self.sim_var.current_packets]
            self.sim_var.buffer.append(Packet(self.sim_var.current_packets, delay))
            # print(f"Packet added to buffer at time {self.env.now}. Buffer size: {len(self.buffer)}")
            self.sim_var.current_packets += 1

            if self.sim_var.current_packets >= self.sim_var.B:
                self.sim_var.overflow += 1
                self.sim_var.simulation_continue = False
                return



def simulate(m, i_values, arrival_rate, encode_distribution='exponential', num_experiments=1000):
    results = []
    for i in i_values:
        sim_var = Sim_var(m, i)
        for exp in range(num_experiments):
            random.seed(sim_var.RANDOM_SEED + exp)
            env = simpy.Environment()
            generator = Generator(m, encode_distribution)
            arrival_process = Arrival_Process(env, sim_var, arrival_rate, generator)
            server_process = Server_Process(env, sim_var)
            env.run(until=sim_var.SIM_TIME)

        results.append((m, i, sim_var.underflow / num_experiments, sim_var.overflow / num_experiments,
                        sim_var.success / num_experiments))

    return results

# main
m_values = [16, 20, 24, 28, 32]
i_values = [2, 6, 10, 14, 18]
B = 20
arrival_rate = 1
encode_distribution = 'exponential'


for m in m_values:
    experiment_results = simulate(m, i_values, arrival_rate, encode_distribution)
    for result in experiment_results:
        print(f"M Size: {result[0]}, i: {result[1]}, Underflow: {result[2]:.3f}, Overflow: {result[3]:.3f}, Success: {result[4]:.3f}")