从伪代码中进行Java模拟退火
我目前正在研究一个项目(TSP),并试图将一些模拟退火伪代码转换为Java。我过去在将伪代码转换为Java代码方面取得了成功,但是我无法成功转换它。
伪代码是:
T0(T and a lowercase 0) Starting temperature
Iter Number of iterations
λ The cooling rate
1. Set T = T0 (T and a lowercase 0)
2. Let x = a random solution
3. For i = 0 to Iter-1
4. Let f = fitness of x
5. Make a small change to x to make x’
6. Let f’ = fitness of new point
7. If f’ is worse than f then
8. Let p = PR(f’, f, Ti (T with a lowercase i))
9. If p > UR(0,1) then
10. Undo change (x and f)
11. Else
12. Let x = x’
13. End if
14. Let Ti(T with a lowercase i) + 1 = λTi(λ and T with a lowercase i)
15. End for
Output: The solution x
如果有人能用Java向我展示这个基本标记,我将非常感激 - 我似乎无法弄明白!
我正在使用许多函数来处理多个类(我不会列出,因为它与我所要求的无关)。我已经有一个smallChange()
方法和一个fitness
函数 - 我是否有可能需要创建所述方法的许多不同版本?例如,我有类似的东西:
public static ArrayList<Integer> smallChange(ArrayList<Integer> solution){
//Code is here.
}
我可能需要另一个接受不同参数的方法吗?有点像:
public static double smallChange(double d){
//Code is here.
}
我所需要的只是基于Java用Java编写时的基本概念 - 一旦我知道它在正确的语法中应该是什么样子,我将能够将它调整到我的代码中,但我似乎无法克服这个特殊的障碍。
谢谢。
米克
没有找到相关结果
已邀请:
3 个回复
臀博
现在只要想要不同版本的smallChange()方法 - 完全可行,但你必须稍微阅读一下继承
马口
葛瞎说漓