Distributing limited resources with choices - algorithm

I'm looking for an algorithm to figure out the maximum number of "products" that can be created given limited "resource choices".
For example, let's say I want to create an ABC and are given these resource choices:
A or B: 3
A or C: 1
B: 1
C: 1
In this case, I can create at most 2 ABC by selecting 2 A and 1 B from the first choice, and 1 C from the second choice. Then I have a total of 2 A, 2 B, and 2 C which can create 2 ABC.
Is there an algorithm, other than brute forcing the permutations, which solves this problem?
For completeness, here are the constraints in my actual problem:
Max number of different resources in product: 10
Max number of resource choices: 20
Max quantity of a choice: 50

It can be solved using Linear Programming or using the Ford-Fulkerson Algorithm to solve the so-called Maximum Network Flow problem. It would take me quiet some time to try to explain any of the 2 mentioned algorithms, so I think you should take a look at some online resources. If you need to solve some real problem I suggest you to go through the algorithm(s) just to get idea how you can model this particular problem and then use some of the existing libraries. If you want to learn algorithms, feel free to write your implementation :)

Related

TSP Genetic Algorithm - path representation and identical tour problem

We are implementing path representation to solve our travelling salesman problem using a genetic algorithm. However, we were wondering how to solve the issue that there might be identical tours in our individuals, but which are recognised by the path representation as different individuals. An example:
Each individual consists of an array, in which the elements are the cities visited in order.
Individual 1:
[1 2 3 4 5]
Individual 2:
[4 5 1 2 3]
You can see that the tour in 1 and 2 are actually identical, only the "starting" location is different.
We see some solutions to this problem, but we were wondering which one would be the best, or if there are best practices to overcome this problem from literature/experiments/....
Solution 1
Sort each individual to see if the individuals are identical:
1. pick an individual
2. shift the elements by 1 element to the right (at the end of the array, elements are placed at the beginning of the array)
3. check if this shift now matches an individual
4. if not, repeat steps 3 to 4
Solution 2
1. At the start if the simulations, choose a fixed starting point of the cities.
2. If the fixed starting point would change (mutation, recombination,...) then
3. Shift the array so that chosen starting point is back on first index.
Solution 3
1. Use the adjacency representation to check which individuals are identical.
2. Pass this information on to the path representation.
3. This is used to "correct" the individuals.
Solution 1 and 2 seem time consuming, although 2 would probably need much less computing time. Solution 3 would need to constantly switch from one to the other representation.
Then there is also the issue that in our example the tour can be read in 2 ways:
[1 2 3 4 5]
is the same as
[5 4 3 2 1]
Again here, are there any best practises the solve this?
Since you need to visit every city and return to the origin city, you can simply fix the origin. That solves your problem of shifted equivalent tours outright.
For the other, less important issue of mirrored tours, you can start by sorting your individuals by cost (which you probably already do), and check any pair of tours with equal costs using a simple palindrome-checking algorithm.

Formal name for this optimization algorithm?

I have the following problem in one of my coding project which I will simplify here:
I am ordering groceries online and want very specific things in very specific quantities. I would like to order the following:
8 Apples
1 Yam
2 Soups
3 Steaks
20 Orange Juices
There are many stores equidistant from me which I will have food delivered from. Not all stores have what I need. I want to obtain what I need with the fewest number of orders made. For example, ordering from Store #2 below is a wasted order, since I can complete my items in less orders by ordering from different stores. What is the name of the optimization algorithm that solves this?
Store #1 Supply
50 Apples
Store #2 Supply
1 Orange Juice
2 Steaks
1 Soup
Store #3 Supply
25 Soup
50 Orange Juices
Store #4 Supply
25 Steaks
10 Yams
The lowest possible orders is 3 in this case. 8 Apples from Store #1. 2 Soup and 20 Orange Juice from Store #3. 1 Yam and 3 Steaks from Store #4.
To me, this most likely sounds like a restricted case of the Integer Linear programming problem (ILP), namely, its 0-or-1 variant, where the integer variables are restricted to the set {0, 1}. This is known to be NP-hard (and the corresponding decision problem is NP-complete).
The problem is formulated as follows (following the conventions in the op. cit.):
Given the matrix A, the constraint vector b, and the weight vector c, find the vector x ∈ {0, 1}N such that all the constraints A⋅x ≥ b are satisfied, and the cost c⋅x is minimal.
I flipped the constraint inequality, but this is equivalent to changing the sign of both A and b.
The inequalities indicate satisfaction of your order: that you can buy at the least the amount of every item in the visited store. Note that b has the same length as the number of rows in A and the number of columns in both c and x. The dot-product c⋅x is, naturally, a scalar.
Since you are minimizing the number of trips, each trip costs the same, so that c = 1, and c⋅x is the total number of trips. The store inventory matrix A has a row per item, and a column per store, and the b is your shopping list.
Naturally, the exact best solution is found by trying all possible 2N values for the x.
Since there is no single approach to NP-hard problems, consider the problem size, and how close to the optimum you want to arrive. A greedy approach would work well (when your next store to visit has the most total number of items not yet satisfied) when the "inventories" are large. If you have the idea in advance about the expected minimum number of trips, you can trim the search beam at some value, exceeding the number of trips by some multiplication coefficient. This is the best approach when your search is time constrained (I routinely do beam searches, closely related to the branch-and-cut approach mentioned in the article, in graphs that take a few GB of memory slightly faster than the limit of 30ms per exploration step with a beam as wide as 10,000). Simulated annealing also works, if the search landscape is not excessively rough.
Also search on cs.SE; it may be even a better place for questions of this type.

Assign items to a minimal number of groups

This is a popular CS pattern, but I'm apparently missing some keywords because I'm not having any luck searching for it.
I have a set of 4 items: [A,B,C,D].
I have 3 groups: 1, 2, 3.
Group 1 can accept A or B.
Group 2 can accept B or C.
Group 3 can accept C or D.
Assign the items in a way that minimizes the number of groups used.
I.e. the solution would be:
Group 1: [A,B]
Group 2: []
Group 3: [C,D]
How would I solve this programatically? I know I've seen this before, so any keywords or links to point me in the right direction would be very appreciated.
This is the set covering problem. It is NP hard, so finding the true minimal set is hard in general and requires exponential time. The greedy algorithm which takes the set covering the most remaining elements may give good approximations. For covering sets with bounded size it can be also solved in reasonable time. For further details see http://en.m.wikipedia.org/wiki/Set_cover_problem
If we visualize this problem as a Graph problem, which all the groups and items are nodes in the graph, and there are edges connect between the group and items, we can see that this problem is a small case of Vertex Cover
However, if the number of items is small (less than 16), we can use dynamic programming to solve it easily.

Second best solution to an assignmentproblem using the Hungarian Algorithm

For finding the best solution in the assignment problem it's easy to use the Hungarian Algorithm.
For example:
A | 3 4 2
B | 8 9 1
C | 7 9 5
When using the Hungarian Algorithm on this you become:
A | 0 0 1
B | 5 5 0
C | 0 1 0
Which means A gets assigned to 'job' 2, B to job 3 and C to job 1.
However, I want to find the second best solution, meaning I want the best solution with a cost strictly greater that the cost of the optimal solution. According to me I just need to find the assignment with the minimal sum in the last matrix without it being the same as the optimal. I could do this by just searching in a tree (with pruning) but I'm worried about the complexity (being O(n!)). Is there any efficient method for this I don't know about?
I was thinking about a search in which I sort the rows first and then greedily choose the lowest cost first assuming most of the lowest costs will make up for the minimal sum + pruning. But assuming the Hungarian Algorithm can produce a matrix with a lot of zero's, the complexity is terrible again...
What you describe is a special case of the K best assignments problem -- there was in fact a solution to this problem proposed by Katta G. Murty in the following 1968 paper "An Algorithm for Ranking all the Assignments in Order of Increasing Cost." Operations Research 16(3):682-687.
Looks like there are actually a reasonable number of implementations of this, at least in Java and Matlab, available on the web (see e.g. here.)
In r there is now an implementation of Murty's algorithm in the muRty package.
CRAN
GitHub
It covers:
Optimization in both minimum and maximum direction;
output by rank (similar to dense rank in SQL), and
the use of either Hungarian algorithm (as implemented in clue) or linear programming (as implemented in lpSolve) for solving the initial assignment(s).
Disclaimer: I'm the author of the package.

Job assignment with NO cost, would Hungarian method work?

So I have a job assignment problem that doesn't have the traditional cost the Hungarian method requires.
For example:
I have 3 workers - A, B and C
I have 5 jobs - 1, 2, 3, 4 and 5
Each worker has a list of jobs he can perform, like so:
worker A can work on job 1, 2, 5
worker B can work on job 1, 2
worker C can work on job 1
The end result (since there's no cost) is the maximum number of assignments I can achieve. In this example, I can achieve a maximum of 3 assignments:
worker A on job 5
worker B on job 2
worker C on job 1
Is the Hungarian method a good way to solve this? Should I just use "dummy" costs? I was thinking maybe using the index of the job preference as the cost; is this a good idea?
The Hungarian algorithm could be made to work here, but an algorithm for unweighted maximum bipartite matching like Hopcroft–Karp would be faster.
Assign the cost -1 to the job which they can, and the others is zero.
Then run the Hungarian algorithm and it will give you the answer(It will returns -answer,in fact).
Don't do it with some large numbers, it may cause overflow(unless you implement the Hungarian very carefully).
Indeed, it's Maximum matchings in bipartite graphs, and there are so many ways to solve this problem, see wiki pages:
http://en.wikipedia.org/wiki/Matching_(graph_theory)#Maximum_matchings_in_bipartite_graphs
PS:Hopcroft–Karp algorithm is faster than hungarian and is more simple also. It worth a try. Some compulicated method is faster than these two, but it's not recommanded to learn these algorithms at very first.
PSS: Your ID in stackoverflow is a method to solve this problem. It's a network flow way. It's called shortest argument path(sap). See:http://coral.ie.lehigh.edu/~ted/files/ie411/lectures/Lecture11.pdf
Dummy costs should do the trick. Assign a cost of 1 to any job they can do, and an infinite cost (if your system allows that) to jobs they can't. The Hungarian algorithm is designed to minimize the total cost across all tasks, so it'll figure things out naturally. There shouldn't be any need to account for what you think their job preferences are; that's the algorithm's job.
Hungarian algorithm will give you an answer, but do not use infinity costs, since you cannot compare (infinity + infinity) and infinity (unless you compare the costs yourself).
A: 1, 2, 3
B: 1
C: 1
The matrix form:
1 2 3
A 1 2 3
B 1 inf inf
C 1 inf inf
How can your computer compare 1, inf, inf and 2, 1, inf?
Instead, use some cost that is so large that it will guarantee to be not assigned (and yes, be careful with overflowing).

Resources