Java实现遗传算法

来源:互联网 发布:淘宝质量问题退货邮费 编辑:程序博客网 时间:2024/05/19 18:41

转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/50277547

http://www.llwjy.com/blogdetail/8d8f9fa295e57c774c2b8223166aee1b.html

个人博客站已经上线了,网址 www.llwjy.com ~欢迎各位吐槽~

-------------------------------------------------------------------------------------------------

      关于遗传算法的详细原理以及具体的定义这里就不多介绍,想了解的可以自行百度,下面就简单介绍下自己对遗传算法的理解,本文对基因的编码采用二进制规则。


算法思想:

      遗传算法参照达尔文的进化论,认为物种都是向好的方向去发展(适者生存),因此可以认为到足够的代数之后,得到的最值可实际的最值很接近。


算法步骤:

1)随机产生一个种群;
2)计算种群的适应度、最好适应度、最差适应度、平均适应度等指标;
3)验证种群代数是否达到自己设置的阈值,如果达到结束计算,否则继续下一步计算;
4)采用转盘赌法选择可以产生下一代的父代,产生下一代种群(种群中个体数量不变);
5)种群发生基因突变;
6)重复2、3、4、5步。


算法实现-基因部分

1、种群个体(这里认为是染色体),在个体中,我们为这个个体添加两个属性,个体的基因和基因对应的适应度(函数值)。

[java] view plain copy
print?
  1. public class Chromosome {  
  2.     private boolean[] gene;//基因序列  
  3.     private double score;//对应的函数得分  
  4. }  


2、随机生成基因序列,基因的每一个位置是0还是1,这里采用完全随机的方式实现。
[java] view plain copy
print?
  1. public Chromosome(int size) {  
  2.     if (size <= 0) {  
  3.         return;  
  4.     }  
  5.     initGeneSize(size);  
  6.     for (int i = 0; i < size; i++) {  
  7.         gene[i] = Math.random() >= 0.5;  
  8.     }  
  9. }  
  10.   
  11. private void initGeneSize(int size) {  
  12.     if (size <= 0) {  
  13.         return;  
  14.     }  
  15.     gene = new boolean[size];  
  16. }  

3、把基因转化为对应的值,比如101对应的数字是5,这里采用位运算来实现。

[java] view plain copy
print?
  1. public int getNum() {  
  2.     if (gene == null) {  
  3.         return 0;  
  4.     }  
  5.     int num = 0;  
  6.     for (boolean bool : gene) {  
  7.         num <<= 1;  
  8.         if (bool) {  
  9.             num += 1;  
  10.         }  
  11.     }  
  12.     return num;  
  13. }  

4、基因发生变异,对于变异的位置这里完全采取随机的方式实现,变异原则是由1变为0,0变为1。

[java] view plain copy
print?
  1. public void mutation(int num) {  
  2.     //允许变异  
  3.     int size = gene.length;  
  4.     for (int i = 0; i < num; i++) {  
  5.         //寻找变异位置  
  6.         int at = ((int) (Math.random() * size)) % size;  
  7.         //变异后的值  
  8.         boolean bool = !gene[at];  
  9.         gene[at] = bool;  
  10.     }  
  11. }  

5、克隆基因,用于产生下一代,这一步就是将已存在的基因copy一份。

[java] view plain copy
print?
  1. public static Chromosome clone(final Chromosome c) {  
  2.     if (c == null || c.gene == null) {  
  3.         return null;  
  4.     }  
  5.     Chromosome copy = new Chromosome();  
  6.     copy.initGeneSize(c.gene.length);  
  7.     for (int i = 0; i < c.gene.length; i++) {  
  8.         copy.gene[i] = c.gene[i];  
  9.     }  
  10.     return copy;  
  11. }  

6、父母双方产生下一代,这里两个个体产生两个个体子代,具体哪段基因差生交叉,完全随机。

[java] view plain copy
print?
  1. public static List<Chromosome> genetic(Chromosome p1, Chromosome p2) {  
  2.     if (p1 == null || p2 == null) { //染色体有一个为空,不产生下一代  
  3.         return null;  
  4.     }  
  5.     if (p1.gene == null || p2.gene == null) { //染色体有一个没有基因序列,不产生下一代  
  6.         return null;  
  7.     }  
  8.     if (p1.gene.length != p2.gene.length) { //染色体基因序列长度不同,不产生下一代  
  9.         return null;  
  10.     }  
  11.     Chromosome c1 = clone(p1);  
  12.     Chromosome c2 = clone(p2);  
  13.     //随机产生交叉互换位置  
  14.     int size = c1.gene.length;  
  15.     int a = ((int) (Math.random() * size)) % size;  
  16.     int b = ((int) (Math.random() * size)) % size;  
  17.     int min = a > b ? b : a;  
  18.     int max = a > b ? a : b;  
  19.     //对位置上的基因进行交叉互换  
  20.     for (int i = min; i <= max; i++) {  
  21.         boolean t = c1.gene[i];  
  22.         c1.gene[i] = c2.gene[i];  
  23.         c2.gene[i] = t;  
  24.     }  
  25.     List<Chromosome> list = new ArrayList<Chromosome>();  
  26.     list.add(c1);  
  27.     list.add(c2);  
  28.     return list;  
  29. }  



算法实现-遗传算法

1、对于遗传算法,我们需要有对应的种群以及我们需要设置的一些常量:种群数量、基因长度、基因突变个数、基因突变率等,具体参照如下代码:

[java] view plain copy
print?
  1. public abstract class GeneticAlgorithm {  
  2.     private List<Chromosome> population = new ArrayList<Chromosome>();//种群  
  3.     private int popSize = 100;//种群数量  
  4.     private int geneSize;//基因最大长度  
  5.     private int maxIterNum = 500;//最大迭代次数  
  6.     private double mutationRate = 0.01;//基因变异的概率  
  7.     private int maxMutationNum = 3;//最大变异步长  
  8.       
  9.     private int generation = 1;//当前遗传到第几代  
  10.       
  11.     private double bestScore;//最好得分  
  12.     private double worstScore;//最坏得分  
  13.     private double totalScore;//总得分  
  14.     private double averageScore;//平均得分  
  15.       
  16.     private double x; //记录历史种群中最好的X值  
  17.     private double y; //记录历史种群中最好的Y值  
  18.     private int geneI;//x y所在代数  
  19. }  

2、初始化种群,在遗传算法开始时,我们需要初始化一个原始种群,这就是原始的第一代。

[java] view plain copy
print?
  1. private void init() {  
  2.     for (int i = 0; i < popSize; i++) {  
  3.         population = new ArrayList<Chromosome>();  
  4.         Chromosome chro = new Chromosome(geneSize);  
  5.         population.add(chro);  
  6.     }  
  7.     caculteScore();  
  8. }  

3、在初始种群存在后,我们需要计算种群的适应度以及最好适应度、最坏适应度和平均适应度等。

[java] view plain copy
print?
  1. private void caculteScore() {  
  2.     setChromosomeScore(population.get(0));  
  3.     bestScore = population.get(0).getScore();  
  4.     worstScore = population.get(0).getScore();  
  5.     totalScore = 0;  
  6.     for (Chromosome chro : population) {  
  7.         setChromosomeScore(chro);  
  8.         if (chro.getScore() > bestScore) { //设置最好基因值  
  9.             bestScore = chro.getScore();  
  10.             if (y < bestScore) {  
  11.                 x = changeX(chro);  
  12.                 y = bestScore;  
  13.                 geneI = generation;  
  14.             }  
  15.         }  
  16.         if (chro.getScore() < worstScore) { //设置最坏基因值  
  17.             worstScore = chro.getScore();  
  18.         }  
  19.         totalScore += chro.getScore();  
  20.     }  
  21.     averageScore = totalScore / popSize;  
  22.     //因为精度问题导致的平均值大于最好值,将平均值设置成最好值  
  23.     averageScore = averageScore > bestScore ? bestScore : averageScore;  
  24. }  

4、在计算个体适应度的时候,我们需要根据基因计算对应的Y值,这里我们设置两个抽象方法,具体实现由类的实现去实现。

[java] view plain copy
print?
  1. private void setChromosomeScore(Chromosome chro) {  
  2.     if (chro == null) {  
  3.         return;  
  4.     }  
  5.     double x = changeX(chro);  
  6.     double y = caculateY(x);  
  7.     chro.setScore(y);  
  8.   
  9. }  
  10.   
  11. /** 
  12.  * @param chro 
  13.  * @return 
  14.  * @Author:lulei   
  15.  * @Description: 将二进制转化为对应的X 
  16.  */  
  17. public abstract double changeX(Chromosome chro);  
  18.   
  19.   
  20. /** 
  21.  * @param x 
  22.  * @return 
  23.  * @Author:lulei   
  24.  * @Description: 根据X计算Y值 Y=F(X) 
  25.  */  
  26. public abstract double caculateY(double x);  

5、在计算完种群适应度之后,我们需要使用转盘赌法选取可以产生下一代的个体,这里有个条件就是只有个人的适应度不小于平均适应度才会长生下一代(适者生存)。

[java] view plain copy
print?
  1. private Chromosome getParentChromosome (){  
  2.     double slice = Math.random() * totalScore;  
  3.     double sum = 0;  
  4.     for (Chromosome chro : population) {  
  5.         sum += chro.getScore();  
  6.         //转到对应的位置并且适应度不小于平均适应度  
  7.         if (sum > slice && chro.getScore() >= averageScore) {  
  8.             return chro;  
  9.         }  
  10.     }  
  11.     return null;  
  12. }  

6、选择可以产生下一代的个体之后,就要交配产生下一代

[java] view plain copy
print?
  1. private void evolve() {  
  2.     List<Chromosome> childPopulation = new ArrayList<Chromosome>();  
  3.     //生成下一代种群  
  4.     while (childPopulation.size() < popSize) {  
  5.         Chromosome p1 = getParentChromosome();  
  6.         Chromosome p2 = getParentChromosome();  
  7.         List<Chromosome> children = Chromosome.genetic(p1, p2);  
  8.         if (children != null) {  
  9.             for (Chromosome chro : children) {  
  10.                 childPopulation.add(chro);  
  11.             }  
  12.         }   
  13.     }  
  14.     //新种群替换旧种群  
  15.     List<Chromosome> t = population;  
  16.     population = childPopulation;  
  17.     t.clear();  
  18.     t = null;  
  19.     //基因突变  
  20.     mutation();  
  21.     //计算新种群的适应度  
  22.     caculteScore();  
  23. }  

7、在产生下一代的过程中,可能会发生基因变异

[java] view plain copy
print?
  1. private void mutation() {  
  2.     for (Chromosome chro : population) {  
  3.         if (Math.random() < mutationRate) { //发生基因突变  
  4.             int mutationNum = (int) (Math.random() * maxMutationNum);  
  5.             chro.mutation(mutationNum);  
  6.         }  
  7.     }  
  8. }  

8、将上述步骤一代一代的重复执行

[java] view plain copy
print?
  1. public void caculte() {  
  2.     //初始化种群  
  3.     generation = 1;  
  4.     init();  
  5.     while (generation < maxIterNum) {  
  6.         //种群遗传  
  7.         evolve();  
  8.         print();  
  9.         generation++;  
  10.     }  
  11. }  


编写实现类

      由于上述遗传算法的类是一个抽象类,因此我们需要针对特定的事例编写实现类,假设我们计算 Y=100-log(X)在[6,106]上的最值。


1、我们假设基因的长度为24(基因的长度由要求结果的有效长度确定),因此对应的二进制最大值为 1<< 24,我们做如下设置

[java] view plain copy
print?
  1. public class GeneticAlgorithmTest extends GeneticAlgorithm{  
  2.       
  3.     public static final int NUM = 1 << 24;  
  4.   
  5.     public GeneticAlgorithmTest() {  
  6.         super(24);    
  7.     }  
  8. }  

2、对X值的抽象方法进行实现

[java] view plain copy
print?
  1. @Override  
  2. public double changeX(Chromosome chro) {  
  3.     // TODO Auto-generated method stub    
  4.     return ((1.0 * chro.getNum() / NUM) * 100) + 6;  
  5. }  

3、对Y的抽象方法进行实现

[java] view plain copy
print?
  1. @Override  
  2. public double caculateY(double x) {  
  3.     // TODO Auto-generated method stub    
  4.     return 100 - Math.log(x);  
  5. }  


运行结果

img


遗传算法思考

      自己看了很多遗传算法的介绍,上面提到的最优解都是最后一代的最值,自己就有一个疑问了,为什么我知道前面所有带中的最值,也就是程序中的X  Y值,为什么不能用X Y值做遗传算法最后的结果值呢?


完整代码

1、Chromosome类

[java] view plain copy
print?
  1.  /**   
  2.  *@Description: 基因遗传染色体    
  3.  */   
  4. package com.lulei.genetic.algorithm;    
  5.   
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. public class Chromosome {  
  10.     private boolean[] gene;//基因序列  
  11.     private double score;//对应的函数得分  
  12.       
  13.     public double getScore() {  
  14.         return score;  
  15.     }  
  16.   
  17.     public void setScore(double score) {  
  18.         this.score = score;  
  19.     }  
  20.   
  21.     /** 
  22.      * @param size 
  23.      * 随机生成基因序列 
  24.      */  
  25.     public Chromosome(int size) {  
  26.         if (size <= 0) {  
  27.             return;  
  28.         }  
  29.         initGeneSize(size);  
  30.         for (int i = 0; i < size; i++) {  
  31.             gene[i] = Math.random() >= 0.5;  
  32.         }  
  33.     }  
  34.       
  35.     /** 
  36.      * 生成一个新基因 
  37.      */  
  38.     public Chromosome() {  
  39.           
  40.     }  
  41.       
  42.     /** 
  43.      * @param c 
  44.      * @return 
  45.      * @Author:lulei   
  46.      * @Description: 克隆基因 
  47.      */  
  48.     public static Chromosome clone(final Chromosome c) {  
  49.         if (c == null || c.gene == null) {  
  50.             return null;  
  51.         }  
  52.         Chromosome copy = new Chromosome();  
  53.         copy.initGeneSize(c.gene.length);  
  54.         for (int i = 0; i < c.gene.length; i++) {  
  55.             copy.gene[i] = c.gene[i];  
  56.         }  
  57.         return copy;  
  58.     }  
  59.       
  60.     /** 
  61.      * @param size 
  62.      * @Author:lulei   
  63.      * @Description: 初始化基因长度 
  64.      */  
  65.     private void initGeneSize(int size) {  
  66.         if (size <= 0) {  
  67.             return;  
  68.         }  
  69.         gene = new boolean[size];  
  70.     }  
  71.       
  72.       
  73.     /** 
  74.      * @param c1 
  75.      * @param c2 
  76.      * @Author:lulei   
  77.      * @Description: 遗传产生下一代 
  78.      */  
  79.     public static List<Chromosome> genetic(Chromosome p1, Chromosome p2) {  
  80.         if (p1 == null || p2 == null) { //染色体有一个为空,不产生下一代  
  81.             return null;  
  82.         }  
  83.         if (p1.gene == null || p2.gene == null) { //染色体有一个没有基因序列,不产生下一代  
  84.             return null;  
  85.         }  
  86.         if (p1.gene.length != p2.gene.length) { //染色体基因序列长度不同,不产生下一代  
  87.             return null;  
  88.         }  
  89.         Chromosome c1 = clone(p1);  
  90.         Chromosome c2 = clone(p2);  
  91.         //随机产生交叉互换位置  
  92.         int size = c1.gene.length;  
  93.         int a = ((int) (Math.random() * size)) % size;  
  94.         int b = ((int) (Math.random() * size)) % size;  
  95.         int min = a > b ? b : a;  
  96.         int max = a > b ? a : b;  
  97.         //对位置上的基因进行交叉互换  
  98.         for (int i = min; i <= max; i++) {  
  99.             boolean t = c1.gene[i];  
  100.             c1.gene[i] = c2.gene[i];  
  101.             c2.gene[i] = t;  
  102.         }  
  103.         List<Chromosome> list = new ArrayList<Chromosome>();  
  104.         list.add(c1);  
  105.         list.add(c2);  
  106.         return list;  
  107.     }  
  108.       
  109.     /** 
  110.      * @param num 
  111.      * @Author:lulei   
  112.      * @Description: 基因num个位置发生变异 
  113.      */  
  114.     public void mutation(int num) {  
  115.         //允许变异  
  116.         int size = gene.length;  
  117.         for (int i = 0; i < num; i++) {  
  118.             //寻找变异位置  
  119.             int at = ((int) (Math.random() * size)) % size;  
  120.             //变异后的值  
  121.             boolean bool = !gene[at];  
  122.             gene[at] = bool;  
  123.         }  
  124.     }  
  125.       
  126.     /** 
  127.      * @return 
  128.      * @Author:lulei   
  129.      * @Description: 将基因转化为对应的数字 
  130.      */  
  131.     public int getNum() {  
  132.         if (gene == null) {  
  133.             return 0;  
  134.         }  
  135.         int num = 0;  
  136.         for (boolean bool : gene) {  
  137.             num <<= 1;  
  138.             if (bool) {  
  139.                 num += 1;  
  140.             }  
  141.         }  
  142.         return num;  
  143.     }  
  144. }  

2、GeneticAlgorithm类

[java] view plain copy
print?
  1.  /**   
  2.  *@Description:      
  3.  */   
  4. package com.lulei.genetic.algorithm;    
  5.   
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9.     
  10. public abstract class GeneticAlgorithm {  
  11.     private List<Chromosome> population = new ArrayList<Chromosome>();  
  12.     private int popSize = 100;//种群数量  
  13.     private int geneSize;//基因最大长度  
  14.     private int maxIterNum = 500;//最大迭代次数  
  15.     private double mutationRate = 0.01;//基因变异的概率  
  16.     private int maxMutationNum = 3;//最大变异步长  
  17.       
  18.     private int generation = 1;//当前遗传到第几代  
  19.       
  20.     private double bestScore;//最好得分  
  21.     private double worstScore;//最坏得分  
  22.     private double totalScore;//总得分  
  23.     private double averageScore;//平均得分  
  24.       
  25.     private double x; //记录历史种群中最好的X值  
  26.     private double y; //记录历史种群中最好的Y值  
  27.     private int geneI;//x y所在代数  
  28.       
  29.     public GeneticAlgorithm(int geneSize) {  
  30.         this.geneSize = geneSize;  
  31.     }  
  32.       
  33.     public void caculte() {  
  34.         //初始化种群  
  35.         generation = 1;  
  36.         init();  
  37.         while (generation < maxIterNum) {  
  38.             //种群遗传  
  39.             evolve();  
  40.             print();  
  41.             generation++;  
  42.         }  
  43.     }  
  44.       
  45.     /** 
  46.      * @Author:lulei   
  47.      * @Description: 输出结果 
  48.      */  
  49.     private void print() {  
  50.         System.out.println("--------------------------------");  
  51.         System.out.println("the generation is:" + generation);  
  52.         System.out.println("the best y is:" + bestScore);  
  53.         System.out.println("the worst fitness is:" + worstScore);  
  54.         System.out.println("the average fitness is:" + averageScore);  
  55.         System.out.println("the total fitness is:" + totalScore);  
  56.         System.out.println("geneI:" + geneI + "\tx:" + x + "\ty:" + y);  
  57.     }  
  58.       
  59.       
  60.     /** 
  61.      * @Author:lulei   
  62.      * @Description: 初始化种群 
  63.      */  
  64.     private void init() {  
  65.         for (int i = 0; i < popSize; i++) {  
  66.             population = new ArrayList<Chromosome>();  
  67.             Chromosome chro = new Chromosome(geneSize);  
  68.             population.add(chro);  
  69.         }  
  70.         caculteScore();  
  71.     }  
  72.       
  73.     /** 
  74.      * @Author:lulei   
  75.      * @Description:种群进行遗传 
  76.      */  
  77.     private void evolve() {  
  78.         List<Chromosome> childPopulation = new ArrayList<Chromosome>();  
  79.         //生成下一代种群  
  80.         while (childPopulation.size() < popSize) {  
  81.             Chromosome p1 = getParentChromosome();  
  82.             Chromosome p2 = getParentChromosome();  
  83.             List<Chromosome> children = Chromosome.genetic(p1, p2);  
  84.             if (children != null) {  
  85.                 for (Chromosome chro : children) {  
  86.                     childPopulation.add(chro);  
  87.                 }  
  88.             }   
  89.         }  
  90.         //新种群替换旧种群  
  91.         List<Chromosome> t = population;  
  92.         population = childPopulation;  
  93.         t.clear();  
  94.         t = null;  
  95.         //基因突变  
  96.         mutation();  
  97.         //计算新种群的适应度  
  98.         caculteScore();  
  99.     }  
  100.       
  101.     /** 
  102.      * @return 
  103.      * @Author:lulei   
  104.      * @Description: 轮盘赌法选择可以遗传下一代的染色体 
  105.      */  
  106.     private Chromosome getParentChromosome (){  
  107.         double slice = Math.random() * totalScore;  
  108.         double sum = 0;  
  109.         for (Chromosome chro : population) {  
  110.             sum += chro.getScore();  
  111.             if (sum > slice && chro.getScore() >= averageScore) {  
  112.                 return chro;  
  113.             }  
  114.         }  
  115.         return null;  
  116.     }  
  117.       
  118.     /** 
  119.      * @Author:lulei   
  120.      * @Description: 计算种群适应度 
  121.      */  
  122.     private void caculteScore() {  
  123.         setChromosomeScore(population.get(0));  
  124.         bestScore = population.get(0).getScore();  
  125.         worstScore = population.get(0).getScore();  
  126.         totalScore = 0;  
  127.         for (Chromosome chro : population) {  
  128.             setChromosomeScore(chro);  
  129.             if (chro.getScore() > bestScore) { //设置最好基因值  
  130.                 bestScore = chro.getScore();  
  131.                 if (y < bestScore) {  
  132.                     x = changeX(chro);  
  133.                     y = bestScore;  
  134.                     geneI = generation;  
  135.                 }  
  136.             }  
  137.             if (chro.getScore() < worstScore) { //设置最坏基因值  
  138.                 worstScore = chro.getScore();  
  139.             }  
  140.             totalScore += chro.getScore();  
  141.         }  
  142.         averageScore = totalScore / popSize;  
  143.         //因为精度问题导致的平均值大于最好值,将平均值设置成最好值  
  144.         averageScore = averageScore > bestScore ? bestScore : averageScore;  
  145.     }  
  146.       
  147.     /** 
  148.      * 基因突变 
  149.      */  
  150.     private void mutation() {  
  151.         for (Chromosome chro : population) {  
  152.             if (Math.random() < mutationRate) { //发生基因突变  
  153.                 int mutationNum = (int) (Math.random() * maxMutationNum);  
  154.                 chro.mutation(mutationNum);  
  155.             }  
  156.         }  
  157.     }  
  158.       
  159.     /** 
  160.      * @param chro 
  161.      * @Author:lulei   
  162.      * @Description: 设置染色体得分 
  163.      */  
  164.     private void setChromosomeScore(Chromosome chro) {  
  165.         if (chro == null) {  
  166.             return;  
  167.         }  
  168.         double x = changeX(chro);  
  169.         double y = caculateY(x);  
  170.         chro.setScore(y);  
  171.   
  172.     }  
  173.       
  174.     /** 
  175.      * @param chro 
  176.      * @return 
  177.      * @Author:lulei   
  178.      * @Description: 将二进制转化为对应的X 
  179.      */  
  180.     public abstract double changeX(Chromosome chro);  
  181.       
  182.       
  183.     /** 
  184.      * @param x 
  185.      * @return 
  186.      * @Author:lulei   
  187.      * @Description: 根据X计算Y值 Y=F(X) 
  188.      */  
  189.     public abstract double caculateY(double x);  
  190.   
  191.     public void setPopulation(List<Chromosome> population) {  
  192.         this.population = population;  
  193.     }  
  194.   
  195.     public void setPopSize(int popSize) {  
  196.         this.popSize = popSize;  
  197.     }  
  198.   
  199.     public void setGeneSize(int geneSize) {  
  200.         this.geneSize = geneSize;  
  201.     }  
  202.   
  203.     public void setMaxIterNum(int maxIterNum) {  
  204.         this.maxIterNum = maxIterNum;  
  205.     }  
  206.   
  207.     public void setMutationRate(double mutationRate) {  
  208.         this.mutationRate = mutationRate;  
  209.     }  
  210.   
  211.     public void setMaxMutationNum(int maxMutationNum) {  
  212.         this.maxMutationNum = maxMutationNum;  
  213.     }  
  214.   
  215.     public double getBestScore() {  
  216.         return bestScore;  
  217.     }  
  218.   
  219.     public double getWorstScore() {  
  220.         return worstScore;  
  221.     }  
  222.   
  223.     public double getTotalScore() {  
  224.         return totalScore;  
  225.     }  
  226.   
  227.     public double getAverageScore() {  
  228.         return averageScore;  
  229.     }  
  230.   
  231.     public double getX() {  
  232.         return x;  
  233.     }  
  234.   
  235.     public double getY() {  
  236.         return y;  
  237.     }  
  238. }  

3、GeneticAlgorithmTest类

[java] view plain copy
print?
  1.  /**   
  2.  *@Description:      
  3.  */   
  4. package com.lulei.genetic.algorithm;    
  5.     
  6. public class GeneticAlgorithmTest extends GeneticAlgorithm{  
  7.       
  8.     public static final int NUM = 1 << 24;  
  9.   
  10.     public GeneticAlgorithmTest() {  
  11.         super(24);    
  12.     }  
  13.       
  14.     @Override  
  15.     public double changeX(Chromosome chro) {  
  16.         // TODO Auto-generated method stub    
  17.         return ((1.0 * chro.getNum() / NUM) * 100) + 6;  
  18.     }  
  19.   
  20.     @Override  
  21.     public double caculateY(double x) {  
  22.         // TODO Auto-generated method stub    
  23.         return 100 - Math.log(x);  
  24.     }  
  25.   
  26.     public static void main(String[] args) {  
  27.         GeneticAlgorithmTest test = new GeneticAlgorithmTest();  
  28.         test.caculte();  
  29.     }  
  30. }  

-------------------------------------------------------------------------------------------------
小福利
-------------------------------------------------------------------------------------------------
      个人在极客学院上《Lucene案例开发》课程已经上线了(目前上线到第二课),欢迎大家吐槽~

第一课:Lucene概述

第二课:Lucene 常用功能介绍

第三课:网络爬虫

第四课:数据库连接池

第五课:小说网站的采集

第六课:小说网站数据库操作

第七课:小说网站分布式爬虫的实现


原创粉丝点击