遗传算法(Genetic Algorithm)是一种模拟天然界的进化规律-优胜劣汰演化来的随机搜索算法,其在解决多种约束条件下的最优解这类问题上具备优秀的表现.java
在遗传算法中有几个基本的概念:基因、个体、种群和进化.基因是个体的表现,不一样个体的基因序列不一样;个体是指单个的生命,个体是组成种群的基础;而进化的基本单位是种群,一个种群里面有多个个体;进化是指一个种群进过优胜劣汰的天然选择后,产生一个新的种群的过程,理论上进化会产生更优秀的种群.算法
一个传统的遗传算法由如下几个组成部分:数据库
经典的遗传算法的流程图以下所示:数组
为了防止进化方向出现误差,在本算法中采用精英主义,即每次进化都保留上一代种群中最优秀的个体。dom
计算适应度函数
/** * 经过和solution比较 ,计算个体的适应值 * @param individual 待比较的个体 * @return 返回适应度 */ public static int getFitness(Individual individual) { int fitness = 0; for (int i = 0; i < individual.size() && i < solution.length; i++) { if (individual.getGene(i) == solution[i]) { fitness++; } } return fitness; }
选择算子测试
/** * 随机选择一个较优秀的个体。用于进行交叉 * @param pop 种群 * @return */ private static Individual tournamentSelection(Population pop) { Population tournamentPop = new Population(tournamentSize, false); // 随机选择 tournamentSize 个放入 tournamentPop 中 for (int i = 0; i < tournamentSize; i++) { int randomId = (int) (Math.random() * pop.size()); tournamentPop.saveIndividual(i, pop.getIndividual(randomId)); } // 找到淘汰数组中最优秀的 Individual fittest = tournamentPop.getFittest(); return fittest; }
交叉算子优化
/** * 两个个体交叉产生下一代 * @param indiv1 父亲 * @param indiv2 母亲 * @return 后代 */ private static Individual crossover(Individual indiv1, Individual indiv2) { Individual newSol = new Individual(); // 随机的从两个个体中选择 for (int i = 0; i < indiv1.size(); i++) { if (Math.random() <= uniformRate) { newSol.setGene(i, indiv1.getGene(i)); } else { newSol.setGene(i, indiv2.getGene(i)); } } return newSol; }
变异算子编码
/** * 突变个体。突变的几率为 mutationRate * @param indiv 待突变的个体 */ private static void mutate(Individual indiv) { for (int i = 0; i < indiv.size(); i++) { if (Math.random() <= mutationRate) { // 生成随机的 0 或 1 byte gene = (byte) Math.round(Math.random()); indiv.setGene(i, gene); } } }
测试结果以下图atom
随着软件和硬件技术的发展,在线考试系统正在逐渐取代传统的线下笔试。对于一个在线考试系统而言,考试试卷的质量很大程度上表明着该系统的质量,试卷是否包含足够多的题型、是否包含指定的知识点以及试卷总体的难度系数是否合适等等,这些都能做为评价一个在线测评系统的指标.若是单纯的根据组卷规则直接从数据库中获取必定数量的试题组成一套试卷,因为只获取一次,并不能保证这样的组卷结果是一个合适的结果,并且能够确定的是,这样获得的结果基本不会是一个优秀的解.显而易见,咱们须要一个优秀的自动组卷算法,遗传算法就很是适合解决自动组卷的问题,其具备自进化、并行执行等特色
使用传统的遗传算法进行组卷时会出现一些误差,进化的结果不是很是理想.具体表现为:进化方向出现误差、搜索后期效率低、容易陷入局部最优解等问题.针对这些问题,本系统对传统的遗传算法作了一些改进,具体表现为:使用精英主义模式(即每次进化都保留上一代种群的最优解)、实数编码以及选择算子的优化.
染色体编码是遗传算法首先要解决的问题,是将个体的特征抽象为一套编码方案.在传统的遗传算法解决方案中,二进制编码使用的最多,就本系统而言,二进制编码造成的基因序列为整个题库,这种方案不是很合适,由于二进制编码按照题库中试题的相对顺序将题库编码成一个01字符串,1表明试题出现,0表明没有显然这样的编码规模太大,对于一个优秀的题库而言,十万的试题总量是很常见的,对于一个长度为十万的字符串进行编码和解码显然太繁琐. 通过查阅资料,因而决定采用实数编码做为替代,将试题的id做为基因,试卷和染色体创建映射关系,同一类型的试题放在一块儿.好比,要组一套java考试试卷,题目总数为15:填空3道,单选10道,主观题2道.那么进行实数编码后,其基因序列分布表现为:
初始化试卷时不采起彻底随机的方式.经过分析不难发现,组卷主要有题型、数量、总分、知识点和难度系数这五个约束条件,在初始化种群的时候,咱们能够根据组卷规则随机产生指定数量的题型,这样在一开始种群中的个体就知足了题型、数量和总分的约束,使约束条件从5个减小为2个:知识点和难度系数.这样算法的迭代次数被减小,收敛也将加快.
在遗传算法中,适应度是评价种群中个体的优劣的惟一指标,适应度能够影响种群的进化方向.因为在初始化时,种群中个体已经知足了题型、数量和总分这三个约束条件,因此个体的适应度只与知识点和难度系数有关.
试卷的难度系数计算公式为:
n是组卷规则要求的题目总数,Ti,Ki分别是第i题的难度系数和分数.
本例中使用知识点覆盖率来评价知识点.即一套试卷要求包含N个知识点,而某个体中包含的知识点数目为M(去重后的结果,M<=N),那么该个体的知识点覆盖率为:M/N. 所以,适应度函数为:
其中,M/N为知识点覆盖率;EP为用户输入的总体指望难度,P为总体实际难度;知识点权重用t1表示,难度系数权重用t2表示.
本例中的选择策略为:指定一个淘汰数组的大小(笔者使用的是5),从原种群中随机挑选个体组成一个淘汰种群,将淘汰种群中的最优个体做为选择算子的结果.
交叉算子其实是染色体的重组.本系统中采用的交叉策略为:在(0,N)之间随机产生两个整数n1,n2,父亲基因序列上n1到n2之间的基因所有遗传给子代,母亲基因序列上的n1到n2以外的基因遗传给子代,可是要确保基因不重复,若是出现重复(实验证实有较大的几率出现重复),那么从题库中挑选一道与重复题的题型相同、分值相同且包含的知识点相同的试题遗传给子代.全部的遗传都要保证基因在染色体上的相对位置不变.
基因变异的出现增长了种群的多样性.在本系统中,每一个个体的每一个基因都有变异的机会,若是随机几率小于变异几率,那么基因就能够突变.突变基因的原则为:与原题的同题型、同分数且同知识点的试题.有研究代表,对于变异几率的选择,在0.1-0.001之间最佳,本例中选取了0.085做为变异几率.
组卷规则是初始化种群的依赖。组卷规则由用户指定,规定了用户指望的试卷的条件:试卷总分、包含的题型与数量、指望难度系数、指望覆盖的知识点。在本例中将组卷规则封装为一个JavaBean
个体,即试卷.本例中将试卷个体抽象成一个JavaBean,其有id,适应度、知识点覆盖率、难度系数、总分、以及个体包含的试题集合这6个属性,以及计算知识点覆盖率和适应度这几个方法.在计算适应度的时候,知识点权重为0.20,难度系数权重为0.80.
/** * 计算试卷总分 * * @return */ public double getTotalScore() { if (totalScore == 0) { double total = 0; for (QuestionBean question : questionList) { total += question.getScore(); } totalScore = total; } return totalScore; } /** * 计算试卷个体难度系数 计算公式: 每题难度*分数求和除总分 * * @return */ public double getDifficulty() { if (difficulty == 0) { double _difficulty = 0; for (QuestionBean question : questionList) { _difficulty += question.getScore() * question.getDifficulty(); } difficulty = _difficulty / getTotalScore(); } return difficulty; } /** * 计算知识点覆盖率 公式为:个体包含的知识点/指望包含的知识点 * * @param rule */ public void setKpCoverage(RuleBean rule) { if (kPCoverage == 0) { Set<String> result = new HashSet<String>(); result.addAll(rule.getPointIds()); Set<String> another = questionList.stream().map(questionBean -> String.valueOf(questionBean.getPointId())).collect(Collectors.toSet()); // 交集操做 result.retainAll(another); kPCoverage = result.size() / rule.getPointIds().size(); } } /** * 计算个体适应度 公式为:f=1-(1-M/N)*f1-|EP-P|*f2 * 其中M/N为知识点覆盖率,EP为指望难度系数,P为种群个体难度系数,f1为知识点分布的权重 * ,f2为难度系数所占权重。当f1=0时退化为只限制试题难度系数,当f2=0时退化为只限制知识点分布 * * @param rule 组卷规则 * @param f1 知识点分布的权重 * @param f2 难度系数的权重 */ public void setAdaptationDegree(RuleBean rule, double f1, double f2) { if (adaptationDegree == 0) { adaptationDegree = 1 - (1 - getkPCoverage()) * f1 - Math.abs(rule.getDifficulty() - getDifficulty()) * f2; } } public boolean containsQuestion(QuestionBean question) { if (question == null) { for (int i = 0; i < questionList.size(); i++) { if (questionList.get(i) == null) { return true; } } } else { for (QuestionBean aQuestionList : questionList) { if (aQuestionList != null) { if (aQuestionList.equals(question)) { return true; } } } } return false; }
种群初始化。将种群抽象为一个Java类Population,其有初始化种群、获取最优个体的方法,关键代码以下
/** * 初始种群 * * @param populationSize 种群规模 * @param initFlag 初始化标志 true-初始化 * @param rule 规则bean */ public Population(int populationSize, boolean initFlag, RuleBean rule) { papers = new Paper[populationSize]; if (initFlag) { Paper paper; Random random = new Random(); for (int i = 0; i < populationSize; i++) { paper = new Paper(); paper.setId(i + 1); while (paper.getTotalScore() != rule.getTotalMark()) { paper.getQuestionList().clear(); String idString = rule.getPointIds().toString(); // 单选题 if (rule.getSingleNum() > 0) { generateQuestion(1, random, rule.getSingleNum(), rule.getSingleScore(), idString, "单选题数量不够,组卷失败", paper); } // 填空题 if (rule.getCompleteNum() > 0) { generateQuestion(2, random, rule.getCompleteNum(), rule.getCompleteScore(), idString, "填空题数量不够,组卷失败", paper); } // 主观题 if (rule.getSubjectiveNum() > 0) { generateQuestion(3, random, rule.getSubjectiveNum(), rule.getSubjectiveScore(), idString, "主观题数量不够,组卷失败", paper); } } // 计算试卷知识点覆盖率 paper.setKpCoverage(rule); // 计算试卷适应度 paper.setAdaptationDegree(rule, Global.KP_WEIGHT, Global.DIFFCULTY_WEIGHt); papers[i] = paper; } } } private void generateQuestion(int type, Random random, int qustionNum, double score, String idString, String errorMsg, Paper paper) { QuestionBean[] singleArray = QuestionService.getQuestionArray(type, idString .substring(1, idString.indexOf("]"))); if (singleArray.length < qustionNum) { log.error(errorMsg); return; } QuestionBean tmpQuestion; for (int j = 0; j < qustionNum; j++) { int index = random.nextInt(singleArray.length - j); // 初始化分数 singleArray[index].setScore(score); paper.addQuestion(singleArray[index]); // 保证不会重复添加试题 tmpQuestion = singleArray[singleArray.length - j - 1]; singleArray[singleArray.length - j - 1] = singleArray[index]; singleArray[index] = tmpQuestion; } }
选择算子的实现:
/** * 选择算子 * * @param population */ private static Paper select(Population population) { Population pop = new Population(tournamentSize); for (int i = 0; i < tournamentSize; i++) { pop.setPaper(i, population.getPaper((int) (Math.random() * population.getLength()))); } return pop.getFitness(); }
交叉算子的实现.本系统实现的算子为两点交叉,在算法的实现过程当中须要保证子代中不出现相同的试题.关键代码以下:
/** * 交叉算子 * * @param parent1 * @param parent2 * @return */ public static Paper crossover(Paper parent1, Paper parent2, RuleBean rule) { Paper child = new Paper(parent1.getQuestionSize()); int s1 = (int) (Math.random() * parent1.getQuestionSize()); int s2 = (int) (Math.random() * parent1.getQuestionSize()); // parent1的startPos endPos之间的序列,会被遗传到下一代 int startPos = s1 < s2 ? s1 : s2; int endPos = s1 > s2 ? s1 : s2; for (int i = startPos; i < endPos; i++) { child.saveQuestion(i, parent1.getQuestion(i)); } // 继承parent2中未被child继承的question // 防止出现重复的元素 String idString = rule.getPointIds().toString(); for (int i = 0; i < startPos; i++) { if (!child.containsQuestion(parent2.getQuestion(i))) { child.saveQuestion(i, parent2.getQuestion(i)); } else { int type = getTypeByIndex(i, rule); QuestionBean[] singleArray = QuestionService.getQuestionArray(type, idString.substring(1, idString .indexOf("]"))); child.saveQuestion(i, singleArray[(int) (Math.random() * singleArray.length)]); } } for (int i = endPos; i < parent2.getQuestionSize(); i++) { if (!child.containsQuestion(parent2.getQuestion(i))) { child.saveQuestion(i, parent2.getQuestion(i)); } else { int type = getTypeByIndex(i, rule); QuestionBean[] singleArray = QuestionService.getQuestionArray(type, idString.substring(1, idString .indexOf("]"))); child.saveQuestion(i, singleArray[(int) (Math.random() * singleArray.length)]); } } return child; }
本系统中变异几率为0.085,对种群的每一个个体的每一个基因都有变异机会.变异策略为:在(0,1)之间产生一个随机数,若是小于变异几率,那么该基因突变.关键代码以下:
/** * 突变算子 每一个个体的每一个基因都有可能突变 * * @param paper */ public static void mutate(Paper paper) { QuestionBean tmpQuestion; List<QuestionBean> list; int index; for (int i = 0; i < paper.getQuestionSize(); i++) { if (Math.random() < mutationRate) { // 进行突变,第i道 tmpQuestion = paper.getQuestion(i); // 从题库中获取和变异的题目类型同样分数相同的题目(不包含变异题目) list = QuestionService.getQuestionListWithOutSId(tmpQuestion); if (list.size() > 0) { // 随机获取一道 index = (int) (Math.random() * list.size()); // 设置分数 list.get(index).setScore(tmpQuestion.getScore()); paper.saveQuestion(i, list.get(index)); } } } }
本系统中采用精英策略,每次进化都保留上一代最优秀个体.这样就能避免种群进化方向发生变化,出现适应度倒退的状况.关键代码以下:
// 进化种群 public static Population evolvePopulation(Population pop, RuleBean rule) { Population newPopulation = new Population(pop.getLength()); int elitismOffset; // 精英主义 if (elitism) { elitismOffset = 1; // 保留上一代最优秀个体 Paper fitness = pop.getFitness(); fitness.setId(0); newPopulation.setPaper(0, fitness); } // 种群交叉操做,从当前的种群pop 来 建立下一代种群 newPopulation for (int i = elitismOffset; i < newPopulation.getLength(); i++) { // 较优选择parent Paper parent1 = select(pop); Paper parent2 = select(pop); while (parent2.getId() == parent1.getId()) { parent2 = select(pop); } // 交叉 Paper child = crossover(parent1, parent2, rule); child.setId(i); newPopulation.setPaper(i, child); } // 种群变异操做 Paper tmpPaper; for (int i = elitismOffset; i < newPopulation.getLength(); i++) { tmpPaper = newPopulation.getPaper(i); mutate(tmpPaper); // 计算知识点覆盖率与适应度 tmpPaper.setKpCoverage(rule); tmpPaper.setAdaptationDegree(rule, Global.KP_WEIGHT, Global.DIFFCULTY_WEIGHt); } return newPopulation; }
组卷规则为:指望试卷难度系数0.82,共100分,20道选择题,2分一道,10道填空题,2分一道,4道主观题,10分一道,要求囊括6个知识点.
外在的条件为:题库试题总量为10950,指望适应度值为0.98,种群最多迭代100次.
测试代码以下:
/** * 组卷过程 * * @param rule * @return */ public static Paper generatePaper(RuleBean rule) { Paper resultPaper = null; // 迭代计数器 int count = 0; int runCount = 100; // 适应度指望值z double expand = 0.98; if (rule != null) { // 初始化种群 Population population = new Population(20, true, rule); System.out.println("初次适应度 " + population.getFitness().getAdaptationDegree()); while (count < runCount && population.getFitness().getAdaptationDegree() < expand) { count++; population = GA.evolvePopulation(population, rule); System.out.println("第 " + count + " 次进化,适应度为: " + population.getFitness().getAdaptationDegree()); } System.out.println("进化次数: " + count); System.out.println(population.getFitness().getAdaptationDegree()); resultPaper = population.getFitness(); } return resultPaper; }
测试结果以下:
能够看到改进后的遗传算法具备较好的表现