0

my university project is to design a route planner of the city i live in, calculating the shortest routes between streets. (traveling salesman)

In C#, im using a graph to store all the streets. Currently i can find the shortest route between two streets in the weighted graph using Dijkstra's algorithm, first section done :)

My next task is, to calculate the safest route with a twist. Each street has a crash percentage on it (e.g 0.4% chance of a car crash based on roads crash history). I need to calculate the safest route HOWEVER solve the problem of if a user would like to travel the safest route possible, but not double their travel time as opposed to the fastest route.

Could anyone give me any ideas of what they think would be the best method to do this?

One method i came up with was to take the last street visited before the end destination is reached, remove that node and calculate a route without that, and do so for 10 routes and choose the one with the lowest accumulated crash percentage, however that also sounds like the worst possible way of doing it.

What maths/logic/algorithms can you guys give me? Anything the user should input? such as a threshold of how much slower they are willing to go? C#, java, pseudocode

Thanks so much guys

Filipe Teixeira
  • 3,565
  • 1
  • 25
  • 45
squish
  • 846
  • 12
  • 24
  • 1
    Can you use `crash percentage` somehow to adjust weights for Dijkstra's algorithm ? I mean some modification like just multiplying that percantage by weight to obtain new weight. – Yuriy Ivaskevych Mar 10 '17 at 14:48
  • Hmm i suppose that would work. Suppose the fastest route between two nodes has a distance of 200. And suppose the distance between the safest route is 300, however the crash percentage is 0.6%, 300*0.6 will make it 180 in distance, making it the preferred node. That makes sense to me if thats what you have come up with – squish Mar 10 '17 at 14:58
  • yes, and before changing weights you can run Dijkstra so that later you can check against requirement about doubled time. – Yuriy Ivaskevych Mar 10 '17 at 15:01
  • 1
    So a street with 0% crashrate will end up with 0 distance? Even if its distance weight is 50000000 ? – Fildor Mar 10 '17 at 15:01
  • @Fildor I just gave a general hint - that's why it is a comment. There could be a lot of limitations like 'there is always a possibility of a crash meaning every crashrate is > 0%' and/or some improvements like scaling, etc. – Yuriy Ivaskevych Mar 10 '17 at 15:04
  • @YuriyIvaskevych That was meant for OP. Just multiplying is way to naive. At least he should make that 300 * 1.06 ... so if it were 0% then at least the weight was still 300, not 0 – Fildor Mar 10 '17 at 15:05
  • Multiplying by a value less than 1 is what decides if the node should be used or not, as multiplying it less than 1 could give enough of a difference and make it more favorable than the distance of the fastest route, therefore a simple check, if the crash percentage equals 0, simply leave the weight of the node as it is?... Maybe im confusing myself now lol – squish Mar 10 '17 at 15:10
  • What if it is a crashrate of 0.0000000001 ... and remember *all* streets have crashrates. So 200 does not compete with 300 * 1.06 but 200 * 1.3 with 300 * 1.06 ... (for example) – Fildor Mar 10 '17 at 15:11
  • 1
    Think of it this way: If you only take distance into account, you have weights d1, d2, d3 ... if you only take crashrate into account, you'd have c1, c2, c3 ... First case: The smallest distance wins, Second case: smallest crashrate wins. Then if you mix the two, you want that a) if two streets have equal distance, the smaller crashrate wins and b) if two streets have equal crashrate the shorter one wins, correct? For all "in between" you must find an optimum between the two. Plus on top you have the overall "not longer than 2*shortest" ... so you may want to introduce a panelty for that. – Fildor Mar 10 '17 at 15:21
  • 1
    Shortest route from A to B isn't traveling salesman. Shortest route that hits all stops and gets you back where you started is traveling salesman. Shortest route is easy; traveling salesman is hard. – Eric Lippert Mar 10 '17 at 18:02
  • Is it a hard constraint that the route found must be the *safest* route whose cost is bounded by a particular amount? Given two routes, one of which is 0.00000000001 safer than another, is it *required* that the safer one be the one found? Because if you are willing to relax that restriction then the number of algorithmic choices you can make goes up considerably. – Eric Lippert Mar 10 '17 at 18:13

1 Answers1

0

You could start by running a normal Dijkstra's algorithm so you know what the fastest route is so that you know when the safest route exceeds this cost by some factor.

You could then run it again with safety as the main cost function, but also calculating the distances to rule out some routes. At first I thought this could be done like the normal Dijkstra's algorithm but by storing an ordered list for each node (previous node, cost, and safety value). The issue with this is that as distance and safety are apparently independent (in reality there's some sort of correlation but not one we know how to model). This is an issue as you need to prevent long distances early in the route wrongly leading you to rejecting options later in the route.

This need not be a problem if you could can apply the distance limit on partial routes. That is you don't accept a route to any intermediate node if it longer than twice the shortest route from the start to that node. I know that this isn't exactly as you stated because you asked about restricting only the route length to the final node, but maybe it still helps you in thinking about the overall solution. For what its worth it might also be more realistic.

Remember to combine the crash probability correctly, you can't just add it up. If we assume the journey stops at the first crash then you'd be calculating the probability of the crash as probability of crash on road 1 plus (probability of not crashing on road 1 * probability of crashing on road 2) plus (probability of getting to road 3 safely * probability of crash on road 3) etc (sorry if that's obvious).


Given your comment, I'll explain more on the probability (and it is probability and not odds you'd be best looking up)

If you want an overall measure of safety that allowed for continuing after a crash then it gets more complex. If you stop when there's a crash then its a matter of calculating probability of crashing exactly once. So a route has a crash if:

you crash on the first road (probability p1) OR

you don't crash on the first road ( probability 1-p1) but you do crash on the second (probability p2). OR

... you don't crash on roads n-1 (1-running total as above) but you do crash on road n (probability pn)

This way you don't get cases where a 50% chance of crash on roads 1&2 doesn't add up to an overall 100% chance - and you'd never get greater than that either.

In reality lots of crashes happen at junctions, and there will be a different probability of crash depending on whether you turn left or right - so the probability is not (in reality) just dependent on the safety factor of the roads between the junctions but also how the route combines them.

The probabilities should be weighted according to use (lookup conditional probabilities) so if there were 20 crashes on a rarely used road that may need to be modelled a having a higher crash probability as a busy road with 50 crashes in the same time period.

ROX
  • 1,256
  • 8
  • 18
  • Thank you for this, and i am actually slightly puzzled with adding the percentages up, originally was going to just divide by 10 each time a new node is visited and add it to a running total, but i suppose this isnt realistic at all. Ill have a look at calculating odds – squish Mar 10 '17 at 15:30
  • @squish: Suppose you roll a die on each corner, and if you get a 1, you crash. If there are 6 corners, you have not got a 6 * 1 / 6 = 100% chance of crashing. You have a 1 - (1 - 1 / 6) ^ 6 chance of crashing. – Eric Lippert Mar 10 '17 at 18:15