A Route Assignment Program Algorithm - algorithm

What im trying to do, is create a program that will assign a route for a driving test. there will be three diffrent routes, linked together at certain points. Never should there be more than one student at a point of intersection.
Best way to solve this is to schedule these interection points by time.
This isnt my only problem, i will need routes to be equally distributed to examiners.
So route 1 will be given to examiner 1
route 2 - examiner 2
route 3- examiner 3...
The Real Baumann suggested this:
Calculate collision times from start.
Route 1 has 6 points. {A,B,C,D,E,F}
Route 2 has 5 points. {A,F,G,H,I}
Route 3 has 6 points. {A,H,K,L,M,N}
Possible Collisions at: {A,F,H}
So you need to calculate the following times:
Route 1: A->F, A->A
Route 2: A->F, A->H, A->A
Route 3: A->H, A->A
From here you can calculate time differences that create a collision.
If it takes you 20 minutes to go from route 1A to Route 1F and 5
minutes to get from Route 2A to Route 2F, then you know a collision
will occur if start an appointment on Route 2 exactly 15 minutes after
you began an appointment at Route 1.
Then you would have a set of non-working collisions:
Route 1 & 2 collide at: 15, 25, 40
Route 1 & 3 collide at: 25, 30
Route 2 & 3 collide at: 30, 40, 45
This i can understand to a point. But in terms of an algorithm i dont know where to start.
IF someone could help me with some pseudo code to work off, or something to make it clearer in my own mind. it would help a lot.

You should be able to calculate collision times from start.
Route 1 has 6 points. {A,B,C,D,E,F}
Route 2 has 5 points. {A,F,G,H,I}
Route 3 has 6 points. {A,H,K,L,M,N}
Possible Collisions at: {A,F,H}
So you need to calculate the following times:
Route 1: A->F, A->A
Route 2: A->F, A->H, A->A
Route 3: A->H, A->A
From here you can calculate time differences that create a collision.
If it takes you 20 minutes to go from route 1A to Route 1F and 5 minutes to get from Route 2A to Route 2F, then you know a collision will occur if start an appointment on Route 2 exactly 15 minutes after you began an appointment at Route 1.
Then you would have a set of non-working collisions:
Route 1 & 2 collide at: 15, 25, 40
Route 1 & 3 collide at: 25, 30
Route 2 & 3 collide at: 30, 40, 45
From here you should pretty easily be able to create your schedule without collisions.

I'm supposing you're not asking for tips on writing a super-duper algorithm that can resolve hundreds of paths with thousands of intersections simultaneously. It sounds like you need something simple and serviceable, so let's aim for that.
First off, let's simplify the problem. Looking at the map, what you're really saying is something like this: If a student starts route 1 at 8am, he'll be in intersection A sometime between 8:03 and 8:05, and then in intersection B sometime between 8:07 and 8:09.
To ensure that no other students are in the intersection, you can consider Intersection A "booked" from 8:03-8:05 by the first guy, and Intersection B "booked" similarly from 8:07-8:09.
Each intersection would have its own busy/free table.
Each time you schedule a route, you book the appropriate intersections during the time you believe the student will be in them.
When looking for the earliest available time for a route, you go through the routes and consider start time X "available" for that route if the of each intersections you'd pass through on the route are available at the time you'd pass through them.

Related

Can we have different distance for single source and destination?

On Monday, August 8, 2022 20:37:30(IST) and Sunday, August 14, 2022 17:02:32(IST), when we called Google Distance Matrix API for following source and destination address combination
origin_addresses: 143 River St, Jaffrey, NH 03452, USA
destination_addresses: 75 Lowell Rd, Salem, NH 03079, USA
We got different Response as following:
August 8, 2022 20:37:30- We got 101 km
August 14, 2022 17:02:32(IST) - We got 85.9 km
We don't have any extra query parameters except source an destination
API URL= Google API URL
Can we know, Is it possible to have different distance for single source and destination?
The short answer is NO. That is, if you are using Distance Matrix API. The API will only return a single route, which is the best possible route.
But it is possible to have different distance for a single origin and destination by including the alternatives parameter and setting it to true using Directions API.
I commented on your question as to why it is recommended to use Directions API if you only have a single origin and a single destination. Or watch this Youtube Video for a quick explanation about Distance Matrix API and it's difference with Directions API.
With that said, I tried the address you used on the Directions API and included the parameter alternatives and set it to true and it showed an array of three alternate routes:
Results are as follows:
Route #1
distance: 101 km
duration: 1 hour 20 mins
Route #2
distance: 78.6 km
duration: 1 hour 22 mins
Route #3
distance: 85.9 km
duration: 1 hour 20 mins
This concludes that the results you got on August 8 was the Route #1 and the one from August 14 was the Route #3.
The most probable cause of this could be the almost the same duration. although duration in hours is the same, if you look at the value on the results, route #1 had a value: 4794 while route #3 had a value: 4793. And it could be interchanged because of traffic data and other factors, given that you had two different date and time on your requests(duration in traffic could be different on different days and different times of the day).
Here's what the docs says:
"The API returns the most efficient routes when calculating directions. Travel time is the primary factor optimized, but the API may also take into account other factors such as distance, number of turns and many more when deciding which route is the most efficient."
Here's my request if you need some reference: https://maps.googleapis.com/maps/api/directions/json?units=metric&origin=143%20River%20St,%20Jaffrey,%20NH%2003452,%20USA&alternatives=true&destination=75%20Lowell%20Rd,%20Salem,%20NH%2003079,%20USA&key=YOUR_API_KEY
Just use your own API key and it should work.
I hope this helps.

Assign buses to routes base on their capacity

I am facing problem in assigning buses to routes. I have four buses and four routes with their capacities.
Bus capacity is the number of seats in each bus. Route capacity is number of people on each stop of a route. Route is actually a combination of multiple stops.
An example of a single test case is:
BUS CAPACITY ROUTE CAPACITY
BUS 1 44 Seats Route 1 30 Peoples
BUS 2 63 Seats Route 2 50 Peoples
BUS 3 14 Seats Route 3 40 Peoples
BUS 4 17 Seats Route 4 17 Peoples
There is a lot of test cases with multiple combinations for this problem. The numbers of routes and buses are always equal.
I am looking for a algorithm that helps out in solve this problem optimally.
Try
https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm
or
https://en.wikipedia.org/wiki/Euclidean_distance
approaches basically you are looking for best match.
In layman's terms what you might want to do is find the best matching route to a bus where best matching is defined as where the bus capacity to route capacity is optimal i.e. busCapacity - routeCapacity is lowest positive number.
Once you find such matches they will be eliminated from the problem world.
Then repeat this for the remaining world.
The end product is that everything is matched.
However prepare of the unexpected:-
You may exhaust all the possible combination and left with smaller capacity and bigger route capacity. In this case you look for difference between i.e. routeCapacity - busCapacity to be least as the best match.
This is so that the number of people you gave to let know that they can't be served is minimal.
You don't actually say what optimal means here, and that might change what the best solution is, but I would be inclined to sort the routes and buses and then assign the largest bus to the route with the largest number of people waiting, the next largest bus to the route with the next largest number of people waiting, and so on.
There will be unhappy people here if a route is assigned a bus that doesn't have enough seats for the people on the route, but with this assignment all of the buses with more seats are assigned to routes with even more people waiting, so giving a bigger bus would leave at least as many other people without a seat.
For a more sophisticated way of matching people and buses look at https://en.wikipedia.org/wiki/Assignment_problem
An example of how the definition of optimal changes things is if you don't care about the number of people annoyed but about the number of routes where people don't get a seat. In this case if you have routes with 2,3,5,9 people waiting and buses to carry 1,2,4,8 people it might make sense to assign them as 2 people-2 seats, 3 people-4 seats, 5 people-8 seats, 9-people-1 seat which leaves 8 people without a seat instead of 4 but all of those 8 people are on just 1 route.

Choosing a particular route for a shuttle vehicle upon demand

I'm trying to solve a problem where I have no idea what algorithm is to be used here to get the optimized answer. The problem is like this...
Consider there are 10 shuttle vehicles and 2 different routes in which they travel. Each vehicle has a seating capacity of 10 people. Normally each route will have 5 vehicles each (10 vehicles, 2 routes). I want to make the shuttle system smarter in a way that if Route 1 has more demand in users(shuttle running with full capacity) and Route 2 has minimal(where the number of passengers in each shuttle is less than 4), to tackle the demand needs, certain number of shuttle vehicles of Route 2 will come to Route 1. If the shuttle service in both routes are not in so demand(all running with less than 4 people), certain number of shuttle vehicles in both routes will stop running, and will be back running when the demand in users arises again.
Things to note : Distance is same in both routes, and the shuttles run free of charge for the passengers.
Which algorithm is best suited for these situations?
Thanks in advance!

Need help to adapt a genetic algorithm to "solve" Traveling Salesman on Ruby

I just downloaded ai4r library http://ai4r.rubyforge.org/ and i am using the genetic algorithm to get a good route from multiple places, just like this:
http://ai4r.rubyforge.org/geneticAlgorithms.html
But i need to be able to set a start city.
Any clue on how to use this on a "fixed" start city?
In general, not looking at the Ruby-specific implementation, you can just remove the start city, and assume that the first edge followed is from your start city to whatever the GA produces. Just make sure that first edge is included in the cost/fitness function.
The solution of a traveling salesmen problem is a round-trip and is therefor independent of a start-city. After you have found your solution you can take any city of the round-trip as your start-city.
EDIT: If you do not need to return to your start-city, you can select the end city, by removing the larger of the two distances that leave your start city. If you remove in your final solution the largest distance in the whole round-trip you get the overall shortest tour that is not a round-trip. This is likely what they did on the web page you linked (Dublin - Moscow looks to be the most expensive direction). However, note that the authors of that page used a wrong location for Vienna and Madrid seems to be off as well.
Another way of when you'll be needing a start-city is when you have an additional time window constraint. This constraint specifies that for each city you need to be there at a certain time, the "depot" as your start-city is called in this case has a time window that covers the whole trip. However the TSPtw is a much more complex problem and often requires advanced genetic operators. You can also model the TSPtw as a CVRPtw (Capacitated Vehicle Routing Problem with Time Windows) if you use just one vehicle. You can try our VRP implementation in HeuristicLab to solve this problem. We have a mailing list if you require further support.
The answer you'll get from the GA to the TSP is a cycle, that means that the first city is the on that you desire. For example, if the answer is [3 4 2 6 1 5], and I want the first city to be 2 then I can "roll" the solution to [2 6 1 5 3 4].
Although, you can reduce your problem by 1 if in your evaluation function you specify the first city. In that case you must take into account that the individuals must be modified to account this. For example, if you set the first city to #2 (problem of 6 cities) and you have an individual that is [1 2 3 4 5] (6 cities minus one). The individual to evaluate is [2 1 3 4 5 6].

Algorithm to find the best possible available times

Here is my scenario,
I run a Massage Place which offers various type of massages. Say 30 min Massage, 45 min massage, 1 hour massage, etc. I have 50 rooms, 100 employees and 30 pieces of equipment.When a customer books a massage appointment, the appointment requires 1 room, 1 employee and 1 piece of equipment to be available.
What is a good algorithm to find available resources for 10 guests for a given day
Resources:
Room – 50
Staff – 100
Equipment – 30
Business Hours : 9AM - 6PM
Staff Hours: 9AM- 6PM
No of guests: 10
Services
5 Guests- (1 hour massages)
3 Guests - (45mins massages)
2 Guests - (1 hour massage).
They are coming around the same time. Assume there are no other appointment on that day
What is the best way to get ::
Top 10 result - Fastest search which meets all conditions gets the top 10 result set. Top ten is defined by earliest available time. 9 – 11AM is best result set. 9 – 5pm is not that good.
Exhaustive search (Find all combinations) - all sets – Every possible combination
First available met (Only return the first match) – stop after one of the conditions have been met
I would appreciate your help.
Thanks
Nick
First, it seems the number of employees, rooms, and equipment are irrelevant. It seems like you only care about which of those is the lowest number. That is your inventory. So in your case, inventory = 30.
Next, it sounds like you can service all 10 people at the same time within the first hour of business. In fact, you can service 30 people at the same time.
So, no algorithm is necessary to figure that out, it's a static solution. If you take #Mario The Spoon's advice and weight the different duration massages with their corresponding profits, then you can start optimizing when you have more than 30 customers at a time.
Looks like you are trying to solve a problem for which there are quite specialized software applications. If your problem is small enough, you could try to do a brute force approach using some looping and backtracking, but as soon as the problem becomes too big, it will take too much time to iterate through all possibilities.
If the problem starts to get big, look for more specialized software. Things to look for are "constraint based optimization" and "constraint programming".
E.g. the ECLIPSe tool is an open-source constraint programming environment. You can find some examples on http://eclipseclp.org/examples/index.html. One nice example you can find there is the SEND+MORE=MONEY problem. In this problem you have the following equation:
S E N D
+ M O R E
-----------
= M O N E Y
Replace every letter by a digit so that the sum is correct.
This also illustrates that although you can solve this brute-force, there are more intelligent ways to solve this (see http://eclipseclp.org/examples/sendmore.pl.txt).
Just an idea to find a solution:
You might want to try to solve it with a constraint satisfaction problem (CSP) algorithm. That's what some people do if they have to solve timetable problems in general (e.g. room reservation at the University).
There are several tricks to improve CSP performance like forward checking, building a DAG and then do a topological sort and so on...
Just let me know, if you need more information about CSP :)

Resources