GA(遗传算法),应用JGAP包的一个例子,有一些注释

这个小例子是从0-9999999中选择两个数,使和最大,用来解释用法。


1:首先是自己的最适函数

import org.jgap.FitnessFunction;
import org.jgap.IChromosome;


public class GADemoFunctionFitness extends FitnessFunction {


@Override
protected double evaluate(IChromosome arg0) {
// TODO Auto-generated method stub
int sum = 1;
//IChromosome类的size()方法是返回染色体的基因数
int numOfGens = arg0.size();

for(int i=0; i<numOfGens; i++) {
//getGene(int i)方法返回染色体位置i上的基因,getAllele()方法是返回基因上的信息
sum += (Integer)arg0.getGene(i).getAllele();
}
return sum;
}

public static int getValueAtGene(IChromosome a_potentialSolution, int a_position) {
Integer value = (Integer)a_potentialSolution.getGene(a_position).getAllele();
return value.intValue();

}



}

2:然后是主函数

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;


import org.jgap.Chromosome;
import org.jgap.Configuration;
import org.jgap.FitnessFunction;
import org.jgap.Gene;
import org.jgap.Genotype;
import org.jgap.IChromosome;
import org.jgap.InvalidConfigurationException;
import org.jgap.UnsupportedRepresentationException;
import org.jgap.data.DataTreeBuilder;
import org.jgap.data.IDataCreators;
import org.jgap.impl.DefaultConfiguration;
import org.jgap.impl.IntegerGene;
import org.jgap.xml.XMLDocumentBuilder;
import org.jgap.xml.XMLManager;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;


public class GADemo {


/**
* @param args
* @throws InvalidConfigurationException
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub

//创建默认的Configuration对象,用来对于后边的一些列参数进行设置。
Configuration conf = new DefaultConfiguration();
conf.setPreservFittestIndividual(true);

//设置适应度函数
FitnessFunction myfunction = new GADemoFunctionFitness();
conf.setFitnessFunction(myfunction);

//构建基因,JGAP这个包已经提供了Gene这个接口,只需要设定基因的长度,以及基因的具体类型,
Gene[] sampleGene = new Gene[2];//基因长度为2
sampleGene[0] = new IntegerGene(conf, 0, 9999999);//这个基因位是整数,取值范围为0到9999999之间。
sampleGene[1] = new IntegerGene(conf, 0, 9999999);//同上

//用上面的基因构建染色体
IChromosome samplechromosome = new Chromosome(conf, sampleGene);
conf.setSampleChromosome(samplechromosome);

//定义种群的大小为80,也就是说染色体的个数为80.
conf.setPopulationSize(80);

//初始化种群,Genotype,基因型是固定长度的染色体种群。一个Genotype实例进化,那么它所有的染色体都进化。
Genotype popution;
popution = Genotype.randomInitialGenotype(conf);
/*try {
Document doc = XMLManager.readFile(new File("GADemo.xml"));
popution = XMLManager.getGenotypeFromDocument(conf, doc);
} catch (UnsupportedRepresentationException uex) {
popution = Genotype.randomInitialGenotype(conf);
} catch (FileNotFoundException e) {
popution = Genotype.randomInitialGenotype(conf);
}*/

//种群开始进化,并且计数进化时间
long starttime = System.currentTimeMillis();
for(int i=0; i<50; i++) {
popution.evolve();
}
long endtime = System.currentTimeMillis();
System.out.println("The total evolve time:"+(endtime-starttime));

//IChromosome是一个染色体接口,而getFittestChromosome()方法找到种群中适应度最高的染色体。
IChromosome bestSolutionSoFar = popution.getFittestChromosome();
System.out.println(GADemoFunctionFitness.getValueAtGene(bestSolutionSoFar, 0));
System.out.println(GADemoFunctionFitness.getValueAtGene(bestSolutionSoFar, 1));

/*//DataTreeBuilder建立一个从基因染色体或者基因型的树结构。
DataTreeBuilder builder = DataTreeBuilder.getInstance();
//IDataCreators 这个接口代表了一个与 org.w3c.dom.Document有可比性的实体
IDataCreators doc2=builder.representGenotypeAsDocument(popution);
XMLDocumentBuilder docbuilder = new XMLDocumentBuilder();
Document xmlDoc = (Document) docbuilder.buildDocument(doc2);
XMLManager.writeFile(xmlDoc, new File("GADEMO.xml"));
*/


}


}

3:总结
可以看出,应用JGAP的包,步骤如下:

a:创建默认的Configuration对象。

b:设置适应度函数

c:构建基因

d:构建染色体

e:定义种群

f:初始化种群

g:开始进化


4:对于遗传算法的了解还算是皮毛啊,需要继续深入研究

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值