写作模拟退火算法0-1背包在C#算法、背包

2023-09-11 04:17:05 作者:旧巷里的少年郎

我在学习模拟退火算法的过程,对我这样修改的例子算法的一些问题解决0-1背包问题。

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.

我发现CP这个伟大的code:

I found this great code on CP:

HTTP://www.$c$cproject.com/KB/食谱/ simulatedAnnealingTSP.aspx

我是pretty的肯定,我明白这一切,现在是如何工作的(除非整个Bolzman条件,就我而言是黑魔法,虽然我明白,对于逃走局部最优,显然这正是这么做的) 。我想重新设计,这解决了0-1 knapsack-ISH的问题。基本上我把一只5000的对象在10麻袋,需要优化最少的未使用的空间。实际的分数我分配到一个解决方案更复杂一点,但没有相关的算法。

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.

这似乎很容易。这意味着退火()函数将是基本相同。我不得不实施GetNextArrangement()函数来满足我的需要。在TSM问题,他只是交换两个随机节点沿路径(即,他提供了一个非常小的变化每一次迭代)。

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).

有关我的问题,在第一次迭代中,我会选择10个随机的对象,并期待在剩余空间。对于下一个迭代,我会随便挑了10个新的随机对象?还是我最好只换了几个对象,如其中一半或仅甚至其中的一个?或者,也许我换出对象的数量相对应的温度是多少?所有的这些似乎可行对我来说,我只是想知道,如果有人对最好的方法了一些建议(虽然我惹周围的改进,一旦我有code工作)。

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).

谢谢!

迈克

推荐答案

通过模拟退火,你想使邻国接近的能量成为可能。如果邻居有显著更大的能量,那么它将只是从来没有跳到他们没有很高的温度 - 足够高的,它永远不会取得进展。在另一方面,如果你能想出了利用低能量状态启发式,然后利用它们。

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.

对于TSP,这意味着交换相邻的城市。对于你的问题,我建议有条件的邻居选择算法如下:

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

如果有适合在空旷的空间物体,那么它总是把最大的一个在 如果没有对象适合在空白区域,然后选择一个对象,以换出 - 但preFER交换类似大小的物体。

即,对象具有概率逆,以它们的尺寸差。你可能想使用类似轮盘赌选择这里,与片尺寸为类似。(1 /(尺寸1 - size2个)^ 2)

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).