Java 数组拆分和重新连接。丢失的元素

发布于 2024-11-30 12:49:51 字数 2161 浏览 1 评论 0原文

我正在尝试实现 TSP 双桥移动,其工作原理如下: 给定城市的排列(游览),它将排列分成 4 部分,并以不同的顺序重新连接这些部分。 例如 perm=[a,b,c,d] ---> [a、d、c、b]。

在下面的方法中,我有 temp[] 数组,其中包含排列的城市。我选择 3 个随机数并将数组分成 4 部分

 public void doubleBridge() {
    City[] temp = this.permArray; // it's a tour so the first element equals the last temp[0]=temp[temp.length-1]
    Random  random = new Random();      
    int pos1  = 1+ random.nextInt(temp.length/4);
    int pos2 = pos1 +  1 + random.nextInt(temp.length/4);
    int pos3 = pos2 + 1 + random.nextInt(temp.length/4);
    System.out.println("\nPositions chosen : "+pos1+" "+pos2+" "+pos3);

    City[] part1 = new City[pos1+1];
    part1 = Arrays.copyOfRange(temp, 0, pos1);

    City[] part2 = new City[pos2-pos1-1];
    part2= Arrays.copyOfRange(temp, pos1+1, pos2);

    City[] part3 = new City[pos3-pos2-1];
    part3= Arrays.copyOfRange(temp, pos2+1, pos3);

    City[] part4 = new City[temp.length-1-pos3-1];
    part4= Arrays.copyOfRange(temp, pos3+1, temp.length);

    //City[] newTemp = new City[temp.length];

    System.out.println("\npart1");
    for (City c: part1) {
        System.out.print(c.getId()+" ");
    }


    System.out.println("\npart2");
    for (City c: part2) {
        System.out.print(c.getId()+" ");
    }

    System.out.println("\npart3");
    for (City c: part3) {
        System.out.print(c.getId()+" ");
    }

    System.out.println("\npart4");
    for (City c: part4) {

        System.out.print(c.getId()+" ");
    }
    /*newTemp = concatAll(part1, part2, part3, part4);
    this.permArray = newTemp;
    this.computePermutationLength();*/
}

运行程序后打印我得到的部分。

{38, 18, 27, 2, 20, 35, 1, 42, 50, 22, 52, 36, 44, 31, 19, 33, 3, 25, 29, 49, 12, 4, 7, 30, 43 , 24, 48, 45, 26, 39, 11, 15, 21, 34, 28, 8, 13, 51, 41, 17, 10, 37, 46, 32, 16, 23, 14, 5, 9, 6, 47, 40, 38, }长度: 23511950

选择的位置: 3 12 24

第 1 部分 38 18 27

第 2 部分 20 35 1 42 50 22 52 36

第三部分 31 19 33 3 25 29 49 12 4 7 30

第4部分 24 48 45 26 39 11 15 21 34 28 8 13 51 41 17 10 37 46 32 16 23 14 5 9 6 47 40 38

问题是丢失了 4 个元素。例如:初始排列中的元素“2”在第 1 部分或第 2 部分中都不存在。

那么问题出在哪里呢?

I'm trying to implement the TSP double bridge move which works as follows:
Given a permutation(tour) of cities it splits the permutation in 4 parts and reconnects these parts in different order.
e.g. perm=[a,b,c,d] ---> [a,d,c,b].

In the following method i have the temp[] array which contains the pemutation's cities. i choose 3 random number and split the array in 4 parts

 public void doubleBridge() {
    City[] temp = this.permArray; // it's a tour so the first element equals the last temp[0]=temp[temp.length-1]
    Random  random = new Random();      
    int pos1  = 1+ random.nextInt(temp.length/4);
    int pos2 = pos1 +  1 + random.nextInt(temp.length/4);
    int pos3 = pos2 + 1 + random.nextInt(temp.length/4);
    System.out.println("\nPositions chosen : "+pos1+" "+pos2+" "+pos3);

    City[] part1 = new City[pos1+1];
    part1 = Arrays.copyOfRange(temp, 0, pos1);

    City[] part2 = new City[pos2-pos1-1];
    part2= Arrays.copyOfRange(temp, pos1+1, pos2);

    City[] part3 = new City[pos3-pos2-1];
    part3= Arrays.copyOfRange(temp, pos2+1, pos3);

    City[] part4 = new City[temp.length-1-pos3-1];
    part4= Arrays.copyOfRange(temp, pos3+1, temp.length);

    //City[] newTemp = new City[temp.length];

    System.out.println("\npart1");
    for (City c: part1) {
        System.out.print(c.getId()+" ");
    }


    System.out.println("\npart2");
    for (City c: part2) {
        System.out.print(c.getId()+" ");
    }

    System.out.println("\npart3");
    for (City c: part3) {
        System.out.print(c.getId()+" ");
    }

    System.out.println("\npart4");
    for (City c: part4) {

        System.out.print(c.getId()+" ");
    }
    /*newTemp = concatAll(part1, part2, part3, part4);
    this.permArray = newTemp;
    this.computePermutationLength();*/
}

After running the programm an printing the parts i get.

{38, 18, 27, 2, 20, 35, 1, 42, 50, 22, 52, 36, 44, 31, 19, 33, 3, 25, 29, 49, 12, 4, 7, 30, 43, 24, 48, 45, 26, 39, 11, 15, 21, 34, 28, 8, 13, 51, 41, 17, 10, 37, 46, 32, 16, 23, 14, 5, 9, 6, 47, 40, 38, }LENGTH: 23511950

Positions chosen : 3 12 24

part1
38 18 27

part2
20 35 1 42 50 22 52 36

part3
31 19 33 3 25 29 49 12 4 7 30

part4
24 48 45 26 39 11 15 21 34 28 8 13 51 41 17 10 37 46 32 16 23 14 5 9 6 47 40 38

The problem is that 4 elements are lost. for example: element "2" from the initial permutation doens't exist either in part1 or part2.

So where's the problem?

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

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

发布评论

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

评论(4

德意的啸 2024-12-07 12:49:51

目前尚不清楚您是否希望“2”成为part1或part2的一部分,但请看一下代码:

City[] part1 = new City[pos1+1];
part1 = Arrays.copyOfRange(temp, 0, pos1);

City[] part2 = new City[pos2-pos1-1];
part2= Arrays.copyOfRange(temp, pos1+1, pos2);

这里,pos1处的元素不是part1的一部分,因为copyOfRange独占。这是典型的 Java API - 例如 "012345".substring(0, 3) 将为您提供“012”,而不是“0123”。

它不是 part2 的一部分,因为您pos1+1 开始

基本上,两个参数应该匹配。尚不清楚它们是否应该都是 pos1 + 1 还是 pos1,但它们应该匹配。

It's not clear whether you want "2" to be part of part1 or part2, but look at the code:

City[] part1 = new City[pos1+1];
part1 = Arrays.copyOfRange(temp, 0, pos1);

City[] part2 = new City[pos2-pos1-1];
part2= Arrays.copyOfRange(temp, pos1+1, pos2);

Here, the element at pos1 is not part of part1 because the last argument of copyOfRange is exclusive. This is typical of Java APIs - for example "012345".substring(0, 3) will give you "012", not "0123".

It's not part of part2 because you're starting with pos1+1.

Basically, the two arguments should match. It's not clear whether they should both be pos1 + 1 or pos1, but they should match.

我做我的改变 2024-12-07 12:49:51

来自 Arrays.copyOfRange docs,参数为:

  • original - 要从中复制范围的数组
  • - 要复制的范围的初始索引,包括
  • -要复制的范围的最终索引,不包括在内。 (该索引可能位于数组之外。)

因此您应该有以下四行:(起始/起始索引不应递增)

City[] part1 = new City[pos1+1];
part1 = Arrays.copyOfRange(temp, 0, pos1);

City[] part2 = new City[pos2-pos1-1];
part2= Arrays.copyOfRange(temp, pos1, pos2);

City[] part3 = new City[pos3-pos2-1];
part3= Arrays.copyOfRange(temp, pos2, pos3);

City[] part4 = new City[temp.length-1-pos3-1];
part4= Arrays.copyOfRange(temp, pos3, temp.length);

问候,
史蒂芬

From Arrays.copyOfRange docs, the parameters are :

  • original - the array from which a range is to be copied
  • from - the initial index of the range to be copied, inclusive
  • to - the final index of the range to be copied, exclusive. (This index may lie outside the array.)

So you should have the following four lines : (start/from indexes should not increment)

City[] part1 = new City[pos1+1];
part1 = Arrays.copyOfRange(temp, 0, pos1);

City[] part2 = new City[pos2-pos1-1];
part2= Arrays.copyOfRange(temp, pos1, pos2);

City[] part3 = new City[pos3-pos2-1];
part3= Arrays.copyOfRange(temp, pos2, pos3);

City[] part4 = new City[temp.length-1-pos3-1];
part4= Arrays.copyOfRange(temp, pos3, temp.length);

Regards,
Stéphane

冷弦 2024-12-07 12:49:51

尝试 Arrays.copyOfRange(temp, pos1, pos2); 等,而不在开始参数中添加 1。

Try Arrays.copyOfRange(temp, pos1, pos2); etc. without adding 1 to the start parameter.

¢蛋碎的人ぎ生 2024-12-07 12:49:51

在上面的代码中,

City[] part1 = new City[pos1+1];
part1 = Arrays.copyOfRange(temp, 0, pos1);

City[] part2 = new City[pos2-pos1-1];
part2= Arrays.copyOfRange(temp, pos1+1, pos2);

在part1= Arrays.copyOfRange(temp, 0, pos1);中,数组被复制到pos-1
而在第2部分= Arrays.copyOfRange(temp, pos1+1, pos2); , pos1 处的元素被忽略,因为部分复制的数组是从 pos1+1 开始的

In the above code

City[] part1 = new City[pos1+1];
part1 = Arrays.copyOfRange(temp, 0, pos1);

City[] part2 = new City[pos2-pos1-1];
part2= Arrays.copyOfRange(temp, pos1+1, pos2);

In part1= Arrays.copyOfRange(temp, 0, pos1);, the arrays are being copied till pos-1
while in part2= Arrays.copyOfRange(temp, pos1+1, pos2); , the element at pos1 is being left out as the array being copied in part is starting from pos1+1

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