Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[sumo-user] Route override after dispatchTaxi()

I am using the dispatchTaxi() and trying to re-route the taxi route after onboarding the passenger, I had specified the route it should follow, but I am getting the error of "no connection edge between B0B1 and B0B1". I am attaching my code with this email

I don't want to use the default route set by traci dispatchTaxi(). How can I safely override the route after dispatchTaxi()? but I want to use my custom route after onboarding the passenger. Plz help me with this issue. 

Thanks, 
Jay
import traci

def run():
    step = 0
    assigned = False
    distances = {
        "bus1": 0,
        "bus2": 0,
        "taxi1": 0
    }

    # 1. Add routes
    traci.route.add("taxi_route", ["B0B1", "B1B2", "B2A2", "A2B2"])
    traci.route.add("bus_route", ["A0A1", "A1A2", "A2B2"])
    # traci.vehicletype.setParameter("bus", "personCapacity", "   5")

    # Add taxi
    traci.vehicle.add("taxi1", "", typeID="taxi", depart="0")
    
    # Add first bus
    # traci.vehicle.add("bus1", "bus_route", typeID="bus", depart="40")
    # traci.vehicle.setBusStop("bus1", "stopA0A1", duration=30)  # Increased stop duration
    # traci.vehicle.setColor("bus1", (0, 255, 0))
    
    # # Add second bus with later departure
    # traci.vehicle.add("bus2", "bus_route", typeID="bus", depart="100")  # Later departure
    # traci.vehicle.setBusStop("bus2", "stopA0A1", duration=30)  # Increased stop duration
    # traci.vehicle.setColor("bus2", (0, 255, 0))

    # Add taxi passenger
    traci.person.add("passenger2", "B0B1", pos=5, depart=2)
    traci.person.appendWalkingStage("passenger2", ["B0B1"], 30, duration=5)
    traci.person.appendDrivingStage("passenger2", "A2B2", lines="taxi")

    # Add bus passengers with different departure times
    for i in range(1, 11):
        if i != 2:  # Skip passenger2 as it's for taxi
            person_id = f"passenger{i}"
            # Add passengers with different departure times
            traci.person.add(person_id, "A0A1", pos=5, depart=0)
            # # Walk to bus stop
            traci.person.appendWalkingStage(person_id, ["A0A1"], 45)
            # # Wait at bus stop
            # traci.person.appendWaitingStage(person_id, duration=25, stopID="stopA0A1")
            # Take bus ride
            traci.person.appendDrivingStage(person_id, "A2B2", lines="taxi")

    print("Step\tBus1\tBus2\tTaxi")
    print("-" * 50)

    while traci.simulation.getMinExpectedNumber() > 0:
        traci.simulationStep()

        for veh_id in distances.keys():
            if veh_id in traci.vehicle.getIDList():
                distances[veh_id] = traci.vehicle.getDistance(veh_id)
        
        # print(f"{step}\t{distances['bus1']:.2f}\t{distances['bus2']:.2f}\t{distances['taxi1']:.2f}")

        fleet = traci.vehicle.getTaxiFleet(0)
        reservations = traci.person.getTaxiReservations(0)
        reservation_ids = [r.id for r in reservations]

        if not assigned and fleet and len(reservations) > 0:
            traci.vehicle.dispatchTaxi(fleet[0], reservation_ids)
            assigned = True

        if traci.vehicle.getPersonNumber("taxi1") > 0: 
            traci.vehicle.setRoute("taxi1", ["B0B1", "B1B2", "B2A2", "A2B2"])





        step += 1

    print("Final Distances: ")
    for veh_id, distance in distances.items():
        print(f"{veh_id}: {distance:.2f}")

    traci.close()

if __name__ == "__main__":
    import traci
    sumoBinary = "sumo-gui"  # or "sumo"

    sumoCmd = [
        sumoBinary,
        "-c", "simulation.sumocfg",
        "--device.taxi.dispatch-algorithm", "traci",
    ]

    traci.start(sumoCmd)
    run()

Back to the top