1.什么是遗传算法?

遗传算法(Genetic Algorithm)是模拟达尔文生物进化论的自然选择和遗传学机理的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法。

2.我们都用它来做什么?

    一是基于遗传算法的机器学习,这一研究课题把遗传算法从历来离散的搜索空间的优化搜索算法扩展到具有独特的规则生成功能的机器学习算法。
     二是遗传算法和神经网络、模糊推理以及混沌理论等其它智能计算方法相互渗透和结合,这对开拓21世纪中新的智能计算技术将具有重要的意义。
     三是并行处理的遗传算法的研究十分活跃。这一研究不仅对遗传算法本身的发展,而且对于新一代智能计算机体系结构的研究都是十分重要的。
     四是遗传算法和另一个称为人工生命的崭新研究领域正不断渗透。所谓人工生命即是用计算机模拟自然界丰富多彩的生命现象,其中生物的自适应、进化和免疫等现象是人工生命的重要研究对象,而遗传算法在这方面将会发挥一定的作用。
     五是遗传算法和进化规划(Evolution Programming,EP)以及进化策略(Evolution Strategy,ES)等进化计算理论日益结合。目前,这三者之间的比较研究和彼此结合的探讨正形成热点。

当然,本人才疏学浅,只会用其自动组个卷。

3.遗传算法基本运算过程

a)初始化:设置进化代数计数器t=0,设置最大进化代数T,随机生成M个个体作为初始群体P(0)。

b)个体评价:计算群体P(t)中各个个体的适应度。

c)选择运算:将选择算子作用于群体。选择的目的是把优化的个体直接遗传到下一代或通过配对交叉产生新的个体再遗传到下一代。选择操作是建立在群体中个体的适应度评估基础上的。

d)交叉运算:将交叉算子作用于群体。遗传算法中起核心作用的就是交叉算子。

e)变异运算:将变异算子作用于群体。即是对群体中的个体串的某些基因座上的基因值做变动

群体P(t)经过选择、交叉、变异运算之后得到下一代群体P(t+1)。

f)终止条件判断:若t=T,则以进化过程中所得到的具有最大适应度个体作为最优解输出,终止计算。

4.实例分析

试题实体类 本试题实体针对大学英语四级设计,包含写作、听力、词汇理解、长阅读、仔细阅读两篇、翻译7个大题。并且含有遗传算法相关属性及方法。

package com.cet.pojo;import java.util.List;
import java.util.Random;
import com.cet.service.ObjectService;
import com.cet.tool.Algorithm;/*** Test entity.*/public class Test implements java.io.Serializable {/*** 试题*/private static final long serialVersionUID = 1L;// Fieldsprivate Integer id;private String name;private BaseWriting baseWriting;private BaseListening baseListening;private BaseWordunderstand baseWordunderstand;private BaseLongreading baseLongreading;private BaseCarereading baseCarereading1;private BaseCarereading baseCarereading2;private BaseTranslate baseTranslate;private int ismeta;// 遗传算法相关属性private float difficulty = 0;// 试卷难度private double Fitness = 0;// 试卷适应度public int genes[] = new int[7];// 试卷基因序列// Constructors/** default constructor */public Test() {}// 遗传算法相关方法/*** 计算试卷个体难度系数 计算公式: 每题难度*分数求和除总分* * @return*/public float getDifficulty() {if (difficulty == 0) {float _difficulty = (baseWriting.getDifficulty() * (float) 106.5+ baseListening.getDifficulty() * (float) 248.5+ baseTranslate.getDifficulty() * (float) 106.5+ baseWordunderstand.getDifficulty() * (float) 36+ baseLongreading.getDifficulty() * (float) 71+ baseCarereading1.getDifficulty() * (float) 71 + baseCarereading2.getDifficulty()* (float) 71);difficulty = (_difficulty / (float) 710.5);}return difficulty;}/*** 计算个体适应度 公式为:f=1-|EP-P| 其中EP为期望难度系数,P为种群个体难度系数* * @param difficulty*            期望难度系数*/public double getFitness(double EP) {if (Fitness == 0) {Fitness = 1 - Math.abs(EP - getDifficulty());}return Fitness;}/*** 功能:避免重复题型* 检查生成的Test的所有题型是否数据库已经存在,只要Test里有种题型和数据库的某个Test一样,就更换该题型为数据库所有Test里都没有的。* * @param test* @return*/public boolean checkTest() {ObjectService objectService = Algorithm.objectService;Random random = new Random();List<?> notInList = objectService.getObjectNotIn("BaseWriting");if (notInList.size() < 1) {return false;}baseWriting = (BaseWriting) notInList.get(random.nextInt(notInList.size()));notInList = objectService.getObjectNotIn("BaseListening");if (notInList.size() < 1) {return false;}baseListening = (BaseListening) notInList.get(random.nextInt(notInList.size()));notInList = objectService.getObjectNotIn("BaseWordunderstand");if (notInList.size() < 1) {return false;}baseWordunderstand = (BaseWordunderstand) notInList.get(random.nextInt(notInList.size()));notInList = objectService.getObjectNotIn("BaseLongreading");if (notInList.size() < 1) {return false;}baseLongreading = (BaseLongreading) notInList.get(random.nextInt(notInList.size()));notInList = objectService.getObjectNotIn("BaseCarereading");if (notInList.size() < 2) {return false;}int ran = random.nextInt(notInList.size());baseCarereading1 = (BaseCarereading) notInList.get(ran);notInList.remove(ran);baseCarereading2 = (BaseCarereading) notInList.get(random.nextInt(notInList.size()));notInList = objectService.getObjectNotIn("BaseTranslate");if (notInList.size() < 1) {return false;}baseTranslate = (BaseTranslate) notInList.get(random.nextInt(notInList.size()));return true;}/*** 功能:随机生成一套试卷* * @return*/public void getRandomTest() {BaseWriting baseWriting = (BaseWriting) getRandomObject(BaseWriting.class, "BaseWriting");setBaseWriting(baseWriting);genes[0] = baseWriting.getId();BaseListening baseListening = (BaseListening) getRandomObject(BaseListening.class, "BaseListening");setBaseListening(baseListening);genes[1] = baseListening.getId();BaseWordunderstand baseWordunderstand = (BaseWordunderstand) getRandomObject(BaseWordunderstand.class, "BaseWordunderstand");setBaseWordunderstand(baseWordunderstand);genes[2] = baseWordunderstand.getId();BaseLongreading baseLongreading = (BaseLongreading) getRandomObject(BaseLongreading.class, "BaseLongreading");setBaseLongreading(baseLongreading);genes[3] = baseLongreading.getId();BaseCarereading baseCarereading1 = (BaseCarereading) getRandomObject(BaseCarereading.class, "BaseCarereading");setBaseCarereading1(baseCarereading1);genes[4] = baseCarereading1.getId();BaseCarereading baseCarereading2;do {baseCarereading2 = (BaseCarereading) getRandomObject(BaseCarereading.class, "BaseCarereading");} while (baseCarereading2.getId() == baseCarereading1.getId());setBaseCarereading2(baseCarereading2);genes[5] = baseCarereading2.getId();BaseTranslate baseTranslate = (BaseTranslate) getRandomObject(BaseTranslate.class, "BaseTranslate");setBaseTranslate(baseTranslate);genes[6] = baseTranslate.getId();}/*** 功能:随机获取一个题型对象* * @param clas* @param table* @return*/public Object getRandomObject(Class<?> clas, String table) {ObjectService objectService = Algorithm.objectService;Random random = new Random();Object obj = objectService.getObjectById(clas, random.nextInt(objectService.getMaxID(table)) + 1);return obj;}// Property accessorspublic Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}public BaseLongreading getBaseLongreading() {return this.baseLongreading;}public void setBaseLongreading(BaseLongreading baseLongreading) {this.baseLongreading = baseLongreading;}public BaseTranslate getBaseTranslate() {return this.baseTranslate;}public void setBaseTranslate(BaseTranslate baseTranslate) {this.baseTranslate = baseTranslate;}public BaseListening getBaseListening() {return this.baseListening;}public void setBaseListening(BaseListening baseListening) {this.baseListening = baseListening;}public BaseCarereading getBaseCarereading1() {return this.baseCarereading1;}public void setBaseCarereading1(BaseCarereading baseCarereading1) {this.baseCarereading1 = baseCarereading1;}public BaseWordunderstand getBaseWordunderstand() {return this.baseWordunderstand;}public void setBaseWordunderstand(BaseWordunderstand baseWordunderstand) {this.baseWordunderstand = baseWordunderstand;}public BaseWriting getBaseWriting() {return this.baseWriting;}public void setBaseWriting(BaseWriting baseWriting) {this.baseWriting = baseWriting;}public BaseCarereading getBaseCarereading2() {return this.baseCarereading2;}public void setBaseCarereading2(BaseCarereading baseCarereading2) {this.baseCarereading2 = baseCarereading2;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public void setIsmeta(int ismeta) {this.ismeta = ismeta;}public int getIsmeta() {return ismeta;}}

种群类  遗传算法种群类,负责存放试卷个体,并且包含相关方法。

package com.cet.tool;import com.cet.pojo.Test;/*** 功能:遗传算法种群类* * @author alin* */
public class Population {Test[] Tests;// 个体集合// 创建一个种群,初始化时initialise=truepublic Population(int populationSize, boolean initialise) {Tests = new Test[populationSize];// 初始化种群if (initialise) {for (int i = 0; i < size(); i++) {Test newTest = new Test();newTest.getRandomTest();saveTest(i, newTest);}}}public Test getTest(int index) {return Tests[index];}// 获取种群适应度最高的个体public Test getFittest() {Test fittest = Tests[0];for (int i = 0; i < size(); i++) {if (fittest.getFitness(Algorithm.ep) <= getTest(i).getFitness(Algorithm.ep)) {fittest = getTest(i);}}return fittest;}// 获取种群大小public int size() {return Tests.length;}// 保存试卷到种群public void saveTest(int index, Test indiv) {Tests[index] = indiv;}
}

操作类  遗传算法操作类,包含对试卷个体的交叉变异等操作

public String GA_Test(){Test test = new AutoGATest().getGATest(objectService, difficulty,0.8, 0.1, 5, 80, true);// 遗传算法自动组卷并返回试卷if (test == null) {// 题型数量已经不够用了,无法生成新的不重复的试卷message = "<script>alert('Error,题库相关难度的题型不足或者题型数量已经不够用,无法生成新的不重复的试卷');</script>";return "error";}test.setName(examname);//设置试题名称objectService.save(test);//保存试题return "success";}

试题主类 通过传入参数生成试题

package com.cet.tool;import com.cet.pojo.Test;
import com.cet.service.ObjectService;/*** 功能:遗传算法主类* * @author alin* */
public class AutoGATest {/*** 功能:遗传算法生成试卷主方法* * @param objectService* @param difficulty*            试卷难度* @param ep*            期望难度* @param uniformRate*            交叉概率* @param mutationRate*            变异概率* @param tournamentSize*            淘汰数组大小* @param popSize*            种群大小* @param isCheck*            每次交叉或变异生成的个体是否需要检查重合,考试时检查,练习时不需检查* @return*/public Test getGATest(ObjectService objectService, float ep,double uniformRate, double mutationRate, int tournamentSize,int popSize, boolean isCheck) {// 设置遗传算法相关参数Algorithm.ep = ep;// 设置期望难度Algorithm.uniformRate = uniformRate;// 设置交叉概率Algorithm.mutationRate = mutationRate;// 设置变异概率Algorithm.tournamentSize = tournamentSize;// 设置淘汰数组大小boolean isFirst = true;// 第一次进化,不进行精英主义,因为如果进行,是将初始的种群的精英放入新种群,而不会让它进行任何交叉变异,而有可能它是与数据库某Test重合的Algorithm.objectService = objectService;Population myPop = new Population(popSize, true);// 初始化一个种群// 不断迭代,进行进化操作。 直到找到期望的基因序列,默认都要进化一次,因为这样,进化后的种群个体都是经过交叉变异来的,// 交叉变异过程中,我进行了重复检查,如果个体和数据库的Test数据重合,则重新赋值一个不重复的。这样避免了// 未进化种群就找到了最优个体,而该最优个体与数据库Test数据重合的问题int generationCount = 0;long begin = System.currentTimeMillis();int Count = isCheck == true ? 10 : 100;do {if (myPop.getFittest().getFitness(Algorithm.ep) < 0.90// 种群中最好的个体适应度都低于0.9,则加大交叉概率&& Algorithm.uniformRate < 0.97) {System.out.print(" 加大交叉概率 ");Algorithm.uniformRate += 0.07;}if (myPop.getFittest().getFitness(Algorithm.ep) < 0.90// 种群中最好的个体适应度都低于0.9,则加大变异概率&& Algorithm.mutationRate < 0.25) {System.out.println(" 加大变异概率 ");Algorithm.mutationRate += 0.03;}if (myPop.getFittest().getFitness(Algorithm.ep) > 0.90// 种群中最好的个体适应度高于0.9,则减小交叉概率&& Algorithm.uniformRate > 0.90) {System.out.print(" 减小交叉概率 ");Algorithm.uniformRate -= 0.07;}if (myPop.getFittest().getFitness(Algorithm.ep) > 0.90// 种群中最好的个体适应度高于0.9,则减小变异概率&& Algorithm.mutationRate > 0.15) {System.out.println(" 减小变异概率 ");Algorithm.mutationRate -= 0.03;}myPop = Algorithm.evolvePopulation(myPop, isFirst, isCheck);if (myPop == null) // 题型数量已经不够用了,无法生成新的不重复的试卷return null;isFirst = false;generationCount++;System.out.println("第" + generationCount + "次进化, 适应度为: "+ myPop.getFittest().getFitness(Algorithm.ep) * 100 + "%");} while (myPop.getFittest().getFitness(Algorithm.ep) < 0.98&& generationCount <= Count);System.out.println("用时:" + (System.currentTimeMillis() - begin)/ 1000.0 + "秒");if (generationCount > Count) {System.err.println("题库相关难度的题型不足,无法生成新的试卷\n");return null;}System.out.println("进化完成!");System.out.println("进化总次数:" + generationCount + ",最终个体适应度:"+ myPop.getFittest().getFitness(Algorithm.ep) * 100 + "%"+ ",期望难度:" + ep + ",试卷难度:" + myPop.getFittest().getDifficulty()+ "\n");return myPop.getFittest();}
}

引用类 只放一个Action方法引用

public String GA_Test(){Test test = new AutoGATest().getGATest(objectService, difficulty,0.8, 0.1, 5, 80, true);// 遗传算法自动组卷并返回试卷if (test == null) {// 题型数量已经不够用了,无法生成新的不重复的试卷message = "<script>alert('Error,题库相关难度的题型不足或者题型数量已经不够用,无法生成新的不重复的试卷');</script>";return "error";}test.setName(examname);//设置试题名称objectService.save(test);//保存试题return "success";}

注:大二时写的代码,比较渣,意思到位就行了。

遗传算法组卷使用心得相关推荐

  1. 数据结构自动组卷系统设计文档

    1. 功能设计 自动组卷 根据设置的题型.题量.难度.考查的知识点等属性自动组合成一张试卷 难度可以选择简单.普通.困难 可以添加各种题库(章节) 在每个题库中可以输入每种题型的数量 以上面的选择为依 ...

  2. 基于遗传算法实现自动组卷

    转自 http://www.cnblogs.com/liulang/articles/1614311.html 1  遗传算法介绍 1.1 遗传算法概要 遗传算法是模拟达尔文的遗传选择和自然淘汰的生物 ...

  3. 协同进化遗传算法 代码_遗传算法在组卷中的应用

    最近看到之前做的项目,是关于使用遗传算法实现智能组卷的.所以这次为大家分享遗传算法的基本原理,以及在实际场景中的应用. "揭开算法神秘的面纱" 在学习计算机相关知识时,我们必定会遇 ...

  4. 基于遗传算法自动组卷的实现

    1  遗传算法介绍 1.1 遗传算法概要 遗传算法是模拟达尔文的遗传选择和自然淘汰的生物进化过程的计算模型,是一种通过模拟自然进化过程搜索最优解的方法,它是用来解决多约束条件下的最优问题. 遗传算法是 ...

  5. python自动组卷系统_基于遗传算法(C#编写)的智能组卷系统优化

    原创 guodongwe1991 机器学习算法与Python学习 2016-08-25 最近由于项目的需要,基于.Net 4.0框架和WPF开发window的客户端(开发环境为win7 旗舰版:Vis ...

  6. 基于遗传算法的试题组卷

    基于遗传算法的试题组卷 IT企业每年都会在春季和秋季举行校园招聘,对于个性化定制的试卷需求量很大,如何组出又好又快的定制化试题对于IT企业非常重要.组卷技术主要针对知识点覆盖率,题型,难度系数,试题数 ...

  7. Java使用遗传算法实现智能组卷

    遗传算法实现智能组卷 0.需求:用户选择知识点.年级.难度系数.题型.题目总数量,一键智能生成试卷,如下 1.遗传算法: 1.0 参考博文: 理论概念详解:https://www.jianshu.co ...

  8. 毕设:智能组卷平台(遗传算法)

    项目描述 智能组卷平台(遗传算法) 分为3个端:管理员端,老师端,学生端 主要功能包括 登录,学生管理,老师管理,题目管理,试卷管理, 知识管理.任务管理,教育管理,试卷管理,批卷管理, 运行环境 j ...

  9. 组卷系统php遗传算法,基于遗传算法的智能组卷系统实现

    考试作为教育测量学和教育统计学和的基本原理,不仅是对学生学习能力和知识水平的检验方式,也是对教师教育教学水平评价和体现的重要手段之一.如何更加客观公正地反映学生的学习状况,全面地掌握和评价教师的教学工 ...

最新文章

  1. Nagios插件NDOUtils安装
  2. C# delegate and event
  3. 鍵盤彈出,頁面佈局被推上去了.....
  4. Logistic Classification
  5. html中评论应该怎么写,HTML-评论
  6. mysql拉数据到本地_从mysql中dump数据到本地
  7. Java HashMap源码剖析
  8. 瀑布流JavaScript
  9. 离职一个月了,也面了很多公司,但都没有offer,软件测试工作怎么就这么难找?
  10. ios ffmpeg+libx264
  11. java后台通用权限管理系统(springboot)
  12. js 图片浏览插件原生
  13. 计算机表格应用试题及答案,2016年职称计算机考试EXCEL练习试题及答案
  14. iOS 在线下载字体
  15. im服务器开源项目,Oschat IM 开源即时通讯项目介绍
  16. 我为什么不要应届毕业生
  17. Ps制作的立体字效果
  18. Poj 2992 Divisors(算数基本定理素数因子个数)
  19. mysql gtid 同步_结合案例说说5.7使用gtid同步后,mysql.gtid_executed引起的从库gtid断层...
  20. 通过TMG发布ActiveSync

热门文章

  1. TypeError: load() missing 1 required positional argument: ‘Loader‘?
  2. DNS使用TCP和UDP的端口号53
  3. 平方和:在1-40中只要数字中含有2,0,1,9的数字一共有28个,他们的和是574,平方和是14362。请问1-2019中,所有这样的输的平方和是多少?
  4. 网易云音乐API分析
  5. IDEA中Maven项目打包方式
  6. nginx修改上传文件大小限制
  7. 单片机基础入门:单片机电源电路设计,搞定电源不求人
  8. Tableau 中的表计算图解(表和区的向下横穿)
  9. 高等数学——简单直观地了解定积分
  10. python数据库操作sqlite_python操作数据库之sqlite3打开数据库、删除、修改示例