示例#1
0
def task_3_2_2():
    """
    Here, we execute task 3.2.2 and print the results to the console.
    The first result string keeps the results for 100s, the second one for 1000s simulation time.
    """
    # TODO Task 3.2.2: Your code goes here
    rho = [.01, .5, .8, .9]
    system_utilization_result = []
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    sim.sim_param.SIM_TIME = 100000
    sim.sim_param.S = 5
    for r in rho:
        sim.sim_param.RHO = r
        sim.reset()
        system_utilization_result.append(sim.do_simulation().system_utilization)
    print "The system utilization results for a simulation time of 100s :"
    print system_utilization_result
    rho = [.01, .5, .8, .9]
    system_utilization_result = []
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    sim.sim_param.SIM_TIME = 1000000
    sim.sim_param.S = 5
    for r in rho:
        sim.sim_param.RHO = r
        sim.reset()
        system_utilization_result.append(sim.do_simulation().system_utilization)
    print "The system utilization results for a simulation time of 1000s :"
    print system_utilization_result
示例#2
0
def task_2_7_2():
    """
    Here, you can execute task 2.7.2 if you want to execute it in a separate function
    """
    # TODO Task 2.7.2: Your code goes here or in the function above
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim_param.SIM_TIME = 1000000
    sim = Simulation(sim_param)
    return do_simulation_study(sim)
示例#3
0
def task_1_7_2():
    """
    Execute task 1.7.2 and perform a simulation study according to the task assignment.
    :return: Minimum number of buffer spaces to meet requirements.
    """
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim_param.SIM_TIME = 1000000
    sim_param.MAX_DROPPED = 100
    sim_param.NO_OF_RUNS = 100
    sim = Simulation(sim_param)
    return do_simulation_study(sim)
def task_4_3_1():
    """
    Run the correlation tests for given rho for all correlation counters in counter collection.
    After each simulation, print report results.
    SIM_TIME is set higher in order to avoid a large influence of startup effects
    """
    # TODO Task 4.3.1: Your code goes here
    rho = [0.01, 0.5, 0.8, 0.95]
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    sim.sim_param.SIM_TIME = 1000000
    sim.sim_param.S = 10000
    for r in rho:
        print "---------- rho = ,", r, " ---------"
        sim.sim_param.RHO = r
        sim.reset()
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            sim.do_simulation()
        print "Correlation    between    IAT    and    waiting     time   of  a packet           : ", sim.counter_collection.cnt_iat_wt.get_cor(
        )
        print "Correlation    between    IAT    and    serving     time   of  a packet           : ", sim.counter_collection.cnt_iat_st.get_cor(
        )
        print "Correlation between IAT and system time (waiting time + serving time) of a packet : ", sim.counter_collection.cnt_iat_syst.get_cor(
        )
        print "Correlation    between    service time and system   time   of a packet            : ", sim.counter_collection.cnt_st_syst.get_cor(
        )
        print "Auto-correlation  of  waiting  time  with  lags  ranging  from  1  to  20         : ", sim.counter_collection.acnt_wt.get_auto_cor(
            2)
示例#5
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))
示例#6
0
def task_4_3_1():
    """
    Run the correlation tests for given rho for all correlation counters in counter collection.
    After each simulation, print report results.
    SIM_TIME is set higher in order to avoid a large influence of startup effects
    """
    # TODO Task 4.3.1: Your code goes here
    sim_param = SimParam()
    sim = Simulation(sim_param)
    sim.sim_param.S = 10000
    sim.sim_param.SIM_TIME = 10000000
    for rho in [0.01, 0.5, 0.8, 0.95]:
        sim.sim_param.RHO = rho
        sim.reset()
        sim.counter_collection.reset()
        sim = sim.do_simulation().sim
        print("RHO = " + str(rho))
        print("Correlation between IAT and waiting time of a packet = " +
              str(sim.counter_collection.cnt_iat_wt.get_cor()))
        print("Correlation between IAT and serving time of a packet = " +
              str(sim.counter_collection.cnt_iat_st.get_cor()))
        print(
            "Correlation between IAT and system time (waiting time + serving time) of a packet = "
            + str(sim.counter_collection.cnt_iat_syst.get_cor()))
        print(
            "Correlation between serving time and system time of a packet = " +
            str(sim.counter_collection.cnt_st_syst.get_cor()))
        for lag in range(0, 21):
            print(
                "Lag = " + str(lag) +
                " Auto-correlation of waiting time with lags ranging from 1 to 20 = "
                + str(sim.counter_collection.acnt_wt.get_auto_cor(lag)))

        print(" ")
示例#7
0
    def __init__(self, user_id, dist=10, slice_list=[], sim_param=SimParam()):
        """

        """
        self.user_id = user_id
        self.slice_list = slice_list
        self.sim_param = sim_param
        self.distance = dist  # np.random.uniform(self.sim_param.dist_range)
        if self.sim_param.cts_service:
            self.channel = ChannelModalCts(self)
        else:
            self.channel = ChannelModal(self)

        self.traffic_generator = TrafficGenerator(self)
        self.traffic_list = []
        self.traffic_list_dict = {}
        for i in self.slice_list:
            if 0:  #i.slice_param.SLICE_ID==0:
                self.traffic_list.append(
                    self.traffic_generator.periodic_arrivals(i))  # for RR
            else:
                self.traffic_list.append(
                    self.traffic_generator.poisson_arrivals(i))
            self.traffic_list_dict.update(
                {i.slice_param.SLICE_ID: self.traffic_list[-1]})
示例#8
0
    def __init__(self, t_final: int = 1000):
        """
        Main ran_simulation
        """
        sim_param = SimParam(t_final)

        self.initialize_spaces (sim_param)

        # other attributes of ran_environment
        self.state = None
        self.sim_param = sim_param
        #self.C_algo = 'RL'
        self.slice_scores = None  # slice scores for reward method 3
        self.user_scores = None

        # generate seed values
        new_seed = seeding.create_seed()
        self.sim_param.update_seeds(new_seed)

        # initialize SD_RAN_Controller
        self.SD_RAN_Controller = Controller(self.sim_param)

        # data
        self.user_score_arr = None
        self.slice_score_arr = None
        self.reward_hist = None
        self.cost_tp_hist = None
        self.cost_bp_hist = None
        self.cost_delay_hist = None
        self.reset_counter = 0

        columns = 'reward_hist slice_score_0 slice_score_1 slice_score_2'
        self.env_df = pd.DataFrame(columns=columns.split())
示例#9
0
def task_5_2_4(rho, alpha, sim_time, num):
    """
    Plot confidence interval as described in the task description for task 5.2.4.
    We use the function plot_confidence() for the actual plotting and run our simulation several times to get the
    samples. Due to the different configurations, we receive eight plots in two figures.
    """
    # TODO Task 5.2.4: Your code goes here

    #rho = 0.5 / alpha = 0.1 / Sim time = 100s
    TIC_SU = TimeIndependentCounter("System Utilization")
    TIC_CI = []
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    sim.sim_param.SIM_TIME = sim_time
    sim.sim_param.S = 100000
    sim.sim_param.RHO = rho
    random.seed(sim.sim_param.SEED_IAT)
    random.seed(sim.sim_param.SEED_ST)
    for i in range(100):
        for j in range(30):
            with warnings.catch_warnings():
                warnings.simplefilter("ignore", category=RuntimeWarning)
                TIC_SU.count(sim.do_simulation().system_utilization)
                sim.reset()
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC_CI.append(
                (TIC_SU.get_mean() - TIC_SU.report_confidence_interval(alpha),
                 TIC_SU.get_mean() + TIC_SU.report_confidence_interval(alpha)))
        TIC_SU.reset()
    plot_confidence(sim, 100, TIC_CI, rho, "alpha=" + str(alpha), num, alpha)
 def __init__(self, setting):
     # Load simulation parameters
     self.sim_param = SimParam(setting)
     # Load simtime
     if setting.dynamictest:
         self.SIMTIME = setting.dynamictest.simtime
     self.freeaccess = False
     if setting.secondwindow.test_values[3]:
         self.freeaccess = True
     # Load the simulation state parameters
     self.sim_state = SimState()
     # Load the result parameters
     self.sim_result = SimResult()
     # Load the class which perfomrs all the methods governing a simple slot
     self.slot = TreeSlot()
     # Load the branch node which keeps track of a tree
     self.branch_node = BranchNode()
     # Create an array of integers of which will contain all active nodes.
     self.active_array = []
     # For gated access, the arrived packets are put into a queue
     self.queue_array = []
     # The number of packets generated in a single slot
     self.packets_gen = 0
     # THe result of a slot
     self.result = 0
     # The current slot no
     self.slot_no = 0
     # Load the parameters for single tree resolution
     self.tree_state = TreeState(self)
def task_2_7_1():
    """
    Here, you should execute task 2.7.1 (and 2.7.2, if you want).
    """
    # TODO Task 2.7.1: Your code goes here
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    do_simulation_study(sim)
示例#12
0
def task_2_7_1():
    """
    Here, we execute tasks 2.7.1 and 2.7.2 in the same function. This makes sense, since we use only one figure with
    four subfigures to display the plots, which makes comparability easier.
    """
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    return do_simulation_study(sim)
示例#13
0
def task_5_2_2():
    """
    Run simulation in batches. Start the simulation with running until a customer count of n=100 or (n=1000) and
    continue to increase the number of customers by dn=n.
    Count the blocking proabability for the batch and calculate the confidence interval width of all values, that have
    been counted until now.
    Do this until the desired confidence level is reached and print out the simulation time as well as the number of
    batches.
    """
    results = [None, None, None, None]
    # TODO Task 5.2.2: Your code goes here
    bp = []
    hw = []
    sim_param = SimParam()
    sim = Simulation(sim_param)
    sim.sim_param.S = 4
    sim.sim_param.RHO = .9
    err = .0015
    half_width = 1.0
    count_bp = TimeIndependentCounter()
    i = 0
    for batch in [100, 1000]:
        for alpha in [.1, .05]:
            first_batch = False
            count_bp.reset()
            sim.reset()
            while 1:
                blocking_pro = sim.do_simulation_n_limit(
                    batch, first_batch).blocking_probability
                first_batch = True  #after first batch
                count_bp.count(blocking_pro)
                half_width = count_bp.report_confidence_interval(alpha)
                sim.sim_state.stop = False  #set the parameter back to original value
                sim.counter_collection.reset()
                sim.sim_state.num_blocked_packets = 0
                sim.sim_state.num_packets = 0
                if half_width < err:
                    break
            results[i] = sim.sim_state.now
            bp.append(count_bp.get_mean())
            hw.append(half_width)
            i += 1

    # print and return results
    print("BATCH SIZE:  100; ALPHA: 10%; TOTAL SIMULATION TIME (SECONDS): " +
          str(results[0] / 1000) + "; Blocking Probability Mean: " +
          str(bp[0]) + "; Half width: " + str(hw[0]))
    print("BATCH SIZE:  100; ALPHA:  5%; TOTAL SIMULATION TIME (SECONDS): " +
          str(results[1] / 1000) + "; Blocking Probability Mean: " +
          str(bp[1]) + "; Half width: " + str(hw[1]))
    print("BATCH SIZE: 1000; ALPHA: 10%; TOTAL SIMULATION TIME (SECONDS): " +
          str(results[2] / 1000) + "; Blocking Probability Mean: " +
          str(bp[2]) + "; Half width: " + str(hw[2]))
    print("BATCH SIZE: 1000; ALPHA:  5%; TOTAL SIMULATION TIME (SECONDS): " +
          str(results[3] / 1000) + "; Blocking Probability Mean: " +
          str(bp[3]) + "; Half width: " + str(hw[3]))
    return results
示例#14
0
def task_1_7_1():
    """
    Execute task 1.7.1 and perform a simulation study according to the task assignment.
    :return: Minimum number of buffer spaces to meet requirements.
    """
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    return do_simulation_study(sim)
def task_1_7_3(queue_size):
    """
    Execute bonus task 1.7.3.
    """
    # TODO Bonus Task 1.7.3: Your code goes here (if necessary)
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    sim.sim_param.S=queue_size
    result_set=[]
    for i in range(sim.sim_param.NO_OF_RUNS):
        sim.reset()
        sim_result=sim.do_simulation()
        result_set.append(sim_result.blocking_probability)
    result_length=len(result_set)
    n, bins, patches = pylab.hist(result_set)
    nc=np.cumsum(n/result_length)
    cdf=[0.0]
    for i in nc:
        cdf.append(i)

    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim_param.SIM_TIME = 1000000
    sim_param.MAX_DROPPED = 100
    sim_param.NO_OF_RUNS = 100
    sim = Simulation(sim_param)
    sim.sim_param.S=queue_size
    result_set=[]
    for i in range(sim.sim_param.NO_OF_RUNS):
        sim.reset()
        sim_result=sim.do_simulation()
        result_set.append(sim_result.blocking_probability)
    result_length=len(result_set)
    n1, bins1, patches1 = pylab.hist(result_set)
    nc1=np.cumsum(n/result_length)
    cdf1=[0.0]
    for i in nc:
        cdf1.append(i)

    pylab.figure(1)
    pylab.xlabel('blocking_probability')
    pylab.ylabel('density')
    pylab.title('Histogram of the probability density function')
    pylab.hist(n, bins)
    pylab.hist(n1, bins1)
    pylab.figure(2)
    pylab.xlim(0.0,1.0)
    pylab.ylim(0.0,1.0)
    pylab.xlabel('blocking_probability')
    pylab.ylabel('CDF')
    pylab.title('CDF function')
    line1, = pylab.plot(bins,cdf, marker='o', label='100000 ms, 10 MAX_DROPPED, 1000 runs')
    line2, = pylab.plot(bins1,cdf1, marker='o', label='1000000 ms, 100 MAX_DROPPED, 100 runs')
    pylab.legend(handler_map={line1: HandlerLine2D(numpoints=4)})
    pylab.show()
示例#16
0
def study_waiting_time():
    sim_param = SimParam()
    sim = Simulation(sim_param)
    sim.sim_param.S = 5
    sim.sim_param.SIM_TIME = 100000  # 100 seconds

    num_run = 100
    dataset = []
    for run in range(num_run):
        sim.reset()
        random.seed()
        sim.do_simulation()

        # Take the values (waiting time) of the first 150 packets
        dataset.append(sim.counter_collection.cnt_wt.values[0:150])

    # Manipulate the data set
    pkt_mean = []
    for pkt in range(150):
        s = 0
        for run in range(num_run):
            s += dataset[run][pkt]
        pkt_mean.append(s/float(num_run))

    plt.subplot(121)
    plt.plot(pkt_mean)
    plt.title("100s Run")
    plt.xlabel("Packet Number")
    plt.ylabel("Waiting Time (ms)")

    sim.sim_param.SIM_TIME = 1000000
    dataset = []
    for run in range(num_run):
        sim.reset()
        random.seed()
        sim.do_simulation()

        # Take the values (waiting time) of the first 1900 packets
        dataset.append(sim.counter_collection.cnt_wt.values[0:1800])

    # Manipulate the dataset
    pkt_mean = []
    for pkt in range(1800):
        s = 0
        for run in range(num_run):
            s += dataset[run][pkt]
        pkt_mean.append(s / float(num_run))

    plt.subplot(122)
    plt.plot(pkt_mean)
    plt.title("1000s Run")
    plt.xlabel("Packet Number")
    plt.ylabel("Waiting Time (ms)")
示例#17
0
def task_5_2_1():
    """
    Run task 5.2.1. Make multiple runs until the blocking probability distribution reaches
    a confidence level alpha. Simulation is performed for 100s and 1000s and for alpha = 90% and 95%.
    """
    results = [None, None, None, None]
    # TODO Task 5.2.1: Your code goes here
    bp = []
    hw = []
    sim_param = SimParam()
    sim = Simulation(sim_param)
    sim.sim_param.S = 4
    sim.sim_param.RHO = .9
    count_bp = TimeIndependentCounter()
    err = .0015
    i = 0
    for sim_time in [100000, 1000000]:
        sim.sim_param.SIM_TIME = sim_time
        for alpha in [.1, .05]:
            count_bp.reset()
            while 1:
                sim.reset()
                blocking_pro = sim.do_simulation().blocking_probability
                count_bp.count(blocking_pro)
                half_width = count_bp.report_confidence_interval(alpha=alpha)
                if half_width < err:
                    break

            results[i] = len(count_bp.values)
            bp.append(count_bp.get_mean())
            hw.append(half_width)
            i += 1


# print and return results
    print("SIM TIME:  100s; ALPHA: 10%; NUMBER OF RUNS: " + str(results[0]) +
          "; TOTAL SIMULATION TIME (SECONDS): " + str(results[0] * 100) +
          "; Blocking Probability Mean: " + str(bp[0]) + "; Half width: " +
          str(hw[0]))
    print("SIM TIME:  100s; ALPHA:  5%; NUMBER OF RUNS: " + str(results[1]) +
          "; TOTAL SIMULATION TIME (SECONDS): " + str(results[1] * 100) +
          "; Blocking Probability Mean: " + str(bp[1]) + "; Half width: " +
          str(hw[1]))
    print("SIM TIME: 1000s; ALPHA: 10%; NUMBER OF RUNS:  " + str(results[2]) +
          "; TOTAL SIMULATION TIME (SECONDS): " + str(results[2] * 1000) +
          "; Blocking Probability Mean: " + str(bp[2]) + "; Half width: " +
          str(hw[2]))
    print("SIM TIME: 1000s; ALPHA:  5%; NUMBER OF RUNS:  " + str(results[3]) +
          "; TOTAL SIMULATION TIME (SECONDS): " + str(results[3] * 1000) +
          "; Blocking Probability Mean: " + str(bp[3]) + "; Half width: " +
          str(hw[3]))
    return results
示例#18
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()
def do_theoretical_iter(sim, setting):
    """
    :param n_stop: till the end of number of users we want to sweep till
    can be used to compare different formulas in different formulas in different papers.
    plots the throughput vs number of users
    :return:
    """

    param = SimParam(setting)
    users = range(param.K + 1, setting.theorsweep.n_stop + 1)
    theoretical = []
    theoretical1 = []
    theoretical2 = []
    theoretical3 = []
    theoretical4 = []
    theoretical5 = []
    for n in users:
        if setting.theorsweep.test_values[0]:
            theoretical.append(TheoreticalPlots().qarysic(n, param))
        if setting.theorsweep.test_values[1]:
            theoretical1.append(TheoreticalPlots().sicta(n, param))
        if setting.theorsweep.test_values[2]:
            theoretical2.append(TheoreticalPlots().simpletree(n))
        if setting.theorsweep.test_values[3]:
            theoretical3.append(TheoreticalPlots().recsicta(n))
        if setting.theorsweep.test_values[4]:
            theoretical4.append(TheoreticalPlots().recquary(n, param))
        if setting.theorsweep.test_values[5]:
            theoretical5.append(TheoreticalPlots().qsicta(n, param))
    if setting.theorsweep.test_values[0]:
        pyplot.plot(users, theoretical, 'b-', label='Quary Sic')
    if setting.theorsweep.test_values[1]:
        pyplot.plot(users, theoretical1, 'g-', label='SICTA')
    if setting.theorsweep.test_values[2]:
        pyplot.plot(users, theoretical2, 'r-', label='Simple Tree')
    if setting.theorsweep.test_values[3]:
        pyplot.plot(users, theoretical3, 'c-', label='Recursive SICTA')
    if setting.theorsweep.test_values[4]:
        pyplot.plot(users, theoretical4, 'm-', label='Recursive Quary')
    if setting.theorsweep.test_values[5]:
        pyplot.plot(users, theoretical5, 'y-', label='QSICTA Giannakkis')

    pyplot.xlabel('Users')
    pyplot.ylabel('Throughput')
    pyplot.legend()
    pyplot.xscale('log')
    figname = F"K{sim.sim_param.K}Q{sim.sim_param.SPLIT}TheoreticalCalc"
    pyplot.savefig(figname + '.png', dpi=300)
    tikzplotlib.save(figname + '.tex')
    pyplot.show()
示例#20
0
def study_waiting_time_dist():

    sim_param = SimParam()
    random.seed(0)
    sim = Simulation(sim_param)

    sim.sim_param.S = 5
    sim.sim_param.SIM_TIME = 100000  # 100 seconds

    sim.reset()
    sim.do_simulation()
    # sim.counter_collection.hist_wt.report()
    print(sim.counter_collection.cnt_wt.get_mean())
    plt.plot(sim.counter_collection.cnt_wt.values)
    plt.show()
示例#21
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"
     """
示例#22
0
def task_5_2_4():
    """
    Plot confidence interval as described in the task description for task 5.2.4.
    We use the function plot_confidence() for the actual plotting and run our simulation several times to get the
    samples. Due to the different configurations, we receive eight plots in two figures.
    """
    # TODO Task 5.2.4: Your code goes here

    sim_param = SimParam()
    sim = Simulation(sim_param)
    sim.sim_param.S = 40000000  #infinite M/M/1/inf
    err = .0015
    plt_no = 1
    for rho in [0.5, 0.9]:
        sim.sim_param.RHO = rho
        for alpha in [0.1, 0.05]:
            for sim_time in [100000, 1000000]:
                sim.sim_param.SIM_TIME = sim_time
                print(" Sim time " + str(sim.sim_param.SIM_TIME / 1000) +
                      "s " + " Alpha " + str(alpha) + " RHO " + str(rho))
                count_util = TimeIndependentCounter()
                mean_count = TimeIndependentCounter()
                y_low = []
                y_high = []
                x = []
                for repeat in range(100):
                    count_util.reset()
                    for sim_run in range(30):
                        sim.reset()
                        count_util.count(
                            sim.do_simulation().system_utilization)

                    mean = count_util.get_mean()
                    half_width = count_util.report_confidence_interval(
                        alpha=alpha)
                    mean_count.count(mean)
                    y_low.append(mean - half_width)
                    y_high.append(mean + half_width)
                    x.append(repeat + 1)

                pyplot.subplot(2, 2, plt_no)
                plt_no += 1
                plot_confidence(sim, x, y_low, y_high, mean_count.get_mean(),
                                sim.sim_param.RHO, "Utilization", alpha)

        pyplot.show()
        plt_no = 1
 def reset(self, setting):
     self.sim_param = SimParam(setting)
     if setting.dynamictest:
         self.SIMTIME = setting.dynamictest.simtime
     self.freeaccess = False
     if setting.secondwindow.test_values[3]:
         self.freeaccess = True
     self.sim_state = SimState()
     self.sim_result = SimResult()
     self.slot = TreeSlot()
     self.active_array = []
     self.queue_array = []
     self.packets_gen = 0
     self.result = 0
     self.slot_no = 0
     self.tree_state = TreeState(self)
     self.branch_node.reset()
def task_4_3_2():
    """
    Exercise to plot auto correlation depending on lags. Run simulation until 10000 (or 100) packets are served.
    For the different rho values, simulation is run and the blocking probability is auto correlated.
    Results are plotted for each N value in a different diagram.
    Note, that for some seeds with rho=0.DES and N=100, the variance of the auto covariance is 0 and returns an error.
    """
    # TODO Task 4.3.2: Your code goes here
    data_n_100 = []
    data_n_10000 = []
    lags = range(21)
    del lags[0]
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    sim.sim_param.SIM_TIME = 1000000
    sim.sim_param.S = 10000
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        sim.do_simulation_n_limit(100)
    for i in lags:
        data_n_100.append(sim.counter_collection.acnt_wt.get_auto_cor(i))
    sim.reset()
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        sim.do_simulation_n_limit(10000)
    for i in lags:
        data_n_10000.append(sim.counter_collection.acnt_wt.get_auto_cor(i))
    pylab.figure(1)
    pylab.xlabel('lag')
    pylab.ylabel('Auto Correlation')
    pylab.title(
        'Auto Correlation of Waiting Times (n=100) for lags ranging from 1 to 20'
    )
    pylab.plot(lags, data_n_100)
    pylab.figure(2)
    pylab.xlabel('lag')
    pylab.ylabel('Auto Correlation')
    pylab.title(
        'Auto Correlation of Waiting Times (n=10000) for lags ranging from 1 to 20'
    )
    pylab.plot(lags, data_n_10000)
    pylab.show()
 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))
示例#26
0
def task_4_3_2():
    """
    Exercise to plot the scatter plot of (a) IAT and serving time, (b) serving time and system time
    The scatter plot helps to better understand the meaning of bit/small covariance/correlation.
    For every rho, two scatter plots are needed.
    The simulation parameters are the same as in task_4_3_1()
    """
    # TODO Task 4.3.2: Your code goes here

    sim_param = SimParam()
    sim = Simulation(sim_param)
    sim.sim_param.S = 10000
    sim.sim_param.SIM_TIME = 10000000

    for rho in [0.01, 0.5, 0.8, 0.95]:
        sim.sim_param.RHO = rho
        sim.reset()
        sim.counter_collection.reset()
        sim = sim.do_simulation().sim
        cnt_iat = sim.counter_collection.cnt_iat_st.values_x
        cnt_st = sim.counter_collection.cnt_iat_st.values_y
        cnt_syst = sim.counter_collection.cnt_st_syst.values_y

        corr_iat_st = float(sim.counter_collection.cnt_iat_st.get_cor())
        corr_st_syst = float(sim.counter_collection.cnt_st_syst.get_cor())
        fig = plt.figure()
        ax1 = fig.add_subplot(1, 1, 1)
        plt.subplot(1, 2, 1)
        plt.title('Rho %.2f Correlation %.3f' % (rho, corr_iat_st))
        plt.xlabel("Correlation IAT - ST")
        plt.plot(cnt_iat, cnt_st, 'o')

        plt.subplot(1, 2, 2)
        plt.title('Rho %.2f Correlation %.3f' % (rho, corr_st_syst))
        plt.xlabel("Correlation ST - SYST")
        plt.plot(cnt_st, cnt_syst, 'o')

    plt.show()
示例#27
0
def task_5_2_2():
    """
    Run simulation in batches. Start the simulation with running until a customer count of n=100 or (n=1000) and
    continue to increase the number of customers by dn=n.
    Count the blocking proabability for the batch and calculate the confidence interval width of all values, that have
    been counted until now.
    Do this until the desired confidence level is reached and print out the simulation time as well as the number of
    batches.
    """
    num_batches1 = 0
    num_batches2 = 0
    num_batches3 = 0
    num_batches4 = 0
    TIC = TimeIndependentCounter("bp")

    # TODO Task 5.2.2: Your code goes here
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    sim.sim_param.S = 4
    sim.sim_param.RHO = 0.9
    ## n = 100
    # ALPHA: 5%
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        TIC.count(sim.do_simulation_n_limit(100).blocking_probability)

    for i in range(10000):
        sim.sim_result = SimResult(sim)
        sim.sim_state.stop = False
        sim.sim_state.num_packets = 0
        sim.sim_state.num_blocked_packets = 0
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(
                sim.do_simulation_n_limit(100, True).blocking_probability)
            print TIC.report_confidence_interval(0.05)
            if TIC.report_confidence_interval(0.05) < 0.0015:
                num_batches1 = i + 1
                break
    t1 = sim.sim_state.now

    # ALPHA: 10%
    sim.reset()
    TIC.reset()
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        TIC.count(sim.do_simulation_n_limit(100).blocking_probability)
    for i in range(10000):
        sim.sim_result = SimResult(sim)
        sim.sim_state.stop = False
        sim.sim_state.num_packets = 0
        sim.sim_state.num_blocked_packets = 0
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(
                sim.do_simulation_n_limit(100, True).blocking_probability)
            print TIC.report_confidence_interval(0.1)
            if TIC.report_confidence_interval(0.1) < 0.0015:
                num_batches2 = i + 1
                break
    t2 = sim.sim_state.now

    sim.reset()
    ## n = 1000
    # ALPHA: 5%
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        TIC.count(sim.do_simulation_n_limit(100).blocking_probability)

    for i in range(10000):
        sim.sim_result = SimResult(sim)
        sim.sim_state.stop = False
        sim.sim_state.num_packets = 0
        sim.sim_state.num_blocked_packets = 0
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(
                sim.do_simulation_n_limit(1000, True).blocking_probability)
            print TIC.report_confidence_interval(0.05)
            if TIC.report_confidence_interval(0.05) < 0.0015:
                num_batches3 = i + 1
                break
    t3 = sim.sim_state.now

    # ALPHA: 10%
    sim.reset()
    TIC.reset()
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        TIC.count(sim.do_simulation_n_limit(100).blocking_probability)
    for i in range(10000):
        sim.sim_result = SimResult(sim)
        sim.sim_state.stop = False
        sim.sim_state.num_packets = 0
        sim.sim_state.num_blocked_packets = 0
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(
                sim.do_simulation_n_limit(1000, True).blocking_probability)
            print TIC.report_confidence_interval(0.1)
            if TIC.report_confidence_interval(0.1) < 0.0015:
                num_batches4 = i + 1
                break
    t4 = sim.sim_state.now

    # print and return both results
    print "N:  100; ALPHA:  5%; NUMBER OF BATCHES: " + str(
        num_batches1) + " and SIM TIME: " + str(t1)
    print "N:  100; ALPHA: 10%; NUMBER OF BATCHES: " + str(
        num_batches2) + " and SIM TIME: " + str(t2)
    print "N: 1000; ALPHA:  5%; NUMBER OF BATCHES: " + str(
        num_batches3) + " and SIM TIME: " + str(t3)
    print "N: 1000; ALPHA: 10%; NUMBER OF BATCHES: " + str(
        num_batches4) + " and SIM TIME: " + str(t4)

    return [t1, t2, t3, t4]
示例#28
0
def task_5_2_1():
    """
    Run task 5.2.1. Make multiple runs until the blocking probability distribution reaches
    a confidence level alpha. Simulation is performed for 100s and 1000s and for alpha = 90% and 95%.
    """
    results = [None, None, None, None]
    TIC = TimeIndependentCounter("bp")

    # TODO Task 5.2.1: Your code goes here
    #SIM TIME:  100s; ALPHA: 10%
    sim_param = SimParam()
    random.seed(sim_param.SEED)
    sim = Simulation(sim_param)
    sim.sim_param.SIM_TIME = 100000
    sim.sim_param.S = 4
    sim.sim_param.RHO = 0.9
    for i in range(10000):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(sim.do_simulation().blocking_probability)
            if TIC.report_confidence_interval(0.1) < 0.0015:
                results[0] = i
                break
            sim.reset()
    #SIM TIME:  100s; ALPHA:  5%
    TIC.reset()
    for i in range(10000):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(sim.do_simulation().blocking_probability)
            if TIC.report_confidence_interval(0.05) < 0.0015:
                results[1] = i
                break
            sim.reset()
    #SIM TIME: 1000s; ALPHA: 10%
    sim.sim_param.SIM_TIME = 1000000
    TIC.reset()
    for i in range(10000):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(sim.do_simulation().blocking_probability)
            if TIC.report_confidence_interval(0.05) < 0.0015:
                results[2] = i
                break
            sim.reset()
    #SIM TIME: 1000s; ALPHA:  5%
    sim.sim_param.SIM_TIME = 1000000
    TIC.reset()
    for i in range(10000):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            TIC.count(sim.do_simulation().blocking_probability)
            if TIC.report_confidence_interval(0.05) < 0.0015:
                results[3] = i
                break
            sim.reset()

    # print and return results
    print "SIM TIME:  100s; ALPHA: 10%; NUMBER OF RUNS: " + str(results[0])
    print "SIM TIME:  100s; ALPHA:  5%; NUMBER OF RUNS: " + str(results[1])
    print "SIM TIME: 1000s; ALPHA: 10%; NUMBER OF RUNS:  " + str(results[2])
    print "SIM TIME: 1000s; ALPHA:  5%; NUMBER OF RUNS:  " + str(results[3])
    return results
示例#29
0
class UWG(object):
    """Morph a rural EPW file to urban conditions using a file with a list of urban parameters.

    args:
        epwDir: The directory in which the rural EPW file sits.
        epwFileName: The name of the rural epw file that will be morphed.
        uwgParamDir: The directory in which the UWG Parameter File (.uwg) sits.
        uwgParamFileName: The name of the UWG Parameter File (.uwg).
        destinationDir: Optional destination directory for the morphed EPW file.
            If left blank, the morphed file will be written into the same directory
            as the rural EPW file (the epwDir).
        destinationFileName: Optional destination file name for the morphed EPW file.
            If left blank, the morphed file will append "_UWG" to the original file name.
    returns:
        newClimateFile: the path to a new EPW file that has been morphed to account
            for uban conditions.
    """
    """ Section 1 - Definitions for constants / other parameters """
    MINTHICKNESS = 0.01  # Minimum layer thickness (to prevent crashing) (m)
    MAXTHICKNESS = 0.05  # Maximum layer thickness (m)
    SOILTCOND = 1  # http://web.mit.edu/parmstr/Public/NRCan/nrcc29118.pdf (Figly & Snodgrass)
    SOILVOLHEAT = 2e6  # http://www.europment.org/library/2013/venice/bypaper/MFHEEF/MFHEEF-21.pdf (average taken from Table 1)
    SOIL = Material(SOILTCOND, SOILVOLHEAT,
                    name="soil")  # Soil material used for soil-depth padding

    # Physical constants
    G = 9.81  # gravity (m s-2)
    CP = 1004.  # heat capacity for air (J/kg K)
    VK = 0.40  # von karman constant (dimensionless)
    R = 287.  # gas constant dry air (J/kg K)
    RV = 461.5  # gas constant water vapor (J/kg K)
    LV = 2.26e6  # latent heat of evaporation (J/kg)
    SIGMA = 5.67e-08  # Stefan Boltzmann constant (W m-2 K-4)
    WATERDENS = 1000.  # water density (kg m-3)
    LVTT = 2.5008e6  #
    TT = 273.16  #
    ESTT = 611.14  #
    CL = 4.218e3  #
    CPV = 1846.1  #
    B = 9.4  # Coefficients derived by Louis (1979)
    CM = 7.4  #
    COLBURN = math.pow(
        (0.713 / 0.621),
        (2 / 3.))  # (Pr/Sc)^(2/3) for Colburn analogy in water evaporation

    # Site-specific parameters
    WGMAX = 0.005  # maximum film water depth on horizontal surfaces (m)

    # File path parameter
    RESOURCE_PATH = os.path.abspath(
        os.path.join(os.path.dirname(__file__), "..", "resources"))
    CURRENT_PATH = os.path.abspath(os.path.dirname(__file__))

    def __init__(self,
                 epwFileName,
                 uwgParamFileName=None,
                 epwDir=None,
                 uwgParamDir=None,
                 destinationDir=None,
                 destinationFileName=None):

        # Logger will be disabled by default unless explicitly called in tests
        self.logger = logging.getLogger(__name__)

        # User defined
        self.epwFileName = epwFileName if epwFileName.lower().endswith(
            '.epw'
        ) else epwFileName + '.epw'  # Revise epw file name if not end with epw
        self.uwgParamFileName = uwgParamFileName  # If file name is entered then will UWG will set input from .uwg file

        # If user does not overload
        self.destinationFileName = destinationFileName if destinationFileName else self.epwFileName.strip(
            '.epw') + '_UWG.epw'
        self.epwDir = epwDir if epwDir else os.path.join(
            self.RESOURCE_PATH, "epw")
        self.uwgParamDir = uwgParamDir if uwgParamDir else os.path.join(
            self.RESOURCE_PATH, "parameters")
        self.destinationDir = destinationDir if destinationDir else os.path.join(
            self.RESOURCE_PATH, "epw_uwg")

        # refdata: Serialized DOE reference data, z_meso height data
        self.readDOE_file_path = os.path.join(self.CURRENT_PATH, "refdata",
                                              "readDOE.pkl")
        self.z_meso_dir_path = os.path.join(self.CURRENT_PATH, "refdata")

        # EPW precision
        self.epw_precision = 1

        # init UWG variables
        self._init_param_dict = None

        # Define Simulation and Weather parameters
        self.Month = None  # starting month (1-12)
        self.Day = None  # starting day (1-31)
        self.nDay = None  # number of days
        self.dtSim = None  # simulation time step (s)
        self.dtWeather = None  # seconds (s)

        # HVAC system and internal laod
        self.autosize = None  # autosize HVAC (1 or 0)
        self.sensOcc = None  # Sensible heat from occupant
        self.LatFOcc = None  # Latent heat fraction from occupant (normally 0.3)
        self.RadFOcc = None  # Radiant heat fraction from occupant (normally 0.2)
        self.RadFEquip = None  # Radiant heat fraction from equipment (normally 0.5)
        self.RadFLight = None  # Radiant heat fraction from light (normally 0.7)

        # Define Urban microclimate parameters
        self.h_ubl1 = None  # ubl height - day (m)
        self.h_ubl2 = None  # ubl height - night (m)
        self.h_ref = None  # inversion height
        self.h_temp = None  # temperature height
        self.h_wind = None  # wind height
        self.c_circ = None  # circulation coefficient
        self.c_exch = None  # exchange coefficient
        self.maxDay = None  # max day threshhold
        self.maxNight = None  # max night threshhold
        self.windMin = None  # min wind speed (m/s)
        self.h_obs = None  # rural average obstacle height

        # Urban characteristics
        self.bldHeight = None  # average building height (m)
        self.h_mix = None  # mixing height (m)
        self.bldDensity = None  # building density (0-1)
        self.verToHor = None  # building aspect ratio
        self.charLength = None  # radius defining the urban area of study [aka. characteristic length] (m)
        self.alb_road = None  # road albedo
        self.d_road = None  # road pavement thickness
        self.sensAnth = None  # non-building sensible heat (W/m^2)
        self.latAnth = None  # non-building latent heat heat (W/m^2)

        # Fraction of building typology stock
        self.bld = None  # 16x3 matrix of fraction of building type by era

        # climate Zone
        self.zone = None

        # Vegetation parameters
        self.vegCover = None  # urban area veg coverage ratio
        self.treeCoverage = None  # urban area tree coverage ratio
        self.vegStart = None  # vegetation start month
        self.vegEnd = None  # vegetation end month
        self.albVeg = None  # Vegetation albedo
        self.rurVegCover = None  # rural vegetation cover
        self.latGrss = None  # latent fraction of grass
        self.latTree = None  # latent fraction of tree

        # Define Traffic schedule
        self.SchTraffic = None

        # Define Road (Assume 0.5m of asphalt)
        self.kRoad = None  # road pavement conductivity (W/m K)
        self.cRoad = None  # road volumetric heat capacity (J/m^3 K)

        # Define optional Building characteristics
        self.flr_h = None  # floor-to-floor height
        self.albRoof = None  # roof albedo (0 - 1)
        self.vegRoof = None  # Fraction of the roofs covered in grass/shrubs (0-1)
        self.glzR = None  # Glazing Ratio
        self.SHGC = None  # Solar Heat Gain Coefficient
        self.albWall = None  # Wall albedo

    def __repr__(self):
        return "UWG: {} ".format(self.epwFileName)

    def is_near_zero(self, num, eps=1e-10):
        return abs(float(num)) < eps

    def read_epw(self):
        """Section 2 - Read EPW file
        properties:
            self.climateDataPath
            self.newPathName
            self._header    # header data
            self.epwinput   # timestep data for weather
            self.lat        # latitude
            self.lon        # longitude
            self.GMT        # GMT
            self.nSoil      # Number of soil depths
            self.Tsoil      # nSoil x 12 matrix for soil temperture (K)
            self.depth_soil # nSoil x 1 matrix for soil depth (m)
        """

        # Make dir path to epw file
        self.climateDataPath = os.path.join(self.epwDir, self.epwFileName)

        # Open epw file and feed csv data to climate_data
        try:
            climate_data = utilities.read_csv(self.climateDataPath)
        except Exception as e:
            raise Exception("Failed to read epw file! {}".format(e.message))

        # Read header lines (1 to 8) from EPW and ensure TMY2 format.
        self._header = climate_data[0:8]

        # Read weather data from EPW for each time step in weather file. (lines 8 - end)
        self.epwinput = climate_data[8:]

        # Read Lat, Long (line 1 of EPW)
        self.lat = float(self._header[0][6])
        self.lon = float(self._header[0][7])
        self.GMT = float(self._header[0][8])

        # Read in soil temperature data (assumes this is always there)
        # ref: http://bigladdersoftware.com/epx/docs/8-2/auxiliary-programs/epw-csv-format-inout.html
        soilData = self._header[3]
        self.nSoil = int(soilData[1])  # Number of ground temperature depths
        self.Tsoil = utilities.zeros(
            self.nSoil, 12)  # nSoil x 12 matrix for soil temperture (K)
        self.depth_soil = utilities.zeros(
            self.nSoil, 1)  # nSoil x 1 matrix for soil depth (m)

        # Read monthly data for each layer of soil from EPW file
        for i in xrange(self.nSoil):
            self.depth_soil[i][0] = float(
                soilData[2 + (i * 16)])  # get soil depth for each nSoil
            # Monthly data
            for j in xrange(12):
                self.Tsoil[i][j] = float(soilData[
                    6 + (i * 16) +
                    j]) + 273.15  # 12 months of soil T for specific depth

        # Set new directory path for the moprhed EPW file
        self.newPathName = os.path.join(self.destinationDir,
                                        self.destinationFileName)

    def read_input(self):
        """Section 3 - Read Input File (.m, file)
        Note: UWG_Matlab input files are xlsm, XML, .m, file.
        properties:
            self._init_param_dict   # dictionary of simulation initialization parameters

            self.sensAnth           # non-building sensible heat (W/m^2)
            self.SchTraffic         # Traffice schedule

            self.BEM                # list of BEMDef objects extracted from readDOE
            self.Sch                # list of Schedule objects extracted from readDOE

        """

        uwg_param_file_path = os.path.join(self.uwgParamDir,
                                           self.uwgParamFileName)

        if not os.path.exists(uwg_param_file_path):
            raise Exception(
                "Param file: '{}' does not exist.".format(uwg_param_file_path))

        # Open .uwg file and feed csv data to initializeDataFile
        try:
            uwg_param_data = utilities.read_csv(uwg_param_file_path)
        except Exception as e:
            raise Exception("Failed to read .uwg file! {}".format(e.message))

        # The initialize.uwg is read with a dictionary so that users changing
        # line endings or line numbers doesn't make reading input incorrect
        self._init_param_dict = {}
        count = 0
        while count < len(uwg_param_data):
            row = uwg_param_data[count]
            row = [row[i].replace(" ", "")
                   for i in xrange(len(row))]  # strip white spaces

            # Optional parameters might be empty so handle separately
            is_optional_parameter = (
                row != [] and \
                    (
                    row[0] == "albRoof" or \
                    row[0] == "vegRoof" or \
                    row[0] == "glzR" or \
                    row[0] == "hvac" or \
                    row[0] == "albWall" or \
                    row[0] == "SHGC"
                    )
                )

            if row == [] or "#" in row[0]:
                count += 1
                continue
            elif row[0] == "SchTraffic":
                # SchTraffic: 3 x 24 matrix
                trafficrows = uwg_param_data[count + 1:count + 4]
                self._init_param_dict[row[0]] = map(
                    lambda r: utilities.str2fl(r[:24]), trafficrows)
                count += 4
            elif row[0] == "bld":
                #bld: 17 x 3 matrix
                bldrows = uwg_param_data[count + 1:count + 17]
                self._init_param_dict[row[0]] = map(
                    lambda r: utilities.str2fl(r[:3]), bldrows)
                count += 17
            elif is_optional_parameter:
                self._init_param_dict[row[0]] = float(
                    row[1]) if row[1] != "" else None
                count += 1
            else:
                self._init_param_dict[row[0]] = float(row[1])
                count += 1

        ipd = self._init_param_dict

        # Define Simulation and Weather parameters
        if self.Month is None: self.Month = ipd['Month']
        if self.Day is None: self.Day = ipd['Day']
        if self.nDay is None: self.nDay = ipd['nDay']
        if self.dtSim is None: self.dtSim = ipd['dtSim']
        if self.dtWeather is None: self.dtWeather = ipd['dtWeather']

        # HVAC system and internal laod
        if self.autosize is None: self.autosize = ipd['autosize']
        if self.sensOcc is None: self.sensOcc = ipd['sensOcc']
        if self.LatFOcc is None: self.LatFOcc = ipd['LatFOcc']
        if self.RadFOcc is None: self.RadFOcc = ipd['RadFOcc']
        if self.RadFEquip is None: self.RadFEquip = ipd['RadFEquip']
        if self.RadFLight is None: self.RadFLight = ipd['RadFLight']

        # Define Urban microclimate parameters
        if self.h_ubl1 is None: self.h_ubl1 = ipd['h_ubl1']
        if self.h_ubl2 is None: self.h_ubl2 = ipd['h_ubl2']
        if self.h_ref is None: self.h_ref = ipd['h_ref']
        if self.h_temp is None: self.h_temp = ipd['h_temp']
        if self.h_wind is None: self.h_wind = ipd['h_wind']
        if self.c_circ is None: self.c_circ = ipd['c_circ']
        if self.c_exch is None: self.c_exch = ipd['c_exch']
        if self.maxDay is None: self.maxDay = ipd['maxDay']
        if self.maxNight is None: self.maxNight = ipd['maxNight']
        if self.windMin is None: self.windMin = ipd['windMin']
        if self.h_obs is None: self.h_obs = ipd['h_obs']

        # Urban characteristics
        if self.bldHeight is None: self.bldHeight = ipd['bldHeight']
        if self.h_mix is None: self.h_mix = ipd['h_mix']
        if self.bldDensity is None: self.bldDensity = ipd['bldDensity']
        if self.verToHor is None: self.verToHor = ipd['verToHor']
        if self.charLength is None: self.charLength = ipd['charLength']
        if self.alb_road is None: self.alb_road = ipd['albRoad']
        if self.d_road is None: self.d_road = ipd['dRoad']
        if self.sensAnth is None: self.sensAnth = ipd['sensAnth']
        if self.latAnth is None: self.latAnth = ipd['latAnth']

        # climate Zone
        if self.zone is None: self.zone = ipd['zone']

        # Vegetation parameters
        if self.vegCover is None: self.vegCover = ipd['vegCover']
        if self.treeCoverage is None: self.treeCoverage = ipd['treeCoverage']
        if self.vegStart is None: self.vegStart = ipd['vegStart']
        if self.vegEnd is None: self.vegEnd = ipd['vegEnd']
        if self.albVeg is None: self.albVeg = ipd['albVeg']
        if self.rurVegCover is None: self.rurVegCover = ipd['rurVegCover']
        if self.latGrss is None: self.latGrss = ipd['latGrss']
        if self.latTree is None: self.latTree = ipd['latTree']

        # Define Traffic schedule
        if self.SchTraffic is None: self.SchTraffic = ipd['SchTraffic']

        # Define Road (Assume 0.5m of asphalt)
        if self.kRoad is None: self.kRoad = ipd['kRoad']
        if self.cRoad is None: self.cRoad = ipd['cRoad']

        # Building stock fraction
        if self.bld is None: self.bld = ipd['bld']

        # Optional parameters
        if self.albRoof is None: self.albRoof = ipd['albRoof']
        if self.vegRoof is None: self.vegRoof = ipd['vegRoof']
        if self.glzR is None: self.glzR = ipd['glzR']
        if self.albWall is None: self.albWall = ipd['albWall']
        if self.SHGC is None: self.SHGC = ipd['SHGC']

    def set_input(self):
        """ Set inputs from .uwg input file if not already defined, the check if all
        the required input parameters are there.
        """

        # If a uwgParamFileName is set, then read inputs from .uwg file.
        # User-defined class properties will override the inputs from the .uwg file.
        if self.uwgParamFileName is not None:
            print "\nReading uwg file input."
            self.read_input()
        else:
            print "\nNo .uwg file input."

        # Required parameters
        is_defined = (type(self.Month) == float or type(self.Month) == int) and \
            (type(self.Day) == float or type(self.Day) == int) and \
            (type(self.nDay) == float or type(self.nDay) == int) and \
            type(self.dtSim) == float and type(self.dtWeather) == float and \
            (type(self.autosize) == float or type(self.autosize) == int) and \
            type(self.sensOcc) == float and type(self.LatFOcc) == float and \
            type(self.RadFOcc) == float and type(self.RadFEquip) == float and \
            type(self.RadFLight) == float and type(self.h_ubl1) == float and \
            type(self.h_ubl2) == float and type(self.h_ref) == float and \
            type(self.h_temp) == float and type(self.h_wind) == float and \
            type(self.c_circ) == float and type(self.c_exch) == float and \
            type(self.maxDay) == float and type(self.maxNight) == float and \
            type(self.windMin) == float and type(self.h_obs) == float and \
            type(self.bldHeight) == float and type(self.h_mix) == float and \
            type(self.bldDensity) == float and type(self.verToHor) == float and \
            type(self.charLength) == float and type(self.alb_road) == float and \
            type(self.d_road) == float and type(self.sensAnth) == float and \
            type(self.latAnth) == float and type(self.bld) == type([]) and \
            self.is_near_zero(len(self.bld)-16.0) and \
            type(self.latTree) == float and type(self.latGrss) == float and \
            (type(self.zone) == float or type(self.zone) == int) and \
            (type(self.vegStart) == float or type(self.vegStart) == int) and \
            (type(self.vegEnd) == float or type(self.vegEnd) == int) and \
            type(self.vegCover) == float and type(self.treeCoverage) == float and \
            type(self.albVeg) == float and type(self.rurVegCover) == float and \
            type(self.kRoad) == float and type(self.cRoad) == float and \
            type(self.SchTraffic) == type([]) and self.is_near_zero(len(self.SchTraffic)-3.0)

        if not is_defined:
            raise Exception(
                "The required parameters have not been defined correctly. Check input parameters and try again."
            )

        # Modify zone to be used as python index
        self.zone = int(self.zone) - 1

    def init_input_obj(self):
        """Section 4 - Create UWG objects from input parameters

            self.simTime            # simulation time parameter obj
            self.weather            # weather obj for simulation time period
            self.forcIP             # Forcing obj
            self.forc               # Empty forcing obj
            self.geoParam           # geographic parameters obj
            self.RSM                # Rural site & vertical diffusion model obj
            self.USM                # Urban site & vertical diffusion model obj
            self.UCM                # Urban canopy model obj
            self.UBL                # Urban boundary layer model

            self.road               # urban road element
            self.rural              # rural road element

            self.soilindex1         # soil index for urban rsoad depth
            self.soilindex2         # soil index for rural road depth

            self.BEM                # list of BEMDef objects
            self.Sch                # list of Schedule objects
        """

        climate_file_path = os.path.join(self.epwDir, self.epwFileName)

        self.simTime = SimParam(self.dtSim, self.dtWeather, self.Month,
                                self.Day,
                                self.nDay)  # simulation time parametrs
        self.weather = Weather(
            climate_file_path, self.simTime.timeInitial, self.simTime.timeFinal
        )  # weather file data for simulation time period
        self.forcIP = Forcing(self.weather.staTemp,
                              self.weather)  # initialized Forcing class
        self.forc = Forcing()  # empty forcing class

        # Initialize geographic Param and Urban Boundary Layer Objects
        nightStart = 18.  # arbitrary values for begin/end hour for night setpoint
        nightEnd = 8.
        maxdx = 250.
        # max dx (m)

        self.geoParam = Param(self.h_ubl1,self.h_ubl2,self.h_ref,self.h_temp,self.h_wind,self.c_circ,\
            self.maxDay,self.maxNight,self.latTree,self.latGrss,self.albVeg,self.vegStart,self.vegEnd,\
            nightStart,nightEnd,self.windMin,self.WGMAX,self.c_exch,maxdx,self.G,self.CP,self.VK,self.R,\
            self.RV,self.LV,math.pi,self.SIGMA,self.WATERDENS,self.LVTT,self.TT,self.ESTT,self.CL,\
            self.CPV,self.B, self.CM, self.COLBURN)

        self.UBL = UBLDef('C', self.charLength, self.weather.staTemp[0], maxdx,
                          self.geoParam.dayBLHeight,
                          self.geoParam.nightBLHeight)

        # Defining road
        emis = 0.93
        asphalt = Material(self.kRoad, self.cRoad, 'asphalt')
        road_T_init = 293.
        road_horizontal = 1
        road_veg_coverage = min(self.vegCover / (1 - self.bldDensity),
                                1.)  # fraction of surface vegetation coverage

        # define road layers
        road_layer_num = int(math.ceil(self.d_road / 0.05))
        thickness_vector = map(lambda r: 0.05, range(
            road_layer_num))  # 0.5/0.05 ~ 10 x 1 matrix of 0.05 thickness
        material_vector = map(lambda n: asphalt, range(road_layer_num))

        self.road = Element(self.alb_road,emis,thickness_vector,material_vector,road_veg_coverage,\
            road_T_init,road_horizontal,name="urban_road")

        self.rural = copy.deepcopy(self.road)
        self.rural.vegCoverage = self.rurVegCover
        self.rural._name = "rural_road"

        # Define BEM for each DOE type (read the fraction)
        if not os.path.exists(self.readDOE_file_path):
            raise Exception("readDOE.pkl file: '{}' does not exist.".format(
                readDOE_file_path))

        readDOE_file = open(self.readDOE_file_path,
                            'rb')  # open pickle file in binary form
        refDOE = cPickle.load(readDOE_file)
        refBEM = cPickle.load(readDOE_file)
        refSchedule = cPickle.load(readDOE_file)
        readDOE_file.close()

        # Define building energy models
        k = 0
        r_glaze = 0  # Glazing ratio for total building stock
        SHGC = 0  # SHGC addition for total building stock
        alb_wall = 0  # albedo wall addition for total building stock
        h_floor = self.flr_h or 3.05  # average floor height

        total_urban_bld_area = math.pow(
            self.charLength, 2
        ) * self.bldDensity * self.bldHeight / h_floor  # total building floor area
        area_matrix = utilities.zeros(16, 3)

        self.BEM = []  # list of BEMDef objects
        self.Sch = []  # list of Schedule objects

        for i in xrange(16):  # 16 building types
            for j in xrange(3):  # 3 built eras
                if self.bld[i][j] > 0.:
                    # Add to BEM list
                    self.BEM.append(refBEM[i][j][self.zone])
                    self.BEM[k].frac = self.bld[i][j]
                    self.BEM[k].fl_area = self.bld[i][j] * total_urban_bld_area

                    # Overwrite with optional parameters if provided
                    if self.glzR:
                        self.BEM[k].building.glazingRatio = self.glzR
                    if self.albRoof:
                        self.BEM[k].roof.albedo = self.albRoof
                    if self.vegRoof:
                        self.BEM[k].roof.vegCoverage = self.vegRoof
                    if self.SHGC:
                        self.BEM[k].building.shgc = self.SHGC
                    if self.albWall:
                        self.BEM[k].wall.albedo = self.albWall

                    # Keep track of total urban r_glaze, SHGC, and alb_wall for UCM model
                    r_glaze = r_glaze + self.BEM[k].frac * self.BEM[
                        k].building.glazingRatio  ##
                    SHGC = SHGC + self.BEM[k].frac * self.BEM[k].building.shgc
                    alb_wall = alb_wall + self.BEM[k].frac * self.BEM[
                        k].wall.albedo
                    # Add to schedule list
                    self.Sch.append(refSchedule[i][j][self.zone])
                    k += 1

        # Reference site class (also include VDM)
        self.RSM = RSMDef(self.lat, self.lon, self.GMT, self.h_obs,
                          self.weather.staTemp[0], self.weather.staPres[0],
                          self.geoParam, self.z_meso_dir_path)
        self.USM = RSMDef(self.lat, self.lon, self.GMT, self.bldHeight / 10.,
                          self.weather.staTemp[0], self.weather.staPres[0],
                          self.geoParam, self.z_meso_dir_path)

        T_init = self.weather.staTemp[0]
        H_init = self.weather.staHum[0]

        self.UCM = UCMDef(self.bldHeight,self.bldDensity,self.verToHor,self.treeCoverage,self.sensAnth,self.latAnth,T_init,H_init,\
        self.weather.staUmod[0],self.geoParam,r_glaze,SHGC,alb_wall,self.road)
        self.UCM.h_mix = self.h_mix

        # Define Road Element & buffer to match ground temperature depth
        roadMat, newthickness = procMat(self.road, self.MAXTHICKNESS,
                                        self.MINTHICKNESS)

        for i in xrange(self.nSoil):
            # if soil depth is greater then the thickness of the road
            # we add new slices of soil at max thickness until road is greater or equal

            is_soildepth_equal = self.is_near_zero(
                self.depth_soil[i][0] - sum(newthickness), 1e-15)

            if is_soildepth_equal or (self.depth_soil[i][0] >
                                      sum(newthickness)):
                while self.depth_soil[i][0] > sum(newthickness):
                    newthickness.append(self.MAXTHICKNESS)
                    roadMat.append(self.SOIL)
                self.soilindex1 = i
                break

        self.road = Element(self.road.albedo, self.road.emissivity, newthickness, roadMat,\
            self.road.vegCoverage, self.road.layerTemp[0], self.road.horizontal, self.road._name)

        # Define Rural Element
        ruralMat, newthickness = procMat(self.rural, self.MAXTHICKNESS,
                                         self.MINTHICKNESS)

        for i in xrange(self.nSoil):
            # if soil depth is greater then the thickness of the road
            # we add new slices of soil at max thickness until road is greater or equal

            is_soildepth_equal = self.is_near_zero(
                self.depth_soil[i][0] - sum(newthickness), 1e-15)

            if is_soildepth_equal or (self.depth_soil[i][0] >
                                      sum(newthickness)):
                while self.depth_soil[i][0] > sum(newthickness):
                    newthickness.append(self.MAXTHICKNESS)
                    ruralMat.append(self.SOIL)

                self.soilindex2 = i
                break

        self.rural = Element(self.rural.albedo, self.rural.emissivity, newthickness,\
            ruralMat,self.rural.vegCoverage,self.rural.layerTemp[0],self.rural.horizontal, self.rural._name)

    def hvac_autosize(self):
        """ Section 6 - HVAC Autosizing (unlimited cooling & heating) """

        for i in xrange(len(self.BEM)):
            if self.is_near_zero(self.autosize) == False:
                self.BEM[i].building.coolCap = 9999.
                self.BEM[i].building.heatCap = 9999.

    def simulate(self):
        """ Section 7 - UWG main section

            self.N                  # Total hours in simulation
            self.ph                 # per hour
            self.dayType            # 3=Sun, 2=Sat, 1=Weekday
            self.ceil_time_step     # simulation timestep (dt) fitted to weather file timestep

            # Output of object instance vector
            self.WeatherData        # Nx1 vector of forc instance
            self.UCMData            # Nx1 vector of UCM instance
            self.UBLData            # Nx1 vector of UBL instance
            self.RSMData            # Nx1 vector of RSM instance
            self.USMData            # Nx1 vector of USM instance
        """

        self.N = int(self.simTime.days *
                     24)  # total number of hours in simulation
        n = 0  # weather time step counter
        self.ph = self.simTime.dt / 3600.  # dt (simulation time step) in hours

        # Data dump variables
        time = range(self.N)

        self.WeatherData = [None for x in xrange(self.N)]
        self.UCMData = [None for x in xrange(self.N)]
        self.UBLData = [None for x in xrange(self.N)]
        self.RSMData = [None for x in xrange(self.N)]
        self.USMData = [None for x in xrange(self.N)]

        print '\nSimulating new temperature and humidity values for {} days from {}/{}.\n'.format(
            int(self.nDay), int(self.Month), int(self.Day))
        self.logger.info("Start simulation")

        for it in range(
                1, self.simTime.nt, 1
        ):  # for every simulation time-step (i.e 5 min) defined by uwg
            # Update water temperature (estimated)
            if self.is_near_zero(self.nSoil):
                self.forc.deepTemp = sum(self.forcIP.temp) / float(
                    len(self.forcIP.temp)
                )  # for BUBBLE/CAPITOUL/Singapore only
                self.forc.waterTemp = sum(self.forcIP.temp) / float(
                    len(self.forcIP.temp)
                ) - 10.  # for BUBBLE/CAPITOUL/Singapore only
            else:
                self.forc.deepTemp = self.Tsoil[
                    self.soilindex1][self.simTime.month -
                                     1]  #soil temperature by depth, by month
                self.forc.waterTemp = self.Tsoil[2][self.simTime.month - 1]

            # There's probably a better way to update the weather...
            self.simTime.UpdateDate()

            self.logger.info("\n{0} m={1}, d={2}, h={3}, s={4}".format(
                __name__, self.simTime.month, self.simTime.day,
                self.simTime.secDay / 3600., self.simTime.secDay))

            self.ceil_time_step = int(
                math.ceil(it * self.ph)
            ) - 1  # simulation time increment raised to weather time step
            # minus one to be consistent with forcIP list index
            # Updating forcing instance
            self.forc.infra = self.forcIP.infra[
                self.
                ceil_time_step]  # horizontal Infrared Radiation Intensity (W m-2)
            self.forc.wind = max(self.forcIP.wind[self.ceil_time_step],
                                 self.geoParam.windMin)  # wind speed (m s-1)
            self.forc.uDir = self.forcIP.uDir[
                self.ceil_time_step]  # wind direction
            self.forc.hum = self.forcIP.hum[
                self.ceil_time_step]  # specific humidty (kg kg-1)
            self.forc.pres = self.forcIP.pres[
                self.ceil_time_step]  # Pressure (Pa)
            self.forc.temp = self.forcIP.temp[
                self.ceil_time_step]  # air temperature (C)
            self.forc.rHum = self.forcIP.rHum[
                self.ceil_time_step]  # Relative humidity (%)
            self.forc.prec = self.forcIP.prec[
                self.ceil_time_step]  # Precipitation (mm h-1)
            self.forc.dif = self.forcIP.dif[
                self.
                ceil_time_step]  # horizontal solar diffuse radiation (W m-2)
            self.forc.dir = self.forcIP.dir[
                self.ceil_time_step]  # normal solar direct radiation (W m-2)
            self.UCM.canHum = copy.copy(
                self.forc.hum)  # Canyon humidity (absolute) same as rural

            # Update solar flux
            self.solar = SolarCalcs(self.UCM, self.BEM, self.simTime, self.RSM,
                                    self.forc, self.geoParam, self.rural)
            self.rural, self.UCM, self.BEM = self.solar.solarcalcs()

            # Update building & traffic schedule
            # Assign day type (1 = weekday, 2 = sat, 3 = sun/other)
            if self.is_near_zero(self.simTime.julian % 7):
                self.dayType = 3  # Sunday
            elif self.is_near_zero(self.simTime.julian % 7 - 6.):
                self.dayType = 2  # Saturday
            else:
                self.dayType = 1  # Weekday

            # Update anthropogenic heat load for each hour (building & UCM)
            self.UCM.sensAnthrop = self.sensAnth * (
                self.SchTraffic[self.dayType - 1][self.simTime.hourDay])

            # Update the energy components for building types defined in initialize.uwg
            for i in xrange(len(self.BEM)):
                # Set temperature
                self.BEM[i].building.coolSetpointDay = self.Sch[i].Cool[
                    self.dayType -
                    1][self.simTime.
                       hourDay] + 273.15  # add from temperature schedule for cooling
                self.BEM[i].building.coolSetpointNight = self.BEM[
                    i].building.coolSetpointDay
                self.BEM[i].building.heatSetpointDay = self.Sch[i].Heat[
                    self.dayType -
                    1][self.simTime.
                       hourDay] + 273.15  # add from temperature schedule for heating
                self.BEM[i].building.heatSetpointNight = self.BEM[
                    i].building.heatSetpointDay

                # Internal Heat Load Schedule (W/m^2 of floor area for Q)
                self.BEM[i].Elec = self.Sch[i].Qelec * self.Sch[i].Elec[
                    self.dayType -
                    1][self.simTime.hourDay]  # Qelec x elec fraction for day
                self.BEM[i].Light = self.Sch[i].Qlight * self.Sch[i].Light[
                    self.dayType -
                    1][self.simTime.hourDay]  # Qlight x light fraction for day
                self.BEM[i].Nocc = self.Sch[i].Nocc * self.Sch[i].Occ[
                    self.dayType -
                    1][self.simTime.
                       hourDay]  # Number of occupants x occ fraction for day
                self.BEM[i].Qocc = self.sensOcc * (1 - self.LatFOcc) * self.BEM[
                    i].Nocc  # Sensible Q occupant * fraction occupant sensible Q * number of occupants

                # SWH and ventilation schedule
                self.BEM[i].SWH = self.Sch[i].Vswh * self.Sch[i].SWH[
                    self.dayType -
                    1][self.simTime.
                       hourDay]  # litres per hour x SWH fraction for day
                self.BEM[i].building.vent = self.Sch[
                    i].Vent  # m^3/s/m^2 of floor
                self.BEM[i].Gas = self.Sch[i].Qgas * self.Sch[i].Gas[
                    self.dayType -
                    1][self.simTime.
                       hourDay]  # Gas Equip Schedule, per m^2 of floor

                # This is quite messy, should update
                # Update internal heat and corresponding fractional loads
                intHeat = self.BEM[i].Light + self.BEM[i].Elec + self.BEM[
                    i].Qocc
                self.BEM[
                    i].building.intHeatDay = intHeat  # W/m2 from light, electricity, occupants
                self.BEM[i].building.intHeatNight = intHeat
                self.BEM[i].building.intHeatFRad = (
                    self.RadFLight * self.BEM[i].Light +
                    self.RadFEquip * self.BEM[i].Elec
                ) / intHeat  # fraction of radiant heat from light and equipment of whole internal heat
                self.BEM[
                    i].building.intHeatFLat = self.LatFOcc * self.sensOcc * self.BEM[
                        i].Nocc / intHeat  # fraction of latent heat (from occupants) of whole internal heat

                # Update envelope temperature layers
                self.BEM[i].T_wallex = self.BEM[i].wall.layerTemp[0]
                self.BEM[i].T_wallin = self.BEM[i].wall.layerTemp[-1]
                self.BEM[i].T_roofex = self.BEM[i].roof.layerTemp[0]
                self.BEM[i].T_roofin = self.BEM[i].roof.layerTemp[-1]

            # Update rural heat fluxes & update vertical diffusion model (VDM)
            self.rural.infra = self.forc.infra - self.rural.emissivity * self.SIGMA * self.rural.layerTemp[
                0]**4.  # Infrared radiation from rural road

            self.rural.SurfFlux(self.forc, self.geoParam, self.simTime,
                                self.forc.hum, self.forc.temp, self.forc.wind,
                                2., 0.)
            self.RSM.VDM(self.forc, self.rural, self.geoParam, self.simTime)

            # Calculate urban heat fluxes, update UCM & UBL
            self.UCM, self.UBL, self.BEM = urbflux(self.UCM, self.UBL,
                                                   self.BEM, self.forc,
                                                   self.geoParam, self.simTime,
                                                   self.RSM)
            self.UCM.UCModel(self.BEM, self.UBL.ublTemp, self.forc,
                             self.geoParam)
            self.UBL.UBLModel(self.UCM, self.RSM, self.rural, self.forc,
                              self.geoParam, self.simTime)
            """
            # Experimental code to run diffusion model in the urban area
            # N.B Commented out in python UWG because computed wind speed in
            # urban VDM: y = =0.84*ln((2-x/20)/0.51) results in negative log
            # for building heights >= 40m.

            Uroad = copy.copy(self.UCM.road)
            Uroad.sens = copy.copy(self.UCM.sensHeat)
            Uforc = copy.copy(self.forc)
            Uforc.wind = copy.copy(self.UCM.canWind)
            Uforc.temp = copy.copy(self.UCM.canTemp)
            self.USM.VDM(Uforc,Uroad,self.geoParam,self.simTime)
            """

            self.logger.info("dbT = {}".format(self.UCM.canTemp - 273.15))
            if n > 0:
                logging.info("dpT = {}".format(self.UCM.Tdp))
                logging.info("RH  = {}".format(self.UCM.canRHum))

            if self.is_near_zero(self.simTime.secDay %
                                 self.simTime.timePrint) and n < self.N:

                self.logger.info("{0} ----sim time step = {1}----\n\n".format(
                    __name__, n))

                self.WeatherData[n] = copy.copy(self.forc)
                _Tdb, _w, self.UCM.canRHum, _h, self.UCM.Tdp, _v = psychrometrics(
                    self.UCM.canTemp, self.UCM.canHum, self.forc.pres)

                self.UBLData[n] = copy.copy(self.UBL)
                self.UCMData[n] = copy.copy(self.UCM)
                self.RSMData[n] = copy.copy(self.RSM)

                self.logger.info("dbT = {}".format(self.UCMData[n].canTemp -
                                                   273.15))
                self.logger.info("dpT = {}".format(self.UCMData[n].Tdp))
                self.logger.info("RH  = {}".format(self.UCMData[n].canRHum))

                n += 1

    def write_epw(self):
        """ Section 8 - Writing new EPW file
        """
        epw_prec = self.epw_precision  # precision of epw file input

        for iJ in xrange(len(self.UCMData)):
            # [iJ+self.simTime.timeInitial-8] = increments along every weather timestep in epw
            # [6 to 21]                       = column data of epw
            self.epwinput[iJ + self.simTime.timeInitial -
                          8][6] = "{0:.{1}f}".format(
                              self.UCMData[iJ].canTemp - 273.15,
                              epw_prec)  # dry bulb temperature  [?C]
            self.epwinput[iJ + self.simTime.timeInitial -
                          8][7] = "{0:.{1}f}".format(
                              self.UCMData[iJ].Tdp,
                              epw_prec)  # dew point temperature [?C]
            self.epwinput[iJ + self.simTime.timeInitial -
                          8][8] = "{0:.{1}f}".format(
                              self.UCMData[iJ].canRHum,
                              epw_prec)  # relative humidity     [%]
            self.epwinput[iJ + self.simTime.timeInitial -
                          8][21] = "{0:.{1}f}".format(
                              float(self.WeatherData[iJ].wind),
                              epw_prec)  # wind speed [m/s]

        # Writing new EPW file
        epw_new_id = open(self.newPathName, "w")

        for i in xrange(8):
            new_epw_line = '{}\r\n'.format(
                reduce(lambda x, y: x + "," + y, self._header[i]))
            epw_new_id.write(new_epw_line)

        for i in xrange(len(self.epwinput)):
            printme = ""
            for ei in xrange(34):
                printme += "{}".format(self.epwinput[i][ei]) + ','
            printme = printme + "{}".format(self.epwinput[i][ei])
            new_epw_line = "{0}\r\n".format(printme)
            epw_new_id.write(new_epw_line)

        epw_new_id.close()

        print "New climate file '{}' is generated at {}.".format(
            self.destinationFileName, self.destinationDir)

    def run(self):

        # run main class methods
        self.read_epw()
        self.set_input()
        self.init_input_obj()
        self.hvac_autosize()
        self.simulate()
        self.write_epw()
def ran_simulation():
    """
    Main ran_simulation
    """


    # define sim_param and inside RB pool: all available Resources list
    sim_param = SimParam()

    no_of_slices = sim_param.no_of_slices
    no_of_users_per_slice = sim_param.no_of_users_per_slice

    # create result directories
    create_dir(sim_param)

    # create logfile and write SimParameters
    results_dir = "results/" + sim_param.timestamp
    log_file = open(results_dir + "/logfile.txt", "wt")
    log_file.write('no_of_slices: %d\nno_of_users_per_slice: %d\n\n' % (no_of_slices, no_of_users_per_slice))
    attrs = vars(sim_param)
    log_file.write('SimParam\n'+''.join("%s: %s\n" % item for item in attrs.items()))
    # log_file.close()

    # initialize SD_RAN_Controller
    SD_RAN_Controller = Controller(sim_param)

    # Each slice has different users
    slices = []
    slice_results = []

    # initialize all slices
    for i in range(no_of_slices):
        slice_param_tmp = SliceParam(sim_param)
        slice_param_tmp.SLICE_ID = i
        slices.append(SliceSimulation(slice_param_tmp))
        slice_results.append([])

        # initialize all users with traffics and distances
        tmp_users = []
        seed_dist = 0  # users in all slices have identical distance distributions
        #rng_dist = RNG(ExponentialRNS(lambda_x=1. / float(sim_param.MEAN_Dist)), s_type='dist') # , the_seed=seed_dist
        rng_dist = RNG(UniformRNS(sim_param.DIST_MIN,sim_param.DIST_MAX, the_seed=seed_dist), s_type='dist')  #
        dist_arr = [10, 100 ]#[30, 30, 100, 100, 100, 100, 100, 100, 100, 100]  # 10*(1+user_id%no_of_users_per_slice)**2
        for j in range(no_of_users_per_slice):
            user_id = i*no_of_users_per_slice + j
            #tmp_users.append(User(user_id, rng_dist.get_dist(), slice_list=[slices[i]], sim_param=sim_param))
            tmp_users.append(User(user_id, dist_arr[j], slice_list=[slices[i]], sim_param=sim_param))

        # insert user to slices
        slices[i].insert_users(tmp_users)


    # Choose Slice Manager Algorithm, 'PF': prop fair, 'MCQI': Max Channel Quality Index, 'RR': round-robin
    slices[0].slice_param.SM_ALGO = 'RR'
    slices[1].slice_param.SM_ALGO = 'MCQI'
    slices[2].slice_param.SM_ALGO = 'PF'

    # log Slice Parameters
    for i in range(no_of_slices):
        attrs = vars(slices[i].slice_param)
        log_file.write('\nSliceParam\n' + ''.join("%s: %s\n" % item for item in attrs.items()))
    #log_file.close()

    # loop rounds for each slice
    for i in range(int(sim_param.T_FINAL/sim_param.T_C)):
        RB_mapping = SD_RAN_Controller.RB_allocate_to_slices(slices[0].sim_state.now, slices)

        for j in range(len(slices)):
            slices[j].prep_next_round(RB_mapping[j,:,:])
            slice_results[j].append(slices[j].simulate_one_round())

    # Store Simulation Results
    # user results
    parent_dir = "results/" + sim_param.timestamp + "/user_results"
    path = parent_dir + "/tp"
    for i in range(len(slice_results)):
        user_count = len(slice_results[i][-1].server_results)   # choose latest result for data
        for k in range(user_count):
            common_name = "/slice%d_user%d_" % (i, slice_results[i][-1].server_results[k].server.user.user_id)
            cc_temp = slice_results[i][-1].server_results[k].server.counter_collection
            # tp
            filename = parent_dir + "/tp" + common_name + "sum_power_two.csv"
            savetxt(filename, cc_temp.cnt_tp.sum_power_two, delimiter=',')
            filename = parent_dir + "/tp" + common_name + "values.csv"
            savetxt(filename, cc_temp.cnt_tp.values, delimiter=',')
            filename = parent_dir + "/tp" + common_name + "timestamps.csv"
            savetxt(filename, cc_temp.cnt_tp.timestamps, delimiter=',')

            filename = parent_dir + "/tp" + common_name + "all_data.csv"
            #savetxt(filename, np.transpose(np.array([cc_temp.cnt_tp.values,cc_temp.cnt_tp.timestamps])), delimiter=',')
            df = DataFrame(np.transpose(np.array([cc_temp.cnt_tp.values,cc_temp.cnt_tp.timestamps])), columns=['Values', 'Timestamps'])
            export_csv = df.to_csv(filename, index=None, header=True)

            # tp2
            filename = parent_dir + "/tp2" + common_name + "sum_power_two.csv"
            savetxt(filename, cc_temp.cnt_tp2.sum_power_two, delimiter=',')
            filename = parent_dir + "/tp2" + common_name + "values.csv"
            savetxt(filename, cc_temp.cnt_tp2.values, delimiter=',')
            filename = parent_dir + "/tp2" + common_name + "timestamps.csv"
            savetxt(filename, cc_temp.cnt_tp2.timestamps, delimiter=',')
            # ql
            filename = parent_dir + "/ql" + common_name + "sum_power_two.csv"
            savetxt(filename, cc_temp.cnt_ql.sum_power_two, delimiter=',')
            filename = parent_dir + "/ql" + common_name + "values.csv"
            savetxt(filename, cc_temp.cnt_ql.values, delimiter=',')
            filename = parent_dir + "/ql" + common_name + "timestamps.csv"
            savetxt(filename, cc_temp.cnt_ql.timestamps, delimiter=',')
            # system time (delay)
            filename = parent_dir + "/delay" + common_name + "values.csv"
            savetxt(filename, cc_temp.cnt_syst.values, delimiter=',')
            filename = parent_dir + "/delay" + common_name + "timestamps.csv"
            savetxt(filename, cc_temp.cnt_syst.timestamps, delimiter=',')
            # Find how to insert histograms


    # plot results
    parent_dir = "results/" + sim_param.timestamp
    plot_results(parent_dir, no_of_slices, no_of_users_per_slice, sim_param, slices)

    # rb dist printing
    filename = "results/" + sim_param.timestamp + "/summary"
    rb_total = 0
    rb_dist = []
    for s in slices:
        rb_dist_slice = []
        for u in s.server_list:
            rb_dist_slice.append(u.RB_counter)
        slicesum = np.nansum(rb_dist_slice)

        print("Slice %d dist: " % s.slice_param.SLICE_ID, *np.round(np.divide(rb_dist_slice,slicesum/100), 1))
        # write these to file savetxt(filename, cc_temp.cnt_ql.sum_power_two, delimiter=',')
        rb_dist.append(slicesum)
    totalsum = np.nansum(rb_dist)
    print("rb dist (RR MCQI PF): ", *np.round(np.divide(rb_dist, totalsum / 100), 1))