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

项目描述

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

运行环境

jdk8+redis+mysql+IntelliJ IDEA+maven

项目技术

springboot+layui+vue

项目截图

老师端

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

学生端

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

部分代码

遗传算法的使用


/**
 * 遗传算法
 */
public class GAUtil {

    //编译概率
    private static final double mutationRate = 0.085;

    //精英主义
    private static final boolean elitism = true;

    //淘汰数组大小
    private static final int tournamentSize = 5;



    /**
     * 种群进化
     * @param pop 种群对象
     * @param rule 进化规则
     * @return
     */
    public static Population evolvePopulation(Population pop, Rule rule,QuestionService questionService){

        Population newPop = new Population(pop.getLength());
        int elitismOffset;
        //精英主义
        if(elitism){
            elitismOffset = 1;
            //保留上一代最优秀的个体
            ExamPaperAlgotithmBean fitness = pop.getFitness();
            fitness.setId(0);
            newPop.setPaper(0,fitness);
        }

        //种群交叉操作,从当前的种群pop来创建下一代种群newPop
        for (int i = elitismOffset; i < newPop.getLength(); i++){
            //得到两个较优选择
            ExamPaperAlgotithmBean parent1 = select(pop);
            ExamPaperAlgotithmBean parent2 = select(pop);
            //保持连个选择不同
            while (parent1.getId() == parent2.getId()){
                parent2 = select(pop);
            }
            //交叉
            ExamPaperAlgotithmBean child = crossover(parent1, parent2, rule,questionService);
            child.setId(i);
            newPop.setPaper(i,child);
        }

        //种群变异操作
        ExamPaperAlgotithmBean tmpPeper;
        for (int i = elitismOffset; i < newPop.getLength(); i++){
            tmpPeper = newPop.getPaper(i);
            mutate(tmpPeper,questionService);
            //计算知识点覆盖率和适应度
            tmpPeper.setChapterCoverage(rule);
            tmpPeper.setAdaptationDegree(rule, ExamPaperWeightEnum.CHAPTER_WEIGHT,ExamPaperWeightEnum.DIFFICULTY_WEIGHT);
        }
        return newPop;
    }

    /**
     * 选择算子:得到最优个体
     * @param population
     * @return
     */
    public static ExamPaperAlgotithmBean 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
     * @param rule
     * @return
     */
    public static ExamPaperAlgotithmBean crossover(ExamPaperAlgotithmBean parent1,ExamPaperAlgotithmBean parent2,Rule rule,QuestionService questionService){
        ExamPaperAlgotithmBean child = new ExamPaperAlgotithmBean(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.getQuestions().get(i));//parent1遗传给下一代的序列
        }
        List<Integer> chapterList = rule.getChapters();
        for (int i = 0; i < startPos; i++){
            if (!child.containsQuestion(parent2.getQuestions().get(i))){
                child.saveQuestion(i,parent2.getQuestions().get(i));
            }else {
                //如果出现相同题目,重新查找一题题目类型、知识点相同的题目
                Integer type = parent2.getQuestions().get(i).getQuestionType();
                List<Question> questions = questionService.selectByLevelTypeChapters(type, chapterList);
                child.saveQuestion(i,questions.get((int) Math.random()*questions.size()));
            }
        }
        for (int i = endPos; i < parent2.getQuestionSize(); i++){
            if (!child.containsQuestion(parent2.getQuestions().get(i))){
                child.saveQuestion(i,parent2.getQuestions().get(i));
            }else {
                //如果出现相同题目,重新查找一题题目类型、知识点相同的题目
                Integer type = parent2.getQuestions().get(i).getQuestionType();
                List<Question> questions = questionService.selectByLevelTypeChapters(type, chapterList);
                child.saveQuestion(i,questions.get((int) Math.random()*questions.size()));
            }
        }
        return child;
    }

    /**
     * 突变算子 每个个体的每个基因都有可能突变 每个基因变异的概率大概为0.085,小于等于时可以变异
     * @param paper
     */
    public static void mutate(ExamPaperAlgotithmBean paper,QuestionService questionService){
        Question tmpQuestion;
        List<Question> list;
        int index;
        for (int i = 0;i < paper.getQuestionSize(); i++){
            if (Math.random() <= mutationRate){
                //进行突变
                tmpQuestion = paper.getQuestions().get(i);
                //从题库中获取和变异的题目类型一样分数相同的的题目(不包含编译题目)
                Integer type = paper.getQuestions().get(i).getQuestionType();
                Integer chapterId = paper.getQuestions().get(i).getChapterId();
                List<Integer> chapterIds = new ArrayList<>();
                chapterIds.add(chapterId);
                list = questionService.selectByLevelTypeChapters(type, chapterIds);
                if (list.size() > 0){
                    index = (int) Math.random()*list.size();
                    paper.saveQuestion(i,list.get(index));
                }

            }
        }
    }

}


智能组卷

 /**
     * 随机生成试卷题目id列表
     * @param model
     * @param user
     * @return
     */
    private List<Integer> getRandomQuetionIds(ExamPaperAttrVM model, User user){ ;
        List<Integer> errorIds = examPaperQuestionCustomerAnswerService.selectErrorIdList(user.getId());
        ExamPaperQuestionsAttrVM questionsAttrVM = new ExamPaperQuestionsAttrVM(model.getSubjectId(), model.getDifficult(), errorIds);
        List<Integer> newIds = questionMapper.selectNotErrorQuestionIds(questionsAttrVM);
        Integer errorNum = model.getErrorQuestionNum();
        Integer newNum = model.getNewQuestionNum();
        List<Integer> ids = ExamUtil.randomNewErrorQuestionIds(errorNum, newNum, errorIds, newIds);
        return ids;
    }

    /**
     * 生成试卷请求编辑vo -- 用于生成试卷对象 -- 智能训练
     * @param model
     * @return
     */
    @Override
    public ExamPaperEditRequestVM getExamPaperEditRequestVM(ExamPaperAttrVM model){
        User user = webContext.getCurrentUser();
        List<Integer> Ids = getRandomQuetionIds(model,user);
        List<Question> questions = questionService.selectQuestionsByIds(Ids);
        String totalScore = ExamUtil.scoreToVM(questions.stream().mapToInt(q -> q.getScore()).sum());//试卷总分
        List<Integer> qTypes = questions.stream().map(q ->
                q.getQuestionType()).distinct().collect(Collectors.toList());//试卷所有类型题名(单选、多选..)
        List<ExamPaperTitleItemVM> titleItems = qTypes.stream().map(qt -> {
            List<Question> titleQuestions =
                    questions.stream().filter(q -> q.getQuestionType() == qt).collect(Collectors.toList());//试卷每个小标题下的题目列表
            List<QuestionEditRequestVM> titleQuestionEditRequestVMs = titleQuestions.stream().map(q -> {
                QuestionEditRequestVM titleQuestionEditRequestVM = questionService.getQuestionEditRequestVM(q);
                return titleQuestionEditRequestVM;
            }).collect(Collectors.toList());//将Quetion转换为vo对象
            ExamPaperTitleItemVM examPaperTitleItemVM = new ExamPaperTitleItemVM();//生产试卷
            examPaperTitleItemVM.setName(QuestionTypeEnum.fromCode(qt).getName());
            examPaperTitleItemVM.setQuestionItems(titleQuestionEditRequestVMs);
            return examPaperTitleItemVM;
        }).collect(Collectors.toList());

        ExamPaperEditRequestVM vm = new ExamPaperEditRequestVM();
        vm.setName(ExamPaperTypeEnum.TelligentTrain.getName() + count.incrementAndGet());
        vm.setLevel(user.getUserLevel());
        vm.setSubjectId(model.getSubjectId());
        vm.setScore(totalScore);
        vm.setPaperType(ExamPaperTypeEnum.TelligentTrain.getCode());
        vm.setSuggestTime(ExamUtil.getExamPaperSuggestTime(questions));
        vm.setTitleItems(titleItems);
        return vm;
    }

    /**
     * 智能组卷
     * @param model
     * @return
     */
    @Override
    public ExamPaperEditRequestVM getExamPaperEditRequestVM(ExamPaperRuleVM model) {
        User user = webContext.getCurrentUser();
        ExamPaperEditRequestVM vm = getIntelligenceExamPaper(model, user, ExamPaperWeightEnum.runCount, ExamPaperWeightEnum.population_size, ExamPaperWeightEnum.expectAdapter);
        return vm;
    }

    /**
     * 智能组卷
     * @param model
     * @param user
     * @param runCount
     * @param populationSize
     * @param expectAdapter
     * @return
     */
    private ExamPaperEditRequestVM getIntelligenceExamPaper(ExamPaperRuleVM model, User user, int runCount, int populationSize,double expectAdapter ){
        Rule rule = getRuleFromVM(model);
        if (rule == null){
            throw new RuntimeException();
        }
        List<List<Question>> lists = getQuestionsByLevelAndChapters(rule.getChapters());
        ExamPaperAlgotithmBean fitness = getFitnessFromPopulation(rule, lists, true, runCount, populationSize, expectAdapter);
        List<Question> questions = fitness.getQuestions();
        String totalScore = ExamUtil.scoreToVM(questions.stream().mapToInt(q -> q.getScore()).sum());
        List<Integer> qTypes = questions.stream().map(q ->
                q.getQuestionType()).distinct().collect(Collectors.toList());
        List<ExamPaperTitleItemVM> titleItems = qTypes.stream().map(qt -> {
            List<Question> titleQuestions =
                    questions.stream().filter(q -> q.getQuestionType() == qt).collect(Collectors.toList());
            List<QuestionEditRequestVM> titleQuestionEditRequestVMs = titleQuestions.stream().map(q -> {
                Chapter chapter = chapterMapper.selectByPrimaryKey(q.getChapterId());
                TextContent textContent = textContentService.selectById(q.getInfoTextContentId());
                QuestionObject questionObject = JsonUtil.toJsonObject(textContent.getContent(), QuestionObject.class);
                QuestionEditRequestVM titleQuestionEditRequestVM = questionService.getQuestionEditRequestVM(q);
                titleQuestionEditRequestVM.setTitle(questionObject.getTitleContent() + "(" + chapter.getName() + ")");
                return titleQuestionEditRequestVM;
            }).collect(Collectors.toList());//将Quetion转换为vo对象
            ExamPaperTitleItemVM examPaperTitleItemVM = new ExamPaperTitleItemVM();//生产试卷
            examPaperTitleItemVM.setName(QuestionTypeEnum.fromCode(qt).getName());
            examPaperTitleItemVM.setQuestionItems(titleQuestionEditRequestVMs);
            return examPaperTitleItemVM;
        }).collect(Collectors.toList());

        ExamPaperEditRequestVM vm = new ExamPaperEditRequestVM();
        vm.setName(ExamPaperTypeEnum.TelligentExam.getName() + count.incrementAndGet());
        vm.setLevel(user.getUserLevel());
        vm.setSubjectId(model.getSubjectId());
        vm.setScore(totalScore);
        vm.setPaperType(ExamPaperTypeEnum.TelligentExam.getCode());
        vm.setSuggestTime(ExamUtil.getExamPaperSuggestTime(questions));
        vm.setTitleItems(titleItems);
        return vm;
    }

    /**
     * 得到不同类型题chapters知识点内的题目,然后添加到列表中
     * @param chapters
     * @return
     */
    private List<List<Question>> getQuestionsByLevelAndChapters(List<Integer> chapters){
        List<List<Question>> questions = new ArrayList<>(5);
        for (int i = QuestionTypeEnum.SingleChoice.getCode();i <= QuestionTypeEnum.ShortAnswer.getCode(); i++){
            questions.add(questionService.selectByLevelTypeChapters(i,chapters));
        }
        return questions;
    }

    private Rule getRuleFromVM(ExamPaperRuleVM model){
        Rule rule = modelMapper.map(model, Rule.class);
        double difficulty = rule.getDifficulty();
        rule.setDifficulty(difficulty * 1.5 / 5);//不限、简单、中等、困难 * 1.5
        Integer total = ExamUtil.getExpectTotalScore(rule);
        rule.setTotalScore(total);
        return  rule;
    }

    /**
     * 种群进化选出最优个体
     * @param rule
     * @param lists
     * @param initFlag
     * @param runCount
     * @param populationSize
     * @param expectAdapter
     * @return
     */
    private ExamPaperAlgotithmBean getFitnessFromPopulation(Rule rule,List<List<Question>> lists,boolean initFlag,int runCount,int populationSize,double expectAdapter){
        int initCount = 0;
        Population population = new Population(populationSize,true,rule,lists);
        System.out.println("---------------------------------------");
        int index = 0;
        for (ExamPaperAlgotithmBean e: population.getPapers()){
            System.out.println("个体:" + ++index + "适应度为:" + e.getAdaptationDegree() + "难度为:" + e.getDifficulty() + "知识点覆盖率为:" + e.getChapterCoverage());
        }
        System.out.println("初始适应度:" + population.getFitness().getAdaptationDegree());
        while (initCount < runCount && population.getFitness().getAdaptationDegree() < expectAdapter){
            initCount++;
            GAUtil.evolvePopulation(population,rule,questionService);
            System.out.println("第 " + initCount + " 次进化,适应度为:" + population.getFitness().getAdaptationDegree());
        }
        System.out.println("进化次数:" + initCount);
        System.out.println(population.getFitness().getAdaptationDegree());
        ExamPaperAlgotithmBean fitness = population.getFitness();
        return fitness;
    }

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
智能组卷系统的研究与实现 功能描述 (1) 传统方式下,教师出题考试,是先把试题写在纸上,然后组成打印试卷,让学生在纸卷和答题卡上书写。考试完成后,教师再把试卷收上来进行改卷和评分。 (2) 传统方式有许多弊病,比如:卷面固定不易调整;出卷或印刷错误会造成废卷,影响考试正常进行;试卷容易被盗造成泄题或给作弊带来可乘之机;每次考试后的试题不易形成可共享的资源;改卷难度大,容易出现人为错误;综合成本高,管理麻烦…… (3) 调查中发现,一线教师对试题库管理系统即应用软件的需求是很迫切的,这些教师都会比较年轻并都会使用计算机。而随着社会不断进步以及越来越多的年轻教师的加入,对试题库系统的需求会更加旺盛。 (4) 另外,学校对建立自己相对独立的试题库资源管理环境的需求也在不断上升。随着教学模式和体制的改善,更多的学校将建立自己的教考系统,在“相对同等难度和级别”的前提下会形成自己特色的试题库资源,形成丰富多彩的教学内容和形式。这种发展趋势在中小学机构已经开始… (5) 社会其他行业对试题库管理系统的需求也在上升。企业在用人管理上,需要进行员工培训或用人评估,需要对考评对象进行测试和评定,并把结果纳入“人员培训系统”或“人力资源管理系统”中进行管理。 (6) 几乎可以肯定,无论大学、中学、小学、社会办学培训机构或企事业单位,都会对试题库管理系统有需求。事实上,这几年的市场形势业已证明了这一点。 (7) 调查中还发现,用户不仅对试题库系统有基本需求,对组卷功能、组卷方式、试卷管理、网上考试、网上改卷评分、试题库资源共享使用等都感兴趣。 数据库文件在DB下,附加即可(MS Sql2008)
下面是遗传算法自动组卷Java代码示例: ``` import java.util.ArrayList; import java.util.Random; public class GeneticAlgorithm { private int populationSize; private double mutationRate; private double crossoverRate; private int elitismCount; public GeneticAlgorithm(int populationSize, double mutationRate, double crossoverRate, int elitismCount) { this.populationSize = populationSize; this.mutationRate = mutationRate; this.crossoverRate = crossoverRate; this.elitismCount = elitismCount; } public ArrayList<Solution> evolvePopulation(ArrayList<Solution> population) { ArrayList<Solution> newPopulation = new ArrayList<>(); // Keep elites for (int i = 0; i < this.elitismCount; i++) { newPopulation.add(population.get(i)); } // Crossover while (newPopulation.size() < this.populationSize) { Solution parent1 = selectPopulation(population); Solution parent2 = selectPopulation(population); Solution child = crossover(parent1, parent2); newPopulation.add(child); } // Mutate for (int i = this.elitismCount; i < newPopulation.size(); i++) { mutate(newPopulation.get(i)); } return newPopulation; } private Solution selectPopulation(ArrayList<Solution> population) { int index = new Random().nextInt(population.size()); return population.get(index); } private Solution crossover(Solution parent1, Solution parent2) { Solution child = new Solution(); int crossoverPoint = new Random().nextInt(parent1.getGenes().size()); for (int i = 0; i < parent1.getGenes().size(); i++) { if (i < crossoverPoint) { child.getGenes().add(parent1.getGenes().get(i)); } else { child.getGenes().add(parent2.getGenes().get(i)); } } return child; } private void mutate(Solution solution) { for (int i = 0; i < solution.getGenes().size(); i++) { if (new Random().nextDouble() < this.mutationRate) { solution.getGenes().set(i, new Random().nextDouble() * 10); } } } } ``` 这段代码演示了如何用遗传算法实现自动组卷。其中,Solution类代表一个试卷或试卷的一道题目,每个Solution对象都有一个genes属性,代表该试卷或题目的答案。在evolvePopulation方法中,我们首先保留一些精英,然后进行交叉和突变操作来生成新的个体。selectPopulation方法是随机选择一个父代个体对象。crossover方法是通过随机选择交叉点来实现交叉。mutate方法是随机突变一个个体对象的genes属性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_2537071370

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值