Beispiel #1
0
    def compute_edge_admitted_traffic(self, traffic_to_propagate, edge):

        pred_admitted_traffic = Traffic()

        for ed in edge.edge_data_list:

            # At succ edges, set the in_port of the admitted match for destination to wildcard
            if edge.edge_type == "outside":
                traffic_to_propagate.set_field("in_port", is_wildcard=True)
                traffic_to_propagate.clear_switch_modifications()

            # If there were modifications along the way...
            if ed.applied_modifications:
                ttp = traffic_to_propagate.get_orig_traffic(
                    ed.applied_modifications)
            else:
                ttp = traffic_to_propagate

            i = ed.edge_filter_traffic.intersect(ttp)
            i.set_enabling_edge_data(ed)

            if not i.is_empty():
                pred_admitted_traffic.union(i)

        return pred_admitted_traffic
Beispiel #2
0
    def init_port_graph_state(self):

        # Need port_number parsed in before this is called
        self.switch_port_graph_ingress_node = PortGraphNode(self.sw,
                                                            PortGraph.get_ingress_node_id(self.sw.node_id,
                                                                                          self.port_number),
                                                            "ingress")

        self.switch_port_graph_egress_node = PortGraphNode(self.sw,
                                                           PortGraph.get_egress_node_id(self.sw.node_id,
                                                                                        self.port_number),
                                                           "egress")

        self.network_port_graph_ingress_node = PortGraphNode(self.sw,
                                                             PortGraph.get_ingress_node_id(self.sw.node_id,
                                                                                           self.port_number),
                                                             "ingress")

        self.network_port_graph_egress_node = PortGraphNode(self.sw,
                                                            PortGraph.get_egress_node_id(self.sw.node_id,
                                                                                         self.port_number),
                                                            "egress")

        self.switch_port_graph_ingress_node.parent_obj = self
        self.switch_port_graph_egress_node.parent_obj = self
        self.network_port_graph_ingress_node.parent_obj = self
        self.network_port_graph_egress_node.parent_obj = self

        self.ingress_node_traffic = Traffic(init_wildcard=True)
        self.ingress_node_traffic.set_field("in_port", int(self.port_number))
Beispiel #3
0
    def __init__(self):
        # Internet topology
        self.generator = Generator(20, 5)
        self.generator.build_topology()
        self.generator.build_matrix()
        # self.generator_v0 = copy.deepcopy(self.generator)

        self.RLs = self.generator.Ts + self.generator.Cs + self.generator.Ms + self.generator.CPs
        # self.RLs_v0 = self.generator_v0.Ts + self.generator_v0.Cs + self.generator_v0.Ms + self.generator_v0.CPs

        self.N = 25
        self.Ns = self.generator.Ts + self.generator.Ms + self.generator.CPs + self.generator.Cs
        # self.Ns_v0 = self.generator_v0.Ts + self.generator_v0.Cs + self.generator_v0.Ms + self.generator_v0.CPs

        self.MAX = 100000
        self.ids = self.generator.ids
        # self.ids_v0 = self.generator_v0.ids

        # for each agent, add [s,a,r,s'] as element. size
        self.experience_pool = collections.defaultdict(list)
        # self.experience_pool_v0 = collections.defaultdict(list)
        self.pool_size = 10
        self.global_optimal = 0
        self.int_optimal = 0

        # TODO, define TF, Matrix, Linprog
        self.traffic = Traffic(0.5)
        self.TF = self.traffic.inject_traffic()
Beispiel #4
0
    def init_port_graph_state(self):

        self.traffic_element = TrafficElement(init_match=self.match)
        self.traffic = Traffic()
        self.complement_traffic = Traffic()
        self.applied_traffic = None

        self.traffic.add_traffic_elements([self.traffic_element])
        self.complement_traffic.add_traffic_elements(self.traffic_element.get_complement_traffic_elements())
Beispiel #5
0
    def get_admitted_traffic(self, node, dst):

        dst_admitted_traffic = Traffic()

        if dst in node.admitted_traffic:
            for succ in node.admitted_traffic[dst]:
                dst_admitted_traffic.union(node.admitted_traffic[dst][succ])

        return dst_admitted_traffic
Beispiel #6
0
 def __init__(self, init_n_ego_dict, logdir):
     self.init_n_ego_dict = init_n_ego_dict
     self.multiego = MultiEgo(copy.deepcopy(self.init_n_ego_dict))
     self.traffic = Traffic(100, 'display', self.init_n_ego_dict)
     self.n_ego_traj_trans = {}
     plt.ion()
     self.fig = plt.figure(figsize=(8, 8))
     self.hist_posi = {egoID: [] for egoID in self.init_n_ego_dict.keys()}
     self.episode_counter = -1
     self.step_counter = -1
     self.logdir = logdir
     self.reset()
Beispiel #7
0
    def __init__(
            self,
            training_task,  # 'left', 'straight', 'right'
            num_future_data=0,
            display=False,
            **kwargs):
        metadata = {'render.modes': ['human']}
        self.dynamics = VehicleDynamics()
        self.interested_vehs = None
        self.training_task = training_task
        self.ref_path = ReferencePath(self.training_task, **kwargs)
        self.detected_vehicles = None
        self.all_vehicles = None
        self.ego_dynamics = None
        self.num_future_data = num_future_data
        self.env_model = EnvironmentModel(training_task, num_future_data)
        self.init_state = {}
        self.action_number = 2
        self.exp_v = EXPECTED_V  #TODO: temp
        self.ego_l, self.ego_w = L, W
        self.action_space = gym.spaces.Box(low=-1,
                                           high=1,
                                           shape=(self.action_number, ),
                                           dtype=np.float32)

        self.seed()
        self.v_light = None
        self.step_length = 100  # ms

        self.step_time = self.step_length / 1000.0
        self.init_state = self._reset_init_state()
        self.obs = None
        self.action = None
        self.veh_mode_dict = VEHICLE_MODE_DICT[self.training_task]
        self.veh_num = VEH_NUM[self.training_task]
        self.virtual_red_light_vehicle = False

        self.done_type = 'not_done_yet'
        self.reward_info = None
        self.ego_info_dim = None
        self.per_tracking_info_dim = None
        self.per_veh_info_dim = None
        if not display:
            self.traffic = Traffic(self.step_length,
                                   mode='training',
                                   init_n_ego_dict=self.init_state,
                                   training_task=self.training_task)
            self.reset()
            action = self.action_space.sample()
            observation, _reward, done, _info = self.step(action)
            self._set_observation_space(observation)
            plt.ion()
Beispiel #8
0
    def get_edge_from_admitted_traffic(self,
                                       pred,
                                       succ,
                                       admitted_traffic,
                                       edge_sw=None,
                                       exclude_inactive=False):

        edge = PortGraphEdge(pred, succ)

        # If the edge filter became empty, reflect that.
        if admitted_traffic.is_empty():
            pass
        else:
            # Each traffic element has its own edge_data, because of how it might have
            # traveled through the switch and what modifications it may have accumulated
            for i, te in enumerate(admitted_traffic.traffic_elements):

                t = Traffic()
                t.add_traffic_elements([te])
                traffic_paths = None

                if edge_sw:

                    # Check to see the exact path of this traffic through the switch
                    traffic_paths = edge_sw.port_graph.get_paths(
                        pred, succ, t, [pred], [], [])

                    if len(traffic_paths) == 0:
                        print "Found traffic but no paths to back it up."
                        #raise Exception("Found traffic but no paths to back it up.")
                    else:
                        # IF asked to exclude in-active...
                        # As long as there a single active path and carries the te, then we are good,
                        # otherwise, continue
                        if exclude_inactive:
                            active_path = False
                            for p in traffic_paths:
                                if p.get_max_active_rank() == 0:
                                    active_path = True
                                    break

                            if not active_path:
                                continue

                edge_data = NetworkPortGraphEdgeData(t,
                                                     te.switch_modifications,
                                                     traffic_paths)

                edge.add_edge_data(edge_data)

        return edge
    def init_switch_port_graph(self):

        print "Initializing Port Graph for switch:", self.sw.node_id

        # Initialize switch ports' port graph state
        for port_num in self.sw.ports:

            if port_num == 1 and self.sw.node_id == "s1":
                pass

            self.sw.ports[port_num].init_port_graph_state()

        # Initialize port graph state per table and add its node to switch port graph
        for flow_table in self.sw.flow_tables:
            flow_table.init_port_graph_state()
            self.add_node(flow_table.port_graph_node)

        # Add two nodes per physical port in port graph one for incoming and outgoing direction
        # Connect incoming direction port to table 0's port
        for port_num in self.sw.ports:

            port = self.sw.ports[port_num]

            self.add_node(port.switch_port_graph_ingress_node)
            self.add_node(port.switch_port_graph_egress_node)

            self.boundary_ingress_nodes.append(
                port.switch_port_graph_ingress_node)
            self.boundary_egress_nodes.append(
                port.switch_port_graph_egress_node)

            edge = PortGraphEdge(port.switch_port_graph_ingress_node,
                                 self.sw.flow_tables[0].port_graph_node)
            edge_traffic_filter = Traffic()
            edge_traffic_filter.union(port.ingress_node_traffic)
            edge_data = SwitchPortGraphEdgeData(edge_traffic_filter, None,
                                                None, None)
            edge.add_edge_data(edge_data)
            self.add_edge(port.switch_port_graph_ingress_node,
                          self.sw.flow_tables[0].port_graph_node, edge)

        # Try passing a wildcard through the flow table
        for flow_table in self.sw.flow_tables:
            flow_table.compute_flow_table_port_graph_edges()
            self.add_flow_table_edges(flow_table)

        # Initialize all groups' active buckets
        for group_id in self.sw.group_table.groups:
            self.sw.group_table.groups[group_id].set_active_bucket()
Beispiel #10
0
	def __init__(self, startTime, duration, bandwidth, priority=TrafficPriority.NORMAL):
		Traffic.__init__(self, startTime, duration, priority)
		
		#private members
		self.__bandwidth = bandwidth #in Megabits
		self.__downtime = 0.0
		
		#public members (these will be set when the flow is allocated in the topology)
		self.sourceID = None
		self.destinationID = None
		self.paths = []
		self.primaryPath = None
		self.inUsePath = None
		self.localPathComponentStatus = dict()
		self.downFromTime = None #this variable should be none when the flow is getting service
Beispiel #11
0
    def get_admitted_traffic_via_succ(self, node, dst, succ):
        try:
            admitted_traffic_via_succ = node.admitted_traffic[dst][succ]
        except KeyError:
            admitted_traffic_via_succ = Traffic(init_wildcard=False)

        return admitted_traffic_via_succ
Beispiel #12
0
 def getThere(self):
     if self.name1.get() != userId or self.pwd1.get() != password:
         tkinter.messagebox.showinfo(
             "Window Title",
             "Wrong Username or Password Entered.Can't Proceed Further")
     else:
         there = Traffic()
Beispiel #13
0
    def __init__(self):
        self.BUTTON = Button(
            pin=11,
        )
        GPIO.setup(
            self.WAITING_PIN,
            GPIO.OUT,
        )

        self.LCD = LCD(
            pin_rs=4,
            pin_e=25,
            pins_d=[24,23,22,27],
        )

        self.TRAFFIC = Traffic(
            pin_red=14,
            pin_amber=3,
            pin_green=2,
            lcd=self.LCD,
            lcd_line=2,
        )

        self.RGB = RGB(
            pin_red=9,
            pin_green=10,
            pin_blue=15,
            lcd=self.LCD,
            lcd_line=3,
        )
        self.RGB.show_colour(self.RGB_MAX, self.RGB_MIN, self.RGB_MIN)
Beispiel #14
0
def create_traffic(traffic, screen, ai_settings, road):
    """Create vehicles for traffic."""
    for i in range(4):
        new_car = Traffic(screen, ai_settings, road)
        traffic.add(new_car)
    shuffle(list(traffic))
    for num, car in enumerate(traffic):
        car.rect.x = road.rect.left + randint(
            35, 40) + (car.rect.width + randint(45, 55)) * num
    set_y_pos(traffic, road)
Beispiel #15
0
 def add_traffic_out(m, traffic):
     # This traffic is coming out *from* this machine (to traffic['dst'])
     assert (m['ip'] == traffic['src'])
     m['count_out'] += traffic['size']
     if traffic['dst'] in m['traffic_out']:
         t = m['traffic_out'][traffic['dst']]
         tt = Traffic.merge(t, traffic)
         m['traffic_out'][traffic['dst']] = tt
     else:
         m['traffic_out'][traffic['dst']] = traffic
    def init_switch_admitted_traffic(self):

        print "Computing Transfer Function for switch:", self.sw.node_id

        # Inject wildcard traffic at each ingress port of the switch's non-host ports
        for port in self.sw.non_host_port_iter():

            egress_node = self.get_egress_node(self.sw.node_id,
                                               port.port_number)
            dst_traffic_at_succ = Traffic(init_wildcard=True)
            end_to_end_modified_edges = []
            self.propagate_admitted_traffic(egress_node, dst_traffic_at_succ,
                                            None, egress_node,
                                            end_to_end_modified_edges)

        # Inject wildcard traffic at each ingress port of the switch
        for port in self.sw.host_port_iter():

            egress_node = self.get_egress_node(self.sw.node_id,
                                               port.port_number)

            dst_traffic_at_succ = Traffic(init_wildcard=True)
            dst_traffic_at_succ.set_field(
                "ethernet_destination",
                int(port.attached_host.mac_addr.replace(":", ""), 16))
            end_to_end_modified_edges = []
            self.propagate_admitted_traffic(egress_node, dst_traffic_at_succ,
                                            None, egress_node,
                                            end_to_end_modified_edges)
    def update_admitted_traffic_due_to_port_state_change(
            self, port_num, event_type):

        end_to_end_modified_edges = []

        egress_node = self.get_egress_node(self.sw.node_id, port_num)

        # This will keep track of all the edges due to flow tables that were modified due to port event
        # This assumes that ports are always successors on these edges.
        all_modified_flow_table_edges = []

        # First the egress node causes bunch of changes
        for pred in self.predecessors_iter(egress_node):

            edge = self.get_edge(pred, egress_node)
            flow_table = pred.parent_obj

            # First get the modified edges in this flow_table (edges added/deleted/modified)
            modified_flow_table_edges = flow_table.update_port_graph_edges()

            self.modify_flow_table_edges(flow_table, modified_flow_table_edges)

            self.update_admitted_traffic(modified_flow_table_edges,
                                         end_to_end_modified_edges)

            all_modified_flow_table_edges.extend(modified_flow_table_edges)

        # Take care of the ingress node which holds the traffic that can get from here to others nodes
        ingress_node = self.get_ingress_node(self.sw.node_id, port_num)
        ingress_node_succ = self.sw.flow_tables[0].port_graph_node
        ingress_node_succ_edge = self.get_edge(ingress_node, ingress_node_succ)

        for other_port in self.sw.ports:

            dst = self.get_egress_node(self.sw.node_id, other_port)

            dst_traffic_at_succ = Traffic()
            if event_type == "port_down":
                pass
            elif event_type == "port_up":
                dst_traffic_at_succ = self.get_admitted_traffic(
                    ingress_node_succ, dst)
            else:
                raise Exception("Unknown event")

            ingress_node_traffic = self.compute_edge_admitted_traffic(
                dst_traffic_at_succ, ingress_node_succ_edge)
            self.propagate_admitted_traffic(ingress_node, ingress_node_traffic,
                                            ingress_node_succ, dst,
                                            end_to_end_modified_edges)

        return end_to_end_modified_edges
    def compute_edge_admitted_traffic(self, traffic_to_propagate, edge):

        pred_admitted_traffic = Traffic()

        for ed in edge.edge_data_list:

            if edge.edge_type == "egress":

                # if the output_action type is applied, no written modifications take effect.
                if ed.edge_action.instruction_type == "applied":
                    traffic_to_propagate.set_written_modifications_apply(False)
                else:
                    traffic_to_propagate.set_written_modifications_apply(True)

            # Always roll-back applied modifications if the edge has any...
            if ed.applied_modifications:
                ttp = traffic_to_propagate.get_orig_traffic(
                    ed.applied_modifications)
            else:
                ttp = traffic_to_propagate

            # At ingress edges, first roll-back any accumulated written modifications
            if edge.edge_type == "ingress":
                ttp = ttp.get_orig_traffic(
                    use_embedded_written_modifications=True)

            i = ed.edge_filter_traffic.intersect(ttp)
            i.set_enabling_edge_data(ed)

            if not i.is_empty():
                # At all the non-ingress edges accumulate written modifications
                if edge.edge_type != "ingress" and ed.written_modifications:
                    i.set_written_modifications(ed.written_modifications)

                pred_admitted_traffic.union(i)

        return pred_admitted_traffic
Beispiel #19
0
def main() -> None:
    """
    Main method prompting for iterations and road length for plotting the steady state
    average speed graph, and also prompting for car density to show animation of the
    particular traffic model.
    """
    road_length = float(input('Provide the length of the road: '))
    car_density = float(input('Provide the density of the cars: '))
    iterations = float(input('Provide the number of iterations: '))

    Traffic.density_graph(road_length, iterations)

    model = Traffic(road_length, car_density, iterations)
    model.visualize_traffic()
Beispiel #20
0
    def _get_port_graph_edges_dict(self):
        port_graph_edges = defaultdict(list)

        remaining_traffic = Traffic(init_wildcard=True)

        for flow in self.flows:

            intersection = flow.traffic.intersect(remaining_traffic)

            if not intersection.is_empty():

                # See what is left after this rule is through
                prev_remaining_traffic = remaining_traffic
                remaining_traffic = flow.complement_traffic.intersect(remaining_traffic)
                flow.applied_traffic = intersection

                flow.get_port_graph_edges(port_graph_edges)

            else:
                # Say that this flow does not matter
                flow.applied_traffic = None

        return port_graph_edges
Beispiel #21
0
    def init_network_admitted_traffic_for_sw(self, sw):
        for non_host_port in sw.non_host_port_iter():

            # Accumulate traffic that is admitted for each host
            admitted_host_traffic = Traffic()
            for host_port in sw.host_port_iter():
                at = sw.port_graph.get_admitted_traffic(
                    non_host_port.switch_port_graph_ingress_node,
                    host_port.switch_port_graph_egress_node)
                admitted_host_traffic.union(at)

            end_to_end_modified_edges = []
            self.propagate_admitted_traffic(
                non_host_port.network_port_graph_ingress_node,
                admitted_host_traffic, None,
                non_host_port.network_port_graph_ingress_node,
                end_to_end_modified_edges)

            admitted_host_traffic.set_field("in_port",
                                            int(non_host_port.port_number))
Beispiel #22
0
class Simulation(object):
    def __init__(self, init_n_ego_dict, logdir):
        self.init_n_ego_dict = init_n_ego_dict
        self.multiego = MultiEgo(copy.deepcopy(self.init_n_ego_dict))
        self.traffic = Traffic(100, 'display', self.init_n_ego_dict)
        self.n_ego_traj_trans = {}
        plt.ion()
        self.fig = plt.figure(figsize=(8, 8))
        self.hist_posi = {egoID: [] for egoID in self.init_n_ego_dict.keys()}
        self.episode_counter = -1
        self.step_counter = -1
        self.logdir = logdir
        self.reset()

    def reset(self):
        self.multiego.reset(copy.deepcopy(self.init_n_ego_dict))
        self.traffic.init_traffic(copy.deepcopy(self.init_n_ego_dict))
        self.traffic.sim_step()
        n_ego_traj = {
            egoID: self.multiego.mpp.generate_path(NAME2TASK[egoID[:2]])
            for egoID in self.multiego.n_ego_dynamics.keys()
        }
        self.n_ego_traj_trans = {}
        for egoID, ego_traj in n_ego_traj.items():
            traj_list = []
            for item in ego_traj:
                temp = np.array([rotate_coordination(x, y, 0, -ROTATE_ANGLE[egoID[0]])[0] for x, y in
                                 zip(item.path[0], item.path[1])]), \
                       np.array([rotate_coordination(x, y, 0, -ROTATE_ANGLE[egoID[0]])[1] for x, y in
                                 zip(item.path[0], item.path[1])])
                traj_list.append(temp)
            self.n_ego_traj_trans[egoID] = traj_list
        self.hist_posi = {egoID: [] for egoID in self.init_n_ego_dict.keys()}
        if self.logdir is not None:
            self.episode_counter += 1
            self.step_counter = -1
            os.makedirs(self.logdir +
                        '/episode{}'.format(self.episode_counter))

    def step(self):
        self.step_counter += 1
        current_n_ego_vehicles = self.traffic.n_ego_vehicles
        current_v_light = self.traffic.v_light
        current_n_ego_collision_flag = self.traffic.n_ego_collision_flag
        start_time = time.time()
        next_n_ego_dynamics = self.multiego.get_next_n_ego_dynamics(
            current_n_ego_vehicles, current_v_light)
        end_time = time.time()
        # print('Time for all vehicles:', end_time - start_time)
        n_ego_done = self.multiego.judge_n_ego_done(
            current_n_ego_collision_flag)
        for egoID, flag_list in n_ego_done.items():
            if flag_list[0]:
                print('Ego {} collision!'.format(egoID))
                return 1
            elif flag_list[1]:
                print('Ego {} achieve goal!'.format(egoID))
                self.multiego.n_ego_dynamics.pop(egoID)
                self.traffic.n_ego_dict.pop(egoID)
                next_n_ego_dynamics.pop(egoID)

                self.multiego.n_ego_select_index.pop(egoID)
                self.n_ego_traj_trans.pop(egoID)
                if len(self.traffic.n_ego_dict) == 0:
                    print('All ego achieve goal!'.format(egoID))

                    return 1
        self.traffic.set_own_car(next_n_ego_dynamics)
        self.traffic.sim_step()
        return 0

    def render(self, ):
        # plot basic map
        square_length = CROSSROAD_SIZE
        extension = 40
        lane_width = LANE_WIDTH
        light_line_width = 3
        dotted_line_style = '--'
        solid_line_style = '-'

        plt.cla()
        ax = plt.axes([-0.05, -0.05, 1.1, 1.1])
        for ax in self.fig.get_axes():
            ax.axis('off')
        ax.axis("equal")

        # ----------arrow--------------
        # plt.arrow(lane_width/2, -square_length / 2-10, 0, 5, color='b', head_width=1)
        # plt.arrow(lane_width/2, -square_length / 2-10+2.5, -0.5, 0, color='b', head_width=1)
        # plt.arrow(lane_width*1.5, -square_length / 2-10, 0, 5, color='b', head_width=1)
        # plt.arrow(lane_width*2.5, -square_length / 2 - 10, 0, 5, color='b')
        # plt.arrow(lane_width*2.5, -square_length / 2 - 10+5, 0.5, 0, color='b', head_width=1)

        # ----------horizon--------------

        plt.plot([-square_length / 2 - extension, -square_length / 2],
                 [0.3, 0.3],
                 color='orange')
        plt.plot([-square_length / 2 - extension, -square_length / 2],
                 [-0.3, -0.3],
                 color='orange')
        plt.plot([square_length / 2 + extension, square_length / 2],
                 [0.3, 0.3],
                 color='orange')
        plt.plot([square_length / 2 + extension, square_length / 2],
                 [-0.3, -0.3],
                 color='orange')

        #
        for i in range(1, LANE_NUMBER + 1):
            linestyle = dotted_line_style if i < LANE_NUMBER else solid_line_style
            linewidth = 1 if i < LANE_NUMBER else 2
            plt.plot([-square_length / 2 - extension, -square_length / 2],
                     [i * lane_width, i * lane_width],
                     linestyle=linestyle,
                     color='black',
                     linewidth=linewidth)
            plt.plot([square_length / 2 + extension, square_length / 2],
                     [i * lane_width, i * lane_width],
                     linestyle=linestyle,
                     color='black',
                     linewidth=linewidth)
            plt.plot([-square_length / 2 - extension, -square_length / 2],
                     [-i * lane_width, -i * lane_width],
                     linestyle=linestyle,
                     color='black',
                     linewidth=linewidth)
            plt.plot([square_length / 2 + extension, square_length / 2],
                     [-i * lane_width, -i * lane_width],
                     linestyle=linestyle,
                     color='black',
                     linewidth=linewidth)

        # ----------vertical----------------
        plt.plot([0.3, 0.3],
                 [-square_length / 2 - extension, -square_length / 2],
                 color='orange')
        plt.plot([-0.3, -0.3],
                 [-square_length / 2 - extension, -square_length / 2],
                 color='orange')
        plt.plot([0.3, 0.3],
                 [square_length / 2 + extension, square_length / 2],
                 color='orange')
        plt.plot([-0.3, -0.3],
                 [square_length / 2 + extension, square_length / 2],
                 color='orange')

        #
        for i in range(1, LANE_NUMBER + 1):
            linestyle = dotted_line_style if i < LANE_NUMBER else solid_line_style
            linewidth = 1 if i < LANE_NUMBER else 2
            plt.plot([i * lane_width, i * lane_width],
                     [-square_length / 2 - extension, -square_length / 2],
                     linestyle=linestyle,
                     color='black',
                     linewidth=linewidth)
            plt.plot([i * lane_width, i * lane_width],
                     [square_length / 2 + extension, square_length / 2],
                     linestyle=linestyle,
                     color='black',
                     linewidth=linewidth)
            plt.plot([-i * lane_width, -i * lane_width],
                     [-square_length / 2 - extension, -square_length / 2],
                     linestyle=linestyle,
                     color='black',
                     linewidth=linewidth)
            plt.plot([-i * lane_width, -i * lane_width],
                     [square_length / 2 + extension, square_length / 2],
                     linestyle=linestyle,
                     color='black',
                     linewidth=linewidth)

        v_light = self.traffic.v_light
        if v_light == 0:
            v_color, h_color = 'green', 'red'
        elif v_light == 1:
            v_color, h_color = 'orange', 'red'
        elif v_light == 2:
            v_color, h_color = 'red', 'green'
        else:
            v_color, h_color = 'red', 'orange'

        plt.plot([0, (LANE_NUMBER - 1) * lane_width],
                 [-square_length / 2, -square_length / 2],
                 color=v_color,
                 linewidth=light_line_width)
        plt.plot([(LANE_NUMBER - 1) * lane_width, LANE_NUMBER * lane_width],
                 [-square_length / 2, -square_length / 2],
                 color='green',
                 linewidth=light_line_width)

        plt.plot([-LANE_NUMBER * lane_width, -(LANE_NUMBER - 1) * lane_width],
                 [square_length / 2, square_length / 2],
                 color='green',
                 linewidth=light_line_width)
        plt.plot([-(LANE_NUMBER - 1) * lane_width, 0],
                 [square_length / 2, square_length / 2],
                 color=v_color,
                 linewidth=light_line_width)

        plt.plot([-square_length / 2, -square_length / 2],
                 [0, -(LANE_NUMBER - 1) * lane_width],
                 color=h_color,
                 linewidth=light_line_width)
        plt.plot([-square_length / 2, -square_length / 2],
                 [-(LANE_NUMBER - 1) * lane_width, -LANE_NUMBER * lane_width],
                 color='green',
                 linewidth=light_line_width)

        plt.plot([square_length / 2, square_length / 2],
                 [(LANE_NUMBER - 1) * lane_width, 0],
                 color=h_color,
                 linewidth=light_line_width)
        plt.plot([square_length / 2, square_length / 2],
                 [LANE_NUMBER * lane_width, (LANE_NUMBER - 1) * lane_width],
                 color='green',
                 linewidth=light_line_width)

        # ----------Oblique--------------
        plt.plot([LANE_NUMBER * lane_width, square_length / 2],
                 [-square_length / 2, -LANE_NUMBER * lane_width],
                 color='black',
                 linewidth=2)
        plt.plot([LANE_NUMBER * lane_width, square_length / 2],
                 [square_length / 2, LANE_NUMBER * lane_width],
                 color='black',
                 linewidth=2)
        plt.plot([-LANE_NUMBER * lane_width, -square_length / 2],
                 [-square_length / 2, -LANE_NUMBER * lane_width],
                 color='black',
                 linewidth=2)
        plt.plot([-LANE_NUMBER * lane_width, -square_length / 2],
                 [square_length / 2, LANE_NUMBER * lane_width],
                 color='black',
                 linewidth=2)

        def is_in_plot_area(x, y, tolerance=5):
            if -square_length / 2 - extension + tolerance < x < square_length / 2 + extension - tolerance and \
                    -square_length / 2 - extension + tolerance < y < square_length / 2 + extension - tolerance:
                return True
            else:
                return False

        def draw_rotate_rec(x, y, a, l, w, c):
            bottom_left_x, bottom_left_y, _ = rotate_coordination(
                -l / 2, w / 2, 0, -a)
            ax.add_patch(
                plt.Rectangle((x + bottom_left_x, y + bottom_left_y),
                              w,
                              l,
                              edgecolor=c,
                              facecolor='white',
                              angle=-(90 - a),
                              zorder=50))

        def plot_phi_line(x, y, phi, color):
            line_length = 3
            x_forw, y_forw = x + line_length * cos(phi * pi / 180.), \
                             y + line_length * sin(phi * pi / 180.)
            plt.plot([x, x_forw], [y, y_forw], color=color, linewidth=0.5)

        # plot other cars
        n_ego_vehicles = {
            egoID: self.multiego.n_ego_instance[egoID].all_vehicles
            for egoID in self.multiego.n_ego_dynamics.keys()
        }
        n_ego_dynamics = {
            egoID: self.multiego.n_ego_instance[egoID].ego_dynamics
            for egoID in self.multiego.n_ego_dynamics.keys()
        }
        n_ego_traj = {
            egoID: self.multiego.mpp.generate_path(NAME2TASK[egoID[:2]])
            for egoID in self.multiego.n_ego_dynamics.keys()
        }

        some_egoID = list(n_ego_vehicles.keys())[0]
        all_vehicles = cal_info_in_transform_coordination(
            n_ego_vehicles[some_egoID], 0, 0, -ROTATE_ANGLE[some_egoID[0]])
        n_ego_dynamics_trans = {}
        for egoID, ego_dynamics in n_ego_dynamics.items():
            n_ego_dynamics_trans[
                egoID] = cal_ego_info_in_transform_coordination(
                    ego_dynamics, 0, 0, -ROTATE_ANGLE[egoID[0]])

        for veh in all_vehicles:
            x = veh['x']
            y = veh['y']
            a = veh['phi']
            l = veh['l']
            w = veh['w']
            if is_in_plot_area(x, y):
                draw_rotate_rec(x, y, a, l, w, 'black')
                plot_phi_line(x, y, a, 'black')

        # plot own car
        for egoID, ego_info in n_ego_dynamics_trans.items():
            ego_x = ego_info['x']
            ego_y = ego_info['y']
            ego_a = ego_info['phi']
            ego_l = ego_info['l']
            ego_w = ego_info['w']
            self.hist_posi[egoID].append((ego_x, ego_y))
            draw_rotate_rec(ego_x, ego_y, ego_a, ego_l, ego_w, 'fuchsia')
            plot_phi_line(ego_x, ego_y, ego_a, 'fuchsia')

        # plot history
        for egoID in self.init_n_ego_dict.keys():
            for hist_x, hist_y in self.hist_posi[egoID]:
                plt.scatter(hist_x, hist_y, color='fuchsia', alpha=0.1)

        # plot trajectory
        color = ['blue', 'coral', 'darkcyan']
        for egoID, planed_traj in self.n_ego_traj_trans.items():
            for i, path in enumerate(planed_traj):
                alpha = 1
                if v_color != 'green':
                    if egoID[:2] in ['DL', 'DU', 'UD', 'UR']:
                        alpha = 0.2
                if h_color != 'green':
                    if egoID[:2] in ['RD', 'RL', 'LR', 'LU']:
                        alpha = 0.2
                if planed_traj is not None:
                    if i == self.multiego.n_ego_select_index[egoID]:
                        ax.plot(path[0], path[1], color=color[i], alpha=alpha)
                    else:
                        ax.plot(path[0], path[1], color=color[i], alpha=0.2)
        # plt.show()
        plt.pause(0.001)
        if self.logdir is not None:
            plt.savefig(self.logdir +
                        '/episode{}'.format(self.episode_counter) +
                        '/step{}.pdf'.format(self.step_counter))
Beispiel #23
0
    def update_admitted_traffic(self, modified_edges,
                                end_to_end_modified_edges):

        # This object holds for each pred/dst combinations
        # that have changed as keys and list of succ ports as values
        admitted_traffic_changes = defaultdict(defaultdict)

        for modified_edge in modified_edges:

            pred = self.get_node(modified_edge[0])
            succ = self.get_node(modified_edge[1])

            # TODO Limit the destinations by using markers in modified_flow_table_edges
            # Right now, just take all the destinations at succ and marking them as having been modified...

            for dst in self.get_admitted_traffic_dsts(succ):
                if dst not in admitted_traffic_changes[pred]:
                    admitted_traffic_changes[pred][dst] = [succ]
                else:
                    if succ not in admitted_traffic_changes[pred][dst]:
                        admitted_traffic_changes[pred][dst].append(succ)

        # Do this for each pred port that has changed
        for pred in admitted_traffic_changes:

            # For each destination that may have been affected at the pred port
            for dst in admitted_traffic_changes[pred]:

                now_pred_traffic = Traffic()

                # Check the fate of traffic from changed successors in this loop
                for succ in admitted_traffic_changes[pred][dst]:

                    edge = self.get_edge(pred, succ)

                    succ_traffic = self.get_admitted_traffic(succ, dst)

                    # Update admitted traffic at successor node to reflect changes
                    pred_traffic_via_succ = self.compute_edge_admitted_traffic(
                        succ_traffic, edge)
                    self.set_admitted_traffic_via_succ(pred, dst, succ,
                                                       pred_traffic_via_succ)

                    # Accumulate total traffic admitted at this pred
                    now_pred_traffic.union(pred_traffic_via_succ)

                # Collect traffic from any succs that was not changed
                for succ in self.get_admitted_traffic_succs(pred, dst):
                    if succ not in admitted_traffic_changes[pred][dst]:
                        now_pred_traffic.union(
                            self.get_admitted_traffic_via_succ(
                                pred, dst, succ))

                # Propagate the net unioned traffic to all of predecessors of the predecessor itself.
                for pred_pred in self.predecessors_iter(pred):

                    edge = self.get_edge(pred_pred, pred)
                    pred_pred_traffic = self.compute_edge_admitted_traffic(
                        now_pred_traffic, edge)

                    self.propagate_admitted_traffic(pred_pred,
                                                    pred_pred_traffic, pred,
                                                    dst,
                                                    end_to_end_modified_edges)
    #network = getSingleRoadNetwork(106, 660, 106, 0, 40)  ## - US101 data set
    #print network
    numberOfTracks = 1
    numberOfPointsPerTrack = 100
    numberOfVehicles = 1000
    for i in range(0, numberOfTracks, numberOfVehicles):
        #vehicles = getVehicles(numberOfVehicles, network)
        #vehicles = getVehiclesFromFile("/Users/gramag`uru/Desktop/car_videos/output_sing_sequences_appended_double.txt", network)
        #vehicles = getVehiclesFromFile("/Users/gramaguru/Desktop/car_videos/output_sing_sequences_appended_double_nonewline.txt", network)
        #vehicles = getVehiclesFromFileReal("/Users/gramaguru/Desktop/car_videos/sing_cropped_vehicle_generated_double.txt", network)
        vehicles = getVehiclesSumoBasic(numberOfVehicles, network)
        #vehicles = getMixedVehicles(numberOfVehicles, network, 0.2)
        ## Calculate homography and send it accross for using it while printing.
        print "Traffic Start"
        #traffic = Traffic(network, vehicles, (1000,1000,3), 10000)
        #traffic = Traffic(network, vehicles, (384,512,3), 86400)  ## - Singapore video
        traffic = Traffic(network, vehicles, (660, 212, 3),
                          10000)  ## - Sumo video
    #traffic = Traffic(network, vehicles, (660,360,3), 10000)  ## - Sumo video
    #traffic = Traffic(network, vehicles, (660,60,3), 1000)  ## - US101 video

    #straight_road_points = np.array([[200, 100],[300, 100],[300, 484],[200, 484]], dtype=float);
    #straight_road_points = np.array([[100, 0],[200, 0],[200, 384],[100, 384]], dtype=float);
    #perspective_road_points = np.array([[202, 105], [240, 105], [255, 300], [0, 300]], dtype=float);
    #H, status = cv2.findHomography(straight_road_points, perspective_road_points);

    #traffic.simulateAndVisualizeWithHomography(H, straight_road_points, perspective_road_points)
    traffic.simulateAndVisualize()
    #traffic.export()
    #traffic.simulateAndExport()
Beispiel #25
0
def red_light():
    trafficlight = Traffic()
    trafficlight.setStand(3)
    trafficlight.update_light()
    return 'Hello, World!'
Beispiel #26
0
def green_light():
    trafficlight = Traffic()
    trafficlight.setStand(0)
    trafficlight.update_light()
    return 'Hello, World!'
Beispiel #27
0
def amber_light():
    trafficlight = Traffic()
    trafficlight.setStand(1)
    trafficlight.update_light()
    return 'Hello, World!'
Beispiel #28
0
class Super(object):
    def __init__(self):
        self.BUTTON = Button(
            pin=11,
        )
        GPIO.setup(
            self.WAITING_PIN,
            GPIO.OUT,
        )

        self.LCD = LCD(
            pin_rs=4,
            pin_e=25,
            pins_d=[24,23,22,27],
        )

        self.TRAFFIC = Traffic(
            pin_red=14,
            pin_amber=3,
            pin_green=2,
            lcd=self.LCD,
            lcd_line=2,
        )

        self.RGB = RGB(
            pin_red=9,
            pin_green=10,
            pin_blue=15,
            lcd=self.LCD,
            lcd_line=3,
        )
        self.RGB.show_colour(self.RGB_MAX, self.RGB_MIN, self.RGB_MIN)

    def update(self):
        self._update_lcd()
        self._update_button()
        self._update_traffic()
        self._update_rgb()

    LCD_LAST_TIME = ''
    def _update_lcd(self):
        # Show the current time
        lcd_time = datetime.now().strftime('%H:%M:%S    %d/%m/%y')
        if lcd_time != self.LCD_LAST_TIME:
            self.LCD_LAST_TIME = lcd_time
            self.LCD.output_line(lcd_time, 0)

    WAITING_PIN = 8
    PREV_WAIT_STATE = None
    def _update_button(self):
        if self.BUTTON.was_pushed():
            self.TRAFFIC.push_the_button()

        wait_state = self.TRAFFIC.is_waiting
        if wait_state != self.PREV_WAIT_STATE:
            if wait_state:
                self.LCD.output_line('Waiting...', 1)
            else:
                self.LCD.output_line('', 1)
            GPIO.output(
                self.WAITING_PIN,
                wait_state,
            )
            self.PREV_WAIT_STATE = wait_state

    def _update_traffic(self):
        # Make the traffic lights work
        self.TRAFFIC.update()

    RGB_MIN = 0.0
    RGB_MAX = 100.0
    RGB_STEP = 0
    RGB_STEPS = (
        (RGB_MAX, RGB_MIN, RGB_MIN),
        (RGB_MAX, RGB_MAX, RGB_MIN),
        (RGB_MIN, RGB_MAX, RGB_MIN),
        (RGB_MIN, RGB_MAX, RGB_MAX),
        (RGB_MIN, RGB_MIN, RGB_MAX),
        (RGB_MAX, RGB_MIN, RGB_MAX),
    )
    def _update_rgb(self):
        # Make a rainbow
        # If the current step is finished, move to the next
        if not self.RGB.goto_colour_active:
            # Show the next step
            self.RGB_STEP = (self.RGB_STEP + 1) % len(self.RGB_STEPS)
            self.RGB.goto_colour(*self.RGB_STEPS[self.RGB_STEP])

        # But always update
        self.RGB.update()
Beispiel #29
0
    def _create_actor(self, carla_actor):  # pylint: disable=too-many-branches,too-many-statements
        """
        create an actor
        """
        parent = None
        if carla_actor.parent:
            if carla_actor.parent.id in self.actors:
                parent = self.actors[carla_actor.parent.id]
            else:
                parent = self._create_actor(carla_actor.parent)

        actor = None
        pseudo_actors = []
        #print("typeid")
        #print(carla_actor.type_id)
        if carla_actor.type_id.startswith('traffic'):
            if carla_actor.type_id == "traffic.traffic_light":
                actor = TrafficLight(carla_actor, parent, self.comm)
            else:
                actor = Traffic(carla_actor, parent, self.comm)
        elif carla_actor.type_id.startswith("vehicle"):
            print(carla_actor.attributes.get('role_name'))
            print(carla_actor.type_id)
            print(carla_actor.id)
            if carla_actor.attributes.get('role_name')\
                    in self.parameters['ego_vehicle']['role_name']:
                actor = EgoVehicle(
                    carla_actor, parent, self.comm, self._ego_vehicle_control_applied_callback)
                pseudo_actors.append(ObjectSensor(parent=actor,
                                                  communication=self.comm,
                                                  actor_list=self.actors,
                                                  filtered_id=carla_actor.id))

            else:
                actor = Vehicle(carla_actor, parent, self.comm)
        elif carla_actor.type_id.startswith("sensor"):
            if carla_actor.type_id.startswith("sensor.camera"):
                if carla_actor.type_id.startswith("sensor.camera.rgb"):
                    actor = RgbCamera(
                        carla_actor, parent, self.comm, self.carla_settings.synchronous_mode)
                elif carla_actor.type_id.startswith("sensor.camera.depth"):
                    actor = DepthCamera(
                        carla_actor, parent, self.comm, self.carla_settings.synchronous_mode)
                elif carla_actor.type_id.startswith("sensor.camera.semantic_segmentation"):
                    actor = SemanticSegmentationCamera(
                        carla_actor, parent, self.comm, self.carla_settings.synchronous_mode)
                else:
                    actor = Camera(
                        carla_actor, parent, self.comm, self.carla_settings.synchronous_mode)
            elif carla_actor.type_id.startswith("sensor.lidar"):
                actor = Lidar(carla_actor, parent, self.comm, self.carla_settings.synchronous_mode)
            elif carla_actor.type_id.startswith("sensor.other.gnss"):
                actor = Gnss(carla_actor, parent, self.comm, self.carla_settings.synchronous_mode)
            elif carla_actor.type_id.startswith("sensor.other.collision"):
                actor = CollisionSensor(
                    carla_actor, parent, self.comm, self.carla_settings.synchronous_mode)
            elif carla_actor.type_id.startswith("sensor.other.lane_invasion"):
                actor = LaneInvasionSensor(
                    carla_actor, parent, self.comm, self.carla_settings.synchronous_mode)
            else:
                actor = Sensor(carla_actor, parent, self.comm, self.carla_settings.synchronous_mode)
        elif carla_actor.type_id.startswith("spectator"):
            actor = Spectator(carla_actor, parent, self.comm)
        elif carla_actor.type_id.startswith("walker"):
            actor = Walker(carla_actor, parent, self.comm)
        else:
            actor = Actor(carla_actor, parent, self.comm)


        with self.update_lock:
            self.actors[carla_actor.id] = actor

        for pseudo_actor in pseudo_actors:

            with self.update_lock:
                self.pseudo_actors.append(pseudo_actor)

        return actor
Beispiel #30
0
#!/usr/bin/python3

# -- @Autora: Andrea del Nido García

import os
from traffic import Traffic

file_list = os.listdir("/home/buhohacker/Documentos/apks")
print("The list of APKs is: ")
print(file_list)
act_path = os.getcwd()

print("Script trafficAll.py is in directory " + str(act_path))

# Llamar a traffic.py con todas las apks
for file_name in file_list:
    print("The actual APK for analyze traffic is: " + file_name)
    apk = "/home/buhohacker/Documentos/apks/" + str(file_name)
    t = Traffic("192.168.1.53", "4000", "192.168.3.20", apk, "Label_PinningTest")
    print(t.configure())
    print(t.upload())
    print(t.phaseOne(25,False, False))
    print(t.phaseTwo(25, True))
    print(t.analysis())
    print(t.result())
    print(t.sanitize())
class Game:
    def __init__(self):
        # Internet topology
        self.generator = Generator(20, 5)
        self.generator.build_topology()
        self.generator.build_matrix()

        self.RLs = self.generator.Ts + self.generator.Cs + self.generator.Ms + self.generator.CPs

        self.N = 25
        self.Ns = self.generator.Ts + self.generator.Ms + self.generator.CPs + self.generator.Cs

        self.MAX = 100000
        self.ids = self.generator.ids

        # for each agent, add [s,a,r,s'] as element. size
        self.experience_pool = collections.defaultdict(list)
        self.pool_size = 10
        self.global_optimal = 0
        self.int_optimal = 0

        # TODO, define TF, Matrix, Linprog
        self.traffic = Traffic(0.5)
        self.TF = self.traffic.inject_traffic()

        # intlp = IntLp(self.generator.matrix, self.TF)
        # self.int_optimal = intlp.solve_ilp()

        # linear = Linprog(self.generator.matrix, self.TF)
        # self.global_optimal = linear.solve_linprog()

    def play_game(self):
        print("play")
        sess = tf.Session()
        print('sess')
        """
            basic states for every node
        """
        states = collections.defaultdict(list)
        for i in range(self.N):
            # add neighbor
            for j in range(len(self.generator.matrix[i])):
                if self.generator.matrix[i][j] == 1:
                    if i in range(len(self.generator.Ts)) and j in range(
                            len(self.generator.Ts)):
                        states[i].append(1000)
                    else:
                        states[i].append(100)

            # reachable end-to-end throughput (all advertised are considered here)
            node = self.ids[i]
            for d in node.table:
                states[i].append(0)
            for d in node.table_peer:
                states[i].append(0)
            for d in node.table_provider:
                states[i].append(0)
        """
            states_v1 to combine neighbor agents' states and actions
        """
        states_v1 = collections.defaultdict(list)
        for i in range(self.N):
            states_v1[i] = []
            # neighbor's state
            for j in range(len(self.generator.matrix[i])):
                if self.generator.matrix[i][j] == 1 or i == j:
                    states_v1[i] += states[j]
            # neighbor's action
            for k in range(len(self.generator.matrix[i])):
                if self.generator.matrix[i][k] == 1 or i == k:
                    states_v1[i].append(0)
        """
            create RL module
        """
        # v0 state
        # TODO, COPY
        for i in self.RLs:
            print("create mode for: " + str(i.id) + ", version 0")
            # node i
            n_features = len(states_v1[i.id])
            actor = Actor(sess, n_features, i.n_actions, i.id, 0)
            critic = Critic(sess, n_features, i.id, 0)
            i.set_rl_setting(actor, critic)
            sess.run(tf.global_variables_initializer())

        print("model created")
        '''
            loop time as time epoch
        '''

        sums_random = []
        sums_v1 = []
        sumt = []

        TF = self.TF
        for time in range(self.MAX):
            print("begin time epoch: " + str(time))
            """
                choose an action
                    id : action label
            """

            # v1
            #TODO COPY
            actions_v1 = {}
            for i in self.Ns:
                if i in self.RLs:
                    s = np.array(states_v1[i.id])
                    pro = random.random()
                    if pro > 0.1:
                        actions_v1[i.id] = i.actor.choose_action(s)
                    else:
                        actions_v1[i.id] = random.randint(0, i.n_actions - 1)
                else:
                    actions_v1[i.id] = 0

            # random
            actions_random = {}
            for i in self.Ns:
                # node i
                if i in self.RLs:
                    actions_random[i.id] = random.randint(0, i.n_actions - 1)
                else:
                    actions_random[i.id] = 0
            """
                actual flow
                    id : id : path
            """
            # v1
            # TODO, COPY
            actual_flow_v1 = collections.defaultdict(dict)
            for i in range(self.N):
                for j in TF[i].keys():
                    hop_path = []
                    cur = i
                    hop_path.append(self.ids[cur])
                    flag = -1
                    count = 0
                    while cur != j:
                        count += 1
                        if count > 10:
                            flag = 1
                            break
                        flag = 0
                        action = self.ids[cur].action_labels[actions_v1[cur]]
                        if action.get(j) is not None:
                            cur = action[j]
                            hop_path.append(self.ids[cur])
                        else:
                            flag = 1
                            break
                    if flag == 0:
                        actual_flow_v1[i][j] = hop_path

            num = 0
            if time == 0:
                for i in actual_flow_v1.keys():
                    for j in actual_flow_v1[i].keys():
                        num += 1
                print('actual flow: ' + str(num))

            # random
            actual_flow_random = collections.defaultdict(dict)
            for i in range(self.N):
                for j in TF[i].keys():
                    hop_path = []
                    cur = i
                    hop_path.append(self.ids[cur])
                    flag = -1
                    count = 0
                    while cur != j:
                        count += 1
                        if count > 10:
                            flag = 1
                            break
                        flag = 0
                        action = self.ids[cur].action_labels[
                            actions_random[cur]]
                        if action.get(j) is not None:
                            cur = action[j]
                            hop_path.append(self.ids[cur])
                        else:
                            flag = 1
                            break
                    if flag == 0:
                        actual_flow_random[i][j] = hop_path
            """
                link load 
                    id : id : V
            """
            # v1
            link_load_v1 = np.zeros([self.N, self.N])
            for i in actual_flow_v1.keys():
                for j in actual_flow_v1[i].keys():
                    path = actual_flow_v1[i][j]
                    for k in range(len(path) - 1):
                        e1 = path[k]
                        e2 = path[k + 1]
                        link_load_v1[e1.id][e2.id] += TF[i][j]
                        link_load_v1[e2.id][e1.id] += TF[i][j]

            # random
            link_load_random = np.zeros([self.N, self.N])
            for i in actual_flow_random.keys():
                for j in actual_flow_random[i].keys():
                    path = actual_flow_random[i][j]
                    for k in range(len(path) - 1):
                        e1 = path[k]
                        e2 = path[k + 1]
                        link_load_random[e1.id][e2.id] += TF[i][j]
                        link_load_random[e2.id][e1.id] += TF[i][j]
            """
                ee throughput
                    id : id : T
            """
            # v1
            ee_throughput_v1 = np.zeros([self.N, self.N])
            for i in actual_flow_v1.keys():
                # input node i
                for j in actual_flow_v1[i].keys():
                    path = actual_flow_v1[i][j]
                    temp_min = 9999
                    for k in range(len(path) - 1):
                        node1 = path[k]
                        node2 = path[k + 1]
                        # TODO, enlarge link capacity of TT
                        if node1.id in range(len(
                                self.generator.Ts)) and node2 in range(
                                    len(self.generator.Ts)):
                            ee = 1000 / (1 + link_load_v1[node1.id][node2.id])
                        else:
                            ee = 100 / (1 + link_load_v1[node1.id][node2.id])
                        if ee < temp_min:
                            temp_min = ee
                    ee_throughput_v1[i][j] = temp_min

            # random
            ee_throughput_random = np.zeros([self.N, self.N])
            for i in actual_flow_random.keys():
                # input node i
                for j in actual_flow_random[i].keys():
                    path = actual_flow_random[i][j]
                    temp_min = 9999
                    for k in range(len(path) - 1):
                        node1 = path[k]
                        node2 = path[k + 1]
                        # TODO, modify here, and the state
                        if node1 in self.generator.Ts and node2 in self.generator.Ts:
                            ee = 1000 / (1 +
                                         link_load_random[node1.id][node2.id])
                        else:
                            ee = 100 / (1 +
                                        link_load_random[node1.id][node2.id])
                        if ee < temp_min:
                            temp_min = ee
                    ee_throughput_random[i][j] = temp_min
            """
                next basic states for every node, neighbor part
            """
            # TODO, COPY
            v1_states_ = collections.defaultdict(list)
            for i in range(self.N):
                for j in range(len(self.generator.matrix[i])):
                    if self.generator.matrix[i][j] == 1:
                        if i in range(len(self.generator.Ts)) and j in range(
                                len(self.generator.Ts)):
                            if link_load_v1[i][j] in range(2):
                                v1_states_[i].append(1000)
                            else:
                                v1_states_[i].append(1000 /
                                                     (1 + link_load_v1[i][j]))
                        else:
                            if link_load_v1[i][j] in range(2):
                                v1_states_[i].append(100)
                            else:
                                v1_states_[i].append(100 /
                                                     (1 + link_load_v1[i][j]))
            """
                reward, 
                basic states, ee part
            """
            # v1
            #TODO, copy
            rewards_v1 = {}
            for agent in self.RLs:
                temp_table_v1 = collections.defaultdict(list)
                for des in agent.table:
                    temp_table_v1[des].append(0)
                for des in agent.table_peer:
                    temp_table_v1[des].append(0)
                for des in agent.table_provider:
                    temp_table_v1[des].append(0)

                sum_flow = 0
                sum_ee = 0
                for i in actual_flow_v1.keys():
                    for j in actual_flow_v1[i].keys():
                        path = actual_flow_v1[i][j]
                        if agent in path and agent is not path[-1]:
                            sum_flow += 1
                            sum_ee += ee_throughput_v1[i][j]
                            temp_table_v1[j].append(ee_throughput_v1[i][j])
                if sum_flow == 0:
                    rewards_v1[agent.id] = 0
                else:
                    rewards_v1[agent.id] = sum_ee / sum_flow

                for i in temp_table_v1:
                    avg = sum(temp_table_v1[i]) / len(temp_table_v1[i])
                    v1_states_[agent.id].append(avg)
            """
                system throughput
            """

            # v1
            sum_all_v1 = 0
            for i in range(self.N):
                for j in range(self.N):
                    sum_all_v1 += ee_throughput_v1[i][j]

            # random
            sum_all_random = 0
            for i in range(self.N):
                for j in range(self.N):
                    sum_all_random += ee_throughput_random[i][j]
            """
                next states v0
            """
            states_v1_ = collections.defaultdict(list)
            for i in range(self.N):
                states_v1_[i] = []
                for j in range(self.N):
                    if self.generator.matrix[i][j] == 1 or i == j:
                        states_v1_[i] += v1_states_[j]
                for k in range(self.N):
                    if self.generator.matrix[i][k] == 1 or i == k:
                        states_v1_[i].append(actions_v1[k])
            """
                agent learns through s, a, r, s_
            """

            # v0
            # TODO COPY
            for agent in self.RLs:
                s = np.array(states_v1[agent.id])
                r = rewards_v1[agent.id]
                s_ = np.array(states_v1_[agent.id])
                a = actions_v1[agent.id]
                exp = []
                exp.append(s)
                exp.append(r)
                exp.append(a)
                exp.append(s_)
                if len(self.experience_pool[agent.id]) < self.pool_size:
                    self.experience_pool[agent.id].append(exp)
                else:
                    self.experience_pool[agent.id] = self.experience_pool[
                        agent.id][1:]
                    self.experience_pool[agent.id].append(exp)
                experience = random.choice(self.experience_pool[agent.id])
                s = experience[0]
                r = experience[1]
                a = experience[2]
                s_ = experience[3]
                td_error = agent.critic.learn(s, r, s_)
                agent.actor.learn(s, a, td_error)

            states_v1 = states_v1_

            # sums.append(sum_all)
            sums_random.append(sum_all_random)
            sums_v1.append(sum_all_v1)
            # sumt.append(rewards[0])  # agent 0
            # print('rl: ' + str(sum_all))
            print("rl-v1: " + str(sum_all_v1))
            print('random: ' + str(sum_all_random))
            print('global optimal: ' + str(self.global_optimal))

            if time % 1000 == 0 and time != 0:
                str2 = 'v1sums_random' + str(time) + '.txt'
                file = open(str2, 'w')
                file.write(str(sums_random))
                file.close()
                str3 = 'sums-v1' + str(time) + '.txt'
                file = open(str3, 'w')
                file.write(str(sums_v1))
                file.close()
Beispiel #32
0
class Port(object):

    def __init__(self, sw, port_raw):

        self.sw = sw
        self.port_id = None
        self.curr_speed = None
        self.max_speed = None

        self.mac_address = None
        self.port_number = None
        self.state = None
        self.attached_host = None

        if self.sw.network_graph.controller == "ryu":
            self.parse_ryu_port_json(port_raw)

        elif self.sw.network_graph.controller == "onos":
            self.parse_onos_port_json(port_raw)

        elif self.sw.network_graph.controller == "grpc":
            self.parse_grpc_port(port_raw)
        else:
            raise NotImplemented

    def init_port_graph_state(self):

        # Need port_number parsed in before this is called
        self.switch_port_graph_ingress_node = PortGraphNode(self.sw,
                                                            PortGraph.get_ingress_node_id(self.sw.node_id,
                                                                                          self.port_number),
                                                            "ingress")

        self.switch_port_graph_egress_node = PortGraphNode(self.sw,
                                                           PortGraph.get_egress_node_id(self.sw.node_id,
                                                                                        self.port_number),
                                                           "egress")

        self.network_port_graph_ingress_node = PortGraphNode(self.sw,
                                                             PortGraph.get_ingress_node_id(self.sw.node_id,
                                                                                           self.port_number),
                                                             "ingress")

        self.network_port_graph_egress_node = PortGraphNode(self.sw,
                                                            PortGraph.get_egress_node_id(self.sw.node_id,
                                                                                         self.port_number),
                                                            "egress")

        self.switch_port_graph_ingress_node.parent_obj = self
        self.switch_port_graph_egress_node.parent_obj = self
        self.network_port_graph_ingress_node.parent_obj = self
        self.network_port_graph_egress_node.parent_obj = self

        self.ingress_node_traffic = Traffic(init_wildcard=True)
        self.ingress_node_traffic.set_field("in_port", int(self.port_number))

    def parse_odl_port_json(self, port_raw):

        self.port_id = str(self.sw.node_id) + ":" + str(port_raw["flow-node-inventory:port-number"])
        self.port_number = port_raw["flow-node-inventory:port-number"]
        self.mac_address = port_raw["flow-node-inventory:hardware-address"]
        self.curr_speed = int(port_raw["flow-node-inventory:current-speed"])
        self.max_speed = int(port_raw["flow-node-inventory:maximum-speed"])

        if port_raw["flow-node-inventory:state"]["link-down"]:
            self.state = "down"
        else:
            self.state = "up"

    def parse_onos_port_json(self, port_raw):

        self.port_number = int(port_raw["port"])
        self.port_id = str(self.sw.node_id) + ":" + str(self.port_number)
        self.mac_address = None
        self.state = "up"

    def parse_ryu_port_json(self, port_raw):

        self.port_id = str(self.sw.node_id) + ":" + str(port_raw["port_no"])
        self.port_number = port_raw["port_no"]
        self.mac_address = port_raw["hw_addr"]

        if "curr_speed" in port_raw:
            self.curr_speed = int(port_raw["curr_speed"])
        if "max_speed" in port_raw:
            self.max_speed = int(port_raw["max_speed"])

        self.state = "up"

    def parse_grpc_port(self, port_raw):
        self.port_id = str(self.sw.node_id) + ":" + str(port_raw.port_num)
        self.port_number = port_raw.port_num
        self.mac_address = port_raw.port_num

        self.state = "up"

    def __str__(self):
        return str(self.port_id)

    def __repr__(self):
        return str(self.port_id)