简体   繁体   English

Java 2D数组-输入值

[英]Java 2D array - input values

Im implementing 2 algorithms for the TSP which uses a class which includes the routes, their cost, etc. At the minute it uses random values which is fine, although I now need to compare the algorithms so to make this fair I need to make the inputs the same (which is obviously unlikely to happen when using random inputs!) The issue im having is I dont know how to change it from random values to inserting pre-determined values into the 2D array, not just that but I also dont know how to calculate the costs of these values. 我为TSP实现了2种算法,该算法使用包含路由,它们的成本等的类。虽然它现在需要比较算法,但为了使这种公平起见,我需要使用输入相同的东西(使用随机输入时,这显然不太可能发生!)我遇到的问题是我不知道如何将其从随机值更改为将预定值插入2D数组,不仅如此,我也不知道如何计算这些值的成本。


Randomly generates node values: 随机生成节点值:

Random rand = new Random();
            for (int i=0; i<nodes; i++) {
                for (int j=i; j<nodes; j++) {
                    if (i == j)
                        Matrix[i][j] = 0;
                    else {
                        Matrix[i][j] = rand.nextInt(max_distance);
                        Matrix[j][i] = Matrix[i][j];
                    }
                }
            }

Im assuming for the above a declare a matrix of say [4][4] and then int matrix [][] = insert values ? 我假设上面的声明声明说[4] [4]的矩阵,然后int矩阵[] [] = 插入值

I do not help with some other sections of this class but I think I need to make sure this part is right before asking anymore! 我对本课程的其他部分无济于事,但我想我需要确保这部分正确之后再问!

Thanks a lot in advance! 在此先多谢!

You could set a seed instead for each random number generator therefore guaranteeing that for each implementation you test, the same sequence of pseudo-random numbers is being created. 您可以为每个随机数生成器设置一个种子,因此可以保证对于您测试的每个实现,都将创建相同的伪随机数序列。

This would save the effort of manually entering lots of values. 这样可以省去手动输入大量值的工作。

Edit to show seed method: 编辑以显示种子方法:

Random r = new Random(56);

Every time r is created with the seed of 56 it will produce the exact same sequence of random numbers. 每次使用56的种子创建r时,都会产生完全相同的随机数序列。 Without a seed I believe the seed is defaulted to the system time (giving the illusion of truly random numbers). 没有种子,我相信种子默认为系统时间(给人以真正随机数的错觉)。

you can do initialization of 2D array like this: 您可以像这样对2D数组进行初始化:

    double matrix[][] = { { v1, v2, ..., vn }, { x1, x2, ..., xn }, ..., { y1, y2, ..., yn } };

where each inner {} represents the outter (first) index and each inner element represents the innermost (second) intex. 其中每个内部{}表示外部(第一)索引,每个内部元素表示最内部(第二)整数。

Example: to acess element x1 you do this: 示例:要访问元素x1,请执行以下操作:

    matrix[1][0];

This is the answer that you asked for, but I still think that it's better to use the same set of random values for both algorithms, Jon Taylor showed a good way for doing that. 这是您所要求的答案,但是我仍然认为最好对两种算法使用相同的随机值集,Jon Taylor展示了一种很好的方法。 The code to set the seed looks like this: 设置种子的代码如下所示:

    int seed = INTEGER_VALUE;
    Random rand = new Random(seed);

this way you will ever get the same set of values. 这样,您将获得相同的一组值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM