def __init__(self, _id, module, learner=None):
		#define variaveis da class
		self.id = _id
		self.horizontal_edge = lane.getEdgeID(trafficlights.getControlledLanes(self.id)[0])
		self.vertical_edge = lane.getEdgeID(trafficlights.getControlledLanes(str(_id))[2])
		#define variaveis da classe pai
		self.horizontalLoad = []
		self.verticalLoad = []
		self.averageHorizontal = []
		self.averageVertical = []
		self.nextAction = None
		self.expectedReward = None
		self.tolerance = None
		LearningAgent.__init__(self, module, learner)
def getCumulativeDelay(tls_id):
    # Get each vehicle in lanearea
    delay = 0
    for lane_id in tl.getControlledLanes(tls_id):
        det_id = la_mapping[lane_id]
        for vid in la.getLastStepVehicleIDs(det_id):
            delay += veh.getAccumulatedWaitingTime(vid)
    return delay
def getState(tls_id):
    EW = 0
    N = 0

    for lane_id in tl.getControlledLanes(tls_id):
        det_id = la_mapping[lane_id]

        if det_id[-1] in ["E", "W"]:
            EW += la.getLastStepHaltingNumber(det_id)
        else:  # in ["N", "S"]
            N += la.getLastStepHaltingNumber(det_id)

    phase = tl.getPhase(tls_id) % 2  # only in this case, for four phases
    db('State:', EW, N)
    sm = stateMap(EW, N, phase)
    visits.append((EW, N))
    return sm
Example #4
0
 def __init__(self, tls_id):
     self.tls_id = tls_id
     self.controlled_lanes = trafficlights.getControlledLanes(tls_id)
     self.directions = ['North', 'East', 'South', 'West']
     # Each edge(one direction) has 5 lanes,
     # two lanes as edge_0, another three as edge_1
     # edge_0_0(lane index:0) is trun-right lane
     # edge_1_1(lane index:3) is turn-left lane
     # edge_1_2(lane index:4) is turn-around lane
     # edge_0_1, edge_1_0 is straight lane
     self.N_edge = Edge(self.controlled_lanes[1], self.controlled_lanes[3])  # North edge_0, edge_1
     self.E_edge = Edge(self.controlled_lanes[6], self.controlled_lanes[8])
     self.S_edge = Edge(self.controlled_lanes[11], self.controlled_lanes[13])
     self.W_edge = Edge(self.controlled_lanes[16], self.controlled_lanes[18])
     self.edges = dict(zip(self.directions,
                           [self.N_edge, self.E_edge, self.S_edge, self.W_edge]))
     self.tls_status = dict()
 def __init__(self, collect_data):
     self.ID = tratl.getIDList()[0]
     self.controlledLanes = tratl.getControlledLanes(self.ID)
     self.timeInPhase = 0
     self.comfortAcceleration = 4
     self.df = pd.DataFrame()
     self.vehicleCount = 0
     self.collect_data = collect_data
     self.phases = tratl.getCompleteRedYellowGreenDefinition(self.ID)[
         0]._phases
     self.phase = 0
     self.greenCheck = re.compile("[gG]")
     self.redCheck = re.compile("[r]")
     self.ICData = {lane: [] for lane in self.controlledLanes}
     self.timeTillGreen = {lane: None for lane in self.controlledLanes}
     self.timeTillNextGreen = {lane: None for lane in self.controlledLanes}
     self.timeTillRed = {lane: None for lane in self.controlledLanes}
     self.Tl = {l: 0 for l in self.controlledLanes}
     self.light_change = traci.simulation.getCurrentTime()
def getDTSE(tls_id):
    # Get all vehicles in the lanearea
    # Given a list of relevant vehicles, convert it t
    # Array of intersection state maps

    DTSE_pos = np.zeros((NUM_LANES * 4, num_cells))
    DTSE_spd = np.zeros((NUM_LANES * 4, num_cells))
    for l, lane_id in enumerate(tl.getControlledLanes(tls_id)):
        det_id = la_mapping[lane_id]
        det_len = la.getLength(det_id)
        lane_len = lane.getLength(la.getLaneID(det_id))

        assert det_len == LANEAREA_LENGTH
        for vid in la.getLastStepVehicleIDs(det_id):
            # What is the correct encoding of pos to cell? Should low indices specify cars close or far away for traffic light?
            #print('lid',veh.getLanePosition(vid))
            pos = LANEAREA_LENGTH - lane_len + veh.getLanePosition(vid)
            if pos < 0:
                continue
            cell = int(
                min(pos // c, num_cells)
            )  # in case pos ends up being exactly the length of the detector

            speed = veh.getSpeed(vid)
            #print("Veh {}: {}".format(vid, pos))
            #print('cell',cell)

            # Record binary position and speed arrays
            DTSE_pos[l][cell] = 1
            DTSE_spd[l][cell] = speed

    phase = int(tl.getPhase(tls_id)) % 2
    signal_state = torch.zeros(1, A).float()
    signal_state[0][phase] = 1

    # Add batch dimension and input filter dimensions to state vectors
    DTSE_pos = torch.from_numpy(DTSE_pos).unsqueeze(0).unsqueeze(0).float()
    DTSE_spd = torch.from_numpy(DTSE_spd).unsqueeze(0).unsqueeze(0).float()
    return DTSE_pos, DTSE_spd, signal_state
Example #7
0
    tl_list=[]    
    for connection in connection_list:
        tl_list.append( connection[2])
    return tl_list
        
#lust= import_datasets()

#tls= extract_tl_ids(lust.iloc[0])
#print tls
#connections = dataset[5]==[from,to,tl,dir,state]
if True:      
    TLSID= "0"
    while step < 1000:
       simulationStep()
       arrived_vehicles_in_last_step= simulation.getArrivedNumber()
       departed_vehicles_in_last_step= simulation.getDepartedNumber()
       current_simulation_time_ms= simulation.getCurrentTime()
       print arrived_vehicles_in_last_step
       phase= trafficlights.getPhase(TLSID)
       trafficlights.setRedYellowGreenState(TLSID, "grrrrrrrrrrr")
       
       lanes= trafficlights.getControlledLanes(TLSID)
       print len(lanes)
       #for lane in lanes:
       #    print lane
       print phase
       #if traci.inductionloop.getLastStepVehicleNumber("0") > 0:
        #   traci.trafficlights.setRedYellowGreenState("0", "GrGr")
       step += 1
    
    close()
Example #8
0
    for connection in connection_list:
        tl_list.append(connection[2])
    return tl_list


#lust= import_datasets()

#tls= extract_tl_ids(lust.iloc[0])
#print tls
#connections = dataset[5]==[from,to,tl,dir,state]
if True:
    TLSID = "0"
    while step < 1000:
        simulationStep()
        arrived_vehicles_in_last_step = simulation.getArrivedNumber()
        departed_vehicles_in_last_step = simulation.getDepartedNumber()
        current_simulation_time_ms = simulation.getCurrentTime()
        print arrived_vehicles_in_last_step
        phase = trafficlights.getPhase(TLSID)
        trafficlights.setRedYellowGreenState(TLSID, "grrrrrrrrrrr")

        lanes = trafficlights.getControlledLanes(TLSID)
        print len(lanes)
        #for lane in lanes:
        #    print lane
        print phase
        #if traci.inductionloop.getLastStepVehicleNumber("0") > 0:
        #   traci.trafficlights.setRedYellowGreenState("0", "GrGr")
        step += 1

    close()
def cumHaltingNumber(tls_id):
    cum_halt_num = 0
    for lane_id in tl.getControlledLanes(tls_id):
        det_id = la_mapping[lane_id]
        cum_halt_num += la.getLastStepHaltingNumber(det_id)
    return cum_halt_num