Optimization Problem

Status
Not open for further replies.

Faramir

Ars Legatus Legionis
36,356
Subscriptor
I'm quite certain I should know the algorithm for this problem, but I can't think of anything or find anything appropriate on google.

----

Given a 2-D matrix of dimensions M rows x N cols, where M > N, filled with real numbers, select one row from each column, with each row used at most once, such that the sum of all selections is maximized If there is a tie any solution is acceptable.

So for example:
Code:
{ 1.0, 3.5, -2.0 }
{ -2.1, 4.0, 2.0 }
{ 1.1, -5.3, 3.4 }
{ 3.6, 1.0, 2.0. }

Would be (4,1), (2, 2), (3, 3).

Thanks!
 

tb12939

Ars Tribunus Militum
2,157
Tuborg":2p03i5hw said:
Feels like it could be one of those difficult combinatorial messes, though.
I'd bet a small cookie it's NP-complete.

I believe you can reduce the shortest superstring problem to this problem (with a square matrix), by using the maximum overlap between the tail of string x and head of string y as the value in m[x], and aiming to maximise the total overlap without string (aka column and row) reuse. Since the shortest superstring problem has already been proven NP-complete, any better solution to this problem is unlikely :)

Strictly speaking, the above reduction would give you a circular minimum superstring, you'd also need to add dummy terminating strings to make it equivalent to the linear form, but it doesn't seem to alter the conclusion.

*Edit: No actually their not equivalent - the minimum superstring has additional constraints, which add the 'hardness' - the assignment problem it is.
 

uec0

Smack-Fu Master, in training
68
Sounds possibly like Linear Assignment Problem. Complexity is generally something like O(m^2 * n).

Hungarian/Munkres Method works, but you have may have to extend it for non-square cost matrices. This algorithm has been around for a long time.

Jonker-Volgenant-(Castanon) (Shortest Augmenting Path) is better, but trickier to understand and implement -- It can be tough to find literature. Be prepared to try to parse really ugly FORTRAN or Pascal...

Even more advanced is network-flow methods like Push-Relabel. There is at least one good Google paper covering Push-Relabel and variations.

When I first got into it, relevant articles were sparse. There's more now (e.g. Wikipedia). Lucky you :D

Of course I may have misinterpreted the problem and everything above is useless (hopefully not).
 

Faramir

Ars Legatus Legionis
36,356
Subscriptor
Ok, so I looked into the Hungarian algorithm, figured out how to structure dummy data to make my array square, coded up an implementation with a little help from google, and found it to run acceptably fast.

Only then while sanity checking the results, did I realize I optimized on the wrong (or at least suboptimal) metric. What I'd really like is to maximize the minimum selection rather than the total sum.

Any ideas?
 

uec0

Smack-Fu Master, in training
68
Assuming everything else is the same, and the only difference is that your objective function to maximize is the minimum capacity...

You may be able to use Shortest Augmenting Path (for Matching) modified to use minimum capacity instead of capacity sum. There's a decent amount of literature on Network Flow and Shortest Augmenting Path (SAP).

Generally you can use Dijkstra to find the SAP. So instead of using summation to calculate minimum distances, use the maximum path distance (or in your case, calculate maximum capacity using minimum path capacity).

To be honest, I haven't thought this through completely...
 
This is just something off the top of my head but here goes. How about sorting each column, then dividing the matrix to only account for the first M rows (since answer could not be outside this bound). Then if each max (of each column) is from a different row, there's your answer. Otherwise, for the conflicting values, check each value's next max and choose the maximum of these, repeat until there are no more conflicting values and you've got your answer. Haven't tested it though.

The sorting take M*NlgN, latter part I think is O(M^2) but I'm not sure about this one.
 

Alamout

Ars Legatus Legionis
27,285
^ I think you're proposing a simple greedy algorithm: take the biggest number you can get, find the next-biggest that doesn't conflict, repeat until finished.

This isn't going to give you the optimal answer in many cases: because you blindly pick the biggest option at any given point, you can close off a path that would be higher overall (by picking X you prevent yourself from picking Y and Z, where X > Y > Z but X < Y + Z)
 

Alamout

Ars Legatus Legionis
27,285
Faramir":1fr49y4w said:
If you think of each potential set of valid row/value picks as a chain, in the original problem I was trying to maximize the sum of the values of each link in the chain.

Now I want to find the chain in which the lowest valued link in the chain is greater than the lowest valued link in any other potential chain.
I'm not sure I understand the "chain" aspect, but the second sentence smells like Network Flow to me.
 
Alamout":1flal1iw said:
^ I think you're proposing a simple greedy algorithm: take the biggest number you can get, find the next-biggest that doesn't conflict, repeat until finished.

This isn't going to give you the optimal answer in many cases: because you blindly pick the biggest option at any given point, you can close off a path that would be higher overall (by picking X you prevent yourself from picking Y and Z, where X > Y > Z but X < Y + Z)

I don't understand your last sentence in relation to this problem. I thought the problem was picking out a chain in which each minimum of the chain was larger than any minimum from any other chain. In such a case, wouldn't we want X even if there are other values in the column that could add up to more for the chain overall?

You're right in that it's probably not an optimized solution. There are cases where greedy algorithms work well though, such as Dijkstra path finding algorithm for acyclic directed graphs. A dynamic programming solution might be better suited for this but the solution I posted was the first thing that came to me.
 

Alamout

Ars Legatus Legionis
27,285
I don't understand your last sentence in relation to this problem. I thought the problem was picking out a chain in which each minimum of the chain was larger than any minimum from any other chain. In such a case, wouldn't we want X even if there are other values in the column that could add up to more for the chain overall?
If I'm understanding the problem correctly (and I very well may not), he wants the set with the highest-minimum-value? In that case, you don't really care about the big values at all, you're just trying to avoid the small ones.

Let's say that choosing X forces you to choose A in some other row, whereas choosing Y < X allows you to choose B > A in that row, raising your minimum value.
 
Alamout":hr1vij91 said:
I don't understand your last sentence in relation to this problem. I thought the problem was picking out a chain in which each minimum of the chain was larger than any minimum from any other chain. In such a case, wouldn't we want X even if there are other values in the column that could add up to more for the chain overall?
If I'm understanding the problem correctly (and I very well may not), he wants the set with the highest-minimum-value? In that case, you don't really care about the big values at all, you're just trying to avoid the small ones.

Let's say that choosing X forces you to choose A in some other row, whereas choosing Y < X allows you to choose B > A in that row, raising your minimum value.
The selection is based on the next max values though not on any other column. After sorting, the chain is optimal since it consists of the maximum of each column. Now say there are two conflicting values, one of them will be changed depending on the value of the next max value in the column. One of them has to be changed since both values could not be part of the chain.

It may indeed break at some point but so far I don't see how, I think I'll write the implementation and throw it some test data.
 

Faramir

Ars Legatus Legionis
36,356
Subscriptor
Alamout":3idk0dzt said:
Faramir":3idk0dzt said:
If you think of each potential set of valid row/value picks as a chain, in the original problem I was trying to maximize the sum of the values of each link in the chain.

Now I want to find the chain in which the lowest valued link in the chain is greater than the lowest valued link in any other potential chain.
I'm not sure I understand the "chain" aspect, but the second sentence smells like Network Flow to me.

The maximum flow network problem seems to well describe the optimization condition, but I can't think of how to express the one from each column, at most one from each row rule as a flow network.
 

Alamout

Ars Legatus Legionis
27,285
^ Yeah, I was thinking about that too and I can't think of a way either. Feels too similar to ignore though.

helios01":30e8ewgz said:
It may indeed break at some point but so far I don't see how, I think I'll write the implementation and throw it some test data.
If I change Faramir's original example, I can illustrate:

Code:
{ 1.0, 3.5, -2.0 }
{ -2.1, 4.0, 3.4 }
{ 1.1, -5.3, 2.0 }
{ 3.6, 1.0, 2.0. }

Under your proposed algorithm (as I understand it), you sort each column and then look at the top values. First you choose 3.6, then you choose 4.0, then you try to choose 3.4 but run into a conflict with 4.0, so you move on to choose 2.0. Your minimum value is thus 2.0.

But clearly we can do better, if we choose 3.6, 3.5 and 3.4: both the sum and the minimum value are improved. I'm not seeing how you avoid this back-tracking situation, keeping in mind that the possible conflict might not be in adjacent columns and could involve cycles of values rather than a simple pair.
 
Alamout":1d971rt1 said:
^ Yeah, I was thinking about that too and I can't think of a way either. Feels too similar to ignore though.

helios01":1d971rt1 said:
It may indeed break at some point but so far I don't see how, I think I'll write the implementation and throw it some test data.
If I change Faramir's original example, I can illustrate:

Code:
{ 1.0, 3.5, -2.0 }
{ -2.1, 4.0, 3.4 }
{ 1.1, -5.3, 2.0 }
{ 3.6, 1.0, 2.0. }

Under your proposed algorithm (as I understand it), you sort each column and then look at the top values. First you choose 3.6, then you choose 4.0, then you try to choose 3.4 but run into a conflict with 4.0, so you move on to choose 2.0. Your minimum value is thus 2.0.

But clearly we can do better, if we choose 3.6, 3.5 and 3.4: both the sum and the minimum value are improved. I'm not seeing how you avoid this back-tracking situation, keeping in mind that the possible conflict might not be in adjacent columns and could involve cycles of values rather than a simple pair.
You wouldn't choose 2.0. You would compare the next max values of the conflicting values, so compare(3.5, 2.0), in this case you would change the 4.0 to 3.5 and leave 3.4 for column 3. The key is to look at the next max values when there conflicting values rather than changing the first conflict.

So after changing 4 to 3.5, we'd have 3.6 3.5 3.4. Say that the 3.5 conflicted with the 3.6. Then you would look at their next max values, comp(1.1, 1.0) and replace 3.6 by 1.1. And so on.
 

Alamout

Ars Legatus Legionis
27,285
You wouldn't choose 2.0. You would compare the next max values of the conflicting values, so compare(3.5, 2.0), in this case you would change the 4.0 to 3.5 and leave 3.4 for column 3. The key is to look at the next max values when there conflicting values rather than changing the first conflict.
Ah, I was not parsing your terminology ("next max values") correctly.

I just don't see how you're going to program this logic--you haven't described it in a way that sounds efficient. What happens when your next-max-value is also a conflict? Or when the top X values in a given column are all conflicts. It sounds like it blows up when the input gets bigger and more complex.

This problem has a sort of weird optimization criteria, in that there might be many solutions that have a given minimum value--without optimizing for something else as well, there's no reason to distinguish between (0.1, 0.0) and (100.0, 0.0), for instance
 

uec0

Smack-Fu Master, in training
68
helios01's method is similar to Auction Algorithm by Bertsekas -- This is somewhat popular for Linear Assignment Problem because it's very easy to implement and possibly parallelize. It generally converges quickly at first, but slows down as it gets closer to the optimum. Most of the art with Auction is tweaking "epsilon". Jonker-Volgenant does something similar before the augmentation step (Row Reduction and Column Reduction).

Basically, Auction and Auction-like steps solve the easy parts quickly, and Augmentation finishes up the rest with decent complexity.

Shortest Augmenting Path is the general Network-Flow algorithm -- Basically, for matching, you have model the problem as a bipartite graph with a source node connected to all nodes in one set, and a sink node connected to all nodes in the other set. All edges have some capacity. Then the problem is to maximize flow in the network. Shortest Augmenting Path generates the maximum matching.

I think you can tweak how the distances are calculated by using a minimum on that path rather than the sum of costs on that path.

Again, there seem to be a lot more resources on the web now. There are definitely MATLAB implementations on the Mathworks site. Jonker also has C++ implementations on his website (search "lapjv").

This is assuming just what you described before and ignoring the links/chains semantics (which may imply looking for Hamiltonian Paths).
 

Faramir

Ars Legatus Legionis
36,356
Subscriptor
My biggest problem in understanding the suggested algorithms sufficiently to use them is that I know nothing about graph theory.

If you look at the wikipedia article on the Hungarian Algorithm:
https://secure.wikimedia.org/wikipedia/ ... _algorithm
The section Matrix interpretation makes perfect sense to me. The section Settings after "The algorithm is easier to describe if we formulate the problem using a bipartite graph." and The algorithm in terms of bipartite graphs look like gobbly-gook to me.

I tried googling for some appropriate reading but all I came up with were article about adjacency and incident matrices.

Any pointers to good articles?
 

uec0

Smack-Fu Master, in training
68
This set of slides helped me a lot:
http://www.cs.princeton.edu/~wayne/kleinberg-tardos/07assignment-2x2.pdf
The duality and pricing slides can be tricky to follow -- However, the main take-away is that duality/pricing is used to ensure costs are always positive. This allows use of Dijkstra's Algorithm.

Jonker's C++ code can be found all around the Internet. This is one location:
http://figment.csee.usf.edu/~psound.../src/textrecognition/linearassignment/lap.cpp
The Augmentation phase starts at line 163. His implementation is a little tricky, but it is essentially a specialized Dijkstra's Algorithm. You can probably pick out where he does the duality/pricing trick as described by Kleinberg-Tardos.


Some basic graph theory relevant to Linear Assignment Problem (LAP)...

Start with two distinct sets of nodes L and R. This is pretty boring so let's add a set of edges E. In formal terms, this graph can be described as:
G(L union R, E)

To model LAP, we restrict E such that all edges in E must be directed and must connect across L and R. Directed edges are one-way so that when we follow an edge, we can only move from L to R. Also, since edges only connect between L and R, the graph is called a bipartite graph.

This is a simple example illustration:
Code:
L    R
-    -
a--->d
b--->e
c--->f

A matching is simply a set of edges which do not share nodes. A maximum matching is the largest possible matching. And a perfect matching is a maximum matching which includes all nodes. For the above example, the maximum perfect matching is:
{ (a,d), (b,e), (c,f) }

Now we can add weights/distances/costs to the edges:
Code:
L    R
-    -
a-1->d
b-2->e
c-3->f

Then finding the solution to LAP can be modeled as finding the maximum matching with minimum weight. You can convert between costs and score by simply negating your values.

I can't speak for anyone else, but I found this subject tricky to pick up, even with a grasp of graph theory.
 
Status
Not open for further replies.