tags:

views:

321

answers:

2

What kind of algorithm is this, I know pretty much nothing but this is what I'm trying to do in code... I have class 'Item', properties int A and int B -- I have multiple lists of List<Item> with a random amount of Item in each list, incosistent with any other List. I must choose 1 item from each list to get the highest possible value of the sum Item.A while conforming that the sum of Item.B must also be at minimum a certain number. In the future there might also be another property Item.C to conform to that the sum must be equal to a certain number. I have no idea how to write this :(

So to put it this way;

class Item
  int A
  int B
  int C

I have a 10x different List<Item> each with a random number of Item inside

We must find the exactly the best combination to have

a) Highest sum of Item.A
b) Constraint that the sum of Item.B must be higher than X
c) Constraint that the sum of Item.C must be equal to X

I have no idea how to code this to be fast and efficient. :(

+1  A: 

I posted this question as an unregistered user and after clicking register, it didn't associate my unregistered user with my registered user, nice =/

In regards to the comment by van:

Typically there will be about 14 lists or so Within each list there will be usually around 5-15 'Items' Each item has those 3 properties. We must exactly 1 item from each list. We are looking for the maximum value of PropertyA when we calculate the sum of all PropertyA after choosing one item from each list The constraints are PropertyB and PropertyC which the chosen combination must confirm too, once again using the sum of the values across the combination.

It must also be the most optimal solution, not an approximation.

ShaunO
14 lists, ~ten items in each, choose exactly one from each, that's roughly a hundred trillion possible combinations. Seems tough to find the optimal one.
Eric Lippert
@ShaunO: would it be possible to post some sample set? it is really random? from what range? 1-10 or 1-10^10? this information especially would help with the Equality constraint on PropertyC? Do you really need Equality constraint, as it makes it much more complex?
van
Property A is typically 100-300~, Property B is typically 0-70, Property C is typically 0-3. It is necessary that in the particular combination Property C is equal to 3. When I get home tonight I will post some real-world sample data. Right now I'm using nested foreach loops but with the current data it is 4.2 trillion possibilites.
ShaunO
What are the data types? Always integer or can they be float? Also, can they be negative?
JP Alioto
They are always integer and they will never be negative.
ShaunO
+3  A: 

As mentionend in my comment, this is a Binary Programming problem, which can be cast as a multi-dimensional Knapsack problem. I would first try to solve it with an off-the-shelf Mixed Integer Programming (MIP) solver like the one suggested by Lieven in one of his comments (lpSolve), given that you "only" have got some 100-200 binary variables. You might have to play a little bit around with the parameters. Some MIP solvers allow you to add search heuristics, which might be helpful. Given your constraints, I must admit I don't have a feeling how long a standard MIP solver will take, but I wouldn't hold my breath.

If a mixed-integer programming solver is not fast enough for you, you want to look at some more specialised algorithms. For your problem, the ones presented in Knapsack Problems, chapter 11.10 on the multiple-choice Knapsack problem (almost exactly your problem) and chapter 9 are relevant.

Edit: based on your comments, the good news is that your data ranges are pretty good and the problem seems solvable in a reasonable time. This paper presents an algorithm that according to the authors solves problems of your size within seconds (see section 4.4 and 5.1). The bad news is that it contains a lot of math...

stephan
Thanks for the references, reading into multiple-choice Knapsack problem right now.
ShaunO