我有一個健身功能作爲實驗室的一部分,並希望將其應用於一組'權重'(ArrayList權重)。我創建了該數組並在其中存儲了一些值。我創建了隨機二進制字符串(最後爲了生成隨機值而在結尾處有一個'x'),我也希望將這些字符串應用於適應度函數;然而,我遇到的問題是健身功能始終返回值0.我在這裏錯過了什麼?Java健身功能不起作用
適應度函數如下:
public static double scalesFitness(ArrayList<Double> weights){
if (scasol.length() > weights.size()) return(-1);
double lhs = 0.0,rhs = 0.0;
double L = 0.0;
double R = 0.0;
for(int i = 0; i < scasol.length(); i++){
if(scasol.charAt(i) == '0'){
L = L += 0;
}
else if(scasol.charAt(i) == '1'){
R = R += 1;
}
}//end for
int n = scasol.length();
return(L-R);
}
隨機二進制字符串方法:
private static String RandomBinaryString(int n){
String s = new String();
for(int i = 0; i <= n; i++){
int y = CS2004.UI(0,1);
if(y == 0){
System.out.print(s + '0');
}
else if(y == 1){
System.out.print(s + '1');
}
}
return(s);
}
主要方法(在單獨的類):
public static void main(String args[]){
for(int i = 0; i < 10; i++){
ScalesSolution s = new ScalesSolution("10101x");
s.println();
}
ArrayList<Double> weights = new ArrayList<Double>();
weights.add(1.0);
weights.add(2.0);
weights.add(3.0);
weights.add(4.0);
weights.add(10.0);
System.out.println();
System.out.print("Fitness: ");
System.out.print(ScalesSolution.scalesFitness(weights));
}
一旦運行,則隨機二進制字符串工作得很好,但適應度函數無法從0改變。下面是一個示例輸出:
1101100
1100111
0001111
1001010
1110000
0011111
1100111
1011001
0000110
1000000
Fitness: 0.0
如果您希望爲整個班級編碼,請讓我知道。
非常感謝你的時間。
米克。
如果這是家庭作業,你應該包括作業標籤。 – Greg 2011-03-14 20:02:05