У меня проблемы с настройкой функции пригодности для работы с моим кодом. Все, что я получаю в итоге - это двоичная строка «10101».
Я был бы очень признателен, если бы кто-нибудь мог мне помочь, так как я потратил очень много времени на это и нигде не получил.
Вот код:
public class Fitness{
public static void main(String args[]){
ScalesSolution s = new ScalesSolution("10101");
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(5.0);
weights.add(6.0);
weights.add(7.0);
weights.add(17.0);
weights.add(117.0);
weights.add(3427.0);
weights.add(5437.0);
weights.add(567.0);
weights.add(7567.0);
ScalesSolution.scalesFitness(weights);
System.out.println();
}
}
Фитнес-функция выглядит следующим образом:
public class ScalesSolution{
private static String scasol;
//Creates a new scales solution based on a string parameter
//The string parameter is checked to see if it contains all zeros and ones
//Otherwise the random binary string generator is used (n = length of parameter)
public ScalesSolution(String s)
{
boolean ok = true;
int n = s.length();
for(int i=0;i<n;++i)
{
char si = s.charAt(i);
if (si != '0' && si != '1') ok = false;
}
if (ok)
{
scasol = s;
}
else
{
scasol = RandomBinaryString(n);
}
}
private static String RandomBinaryString(int n)
{
String s = new String();
for(int i = 0; i < s.length(); i++){
CS2004.UI(0,1);
if(i == 0){
System.out.println(s + "0");
}
else if(i == 1){
System.out.println(s + "1");
}
}
return(s);
}
public ScalesSolution(int n)
{
scasol = RandomBinaryString(n);
}
//This is the fitness function for the Scales problem
//This function returns -1 if the number of weights is less than
//the size of the current solution
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(i == 0){
L = L += i;
}
if(i == 1){
R = R += i;
}
}//end for
int n = scasol.length();
return(L-R);
//return(Math.abs(lhs-rhs));
}
//Display the string without a new line
public void print()
{
System.out.print(scasol);
}
//Display the string with a new line
public void println()
{
print();
System.out.println();
}
}
Когда я запускаю программу, вывод выглядит следующим образом:
10101
Больше ничего нет, независимо от того, что я ввожу для значений ArrayList ...
Я часами ломаю себе голову и все еще не обескуражен - любая помощь будет принята с благодарностью!
Большое спасибо.
Мик.
РЕДАКТИРОВАТЬ: код был обновлен, и в списке указан полный класс - извинения за любую путаницу.