简体   繁体   中英

What kind of algorithm is this?

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. :(

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 ( DOI in case the link vanishes) 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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM