简体   繁体   中英

What kind of Algorithm am I looking for to combine quantities?

I have been stuck on this problem now for 8 weeks and I think that I almost have a solution however the last bit of math is racking my mind. I will try to explain a simple problem that requires a complex solution. I am programing in C#.net MVC Web Project. Here is the situation.

I have an unknown group of quantities incoming to look for like items. Those like items share a max level to make it a full box. Here is an example of this:

Revision****** This is the real world case I have many, let say candy, orders coming in to a company.

Qty Item     MaxFill Sold-To DeliverNumber
60  candy#14 26      Joe     1
1   candy#12 48      Jim     2
30  candy#11 48      Jo      3
60  candy#15 48      Tom     4
6   candy#8  48      Kat     5
30  candy#61 48      Kim     6
44  candy#12 48      Jan     7
10  candy#12 48      Yai     8
10  candy#91 48      Jun     9
55  candy#14 26      Qin     10
30  candy#14 26      Yo      11
40  candy#14 26      Moe     12

in this list I am looking for like candy items to combine to make all the full boxes of candy that I can based off the MaxFill number. Here we see the like items are:

Qty Item     MaxFill Sold-To DeliverNumber
60  candy#14 26      Joe     1
55  candy#14 26      Qin     10
30  candy#14 26      Yo      11
40  candy#14 26      Moe     12

1   candy#12 48      Jim     2
44  candy#12 48      Jan     7
10  candy#12 48      Yai     8

Now lets take the first set of numbers for candy#14.

I know that the total of candy#14 is 185 and I can get 7 full boxes of 26 with one box having only 3 in the last box. So how do I do this with the values that I have without losing the information of the original order. So this is how I am working it out right now

See below End of Revision****** Like candy#14 max fill level is 26.

Like candy#14 quantities: 60 55 30 40

Now I already have a recursive function to break these down to the 26 level and is working fine. I feel that I need another recursive function to deal with the remainders that come out of this. As you can see most of the time there will be remainders from any given list but those remainders could total up to another full box of 26.

60 = 26+26+8
55 = 26+26+3
30 = 26+4
40 = 26+14

The 8,3,4,14 = 29 so I can get another 26 out of this. But in the real unknown world I could have the remainders come up with a new set of remainders that could repeat the same situation. To make this even more complicated I have to save the data that is originality with the 60,55,30,40 that is carried with it such as who it was sold to and delivery number. This will also be helpful with knowing how the original amount was broken down and combined together.

from the 8,3,4,14 the best way that I was think to add to that value is to take the 8,4,14 this will give me the 26 that I am looking for and I would not have to split any value because 3 is the remainder and I could save all other data without issue. However this just works in this situation only. If I go in a linear motion 8+3+4=15 so I would have to take 11 from the next value 14 with a remainder of 3.

In reading about different algorithms I was thinking that this might fall into the NP,NP-Complete,NP-Hard category. But with all the situations it is very technical and not a lot of real world scenarios are to be found.

Any suggestions would help here if I should go through the list of number to find the best combinations to reach the 26 or if the linear progression and splitting of the next value is the best solution. I know that I can solve to get how many full boxes I could get from the remainders and what the left over amount would be such as 8+3+4+14=29 which would give me 1, 26 and 1, 3 but I have no idea about the math in a recursive way to solve this. I have this much done and I "feel" that this is on the right track but can't see how to adjust to make this work with the linear or "test every possible combination".

    public static void Main(string[] args)
{
                var numbers = new List<int>() { 8, 3, 4, 14 };
                var target = 26;
                sum_up(numbers, target);
}           
private static void sum_up(List<int> numbers, int target)
        {
            sum_up_recursive(numbers, target, new List<int>());
        }

        private static void sum_up_recursive(List<int> numbers, int target, List<int> partial)
        {
            int s = 0;
            foreach (int x in partial) s += x;

            if (s == target)
            {
                var outputtext = "sum(" + string.Join(",", partial.ToArray()) + ")=" + target;
            }

            if (s >= target)
                return;

            for (int i = 0; i < numbers.Count; i++)
            {
                List<int> remaining = new List<int>();
                int n = numbers[i];
                for (int j = i + 1; j < numbers.Count; j++) remaining.Add(numbers[j]);

                List<int> partial_rec = new List<int>(partial);
                partial_rec.Add(n);
                sum_up_recursive(remaining, target, partial_rec);
            }
        }

I wrote sample project in javascript. Please check my repo.

https://github.com/panghea/packaging_sample

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