Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of how to reroute all vehicles [DO NOT MERGE] #759

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

eugenevinitsky
Copy link
Member

Adds an optional param that lets you reroute every vehicle if it is on its final edge.

@eugenevinitsky eugenevinitsky changed the title Example of how to reroute all vehicles Example of how to reroute all vehicles [DO NOT MERGE] Oct 18, 2019
@nicofirst1
Copy link

The checking of the final edge is not valid for certain OSM maps (e.g. this). For most vehicles the route is a single element list.

@eugenevinitsky
Copy link
Member Author

eugenevinitsky commented Oct 19, 2019

You are correct. You can use self.k.vehicle.get_position(veh_id) to figure out the vehicle position and self.k.network.edge_length(edge) to get the edge length and figure out when you are near the end of the edge. Adding that check for single edge routes should solve this issue. I can push that fix after the weekend (I don't use electronics on Saturdays) or you can implement it if you need it before then.

@nicofirst1
Copy link

Something like this?

            edge = self.k.vehicle.get_edge(veh_id)
            self.k.vehicle.get_position(veh_id)

            pos=self.k.vehicle.get_position(veh_id)
            length=self.k.network.edge_length(edge)

            eta=length*0.01


            # check if its on the final edge
            if edge == route[-1] and pos+eta>=length:
                type_id = self.k.vehicle.get_type(veh_id)
                # remove the vehicle
                self.k.vehicle.remove(veh_id)

@nicofirst1
Copy link

Moreover I get this weird situation in which the previous if statement
if edge == route[-1] and pos+eta>=length
is False but the vehicle id is in
sim_obs[tc.VAR_ARRIVED_VEHICLES_IDS] (here)
therefore it is removed.

@eugenevinitsky
Copy link
Member Author

Yes that's the idea! One possible issue could be your value of eta is too small; if one of your edges is not too long and your speed is high you could travel off the edge before this condition catches it. Could you check if this is what's hapening?

A trick we used to use for this situation (that you could try) is to add long fake edges to every edge in the network that way there's zero chance of this happening. This is why there are weirdly long edges at the end of the grid example.

@nicofirst1
Copy link

Using
eta=length*0.1
Seems to solve the problem, I'll try with higher horizons (currently using 10).

Since this method seems a little forced in my opinion, I was wondering if there is a way to extract routes from an OSM map programmatically.

@eugenevinitsky
Copy link
Member Author

eugenevinitsky commented Oct 19, 2019

Cool! There isn't one that I know of, at least because it's an ill-posed problem. Technically any route is valid but you wouldn't take some routes (very long ones, routes that contain cycles). We have some work in progress to add this feature but it's not ready yet. If you wind up developing that feature and want to contribute it it would be very appreciated!

@nicofirst1
Copy link

nicofirst1 commented Oct 20, 2019

I would love to contribute, but my project is due to next Sunday and I don't think I have the time.
But I will keep my repo open which has some nice addition to flow.

@eugenevinitsky
Copy link
Member Author

eugenevinitsky commented Oct 20, 2019

Np. Hope it helps and let me know if anything comes up. Feel free to open a PR with any of these additions, I'm sure other people will find use for them and its a good way to give your work more visibility.

@nicofirst1
Copy link

I found out that using the add method, you do not reintroduce the agent in the rl_ids. This causes the samples to have inconsistent length, thus leading to the error.

I tried using a custom add funtion:

    def add_rl(self, veh_id, type_id, edge, pos,sumo_obs):

        """See parent class."""
        if veh_id in self.master_kernel.network.rts:
            # If the vehicle has its own route, use that route. This is used in
            # the case of network templates.
            route_id = 'route{}_0'.format(veh_id)
        else:
            num_routes = len(self.master_kernel.network.rts[edge])
            frac = [val[1] for val in self.master_kernel.network.rts[edge]]
            route_id = 'route{}_{}'.format(edge, np.random.choice(
                [i for i in range(num_routes)], size=1, p=frac)[0])

        self.kernel_api.vehicle.addFull(
            veh_id,
            route_id,
            typeID=str(type_id),
            departLane="random",
            departPos=str(pos),
            departSpeed=str(0))

        self._add_departed(veh_id, type_id)



        # modify the number of vehicles and RL vehicles
        self.num_vehicles = len(self.get_ids())
        self.num_rl_vehicles = len(self.get_rl_ids())

But for some reason is not working as expected. Is there a way to reintroduce an RL vehicle immediately after the rerouting so that the next update will consider its observations?

@eugenevinitsky
Copy link
Member Author

There will be occasions where it’s not possible to reinsert the vehicle immediately. I’d just track the IDs and output a zero vector for that vehicle for the timestep it is missing. That shouldn’t be a problem as long as the amount of time it’s missing is small.

@nicofirst1
Copy link

nicofirst1 commented Oct 23, 2019

Which are such occasions? Why wouldn't it be possible to reroute the agent immediately?

@eugenevinitsky
Copy link
Member Author

Where you want to insert them is blocked for example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants