gpt4 book ai didi

Java 健身功能 - 体重秤

转载 作者:行者123 更新时间:2023-11-30 11:38:26 24 4
gpt4 key购买 nike

我有一个适应度函数作为实验室的一部分,并希望将其应用于一组“权重”(ArrayList 权重)。我创建了数组并在其中存储了一些值。我创建了随机二进制字符串(末尾有一个“x”以生成随机值),我也希望将适应度函数应用于这些字符串;但是,我遇到的问题是适应度函数总是返回 0 值。我在这里遗漏了什么吗?

适应度函数如下:

import java.util.*;
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();
//Code goes here
//Create a random binary string of just ones and zeros of length n
for(int i = 0; i < n; i++){
int x = CS2004.UI(0,1);
if(x == 0){
System.out.print(s + '0');
}
else if(x == 1){
System.out.print(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
//EXERCISE 3
public static double ScalesFitness(ArrayList<Double> weights)
{
int n = scasol.length();
double lhs = 0.0, rhs = 0.0;
if (n > weights.size()) return(-1);

for(int i = 0; i < n; i++){
if(scasol.charAt(i) == 0){
lhs += weights.get(i);
}
else{
rhs += weights.get(i);
}
}
//Code goes here
//Check each element of scasol for a 0 (lhs) and 1 (rhs) add the weight wi
//to variables lhs and rhs as appropriate
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();
}}

主要方法(在​​单独的类中):

import java.util.ArrayList;
public class Lab8 {

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.println(weights);
System.out.print("Fitness: ");

double fitness = ScalesSolution.ScalesFitness(weights);
System.out.println(fitness);

}}

CS2004类:

import java.util.*;
import java.io.*;

//Some useful code that we will probably reuse in later laboratories...
public class CS2004
{
//Shared random object
static private Random rand;
//Create a uniformly distributed random integer between aa and bb inclusive
static public int UI(int aa,int bb)
{
int a = Math.min(aa,bb);
int b = Math.max(aa,bb);
if (rand == null)
{
rand = new Random();
rand.setSeed(System.nanoTime());
}
int d = b - a + 1;
int x = rand.nextInt(d) + a;
return(x);
}
//Create a uniformly distributed random double between a and b inclusive
static public double UR(double a,double b)
{
if (rand == null)
{
rand = new Random();
rand.setSeed(System.nanoTime());
}
return((b-a)*rand.nextDouble()+a);
}
//This method reads in a text file and parses all of the numbers in it
//This code is not very good and can be improved!
//But it should work!!!
//It takes in as input a string filename and returns an array list of Doubles
static public ArrayList<Double> ReadNumberFile(String filename)
{
ArrayList<Double> res = new ArrayList<Double>();
Reader r;
try
{
r = new BufferedReader(new FileReader(filename));
StreamTokenizer stok = new StreamTokenizer(r);
stok.parseNumbers();
stok.nextToken();
while (stok.ttype != StreamTokenizer.TT_EOF)
{
if (stok.ttype == StreamTokenizer.TT_NUMBER)
{
res.add(stok.nval);
}
stok.nextToken();
}
}
catch(Exception E)
{
System.out.println("+++ReadFile: "+E.getMessage());
}
return(res);
}}

运行后,随机二进制字符串运行良好,但适应度函数无法从 0 开始改变。这是一个示例输出:

011100
111010
001110
111011
001000
010101
001010
100011
110100
011001

[1.0, 2.0, 3.0, 4.0, 10.0]
Fitness: 0.0

非常感谢大家的宝贵时间。斯特凡诺斯。

最佳答案

scasol 在调用 ScalesFitness 时显示为空。在 RandomBinaryString 方法中,您从未真正构造过 s,而只是将其打印出来。而不是 System.out.print(s+'0') 和另一行,你应该有 s += '0';

因为这似乎是一个练习,我会把剩下的留给你,但这里有一个下次的提示:不要让一个函数做比它应该做的更多的事情(比如打印出它的结果),否则,这组函数可能看起来像是在实际工作,但实际上并非如此。

在这种情况下,看起来一切正常,因为它看起来像 s.println() 函数实际打印出 scasol,但实际上,scasol 是空的,RandomBinaryString 方法实际上在进行打印。

关于Java 健身功能 - 体重秤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13630288/

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