gpt4 book ai didi

java - 深度复制和遗传算法

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

我目前正在开展一个项目,尝试创建一个简单的遗传算法。关于结构,我有一个基因,它是带有四个数字的 char [] 和一个染色体,它是 10 个基因对象的数组。

我正在尝试编写一个简单的交叉方法,该方法从每个父染色体的一半创建一个子代。我从下面的方法开始,但很快意识到我只是浅层复制。在过去的几天里,我阅读了大量有关使用可克隆接口(interface)来实现深度复制的文章,但我的所有尝试都没有成功。请有人帮我解决这个问题。任何提示和指示将不胜感激

public Chromosome createChild (Chromosome parentA, Chromosome parentB)
{
Chromosome child=new Chromosome();
for (int i=0;i<10; i++)
{
if (i<5)
{
child.genes[i] = parentA.genes[i];
}
else
{
child.genes[i] = parentB.genes[i];
}

}
return child;
}

最佳答案

让我们首先考虑 Gene 类:根据您的规范(我有一个带有四个数字的 char [] 的 Gene),您需要一个 char 数组作为类的属性。而且,这个类应该是可克隆的,那么你必须让这个类实现 Cloneable接口(interface):为此,您必须声明 Gene 类实现 Cloneable 接口(interface)(只需在类定义中编写 implements Cloneable 即可)并且您必须实现clone此类中的方法(在该方法中您必须对对象字段进行深度复制并返回克隆的对象,详细信息请参阅下面的代码)。

import java.util.Arrays;

/*
* Definition of the class that also includes the declaration
* of the implementation of the Cloneable interface.
*/
public class Gene implements Cloneable {

/*
* The length of a gene.
* It is defined as constant (final) in order to use the same value
* in the whole class, where and when necessary.
*/
private static final int GENE_LENGTH = 4;

/*
* In biology a gene it corresponds to a sequence of nucleic acids,
* so I thought of naming m_sequence this field.
*/
private char m_sequence[];

/*
* This constructor allows you to instantiate a new object from a char array.
*/
public Gene(char sequence[]) {
// The field m_sequence is initialized with a copy
// of the array specified in the constructor.
m_sequence = Arrays.copyOf(sequence, GENE_LENGTH);
}

/*
* Simple getter method.
* Since m_sequence is private, you need a method like this
* in order to access elements of the array.
*/
public char getUnit(int index) {
return m_sequence[index];
}

/*
* Simple setter method.
* Since m_sequence is private, you need a method like this
* in order to set the elements of the array.
*/
public void setUnit(int index, char unit) {
m_sequence[index] = unit;
}

/*
* The Cloneable declaration requires that this class has clone method.
* This method should return an Gene object within an Object.
*/
protected Object clone() throws CloneNotSupportedException {
// First, we invoke the clone method of the superclass
Gene clone = (Gene)(super.clone());

// Then, make the deep copy of the object.
// In this case the only field present in the Gene object is an array,
// then you must make a deep copy of this array: in order to make a deep
// copy of the array, you should use the Arrays.copyOf method.
clone.m_sequence = Arrays.copyOf(m_sequence, GENE_LENGTH);

return clone;
}

/*
* Get a representation of this object as a String.
* Just a method for simple testing.
*/
@Override
public String toString() {
return Arrays.toString(m_sequence);
}
}

请注意,为了复制数组,我使用了方法 copyOf Arrays 类(请阅读 here 了解有关数组复制的更多详细信息)。

一个简单的测试来检查 Gene 对象中深度复制的功能:

public static void main(String args[]) throws CloneNotSupportedException {
Gene g1 = new Gene(new char[]{'a', 'b', 'c', 'd'});
Gene g2 = (Gene)(g1.clone());

// now Let's modify g1
g1.setUnit(0, 'e');
g1.setUnit(1, 'f');
g1.setUnit(2, 'g');
g1.setUnit(3, 'h');

System.out.println("g1: " + g1);
System.out.println("g2: " + g2); // g2 has not changed
}

因此,您应该按如下方式更改 createChild 方法。

public class Chromosome {
private static final int CHROMOSOME_LENGTH = 10;

/* Array of Gene object. */
private Gene genes[];

/* Default constructor. */
public Chromosome() {
// Simply allocates an array of 10 elements.
genes = new Gene[CHROMOSOME_LENGTH];
}

/*
* Simple getter method.
* Since m_Genes is private, you need a method like this
* in order to access elements of the array.
*/
public Gene getGene(int index) {
return genes[index];
}

/*
* Simple setter method.
* Since m_Genes is private, you need a method like this
* in order to set the elements of the array.
*/
public void setGene(int index, Gene gene) {
genes[index] = gene;
}

/* The method which make the cross-over. */
public Chromosome createChild(Chromosome parentA, Chromosome parentB) throws CloneNotSupportedException {
Chromosome child = new Chromosome();

// make the cross-over
for (int i=0;i<10; i++)
{
if (i<5)
{
// you need to call the clone() method
child.genes[i] = (Gene)(parentA.genes[i].clone());
}
else
{
// you need to call the clone() method
child.genes[i] = (Gene)(parentB.genes[i].clone());
}
}

return child;
}
}

关于java - 深度复制和遗传算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10381267/

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