简体   繁体   English

生成随机顺序,但在Java中有约束

[英]generate random order but with constraint in java

When I put a integer list, how can I generate another random order but with constraint? 当我放置一个整数列表时,如何生成另一个具有约束的随机顺序?

For example, I put integer 1, 2, 3, 4 into the collection and when I try to print result like be "1 2 3 4","1 2 4 3","1 3 2 4" ,"2 1 3 4",or "2 1 4 3"(1 must before 3, 2 must before 4) 例如,我将整数1、2、3、4放入集合中,当我尝试打印结果时,例如“ 1 2 3 4”,“ 1 2 4 3”,“ 1 3 2 4”,“ 2 1 3” 4”或“ 2 1 4 3”(1必须在3之前,2必须在4之前)

thanks in advance 提前致谢

One thing you can consider is swapping elements at random. 您可以考虑的一件事是随机交换元素。 You could pick a random position in your collection, then swap the element in that position with the next element. 您可以在集合中选择一个随机位置,然后将该位置中的元素与下一个元素交换。 This way, you can prevent swapping 1 with 3, or 2 with 4. You can do this repetitively, until the numbers are properly scrambled: 这样,您可以防止1与3交换,或2与4交换。您可以重复执行此操作,直到数字正确地加扰为止:

[1, 2, 3, 4] random number is 0, swap with element at position 1. [1, 2, 3, 4]随机数为0,与位置1的元素交换。

[2, 1, 3, 4] random number is 1, swap with element at position 2. [2, 1, 3, 4]随机数为1,与位置2的元素交换。

elements are 1 and 3, so don't swap. 元素是1和3,所以不要交换。

[2, 1, 3, 4] random number is 2, swap with element at position 3. [2, 1, 3, 4]随机数为2,与位置3的元素交换。

[2, 1, 4, 3] etc. [2, 1, 4, 3]

If you'd like to generalize the constraint, you can simply change the condition. 如果您想概括约束,则只需更改条件即可。 Instead of refusing to swap when the elements are either 1 and 3, or 2 and 4 (as in the example above), you could make sure the two elements at the positions to be swapped are not within 2 of each other, so something like if(b==a+2)continue; 您可以确保要交换位置上的两个元素彼此之间不在2个之内,而不是在元素分别为1和3或2和4时拒绝交换(如上例所示)。 if(b==a+2)continue; :

elements are 5 and 7, so don't swap. 元素是5和7,所以不要交换。

if(7==5+2)continue; // ie don't swap.

If you use it as a string then you could use this answer's algorithm to swap all the numbers 如果将其用作字符串,则可以使用答案的算法交换所有数字

When you enter all the numbers then just concatenate them together. 输入所有数字后,只需将它们连接在一起即可。 There is no need to treat them as numbers or strings. 无需将它们视为数字或字符串。 All you want to do is reorder them. 您要做的就是重新排序它们。

When you get the result you could then check to see if your constraints match and then print out another list. 得到结果后,可以检查约束条件是否匹配,然后打印出另一个列表。 Something like this perhaps 像这样的东西

private boolean isConstraintSatisfied(String wholeString, String firstNum, String secondNum){
    return wholeString.indexOf(firstNum) <= wholeString.indexOf(secondNum);
}

Not the most elegant solution but I think it would work. 不是最优雅的解决方案,但我认为它会起作用。 For small input sets it shouldnt be too inefficient. 对于小的输入集,它应该不会太低效。

What you've defined here is known as a partial order . 您在此处定义的称为部分订单 You wish to generate a random permutation which still satisfies the partial order, ie a random linear extension. 您希望生成一个仍满足部分顺序的随机排列,即随机线性扩展。

Luckily, the Java API specifies Collections.shuffle , which implements the Fisher-Yates algorithm to generate a random permutation. 幸运的是,Java API指定了Collections.shuffle ,它实现了Fisher-Yates算法来生成随机排列。 Unfortunately, the standard Java technique via Collections.sort is a comparison sort , and thus focused on total orders -- unlike the partial order we want. 不幸的是,通过Collections.sort的标准Java技术是一种比较sort ,因此专注于总订单-与我们想要的部分订单不同。 In fact, the Java API lacks a sorting algorithm we could use here. 实际上,Java API缺乏我们可以在此处使用的排序算法。

One approach covered in "Generating Linear Extensions of Posets by Transpositions" involves that of swapping adjacent elements in the set in a fashion similar to Hassan's solution. “通过转置生成Poset的线性扩展”中介绍的一种方法涉及以类似于Hassan解决方案的方式交换集合中的相邻元素。 This appears to be a functioning way for the localized problem at hand. 这似乎是解决本地化问题的有效方法。

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

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