Multi-condition exam extraction (with test code) example: genetic algorithm-Automatic Paper Generation System Based on Genetic Algorithm [theoretical] example: genetic algorithm-Automatic Paper Generation System Based on Genetic Algorithm [practice]

Source: Internet
Author: User

Problem from http://q.cnblogs.com/q/38789/

The questions are as follows:

Recently, I was developing a project to create a test system, which has a test paper function (random question extraction). The test attributes include: Major, question type, difficulty, and cognitive level. Now we need to create a random exam and extract 100 questions to meet the following conditions:

Condition 1:

Major: Internal Medicine 20% surgery 30% Stomatology 25% neurology 25.

Condition 2:

Question type: Single-choice question 30% multiple-choice question 40% short answer question 30%

Condition 3:

Difficulty: Hard 20% medium 60% easy 20%

Condition 4:

Cognitive Level: Memory 25% application 40% comprehension 35%

This type of question has been seen several times in the blog, and it took some time to write the following code to help people who need it.

 

In fact, the difficulty of Multi-condition problems lies in efficiency, so it is generally solved in turn, depending on the matching conditions. In the test, because the sample library is randomly generated, the number of random questions is less than 100, so the corresponding questions are generated directly.

The following class implements efficiency considerations in two scenarios. If traversal is fast, you can directly call AddTest to add one more time. If the query is fast, you can use KindOfTestNeed to extract the query conditions, the results generated by the current code are the same each time. If there is a real library, you can use the Randomization of the query results and the Randomization of the KindOfTestNeed condition to generate different volumes.

For actual reference, refer to Artwl's article:

 

Example: genetic algorithm-an automatic paper Generation System Based on Genetic Algorithm [theoretical]

 

Example: genetic algorithm-an automatic paper Generation System Based on Genetic Algorithm [practice]

Directly run the Code:

Using System; using System. collections. generic; using System. componentModel; using System. linq; using System. text; using FizzWare. NBuilder; using Xunit; namespace ExerciseGenerateSystem {// condition 1: // Major: Internal Medicine 20% surgery 30% Stomatology 25% neurology 25. // Condition 2: // question type: Single-choice question 30% multiple-choice question 40% short answer 30% // Condition 3: // difficulty: Hard 20% medium 60% easy 20% // Condition 4: // cognitive level: Memory 25% application 40% comprehension 35% public class ExerciseItem {public long Id {get; set;} public Major {get; set;} public TestType {get; set;} public Difficulty {get; set;} public Awareness {get; set;} public override string ToString () {return string. format ("{0} \ t {1} \ t {2} \ t {3} \ t {4 }", This. id, this. major, this. testType, this. difficulty, this. awareness) ;}} public enum Major {[Description ("Internal Medicine")] Medicine, [Description ("Surgery")] Surgery, [Description ("Dental")] Stomatology, [Description ("Neurology")] Neurology} public enum TestType {SingleAnswer, MultiAnswer, ShortAnswer} public enum Difficulty {Hard, Middle, Easy} public enum Awareness {Remember, Impl, understand} public class Gene RateMyTest {private const int TestTotal = 100; [Fact] public void DoGenerate () {// var testLibary = Builder <ExerciseItem> //. createListOfSize (299999 )//. all ()//. with (e => e. awareness = (Awareness) random (2 ))//. with (e => e. difficulty = (Difficulty) random (2 ))//. with (e => e. major = (Major) random (3 ))//. with (e => e. testType = (TestType) random (2 ))//. build (); var myTest = new MyTest (TestTotal); m YTest. addMajorCondition (Major. medicine, 0.2); myTest. addMajorCondition (Major. surgery, 0.3); myTest. addMajorCondition (Major. stomatology 0.25); myTest. addMajorCondition (Major. neuro, 0.25); myTest. addTestTypeCondidtion (TestType. singleAnswer (0.3); myTest. addTestTypeCondidtion (TestType. multiAnswer (0.4); myTest. addTestTypeCondidtion (TestType. shortAnswer, 0.3); myTest. addDifficultyCondition (Diffic Ulty. hard, 0.2); myTest. addDifficultyCondition (Difficulty. middle, 0.6); myTest. addDifficultyCondition (Difficulty. easy, 0.2); myTest. addAwarenessCondition (Awareness. remember 0.25); myTest. addAwarenessCondition (Awareness. impl, 0.4); myTest. addAwarenessCondition (Awareness. understand, 0.35); var kindOfTestNeed = myTest. kindOfTestNeed (); for (var I = 1l ;! MyTest. IsValid () & kindOfTestNeed! = Null; I ++) {var item = new ExerciseItem {Id = I, Awareness = kindOfTestNeed. item4, Difficulty = kindOfTestNeed. item3, Major = kindOfTestNeed. item1, TestType = kindOfTestNeed. item2}; // testLibary. firstOrDefault (// t => //! MyTest. contains (t) // & t. major = kindOfTestNeed. item1 // & t. difficulty = kindOfTestNeed. item3 // & t. testType = kindOfTestNeed. item2 // & t. awareness = kindOfTestNeed. item4); if (item = null) {myTest. print (); throw new Exception ("Libary is not fit this conditions! ");} If (! MyTest. addTest (item) {kindOfTestNeed = myTest. kindOfTestNeed () ;}} myTest. print (); Assert. true (myTest. validate ();} Random r = new Random (); private int random (int max) {return r. next (0, max);} [Fact] public void Test () {Console. writeLine (123d/123);} public class ConditionStats {public ConditionStats (double scale) {this. scale = scale;} public double Scale; public bool IsEnough {get; Set;} public bool IsTooMuch {get; set;} public void Update (long count, long total) {var ret = (double) count/(double) total; isEnough = ret> = Scale; IsTooMuch = ret> Scale; }} public class MyTest {private readonly IList <ExerciseItem> _ exerciseItems = new List <ExerciseItem> (); private readonly long _ total; private readonly Dictionary <Major, ConditionStats> _ majorConditions = new Dictionary <Major, ConditionStats> (); private readonly Dictionary <TestType, ConditionStats> _ testTypeConditions = new Dictionary <TestType, ConditionStats> (); private readonly Dictionary <Difficulty, conditionStats> _ difficultyConditions = new Dictionary <Difficulty, ConditionStats> (); private readonly Dictionary <Awareness, ConditionStats> _ condition = new Dictionary <Awareness, ConditionStats> (); publ Ic MyTest (long total) {_ total = total;} public void AddMajorCondition (Major major, double scale) {_ majorConditions. add (major, new ConditionStats (scale);} public void AddTestTypeCondidtion (TestType testType, double scale) {_ testTypeConditions. add (testType, new ConditionStats (scale);} public void AddDifficultyCondition (Difficulty difficulty, double scale) {_ difficultyConditions. add (difficul Ty, new ConditionStats (scale);} public void AddAwarenessCondition (Awareness awareness, double scale) {_ awarenessConditions. add (awareness, new ConditionStats (scale);} public bool Contains (ExerciseItem) {return _ exerciseItems. any (e => e. id = item. id);} public bool AddTest (ExerciseItem item) {_ exerciseItems. add (item); UpdateStats (item); if (IsTooMuch () {_ exerciseItems. remove (item); Up DateStats (item); return false;} return true;} private int I = 1; public void UpdateStats (ExerciseItem exerciseItem) {I ++; var major = _ majorConditions [exerciseItem. major]; major. update (_ exerciseItems. longCount (e => e. major = exerciseItem. major), _ total); var testType = _ testTypeConditions [exerciseItem. testType]; testType. update (_ exerciseItems. longCount (e => e. testType = exerciseItem. testType), _ Total); var difficulty = _ difficultyConditions [exerciseItem. difficulty]; difficulty. update (_ exerciseItems. longCount (e => e. difficulty = exerciseItem. difficulty), _ total); var awareness = _ awarenessConditions [exerciseItem. awareness]; awareness. update (_ exerciseItems. longCount (e => e. awareness = exerciseItem. awareness), _ total);} public bool IsTooMuch () {return _ majorConditions. any (mc => mc. va Lue. isTooMuch) | _ testTypeConditions. any (mc => mc. value. isTooMuch) | _ difficultyConditions. any (mc => mc. value. isTooMuch) | _ awarenessConditions. any (mc => mc. value. isTooMuch);} public Tuple <Major, TestType, Difficulty, Awareness> KindOfTestNeed () {if (_ majorConditions. any (mc =>! Mc. Value. IsEnough) & _ testTypeConditions. Any (mc =>! Mc. Value. IsEnough) & _ difficultyConditions. Any (mc =>! Mc. Value. IsEnough) & _ awarenessConditions. Any (mc =>! Mc. Value. IsEnough) {return new Tuple <Major, TestType, Difficulty, Awareness> (_ majorConditions. First (mc =>! Mc. Value. IsEnough). Key, _ testTypeConditions. First (mc =>! Mc. Value. IsEnough). Key, _ difficultyConditions. First (mc =>! Mc. Value. IsEnough). Key, _ awarenessConditions. First (mc =>! Mc. value. isEnough ). key);} return null;} public bool Validate () {return _ majorConditions. all (mc => mc. value. isEnough) & _ testTypeConditions. all (mc => mc. value. isEnough) & _ difficultyConditions. all (mc => mc. value. isEnough) & _ awarenessConditions. all (mc => mc. value. isEnough);} public bool IsValid () {return _ total = _ exerciseItems. longCount ();} public void Print () {Console. writeLine ("Totoal: {0}", _ exerciseItems. count (); _ exerciseItems. toList (). forEach (Console. writeLine );}}}

Test results:

DoGenerate : PassedTotoal:1001    Medicine    SingleAnswer    Hard    Remember2    Medicine    SingleAnswer    Hard    Remember3    Medicine    SingleAnswer    Hard    Remember4    Medicine    SingleAnswer    Hard    Remember5    Medicine    SingleAnswer    Hard    Remember6    Medicine    SingleAnswer    Hard    Remember7    Medicine    SingleAnswer    Hard    Remember8    Medicine    SingleAnswer    Hard    Remember9    Medicine    SingleAnswer    Hard    Remember10    Medicine    SingleAnswer    Hard    Remember11    Medicine    SingleAnswer    Hard    Remember12    Medicine    SingleAnswer    Hard    Remember13    Medicine    SingleAnswer    Hard    Remember14    Medicine    SingleAnswer    Hard    Remember15    Medicine    SingleAnswer    Hard    Remember16    Medicine    SingleAnswer    Hard    Remember17    Medicine    SingleAnswer    Hard    Remember18    Medicine    SingleAnswer    Hard    Remember19    Medicine    SingleAnswer    Hard    Remember20    Medicine    SingleAnswer    Hard    Remember22    Surgery    SingleAnswer    Middle    Remember23    Surgery    SingleAnswer    Middle    Remember24    Surgery    SingleAnswer    Middle    Remember25    Surgery    SingleAnswer    Middle    Remember26    Surgery    SingleAnswer    Middle    Remember28    Surgery    SingleAnswer    Middle    Impl29    Surgery    SingleAnswer    Middle    Impl30    Surgery    SingleAnswer    Middle    Impl31    Surgery    SingleAnswer    Middle    Impl32    Surgery    SingleAnswer    Middle    Impl34    Surgery    MultiAnswer    Middle    Impl35    Surgery    MultiAnswer    Middle    Impl36    Surgery    MultiAnswer    Middle    Impl37    Surgery    MultiAnswer    Middle    Impl38    Surgery    MultiAnswer    Middle    Impl39    Surgery    MultiAnswer    Middle    Impl40    Surgery    MultiAnswer    Middle    Impl41    Surgery    MultiAnswer    Middle    Impl42    Surgery    MultiAnswer    Middle    Impl43    Surgery    MultiAnswer    Middle    Impl44    Surgery    MultiAnswer    Middle    Impl45    Surgery    MultiAnswer    Middle    Impl46    Surgery    MultiAnswer    Middle    Impl47    Surgery    MultiAnswer    Middle    Impl48    Surgery    MultiAnswer    Middle    Impl49    Surgery    MultiAnswer    Middle    Impl50    Surgery    MultiAnswer    Middle    Impl51    Surgery    MultiAnswer    Middle    Impl52    Surgery    MultiAnswer    Middle    Impl53    Surgery    MultiAnswer    Middle    Impl55    Stomatology    MultiAnswer    Middle    Impl56    Stomatology    MultiAnswer    Middle    Impl57    Stomatology    MultiAnswer    Middle    Impl58    Stomatology    MultiAnswer    Middle    Impl59    Stomatology    MultiAnswer    Middle    Impl60    Stomatology    MultiAnswer    Middle    Impl61    Stomatology    MultiAnswer    Middle    Impl62    Stomatology    MultiAnswer    Middle    Impl63    Stomatology    MultiAnswer    Middle    Impl64    Stomatology    MultiAnswer    Middle    Impl65    Stomatology    MultiAnswer    Middle    Impl66    Stomatology    MultiAnswer    Middle    Impl67    Stomatology    MultiAnswer    Middle    Impl68    Stomatology    MultiAnswer    Middle    Impl69    Stomatology    MultiAnswer    Middle    Impl71    Stomatology    MultiAnswer    Middle    Understand72    Stomatology    MultiAnswer    Middle    Understand73    Stomatology    MultiAnswer    Middle    Understand74    Stomatology    MultiAnswer    Middle    Understand75    Stomatology    MultiAnswer    Middle    Understand77    Stomatology    ShortAnswer    Middle    Understand78    Stomatology    ShortAnswer    Middle    Understand79    Stomatology    ShortAnswer    Middle    Understand80    Stomatology    ShortAnswer    Middle    Understand81    Stomatology    ShortAnswer    Middle    Understand83    Neurology    ShortAnswer    Middle    Understand84    Neurology    ShortAnswer    Middle    Understand85    Neurology    ShortAnswer    Middle    Understand86    Neurology    ShortAnswer    Middle    Understand87    Neurology    ShortAnswer    Middle    Understand89    Neurology    ShortAnswer    Easy    Understand90    Neurology    ShortAnswer    Easy    Understand91    Neurology    ShortAnswer    Easy    Understand92    Neurology    ShortAnswer    Easy    Understand93    Neurology    ShortAnswer    Easy    Understand94    Neurology    ShortAnswer    Easy    Understand95    Neurology    ShortAnswer    Easy    Understand96    Neurology    ShortAnswer    Easy    Understand97    Neurology    ShortAnswer    Easy    Understand98    Neurology    ShortAnswer    Easy    Understand99    Neurology    ShortAnswer    Easy    Understand100    Neurology    ShortAnswer    Easy    Understand101    Neurology    ShortAnswer    Easy    Understand102    Neurology    ShortAnswer    Easy    Understand103    Neurology    ShortAnswer    Easy    Understand104    Neurology    ShortAnswer    Easy    Understand105    Neurology    ShortAnswer    Easy    Understand106    Neurology    ShortAnswer    Easy    Understand107    Neurology    ShortAnswer    Easy    Understand108    Neurology    ShortAnswer    Easy    Understand

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.