在下面的函数中,我想根据突变概率突变一个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;
}我注意到的问题是,有时翻转一些比特后,BitSet的长度会减少!
以下是解释这个问题的一些结果:
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正如你所看到的,第一个和第四个在翻转后并没有减少。而另一个则减少了。我试着去理解是什么导致了这个问题,但我做不到,我所需要的是,在翻转后,比特集的长度应该与原来的翻转前相同。
发布于 2016-07-23 20:23:02
BitSet flip()会影响BitSet的长度吗?
是
Javadoc for BitSet::length说:
公共整数长度() 返回此BitSet的“逻辑大小”:BitSet中最高集位的索引加一个。如果BitSet不包含set位,则返回零。
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()); // 0https://stackoverflow.com/questions/38545991
复制相似问题