def getMARLState(tls_id):
    P, V, L = getDTSE(tls_id)

    # traffic light ids of lights that connect to the tls_id light
    # Here we assume the ids are all integer
    tls_id1 = str((int(tls_id) + 1) % 4)
    tls_id2 = str((int(tls_id) - 1) % 4)

    if ma == 1:
        phase1 = tl.getPhase(tls_id1) // 2
        phase2 = tl.getPhase(tls_id2) // 2

        #print('MARL tls ids & phases:', tls_id, tls_id1, tls_id2, phase1, phase2)
        other_signal_state1 = torch.zeros(1, A).float()
        other_signal_state1[0][phase1] = 1

        other_signal_state2 = torch.zeros(1, A).float()
        other_signal_state2[0][phase2] = 1
    elif ma == 2:
        state_info1 = cumHaltingNumber(tls_id1)
        state_info2 = cumHaltingNumber(tls_id2)

        other_signal_state1 = torch.Tensor([[state_info1]])
        other_signal_state2 = torch.Tensor([[state_info2]])

    L = torch.cat((L, other_signal_state1, other_signal_state2), 1)
    return P, V, L
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
def inductionloop():
    """execute the TraCI control loop"""
    # we start with phase 2 where EW has green
    tl.setPhase("0", 2)
    i = 2
    while sim.getMinExpectedNumber() > 0:
        traci.simulationStep()
        print("E:", la.getLastStepHaltingNumber("0"))
        print("N:", la.getLastStepHaltingNumber("1"))
        # Get occupancy of East lane
        #print(lane.getLastStepOccupancy("2i_0"))
        if tl.getPhase("0") == 2:
            # we are not already switching
            if traci.inductionloop.getLastStepVehicleNumber("0") > 0:
                # there is a vehicle from the north, switch
                tl.setPhase("0", 3)
            else:
                # otherwise try to keep green for EW
                tl.setPhase("0", 2)

    traci.close()
    sys.stdout.flush()
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 #5
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 #6
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 run(models, state_fn, action_time=12, learn=True, writer=None):
    print("File beginning", flush=True)
    tls_ids = tl.getIDList()
    step()  #traci.simulationStep()
    s = [None for i in range(len(tls_ids))]
    a = [None for i in range(len(tls_ids))]
    r = [None for i in range(len(tls_ids))]
    s_ = [None for i in range(len(tls_ids))]

    next_action_times = np.zeros(len(tls_ids), dtype=int)

    for t, tls_id in enumerate(tls_ids):
        tl.setPhase(tls_id, 2)
        # Find current states
        s[t] = state_fn(tls_id)

    checkpoint = False
    while sim.getMinExpectedNumber() > 0:
        if not checkpoint and "2000" in veh.getIDList():
            writer.add_text("Rate switch", "Vehicle 2000 encountered",
                            sim.getCurrentTime())
            checkpoint = True

        for t, tls_id in enumerate(tls_ids):
            db("Phase duration for " + tls_id, tl.getPhaseDuration(tls_id))
            if next_action_times[t] > 0:
                continue

            tl.setPhaseDuration(tls_id, 10000)
            # Take actions
            a[t] = models[t].select_action(s[t])

            current_phase = tl.getPhase(tls_id)
            if a[t] * 2 != current_phase:  # Change action
                db('Change to other green light')
                tl.setPhase(tls_id, (current_phase + 1) % 4)
                #print("phase:", tl.getPhase("0"))
                yellow_time = (tl.getNextSwitch(tls_id) -
                               sim.getCurrentTime()) // 1000
            else:
                yellow_time = 0

            next_action_times[t] = action_time + yellow_time

        db('actions:', a)
        db("-" * 10)

        # Move simulation forward one step and obtain reward
        #db(action_time + yellow_time)
        step_size = min(next_action_times)
        for j in range(step_size):
            step()  #traci.simulationStep()
            if sim.getMinExpectedNumber() <= 0:
                return

        next_action_times -= step_size

        # Get some kind of reward signal from the E2 loops
        # Let's say its negative of the number of cars on the loop

        for t, tls_id in enumerate(tls_ids):
            if next_action_times[t] > 0:
                continue

            r[t] = getReward(tls_id)
            if writer:
                writer.add_scalar('reward', r[t], sim.getCurrentTime())
            rewards_t.append(r)

            db("reward:", r)
            s_[t] = state_fn(tls_id)

            if learn:
                models[t].update(s[t], a[t], r[t], s_[t], alpha)
            s[t] = s_[t]