示例#1
0
    def reset(self):
        self.steps_taken = 0
        self.net.reset()
        self.current_trace = self.trace_scheduler.get_trace()
        self.current_trace.reset()
        self.run_dur = 0.01
        self.links = [Link(self.current_trace), Link(self.current_trace)]
        self.senders = [
            AuroraSender(
                10 * BYTES_PER_PACKET /
                (self.current_trace.get_delay(0) * 2 / 1000), self.features,
                self.history_len, 0, 0, self.current_trace)
        ]
        self.net = Network(self.senders, self.links, self.record_pkt_log)
        self.episodes_run += 1
        self.senders[0].on_mi_start()
        self.net.run(self.run_dur)
        _, run_dur = self.senders[0].on_mi_finish()
        if run_dur != 0:
            self.run_dur = run_dur

        self.reward_ewma *= 0.99
        self.reward_ewma += 0.01 * self.reward_sum
        self.reward_sum = 0.0
        return self._get_all_sender_obs()
示例#2
0
文件: network.py 项目: zxxia/PCC-RL
 def create_new_links_and_senders(self):
     self.links = [Link(self.current_trace), Link(self.current_trace)]
     self.senders = [Sender(
         10 / (self.current_trace.get_delay(0) *2/1000),
                            [self.links[0], self.links[1]], 0,
                            self.features,
                            history_len=self.history_len,
                            delta_scale=self.delta_scale)]
     # self.run_dur = 3 * lat
     # self.run_dur = 1 * lat
     if not self.senders[0].rtt_samples:
         # self.run_dur = 0.473
         # self.run_dur = 5 / self.senders[0].rate
         self.run_dur = 0.01
示例#3
0
    def __init__(
            self,
            trace_scheduler: Scheduler,
            history_len: int = 10,
            # features="sent latency inflation,latency ratio,send ratio",
            features: List[str] = [
                "sent latency inflation", "latency ratio", "recv ratio"
            ],
            record_pkt_log: bool = False):
        """Network environment used in simulation."""
        self.record_pkt_log = record_pkt_log
        self.trace_scheduler = trace_scheduler
        self.current_trace = self.trace_scheduler.get_trace()

        self.history_len = history_len
        self.features = features

        # construct sender and network
        self.links = [Link(self.current_trace), Link(self.current_trace)]
        self.senders = [
            AuroraSender(
                10 * BYTES_PER_PACKET /
                (self.current_trace.get_delay(0) * 2 / 1000), self.features,
                self.history_len, 0, 0, self.current_trace)
        ]
        self.net = Network(self.senders, self.links, self.record_pkt_log)
        self.run_dur = 0.01
        self.steps_taken = 0

        self.action_space = spaces.Box(np.array([-1e12]),
                                       np.array([1e12]),
                                       dtype=np.float32)

        single_obs_min_vec = sender_obs.get_min_obs_vector(self.features)
        single_obs_max_vec = sender_obs.get_max_obs_vector(self.features)
        self.observation_space = spaces.Box(np.tile(single_obs_min_vec,
                                                    self.history_len),
                                            np.tile(single_obs_max_vec,
                                                    self.history_len),
                                            dtype=np.float32)

        self.reward_sum = 0.0
        self.reward_ewma = 0.0

        self.episodes_run = -1
示例#4
0
文件: bbr_old.py 项目: zxxia/PCC-RL
    def test(self,
             trace: Trace,
             save_dir: str,
             plot_flag: bool = False) -> Tuple[float, float]:
        """Test a network trace and return rewards.

        The 1st return value is the reward in Monitor Interval(MI) level and
        the length of MI is 1 srtt. The 2nd return value is the reward in
        packet level. It is computed by using throughput, average rtt, and
        loss rate in each 500ms bin of the packet log. The 2nd value will be 0
        if record_pkt_log flag is False.

        Args:
            trace: network trace.
            save_dir: where a MI level log will be saved if save_dir is a
                valid path. A packet level log will be saved if record_pkt_log
                flag is True and save_dir is a valid path.
        """

        links = [Link(trace), Link(trace)]
        senders = [BBRSender(0, 0, self.seed)]
        net = Network(senders, links, self.record_pkt_log)

        rewards = []
        start_rtt = trace.get_delay(0) * 2 / 1000
        run_dur = start_rtt
        if save_dir:
            os.makedirs(save_dir, exist_ok=True)
            f_sim_log = open(
                os.path.join(save_dir,
                             '{}_simulation_log.csv'.format(self.cc_name)),
                'w', 1)
            writer = csv.writer(f_sim_log, lineterminator='\n')
            writer.writerow([
                'timestamp', "send_rate", 'recv_rate', 'latency', 'loss',
                'reward', "action", "bytes_sent", "bytes_acked", "bytes_lost",
                "send_start_time", "send_end_time", 'recv_start_time',
                'recv_end_time', 'latency_increase', "packet_size",
                'bandwidth', "queue_delay", 'packet_in_queue', 'queue_size',
                'cwnd', 'ssthresh', "rto", "packets_in_flight"
            ])
        else:
            f_sim_log = None
            writer = None

        while True:
            net.run(run_dur)
            mi = senders[0].get_run_data()

            throughput = mi.get("recv rate")  # bits/sec
            send_rate = mi.get("send rate")  # bits/sec
            latency = mi.get("avg latency")
            avg_queue_delay = mi.get("avg queue delay")
            loss = mi.get("loss ratio")

            reward = pcc_aurora_reward(
                throughput / BITS_PER_BYTE / BYTES_PER_PACKET, latency, loss,
                trace.avg_bw * 1e6 / BITS_PER_BYTE / BYTES_PER_PACKET)
            rewards.append(reward)
            try:
                ssthresh = senders[0].ssthresh
            except:
                ssthresh = 0
            action = 0

            if save_dir and writer:
                writer.writerow([
                    net.get_cur_time(), send_rate, throughput, latency, loss,
                    reward, action, mi.bytes_sent, mi.bytes_acked,
                    mi.bytes_lost, mi.send_start, mi.send_end, mi.recv_start,
                    mi.recv_end,
                    mi.get('latency increase'), mi.packet_size,
                    links[0].get_bandwidth(net.get_cur_time()) *
                    BYTES_PER_PACKET * BITS_PER_BYTE, avg_queue_delay,
                    links[0].pkt_in_queue, links[0].queue_size,
                    senders[0].cwnd, ssthresh, senders[0].rto,
                    senders[0].bytes_in_flight / BYTES_PER_PACKET
                ])
            if senders[0].srtt:
                run_dur = senders[0].srtt
            should_stop = trace.is_finished(net.get_cur_time())
            if should_stop:
                break
        if f_sim_log:
            f_sim_log.close()
        avg_sending_rate = senders[0].avg_sending_rate
        tput = senders[0].avg_throughput
        avg_lat = senders[0].avg_latency
        loss = senders[0].pkt_loss_rate
        pkt_level_reward = pcc_aurora_reward(tput,
                                             avg_lat,
                                             loss,
                                             avg_bw=trace.avg_bw * 1e6 /
                                             BITS_PER_BYTE / BYTES_PER_PACKET)
        pkt_level_original_reward = pcc_aurora_reward(tput, avg_lat, loss)
        if save_dir:
            with open(
                    os.path.join(save_dir,
                                 "{}_summary.csv".format(self.cc_name)),
                    'w') as f:
                summary_writer = csv.writer(f, lineterminator='\n')
                summary_writer.writerow([
                    'trace_average_bandwidth', 'trace_average_latency',
                    'average_sending_rate', 'average_throughput',
                    'average_latency', 'loss_rate', 'mi_level_reward',
                    'pkt_level_reward'
                ])
                summary_writer.writerow([
                    trace.avg_bw, trace.avg_delay,
                    avg_sending_rate * BYTES_PER_PACKET * BITS_PER_BYTE / 1e6,
                    tput * BYTES_PER_PACKET * BITS_PER_BYTE / 1e6, avg_lat,
                    loss,
                    np.mean(rewards), pkt_level_reward
                ])

        if self.record_pkt_log and save_dir:
            with open(
                    os.path.join(save_dir,
                                 "{}_packet_log.csv".format(self.cc_name)),
                    'w', 1) as f:
                pkt_logger = csv.writer(f, lineterminator='\n')
                pkt_logger.writerow([
                    'timestamp', 'packet_event_id', 'event_type', 'bytes',
                    'cur_latency', 'queue_delay', 'packet_in_queue',
                    'sending_rate', 'bandwidth'
                ])
                pkt_logger.writerows(net.pkt_log)
        # with open(os.path.join(save_dir, "{}_log.csv".format(self.cc_name)), 'w', 1) as f:
        #     writer = csv.writer(f, lineterminator='\n')
        #     writer.writerow(
        #         ['timestamp', 'pacing_gain', "pacing_rate", 'cwnd_gain',
        #          'cwnd', 'target_cwnd', 'prior_cwnd', "btlbw", "rtprop",
        #          "full_bw", 'state', "packets_in_flight",
        #          "in_fast_recovery_mode", 'rs_delivery_rate', 'round_start',
        #          'round_count', 'rto', 'exit_fast_recovery_ts',
        #          'pkt_in_queue'])
        #     writer.writerows(senders[0].bbr_log)
        if plot_flag and save_dir:
            plot_mi_level_time_series(
                trace,
                os.path.join(save_dir,
                             '{}_simulation_log.csv'.format(self.cc_name)),
                save_dir, self.cc_name)
            plot(trace, *senders[0].bin_tput, *senders[0].bin_sending_rate,
                 tput * BYTES_PER_PACKET * BITS_PER_BYTE / 1e6,
                 avg_sending_rate * BYTES_PER_PACKET * BITS_PER_BYTE / 1e6,
                 *senders[0].latencies, avg_lat * 1000, loss,
                 pkt_level_original_reward, pkt_level_reward, save_dir,
                 self.cc_name)
        return np.mean(rewards), pkt_level_reward