[英]Does BitSet flip() affect the length of BitSet?
In the following function, I would like to mutate a BitSet based on a mutation probability. 在下面的函数中,我想基于突变概率对BitSet进行突变。
public static Cell mutate(Cell original_){
Double mProb = 0.2;
BitSet original = new BitSet(original_.getChrom().size());
original = (BitSet) original_.getChrom().clone();
Random rand = new Random();
System.out.print(" " + original.length() + " "); //to check the length of original before applying flip()
for(int m = 0; m < original.length(); m++)
{
if(rand.nextDouble() <= mProb){
original.flip(m);
}
}
System.out.print(" " + original.length() + " "); //to check the length of original after applying flip()
Cell mutated = new Cell(original);
//System.out.print("{" + mutated.getFitness() + "} ");
return mutated;
}
The problem that I noticed is that sometimes the length of BitSet is reduced after flipping some bits!! 我注意到的问题是,有时翻转某些位后,BitSet的长度会减少!!
The following some results to explain the problem: 以下一些结果来解释该问题:
original before flip || length before flip || original after flip || length after flip
110111 || 6 || 110111 || 6
101111 || 6 || 111 || 3
110111 || 6 || 10111 || 5
110111 || 6 || 111111 || 6
111010 || 6 || 11010 || 5
As you can see, the first and the fourth weren't reduced after flipping. 如您所见,翻转后第一个和第四个没有减少。 While the other have been reduced. 同时其他都减少了。 I tried to understand what caused the problem but I couldn't. 我试图了解是什么引起了问题,但我没有。 All what I need is that after the flipping the bitset should have the same length as the original before flipping. 我所需要的是,翻转后,位集的长度应与翻转前的原始长度相同。
Does BitSet flip() affect the length of BitSet? BitSet flip()是否会影响BitSet的长度?
Yes 是
Javadoc for BitSet::length
says: 用于BitSet::length
Javadoc说:
public int length() public int length()
Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one. 返回此BitSet的“逻辑大小”:BitSet中最高设置位的索引加1。 Returns zero if the BitSet contains no set bits. 如果BitSet不包含任何设置位,则返回零。
BitSet b = new BitSet();
System.out.println(b.length()); // 0
b.flip(1);
System.out.println(b.length()); // 2
b.flip(1);
System.out.println(b.length()); // 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.