gpt4 book ai didi

java - 从一个类调用静态方法的问题

转载 作者:行者123 更新时间:2023-12-01 09:40:06 24 4
gpt4 key购买 nike

我有一个名为 Operators 的类,其中包含三个静态方法:crossovermutatebestOffSpring。我在另一个类中调用这些方法,例如:

List<Cell> offSpring = Operators.crossover(parent1, parent2);           
Cell bestOffSpring = Operators.bestOffSpring(offSpring);
Cell mutatedChild = Operators.mutate(bestOffSpring);

我面临的问题是,当我调用crossoverbestOffSpring时,它们的结果会正确生成。但是,当我调用 mutate 方法时,其他两个方法的结果不正确。换句话说,crossoverbestOffSpring 的结果会受到调用 mutate 方法的影响。我检查了三个方法的逻辑,没有发现任何错误。

我的问题是从一个类调用许多静态方法会影响它们的结果吗?

以下是这三个方法的代码:

public class Operators {

public static List<Cell> crossover(Cell parenta_,Cell parentb_)
{

BitSet parenta = parenta_.getChrom();
BitSet parentb = parentb_.getChrom();

Random rand = new Random();
int setLength = parenta.length();
//System.out.println("<"+setLength+">");
BitSet child1 = new BitSet(setLength);
BitSet child2 = new BitSet(setLength);

//One point splicing
int slicePoint = rand.nextInt(setLength); //rnd num between 0-70
System.out.print("<"+slicePoint+">");
BitSet a = (BitSet)parenta.clone();
a.clear(slicePoint,setLength);
BitSet b = (BitSet)parenta.clone();
b.clear(0,slicePoint);
BitSet c = (BitSet)parentb.clone();
c.clear(slicePoint,setLength);
BitSet d = (BitSet)parentb.clone();
d.clear(0,slicePoint);

//Combine start of p1 with end of p2
child1.or(a);
child1.or(d);
//Combine start of p2 with end of p1
child2.or(c);
child2.or(b);

//Return the children
//BitSet[] offspring = {child1, child2};

Cell child1_ = new Cell(child1);
Cell child2_ = new Cell(child2);

System.out.print("C1 = " + child1_.printBit());
System.out.print(", C2 = " + child2_.printBit() + " ");

List<Cell> offSpring = new ArrayList<>();

offSpring.add(child1_);
offSpring.add(child2_);

return offSpring;
}

public static Cell mutate(Cell original_){
BitSet original = original_.getChrom();

Double mProb = 0.4;
Random rand = new Random();
for(int m = 0; m < original.length(); m++)
{
//Small possibility a bit copied from parent to child is mutated
if(rand.nextDouble() <= mProb)
original.flip(m);
}
//Return the (possibly) strategy
Cell mutated = new Cell(original);
return mutated;
}

public static Cell bestOffSpring(List<Cell> offSpring){
int offSpringFit1 = offSpring.get(0).getFitness();
int offSpringFit2 = offSpring.get(1).getFitness();

if (offSpringFit1 > offSpringFit2)
return offSpring.get(0);
else
return offSpring.get(1);
}
}

下面是一个例子,调用crossover和bestOffSpring的正确结果是:

OffSpring are 100111 <4> and 111101 <5>, best is 111101
OffSpring are 101010 <3> and 111101 <5>, best is 111101
OffSpring are 101111 <5> and 110101 <4>, best is 101111
OffSpring are 110111 <5> and 110111 <5>, best is 110111
OffSpring are 101101 <4> and 110111 <5>, best is 110111
OffSpring are 101010 <3> and 111010 <4>, best is 111010

当调用 mutate 方法时,结果是:

OffSpring are 101101 <4> and 100110 <5>, best is 100110
OffSpring are 101010 <3> and 1100 <5>, best is 1100
OffSpring are 100111 <4> and 10101 <5>, best is 10101
OffSpring are 110111 <5> and 111100 <5>, best is 111100
OffSpring are 101100 <5> and 110101 <4>, best is 101100
OffSpring are 11101 <4> and 101010 <3>, best is 11101

两个结果的区别在于,正确结果中的两个后代具有正确的长度(每个后代中有多少个 1),并且正确选择了其中最好的。而后者没有给出正确的长度,然后产生错误的最佳后代。

最佳答案

副作用...

if(rand.nextDouble() <= mProb)
original.flip(m);

...

可以假设 original_.getChrom() 直接返回底层 BitSet,因此您最终会翻转 bestOffSpring 的咬合,这反过来会影响适应度函数运行 mutate() 后得分。
为了避免副作用,只需创建原始 Bitset 的副本

Bitset original = new Bitset(original_.getChrom().size());
original.or(original_.getChrom().size())

检查Defensive programming标签

关于java - 从一个类调用静态方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38535234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com