Assign buses to routes base on their capacity - algorithm

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.

Related

Algorithm for identifying route in urban subways

I have some 26 subway stations and I have built a route id for each possible journey including which line and transfer point.
As a device travels through the subway network I collect bluetooth probes as they are seen at the different locations. The probes are not always picked up at every station.
What I am trying to understand is the best approach to taking the probe data and classifying which route the a device traveled. The ultimate goal is to understand what percentage of devices choose different routes that have the same start and end station and what affect the time of day has.
Ideally this is something I would like to address without the aid of machine learning.
Here is an illustration of the problem:
I think you can simplify the problem (at the cost of losing information) by breaking your observations of all the probes seen in a journey up into overlapping observations of just two or three probes.
Then if somebody gives me a proposed allocation of probe points to positions in the network and a short section of two or three probes I can come up with the probability of seeing the final probe in the section given the first one or two probes and the allocation of probes to network positions. Looking at your graph, it is likely that you will see 6 if the previous two probes were 4 and 5, and unlikely that you will see 11 immediately after 4 and 5. You can treat the known station information as a special case of this - treat Station A as a probe with an initially known location that is always picked up.
Given a proposed allocation of probe points to positions you can then go through all of the short segments of probe and station information and sum up the logs of the probabilities that you get. This gives you a score to assess how credible that allocation of probe points to positions is. Now all you have to do is to find the allocation of probe points to positions that gives the highest score.
I would try hill-climbing from multiple random starts. Given a proposed assignment I would score alternative assignments by changing the position of one of the probe points and see if any of these alternative assignments got a higher score than the original. If so, modify the original and keep going until you have hill-climbed to a local optimum. You could also try a genetic algorithm, or you could try scoring and extending partial assignments of probe points to positions.
There might well be a HMM approach to this too. The hidden state would be the assignment of probes to positions in the network. Again, each short stretch of two or three probes seen could amount to an observation of behaviour from the HMM.
There is also a hidden markov model approach to assessing the probability of seeing an entire journey, given an allocation of probes to positions. The hidden state at each point is the location of the observer. You could assume that the observer moves to a random neighbour of the current position at each change of state (I'm not sure of the details - have you some means of knowing when you get to observe a new set of probes, or perhaps none at all, or do you just notice when the set of probes changes?). From this you can calculate the log probability of the sequence of probes seen during a journey. As before, sum the log probabilities to get the total log probability of all recorded journeys, given an allocation of probes to positions, and hill-climb on this from multiple random starts to find the allocation of probes to positions which gives you the highest log probability.

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!

A fast algorithm for determining timeshare requests

I am writing a program that will simulate how members of a certain timeshare will request their apartments. The apartments are only available for certain 'events' throughout the year, and the events each have different durations (counted in days). For each event, there are a number of apartments available, which are divided into groups according to cost (points per day).
We may also have the situation that some of the apartments are already rented out, so it is possible that certain events cannot admit a certain apartment type.
Now I want a member to request apartments according to this strategy:
Maximizing the number of total event days is first priority.
Maximizing the number of points spent is second priority (so the user requests the best possible apartment that he/she can afford and still have as many days as possible).
The total cost of the requested apartments cannot exceed the total amount of available points for that user.
Obviously, if there are no more apartments of a given type for a certain event, the user should not request that particular apartment/event combo.
I am wondering whether the problem is similar to the problem described here:
http://en.wikipedia.org/wiki/Hungarian_algorithm
in that it (I suppose) possible to frame this as a matrix problem where each entry has a cost associated with it.
However, the difference is that for my problem, I am allowed to use the same apartment for several events - it's not 'spent' once it has been used for one event. Also, the cost per entry is not really one-dimensional, since each event/apartment combo both has a number of days associated with it and a number of points - both of which should be maximized (but with priority given to the number of days).
As an example, let's say there are three apartment types, costing 75, 100, and 125 points per day, and three events, with a duration of 2, 10, and 4 days. Let's further say many of the apartments are taken, so the availability matrix looks like this:
cost
75 100 125
2 True False True
days 10 False False True
4 True False True
Let's also say the user has 1250 points available. The solution, in this case, would be that the user requests the 10-day event with the 125-point apartment and nothing else.
The brute-force way of doing this would perhaps be a recursive algorithm:
Let n be the number of events you are currently trying
Find all combinations of events and apartments, and calculate the combo that maximizes the number of days, then the number of points spent (this will both include all permutations of n events, but also the number of ways 3 apartment types can be assigned to n events).
Let n=n-1
This will quickly become overwhelming when the number of events goes up, I think, so I am wondering whether there are any algorithms that can solve this in a less expensive way?
If you have access to a library for http://en.wikipedia.org/wiki/Integer_programming you could try throwing this at it.
Even if you have only one choice of cost for each event, so that you are just trying to chose combinations of events that cover as many total days as possible without going over budget, I think this reduces to http://en.wikipedia.org/wiki/Knapsack_problem. This means that it is unlikely to be solved exactly by a worst case polynomial time algorithm such as the Hungarian algorithm.

Optimizing allocation algorithm needed

Today I encountered an interesting problem when accounting for the travel costs of my last journey with my friends.
Let's assume we had the following expenditures:
# expenditures in US-$
Peter= 117
Joe= 38
Bill= 15
Chris= 0
Alan= 209
Tim= 201
Ahmet= 124
Pati= 57
Steven= 74
Now we have decided that everybody should pay the same amount of money. Given that the average expenditure was 92.77778 US-$, The Balances look like this:
# balances in US-$
Peter= 24.22
Joe= -54.78
Bill= -77.78
Chris= -92.78
Alan= 116.22
Tim= 108.22
Ahmet= 31.22
Pati= -35.78
Steven=-18.78
So now I would like to find an optimization method where we have a minimum number of total bank-transactions and a fairly distributed share of transactions over all participants (so two optimization aims)
I have looked up the Stable Marriage Problem but I think it doesn't apply in this case.
This doesn't seem like a difficult problem -- sort the participants by ascending expenditures, then, in that order, have each participant transfer their negated (current) balance to the next participant. n-1 transactions, and at most one transaction per participant. Each of which, of course, is the best you can do in general.
Or is there some extra constraint I'm missing?
This method only solves the part of "minimum number of total bank-transactions".
People are grouped into two categories, say Giver (whose balance is negative) and Receiver(whose balance is positive).
Key Observation In an optimum solution, a giver should always give away money, a receiver should always receive money. It's impossible for person A gives money to B and then B gives some money to C.
Now comes the solution.
While (there is some giver or receiver) {
Find the giver with the largest balance, say A.
Find the receiver with the largest balance, say B.
Let A gives as much money as B needs.
Delete Person if his/her balance becomes 0.
}

What class of algorithms can be used to solve this?

EDIT: Just to make sure someone is not breaking their head on the problem... I am not looking for the best optimal algorithm. Some heuristic that makes sense is fine.
I made a previous attempt at formulating this and realized I did not do a great job at it so I removed that question. I have taken another shot at formulating my problem. Please feel free to provide any constructive criticism that can help me improve this.
Input:
N people
k announcements that I can make
Distance that my voice can be heard (say 5 meters) i.e. I may decide to announce or not depending on the number of people within these 5 meters
Goal:
Maximize the total number of people who have heard my k announcements and (optionally) minimize the time in which I can finish announcing all k announcements
Constraints:
Once a person hears my announcement, he is be removed from the total i.e. if he had heard my first announcement, I do not count him even if he hears my second announcement
I can see the same person as well as the same set of people within my proximity
Example:
Let us consider 10 people numbered from 1 to 10 and the following pattern of arrival:
Time slot 1: 1 (payoff = 1)
Time slot 2: 2 3 4 5 (payoff = 4)
Time slot 3: 5 6 7 8 (payoff = 4 if no announcement was made previously in time slot 2, 3 if an announcement was made in time slot 2)
Time slot 4: 9 10 (payoff = 2)
and I am given 2 announcements to make. Now if I were an oracle, I would choose time slots 2 and time slots 3 because then 7 people would have heard (because 5 already heard my announcement in Time slot 2, I do not consider him anymore). I am looking for an online algorithm that will help me make these decisions on whether or not to make an announcement and if so based on what factors. Does anyone have any ideas on what algorithms can be used to solve this or a simpler version of this problem?
There should be an approach relying upon a max-flow algorithm. In essence, you're trying to push the maximum amount of messages from start->end. Though it would be multidimensional, you could have a super-sink, which connects to each value of t, then have each value of t connect to the people you can reach at this time and then have a super-sink. This way, you simply have to compute a max-flow (with the added constraint of no more than k shouts, which should be solvable with a bit of dynamic programming). It's a terrifically dirty way to solve it, but it should get the job done deterministically and without the use of heuristics.
I don't know that there is really a way to solve this or an algorithm to do it the way you have formulated it.
It seems like basically you are trying to reach the maximum number of people with exactly 2 announcements. But without knowing any information about the groups of people in advance, you can't really make any kind of intelligent decision about whether or not to use your first announcement. Your second one at least has the benefit of knowing when not to be used (i.e. if the group has no new members then you can know its not worth wasting the announcement). But it still has basically the same problem.
The only real way to solve this is to use knowledge about the type of data or the desired outcome to make guesses. If you know that groups average 100 people with a standard deviation of 10, then you could just refuse to announce if less than 90 people are present. Or, if you know you need to reach at least 100 people with two announcements, you could choose never to announce to less than 50 at once. Obviously those approaches risk never announcing at all if the actual data does not meet what you would expect. But that's always going to be a risk, since you could get 1 person in the first group and then 0 in all of the rest, no matter what you do.
Or, you could try more clearly defining the problem, I have a hard time figuring out how to relate this to computers.
Lets start my trying to solve the simplest possible variant of the problem: Lets assume N people and K timeslots, but only one possible announcement. Lets also assume that each person will only ever stay for one timeslot and that each person who hasn't yet shown up has an equally probable chance of showing up at any future timeslot.
Given these simplifications, at each timeslot you look at the payoff of announcing at the current timeslot and compare to the chance of a future timeslot having a higher payoff, eg, lets assume 4 people 3 timeslots:
Timeslot 1: Person 1 shows up, so you know you could get a payoff of 1 by announcing, but then you have 3 people to show up in 2 remaining timeslots, so at least one of those timeslots is guaranteed to have 2 people, so don't announce..
So at each timeslot, you can calculate the chance that a later timeslot will have a higher payoff than the current by treating the remaining (N) people and (K) timeslots as being N independent random numbers each from 1..k, and calculate the chance of at least one value k being hit more than or equal to the current-payoff times. (Similar to the Birthday problem, but for more than 1 collision) and then you need to decide hwo much to discount based on expected variances. (bird in the hand, etc)
Generalization of this solution to the original problem is left as an exercise for the reader.

Resources