伪代码的 Java 模拟退火

发布于 2024-10-26 02:12:48 字数 1274 浏览 6 评论 0原文

我目前正在开发一个项目(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 编写时的外观的基本想法 - 一旦我知道它在正确的语法中应该是什么样子,我就能够将它调整到我的代码中,但我不能看起来克服这个特殊的障碍。

I am currently working on a project (TSP) and am attempting to convert some simulated annealing pseudocode into Java. I have been successful in the past at converting pseudocode into Java code, however I am unable to convert this successfully.

The pseudocode is:

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

If somebody could show me a basic mark-up of this in Java I would be extremely grateful - I just can't seem to figure it out!

I am working across multiple classes using a number of functions (which I will not list as it is irrelevant for what I am asking). I already have a smallChange() method and a fitness function - could there be a chance that I would need to create a number of different versions of said methods? For example, I have something like:

public static ArrayList<Integer> smallChange(ArrayList<Integer> solution){

//Code is here.

}

Could I possibly need another version of this method which accepts different parameters? Something along the lines of:

public static double smallChange(double d){

//Code is here.

}

All I require is a basic idea of how this would look when written in Java - I will be able to adapt it to my code once I know what it should look like in the correct syntax, but I cannot seem to get past this particular hurdle.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何止钟意 2024-11-02 02:12:48

基本代码应该如下所示:

public class YourClass {
  public static Solution doYourStuff(double startingTemperature, int numberOfIterations, double coolingRate) {
    double t = startingTemperature;
    Solution x = createRandomSolution();
    double ti = t;

    for (int i = 0; i < numberOfIterations; i ++) {
      double f = calculateFitness(x);
      Solution mutatedX = mutate(x);
      double newF = calculateFitness(mutatedX);
      if (newF < f) {
        double p = PR(); // no idea what you're talking about here
        if (p > UR(0, 1)) { // likewise
          // then do nothing
        } else {
          x = mutatedX;
        }
        ti = t * coolingRate;
      }
    }
    return x;
  }

  static class Solution {
    // no idea what's in here...
  }
}

现在就想要不同版本的smallChange()方法而言 - 完全可行,但是您必须阅读一点继承

The basic code should look like this:

public class YourClass {
  public static Solution doYourStuff(double startingTemperature, int numberOfIterations, double coolingRate) {
    double t = startingTemperature;
    Solution x = createRandomSolution();
    double ti = t;

    for (int i = 0; i < numberOfIterations; i ++) {
      double f = calculateFitness(x);
      Solution mutatedX = mutate(x);
      double newF = calculateFitness(mutatedX);
      if (newF < f) {
        double p = PR(); // no idea what you're talking about here
        if (p > UR(0, 1)) { // likewise
          // then do nothing
        } else {
          x = mutatedX;
        }
        ti = t * coolingRate;
      }
    }
    return x;
  }

  static class Solution {
    // no idea what's in here...
  }
}

Now as far as wanting different versions of smallChange() method - totally doable, but you have to read up on inheritance a little bit

ぇ气 2024-11-02 02:12:48

您可以将您的答案与教科书提供的代码进行比较
人工智能是一种现代方法

You can compare your answer to the code provided for the textbook
Artificial Intelligence a Modern Approach.

尽揽少女心 2024-11-02 02:12:48

此外,这里还提供了一种基于 Java 的模拟退火教学方法(带有示例代码):

Neller、Todd。 教学随机局部搜索,I. Russell 和 Z. Markov 编辑。第 18 届国际 FLAIRS 会议 (FLAIRS-2005) 会议记录,佛罗里达州克利尔沃特海滩,2005 年 5 月 15-17 日,AAAI Press,第 8-13 页。

相关资源、参考资料和演示位于:http://cs.gettysburg。 edu/~tneller/resources/sls/index.html

Also, a Java-based approach to teaching simulated annealing (with sample code) is here:

Neller, Todd. Teaching Stochastic Local Search, in I. Russell and Z. Markov, eds. Proceedings of the 18th International FLAIRS Conference (FLAIRS-2005), Clearwater Beach, Florida, May 15-17, 2005, AAAI Press, pp. 8-13.

Related resources, references, and demos are here: http://cs.gettysburg.edu/~tneller/resources/sls/index.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文