开发者

Algorithm to optimise utilisation of capacity

开发者 https://www.devze.com 2023-03-10 06:05 出处:网络
I\'ve set myself a \"simple\" programming challenge in C# to optimise capacity. I didn\'t do to well on my first attempt (as described further down) so was looking to see if there is a standardised al

I've set myself a "simple" programming challenge in C# to optimise capacity. I didn't do to well on my first attempt (as described further down) so was looking to see if there is a standardised algorithm for doing this, without using AI/Heuristic techniques as i do not know them at all. I believe there is a known method for doing this as the problem would probably apply to common situations like load balancing of CPUs and other resources.

The problem I set myself. I have 3 co开发者_如何学编程ntainers.

  • Container 1 - Capacity 15
  • Container 2 - Capacity 30
  • Container 3 - Capacity 15

The containers all start empty, so have full capacity.

I also have 20 items. For the sake of a long list I will not list them all. But they are simple and can be like

  • Item 1 - Amount 3
  • Item 2 - Amount 8
  • Item 3 - Amount 1
  • Item 4 - Amount 1
  • Item 5 - Amount 5
  • and so on .........

What i want to do is to fit all the items in the fewest number of containers without breaking the constraints, i.e. overfilling the capacity.

  • Container 1 - Items 2, 1
  • Container 2 - Items 3, 4, 5
  • and so on ..............

I'm looking for a solution, although obviously the more efficient the solution the better. Eventually I'll make the containers and items auto generate, and probably even add more properties say (weight & amount). I know there are a finite amount of solutions even if you randomly generate them, the idea is to find the best, or close to the best is a reasonable time. My initial attempt was first to define container and item objects, then to randomly allocate items to containers, then try to optimise by finding available space in those close to capacity and fill them with items from containers that are not as full. But it didn't work out to well. The simplest solution i believe is to allocated items in order of those with the largest amount, then use small amounts to fill up the gaps. I stayed away from this initial as I feel this would have drawbacks if I added in more constraints, i.e. amount is high (20), but say weight a new constraint is only (1). I may then end up getting a container full with amount but barely as weight usage leading to over containers getting to heavy to hold any more.

Whilst I'm focusing on the problem more so than the language I'm using C# .Net 4.0, so if you have ideas feel free to use frameworks like LINQ etc.

If I've posted a problem with a well known solution I have missed, feel free to point me right to it. But I am interested in any solutions you can come up with. i look forward to reading the replies.


You've chosen a very hard challenge:

  • The Knapsack problem

and this problem is NP-complete which means that the only known correct solution to the problem invokes examining all possible combinations.

You can try the Greedy approximation algorithm described in the first link if you can settle for a heuristic/approximate approach.

0

精彩评论

暂无评论...
验证码 换一张
取 消