当前位置:编程学习 > C#/ASP.NET >>

实例讲解遗传算法——基于遗传算法的自动组卷系统【实践篇】

本篇将给出上一篇中所述理论的实践。
 先上两张运行后的效果图吧:

 

 基于遗传算法的自动组卷系统运行效果图(1)

 

基于遗传算法的自动组卷系统运行效果图(2)

 一、准备工作
1、问题实体
 问题实体包含编号、类型(类型即题型,分为五种:单选,多选,判断,填空,问答, 分别用1、2、3、4、5表示)、分数、难度系数、知识点。一道题至少有一个知识点,为简单易懂,知识点用List<int> 表示(知识点编号集合)。

 代码如下: 

public class Problem 
{
    public Problem()
    {
        ID = 0;
        Type = 0;
        Score = 0;
        Difficulty = 0.00;
        Points = new List<int>();
    }

    public Problem(Problem p)
    {
        this.ID = p.ID;
        this.Type = p.Type;
        this.Score = p.Score;
        this.Difficulty = p.Difficulty;
        this.Points = p.Points;
    }

    /// <summary>
    /// 编号
    /// </summary>
    public int ID { get; set; }

    /// <summary>
    /// 题型(1、2、3、4、5对应单选,多选,判断,填空,问答)
    /// </summary>
    public int Type { get; set; }

    /// <summary>
    /// 分数
    /// </summary>
    public int Score { get; set; }

    /// <summary>
    /// 难度系数
    /// </summary>
    public double Difficulty { get; set; }

    /// <summary>
    /// 知识点
    /// </summary>
    public List<int> Points { get; set; }

 }

 

2、题库
 为了简单,这里没有用数据库,题目信息临时创建,保存在内存中。因为对不同层次的考生一道题目在不同试卷中的分数可能不一样,因此题目分数一般是老师出卷时定的,不保存在题库中。且单选,多选,判断题每题分数应该相同,填空题一般根据空数来定分数,而问答题一般根据题目难度来定的,因此这里的单选、多选、判断分数相同,填空空数取1-4间的随机数,填空题分数即为空数,问答题即为该题难度系数*10取整。这里各种题型均为1000题,具体应用时改为数据库即可。

 代码如下:

public class DB
{
    /// <summary>
    /// 题库
    /// </summary>
    public List<Problem> ProblemDB;

    public DB()
    {
        ProblemDB = new List<Problem>();
        Problem model;
        Random rand = new Random();
        List<int> Points;
        for (int i = 1; i <= 5000; i++)
        {
            model = new Problem();
            model.ID = i;

            //试题难度系数取0.3到1之间的随机值
            model.Difficulty = rand.Next(30, 100) * 0.01;

            //单选题1分
            if (i < 1001)
            {
                model.Type = 1;
                model.Score = 1;
            }

            //单选题2分
            if (i > 1000 && i < 2001)
            {
                model.Type = 2;
                model.Score = 2;
            }

            //判断题2分
            if (i > 2000 && i < 3001)
            {
                model.Type = 3;
                model.Score = 2;
            }

            //填空题1—4分
            if (i > 3000 && i < 4001)
            {
                model.Type = 4;
                model.Score = rand.Next(1, 5);
            }

            //问答题分数为难度系数*10
            if (i > 4000 && i < 5001)
            {
                model.Type = 5;
                model.Score = model.Difficulty > 0.3 ? (int)(double.Parse(model.Difficulty.ToString("f1")) * 10) : 3;
            }

            Points = new List<int>();
            //每题1到4个知识点
            int count = rand.Next(1, 5);
            for (int j = 0; j < count; j++)
            {
                Points.Add(rand.Next(1, 100));
            }
            model.Points = Points;
            ProblemDB.Add(model);
   &nb

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,