Пример #1
0
    def __init__(self, sim_param=SimParam(), no_seed=False):
        """
        Initialize the Simulation object.
        :param sim_param: is an optional SimParam object for parameter pre-configuration
        :param no_seed: is an optional parameter. If it is set to True, the RNG should be initialized without a
        a specific seed.
        """
        self.sim_param = sim_param
        self.sim_state = SimState()
        self.system_state = SystemState(self)
        self.event_chain = EventChain()
        self.sim_result = SimResult(self)
        # TODO Task 2.4.3: Uncomment the line below
        self.counter_collection = CounterCollection(self)
        # TODO Task 3.1.2: Uncomment the line below and replace the "None"

        if no_seed:
            #if the mean = 1.0, then 1/lambda_ = 1.0 -> lambda_ = 1
            self.rng = RNG(ExponentialRNS(1.0),
                           ExponentialRNS(1. / float(self.sim_param.RHO)))
        else:
            self.rng = RNG(
                ExponentialRNS(1.0, self.sim_param.SEED_IAT),
                ExponentialRNS(1. / float(self.sim_param.RHO),
                               self.sim_param.SEED_ST))
Пример #2
0
def task_3_2_1():
    """
    This function plots two histograms for verification of the random distributions.
    One histogram is plotted for a uniform distribution, the other one for an exponential distribution.
    """
    rns_exp = ExponentialRNS(1, the_seed=0)
    rns_uni = UniformRNS(1, 100, the_seed=0)
    n = 10000

    exp_distr = []
    uni_distr = []
    weights = []

    for _ in range(n):
        exp_distr.append(rns_exp.next())
        uni_distr.append(rns_uni.next())
        weights.append(1. / float(n))

    pyplot.subplot(121)
    pyplot.hist(exp_distr, bins=30, weights=weights, edgecolor='black')
    pyplot.xlabel("x")
    pyplot.ylabel("distribution over n")
    pyplot.title("Exponential distribution")

    pyplot.subplot(122)
    pyplot.hist(uni_distr, bins=30, weights=weights, edgecolor='black')
    pyplot.xlabel("x")
    pyplot.ylabel("distribution over n")
    pyplot.title("Uniform distribution")

    pyplot.show()
Пример #3
0
def task_3_2_1():
    """
    This function plots two histograms for verification of the random distributions.
    One histogram is plotted for a uniform distribution, the other one for an exponential distribution.
    """
    # TODO Task 3.2.1: Your code goes here
    no_of_runs = 1000
    rns_exp = ExponentialRNS(5.0)
    rns_uni = UniformRNS(1, 300)
    exponential_values = []
    uniform_values = []
    weight = numpy.full(no_of_runs, 1.0 / float(no_of_runs))

    while no_of_runs != 0:
        exponential_values.append(rns_exp.next())
        uniform_values.append(rns_uni.next())
        no_of_runs -= 1

    pyplot.subplot(121)
    pyplot.title("Uniform Distribution")
    pyplot.xlabel("x")
    pyplot.ylabel("Distribution")
    pyplot.hist(uniform_values, bins=25, weights=weight)
    pyplot.subplot(122)
    pyplot.title("Exponential distribution for Lambda = 5")
    pyplot.xlabel("x")
    pyplot.ylabel("Distribution")
    pyplot.hist(exponential_values, bins=25, weights=weight)
    pyplot.show()
Пример #4
0
def task_3_2_1():
    """
    This function plots two histograms for verification of the random distributions.
    One histogram is plotted for a uniform distribution, the other one for an exponential distribution.
    """
    # TODO Task 3.2.1: Your code goes here
    exp_rns = ExponentialRNS(mean=1, the_seed=0)
    uni_rns = UniformRNS(low=2, high=3, the_seed=0)
    exp_list = []
    uni_list = []
    for i in range(100000):
        x = exp_rns.next()
        y = uni_rns.next()
        exp_list.append(x)
        uni_list.append(y)
    weights1 = numpy.full(len(exp_list), 1.0 / float(len(exp_list)))
    weights2 = numpy.full(len(uni_list), 1.0 / float(len(uni_list)))
    pyplot.hist(exp_list, bins=10, weights=weights1, histtype='bar')
    pyplot.xlabel("Exponential")
    pyplot.show()
    pyplot.hist(uni_list, bins=10, weights=weights2, histtype='bar')
    pyplot.xlabel("Uniform")
    pyplot.show()

    pass
Пример #5
0
def task_3_2_1():
    """
    This function plots two histograms for verification of the random distributions.
    One histogram is plotted for a uniform distribution, the other one for an exponential distribution.
    """
    # TODO Task 3.2.1: Your code goes here
    # For exponential dist. & uniform dist.
    samples = 10000
    exp = ExponentialRNS(1.)
    uni = UniformRNS(0.,10.)
    exp_dist = []
    uni_dist = []
    for k in range(samples):
        exp_dist.append(exp.next())
        uni_dist.append(uni.next())

    #weights_ = numpy.full(len(uni_dist), 1.0 / float(len(uni_dist)))
    ax0=plt.subplot(121)
    plt.xlabel("x")
    plt.ylabel("Number of instances")
    ax0.set_title("Exponential Distribution")
    plt.hist(exp_dist, density=True, bins=int(samples/100))

    ax1=plt.subplot(122)
    plt.xlabel("x")
    plt.ylabel("Probability density function of bin")
    ax1.set_title("Uniform Distribution")
    plt.hist(uni_dist,density=True,bins=int(samples/100))
    plt.show()
Пример #6
0
 def reset(self, no_seed=False):
     """
     Reset the Simulation object.
     :param no_seed: is an optional parameter. If it is set to True, the RNG should be reset without a
     a specific seed.
     """
     self.sim_state = SimState()
     self.system_state = SystemState(self)
     self.event_chain = EventChain()
     self.sim_result = SimResult(self)
     # TODO Task 2.4.3: Uncomment the line below
     self.counter_collection = CounterCollection(self)
     # TODO Task 3.1.2: Uncomment the line below and replace the "None"
     if no_seed:
         self.rng = RNG(ExponentialRNS(1.0), ExponentialRNS(1./float(self.sim_param.RHO)))
     else:
         self.rng = RNG(ExponentialRNS(1.0, self.sim_param.SEED_IAT), ExponentialRNS(1./float(self.sim_param.RHO),self.sim_param.SEED_ST))
 def reset(self, no_seed=False):
     """
     Reset the Simulation object.
     :param no_seed: is an optional parameter. If it is set to True, the RNG should be reset without a
     a specific seed.
     """
     self.sim_state = SimState()
     self.system_state = SystemState(self)
     self.event_chain = EventChain()
     self.sim_result = SimResult(self)
     self.counter_collection = CounterCollection(self)
     if no_seed:
         self.rng = RNG(ExponentialRNS(1),
                        ExponentialRNS(1. / float(self.sim_param.RHO)))
     else:
         self.rng = RNG(
             ExponentialRNS(1, self.sim_param.SEED_IAT),
             ExponentialRNS(1. / float(self.sim_param.RHO),
                            self.sim_param.SEED_ST))
 def __init__(self, sim_param=SimParam(), no_seed=False):
     """
     Initialize the Simulation object.
     :param sim_param: is an optional SimParam object for parameter pre-configuration
     :param no_seed: is an optional parameter. If it is set to True, the RNG should be initialized without a
     a specific seed.
     """
     self.sim_param = sim_param
     self.sim_state = SimState()
     self.system_state = SystemState(self)
     self.event_chain = EventChain()
     self.sim_result = SimResult(self)
     self.counter_collection = CounterCollection(self)
     if no_seed:
         self.rng = RNG(ExponentialRNS(1),
                        ExponentialRNS(1. / float(self.sim_param.RHO)))
     else:
         self.rng = RNG(
             ExponentialRNS(1, self.sim_param.SEED_IAT),
             ExponentialRNS(1. / float(self.sim_param.RHO),
                            self.sim_param.SEED_ST))
    def poisson_arrivals(self, slicesim):
        """

        """
        iat = slicesim.slice_param.MEAN_IAT
        self.rng = RNG(ExponentialRNS(1. / float(iat), self.seed_iat),
                       s_type='iat')

        #iat = self.sim_param.MEAN_IAT
        t_start = slicesim.sim_state.now
        t_end = self.sim_param.T_FINAL

        # Poisson
        t_arr = []
        iat_arr = []
        t_temp = t_start + self.rng.get_iat()
        while t_temp < t_end:
            t_arr.append(t_temp)
            t_temp += self.rng.get_iat()
            if len(t_arr) > 2:
                iat_arr.append(t_arr[-1] - t_arr[-2])

        # # Fixed IAT
        # t_arr = np.arange(t_start, t_end, iat)

        # # Plot histogram of interarrivals for poisson dist
        # count, bins, ignored = plt.hist(iat_arr, 30, normed=True)
        # lmda = 1/iat
        # plt.plot(bins, lmda * np.exp(- lmda*bins), linewidth=2, color='r')
        # plt.show()

        arrivals = []
        for i in t_arr:
            arrivals.append(PacketArrival(slicesim, i))

        return arrivals
Пример #10
0
def task_3_2_1():
    """
    This function plots two histograms for verification of the random distributions.
    One histogram is plotted for a uniform distribution, the other one for an exponential distribution.
    """
    # TODO Task 3.2.1: Your code goes here
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim_param.RHO = 0.01
    sim = Simulation(sim_param)
    rns_iat = ExponentialRNS(1.0)
    rns_st = ExponentialRNS(1.0/sim.sim_param.RHO)
    rns_uniform = UniformRNS((2,4))
    hist1 = TimeIndependentHistogram(sim, "Line")
    hist2 = TimeIndependentHistogram(sim, "Line")
    hist3 = TimeIndependentHistogram(sim, "bp")
    for i in range(1000000):
        hist1.count(rns_iat.next())
        hist2.count(rns_st.next())
        hist3.count(rns_uniform.next())
    hist1.report()
    hist2.report()
    hist3.report()