gpt4 book ai didi

java - 在Java中复制对象

转载 作者:行者123 更新时间:2023-12-02 09:37:28 27 4
gpt4 key购买 nike

所以我的任务是研究一种基于随机突变爬山的算法。我有一种方法 RMHC,它接受一个权重 ArrayList 和两个 int,一个用于权重数量,另一个用于迭代。我的指示告诉我创建一个初始解决方案,复制它,然后将突变方法 SmallChange() 应用于初始解决方案。我还了解到如何使用 ScalesSolution 类中的 GetSol() 方法复制解决方案。该突变采用二进制字符串值(即 11101)并将二进制中的随机子字符串更改为 01 所以我可能是如果第二个子字符串发生突变,则会遇到诸如 10101 之类的输出。

我的问题是,当我对解决方案进行 SmallChange() 时,它也会对原始解决方案进行更改。

我已经尝试按照我发现的另一个问题中的建议添加复制构造函数,但它不起作用。

主要方法

public class Worksheet9 {
public static void main(String[] args) {

ArrayList<Double> myArray = new ArrayList<Double>();

myArray.add(1.0);
myArray.add(2.0);
myArray.add(3.0);
myArray.add(4.0);
myArray.add(10.0);

RMHC(myArray, 5, 2);
}

RMHC方法

public static ScalesSolution RMHC(ArrayList<Double> weights,int n,int iter)
{

ScalesSolution oldsol = new ScalesSolution(n);
ScalesSolution newsol = new ScalesSolution(oldsol.GetSol());
//Attempting Copy Constructor
ScalesSolution newsol = new ScalesSolution(oldsol);



double origfitness = oldsol.ScalesFitness(weights);
System.out.println("Original Fitness: " + origfitness);
double origfitness1 = newsol.ScalesFitness(weights);
System.out.println("Cloned Original Fitness: " + origfitness1);
newsol.SmallChange();
double origfitness2 = newsol.ScalesFitness(weights);
System.out.println("Changed Fitness: " + origfitness2);
double origfitness3 = oldsol.ScalesFitness(weights);
System.out.println("Cloned Original Fitness: " + origfitness3);

return(oldsol);
}



}

ScalesSolution 类

import java.util.ArrayList;
import java.util.Random;

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(ScalesSolution another) {
this.scasol = another.scasol; // you can access
}




public void SmallChange() {

int n = scasol.length();
String s = scasol;



Random rand = new Random();
int p = (rand.nextInt(n));

String x;
x = scasol.substring(0, p);
if (scasol.charAt(p) == '0') {
x += '1';
} else {
x += '0';
}
x += scasol.substring(p + 1, n);
scasol = x;

}





public String GetSol()
{
return(scasol);
}


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){
s += '0';
} else if (x == 1) {
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;
int n = scasol.length();

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();
}
}

当我在 main 方法中调用 RMHC 函数时,我得到如下输出:

Original Fitness: 16.0

Cloned Original Fitness: 16.0

Changed Fitness: 14.0

Cloned Original Fitness: 14.0

在此示例中,第二个克隆原始健身值也应为 16.0。一旦我弄清楚了这个初始问题,我将把代码实现到 for 循环中以包含迭代。谢谢。

最佳答案

假设这是您尝试复制数据的位置:

ScalesSolution oldsol = new ScalesSolution(n);
ScalesSolution newsol = new ScalesSolution(oldsol.GetSol());

这不起作用,因为变量是静态的:

public class ScalesSolution
{
private static String scasol;
//...

public String GetSol()
{
return(scasol);
}

由于您所做的只是将值分配给静态字符串 scasol,因此不会进行任何实际更改或复制。

关于java - 在Java中复制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57379716/

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