4

I'm in the process of learning about simulated annealing algorithms and have a few questions on how I would modify an example algorithm to solve a 0-1 knapsack problem.

I found this great code on CP:

http://www.codeproject.com/KB/recipes/simulatedAnnealingTSP.aspx

I'm pretty sure I understand how it all works now (except the whole Bolzman condition, as far as I'm concerned is black magic, though I understand about escaping local optimums and apparently this does exactly that). I'd like to re-design this to solve a 0-1 knapsack-"ish" problem. Basically I'm putting one of 5,000 objects in 10 sacks and need to optimize for the least unused space. The actual "score" I assign to a solution is a bit more complex, but not related to the algorithm.

This seems easy enough. This means the Anneal() function would be basically the same. I'd have to implement the GetNextArrangement() function to fit my needs. In the TSM problem, he just swaps two random nodes along the path (ie, he makes a very small change each iteration).

For my problem, on the first iteration, I'd pick 10 random objects and look at the leftover space. For the next iteration, would I just pick 10 new random objects? Or am I best only swapping out a few of the objects, like half of them or only even one of them? Or maybe the number of objects I swap out should be relative to the temperature? Any of these seem doable to me, I'm just wondering if someone has some advice on the best approach (though I can mess around with improvements once I have the code working).

Thanks!

Mike

7
  • If you always put one object into one sack, you simply need to find the 10 biggest objects that fit. I do not think that you mean that. Could you make this clearer?
    – Svante
    Aug 2, 2010 at 9:22
  • It's actually a multidimensional knapsack problem, with a scoring system based on various user variables. The details are rather complicated to explain, however rest assured it's nothing that can be determined in O(n) like you mention.. Aug 2, 2010 at 9:45
  • Well, as described before, the problem seems to be overreduced then. If "take the 10 biggest objects that fit" is not a solution, you need to restate the problem. Otherwise, I do not think that there can be a sensible answer.
    – Svante
    Aug 2, 2010 at 11:40
  • Here, take a look at my generic SA implementation (parts of it). It's in java, but the concepts should be transferable. stackoverflow.com/a/18657788/1809463
    – mike
    Sep 6, 2013 at 12:41
  • @mike - Thanks, but I finished this years ago.. If you're interested in what this is used for, check out the what can I make? section of my site.. Sep 6, 2013 at 15:52

2 Answers 2

3

With simulated annealing, you want to make neighbour states as close in energy as possible. If the neighbours have significantly greater energy, then it will just never jump to them without a very high temperature -- high enough that it will never make progress. On the other hand, if you can come up with heuristics that exploit lower-energy states, then exploit them.

For the TSP, this means swapping adjacent cities. For your problem, I'd suggest a conditional neighbour selection algorithm as follows:

  1. If there are objects that fit in the empty space, then it always puts the biggest one in.
  2. If no objects fit in the empty space, then pick an object to swap out -- but prefer to swap objects of similar sizes.

That is, objects have a probability inverse to the difference in their sizes. You might want to use something like roulette selection here, with the slice size being something like (1 / (size1 - size2)^2).

3

Ah, I think I found my answer on Wikipedia.. It suggests moving to a "neighbor" state, which usually implies changing as little as possible (like swapping two cities in a TSM problem)..

From: http://en.wikipedia.org/wiki/Simulated_annealing

"The neighbours of a state are new states of the problem that are produced after altering the given state in some particular way. For example, in the traveling salesman problem, each state is typically defined as a particular permutation of the cities to be visited. The neighbours of some particular permutation are the permutations that are produced for example by interchanging a pair of adjacent cities. The action taken to alter the solution in order to find neighbouring solutions is called "move" and different "moves" give different neighbours. These moves usually result in minimal alterations of the solution, as the previous example depicts, in order to help an algorithm to optimize the solution to the maximum extent and also to retain the already optimum parts of the solution and affect only the suboptimum parts. In the previous example, the parts of the solution are the parts of the tour."

So I believe my GetNextArrangement function would want to swap out a random item with an item unused in the set..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.