Пример #1
0
        static public void DemoBackwardChainWithNullMemory()
        {
            RuleInferenceEngine rie = getInferenceEngine();

            Console.WriteLine("Infer with All Facts Cleared:");
            rie.ClearFacts();

            List <Clause> unproved_conditions = new List <Clause>();

            Clause conclusion = null;

            while (conclusion == null)
            {
                conclusion = rie.Infer("vehicle", unproved_conditions);
                if (conclusion == null)
                {
                    if (unproved_conditions.Count == 0)
                    {
                        break;
                    }
                    Clause c = unproved_conditions[0];
                    Console.WriteLine("ask: " + c + "?");
                    unproved_conditions.Clear();
                    Console.WriteLine("What is " + c.Variable + "?");
                    String value = Console.ReadLine();
                    rie.AddFact(new ClauseIs(c.Variable, value));
                }
            }

            Console.WriteLine("Conclusion: " + conclusion);
            Console.WriteLine("Memory: ");
            Console.WriteLine("{0}", rie.Facts);
        }
Пример #2
0
        static void Main(string[] args)
        {
            RuleInferenceEngine ruleInferenceEngine = new RuleInferenceEngine();

            Rule rule = new Rule("seo_hard_freelancer");

            rule.AddAntecedents(new GreaterEqualClause("experience", "4"));
            rule.AddAntecedents(new IsClause("type", "SEO"));
            rule.Consequent = new BaseClause("freelancer", "Oleg");
            ruleInferenceEngine.Rules.Add(rule);

            rule = new Rule("seo_easy_freelancer");
            rule.AddAntecedents(new LessClause("experience", "4"));
            rule.AddAntecedents(new IsClause("type", "SEO"));
            rule.Consequent = new BaseClause("freelancer", "Ivan");

            ruleInferenceEngine.Rules.Add(rule);

            List <BaseClause> unproved_conditions = new List <BaseClause>();

            Console.WriteLine("Write freencer experience:");

            ruleInferenceEngine.AddFact(new IsClause("type", Console.ReadLine()));
            ruleInferenceEngine.AddFact(new IsClause("experience", Console.ReadLine()));

            BaseClause conclusion = ruleInferenceEngine.Infer("freelancer", unproved_conditions);

            Console.WriteLine("Conclusion: " + conclusion);
        }
Пример #3
0
        static public void TestBackwardChain()
        {
            RuleInferenceEngine rie = getInferenceEngine();

            rie.AddFact(new ClauseIs("num_wheels", "4"));
            rie.AddFact(new ClauseIs("motor", "yes"));
            rie.AddFact(new ClauseIs("num_doors", "3"));
            rie.AddFact(new ClauseIs("size", "medium"));

            Console.WriteLine("Infer: vehicle");

            List <Clause> unproved_conditions = new List <Clause>();

            Clause conclusion = rie.Infer("vehicle", unproved_conditions);

            Console.WriteLine("Conclusion: " + conclusion);

            Debug.Assert(conclusion.Value == MiniVan);
        }
Пример #4
0
        static public void TestForwardChain()
        {
            RuleInferenceEngine rie = getInferenceEngine();

            rie.AddFact(new ClauseIs("num_wheels", "4"));
            rie.AddFact(new ClauseIs("motor", "yes"));
            rie.AddFact(new ClauseIs("num_doors", "3"));
            rie.AddFact(new ClauseIs("size", "medium"));

            Console.WriteLine("before inference");
            Console.WriteLine("{0}", rie.Facts);
            Console.WriteLine("");

            rie.Infer();             //forward chain

            Console.WriteLine("after inference");
            Console.WriteLine("{0}", rie.Facts);
            Console.WriteLine("");

            Debug.Assert(rie.Facts.Count == 6);
        }
Пример #5
0
        public void TestForwardChain()
        {
            RuleInferenceEngine rie = getInferenceEngine();

            rie.AddFact(new IsClause("num_wheels", "4"));
            rie.AddFact(new IsClause("motor", "yes"));
            rie.AddFact(new IsClause("num_doors", "3"));
            rie.AddFact(new IsClause("size", "medium"));

            console.WriteLine("before inference");
            console.WriteLine("{0}", rie.Facts);
            console.WriteLine("");

            rie.Infer(); //forward chain

            console.WriteLine("after inference");
            console.WriteLine("{0}", rie.Facts);
            console.WriteLine("");

            Assert.Equal(6, rie.Facts.Count);
        }
Пример #6
0
        private static void ReadStudentRecords()
        {
            var csv     = new CsvReader(File.OpenText("Intakes.csv"));
            var records = csv.GetRecords <StudentInfo>();

            foreach (StudentInfo record in records)
            {
                if (record.gewogen_gemiddelde == "")
                {
                    record.gewogen_gemiddelde = "0";
                }

                RuleInferenceEngine rie = GetInferenceEngine();
                rie.AddFact(new IsClause("gewogen_gemiddelde", record.gewogen_gemiddelde));
                rie.AddFact(new IsClause("competenties", record.competenties));
                rie.AddFact(new IsClause("capaciteiten", record.capaciteiten));
                rie.AddFact(new IsClause("intr. Motivatie", record.intr_motivatie));
                rie.AddFact(new IsClause("extr. Motivatie", record.extr_motivatie));
                rie.AddFact(new IsClause("is_mbo_deficient", record.is_mbo_deficient));
                rie.AddFact(new IsClause("Persoonlijk 'bijspijker'-advies", record.persoonlijk_bijspijker_advies));
                rie.AddFact(new IsClause("Aanmelden voor verkort opleidingstraject", record.Aanmelden_voor_verkort_opleidingstraject));
                rie.AddFact(new IsClause("reden_stoppen", record.reden_stoppen));

                List <Clause> unproved_conditions = new List <Clause>();
                var           conclusion          = rie.Infer("advies", unproved_conditions);

                if (conclusion != null)
                {
                    Console.WriteLine($"{record.studentnummer}: {conclusion.Value}");
                }
                else
                {
                    Console.WriteLine($"Could not generate advice for student {record.studentnummer}");
                }
            }
        }
Пример #7
0
        private void ExpertCycle()
        {
            RuleInferenceEngine rie = AutoRules.getInferenceEngine();

            rie.ClearFacts();

            List <Clause> unproved_conditions = new List <Clause>();

            conclusion = null;
            while (conclusion == null)
            {
                conclusion = rie.Infer("vehicle", unproved_conditions);
                if (conclusion == null)
                {
                    if (unproved_conditions.Count == 0)
                    {
                        break;
                    }
                    Clause c = unproved_conditions[0];

                    number_value = false;

                    for (int i = 0; i < rie.m_rules.Count(); i++)
                    {
                        if (number_value == true)
                        {
                            break;
                        }
                        for (int j = 0; j < rie.m_rules[i].m_antecedents.Count(); j++)
                        {
                            if (rie.m_rules[i].m_antecedents[j].Variable == c.Variable)
                            {
                                if (rie.m_rules[i].m_antecedents[j].Condition != "=")
                                {
                                    number_value = true;
                                    break;
                                }
                                else
                                {
                                    if (!answers_list.Contains(rie.m_rules[i].m_antecedents[j].Value))
                                    {
                                        answers_list.Add(rie.m_rules[i].m_antecedents[j].Value);
                                    }
                                }
                            }
                        }
                    }
                    if (number_value)
                    {
                        question = "У данного ТС сколько " + c.Variable + "?";
                    }
                    else
                    {
                        question = "У данного ТС " + c.Variable + "?";
                    }
                    //Console.WriteLine("ask: " + c + "?");
                    //question = "What is " + c.Variable + "?";
                    readyEvent.Set();
                    nextEvent.Reset();
                    unproved_conditions.Clear();
                    nextEvent.WaitOne();
                    answers_list.Clear();
                    //Console.WriteLine("What is " + c.Variable + "?");
                    String value = answer;//Console.ReadLine();
                    rie.AddFact(new IsClause(c.Variable, value));
                }
            }
            readyEvent.Set();
            finished = true;
            //Console.WriteLine("Conclusion: " + conclusion);
            //Console.WriteLine("Memory: ");
            //Console.WriteLine("{0}", rie.Facts);
            //Console.ReadLine();
        }
Пример #8
0
        private void btnFuzzy_Click(object sender, EventArgs e)
        {
            RuleInferenceEngine rie = new RuleInferenceEngine();

            FuzzySet output = new FuzzySet("Output", -100, 100, 1);

            output.AddMembership("Cooling", new FuzzyReverseGrade(-50, 0));
            output.AddMembership("Zero", new FuzzyTriangle(-50, 0, 50));
            output.AddMembership("Heating", new FuzzyGrade(0, 50));
            rie.AddFuzzySet(output.Name, output);

            FuzzySet tempError = new FuzzySet("Temperature Error", -4, 4, 0.05);

            tempError.AddMembership("Negative", new FuzzyReverseGrade(-2, 0));
            tempError.AddMembership("Zero", new FuzzyTriangle(-2, 0, 2));
            tempError.AddMembership("Positive", new FuzzyGrade(0, 2));
            rie.AddFuzzySet(tempError.Name, tempError);


            FuzzySet tempErrorDot = new FuzzySet("Temperature Error", -10, 10, 0.1);

            tempErrorDot.AddMembership("Negative", new FuzzyReverseGrade(-5, 0));
            tempErrorDot.AddMembership("Zero", new FuzzyTriangle(-5, 0, 5));
            tempErrorDot.AddMembership("Positive", new FuzzyGrade(0, 5));
            rie.AddFuzzySet(tempErrorDot.Name, tempErrorDot);

            /*
             *      1. If (e < 0) AND (er < 0) then Cool 0.5 & 0.0 = 0.0
             *      2. If (e = 0) AND (er < 0) then Heat 0.5 & 0.0 = 0.0
             *      3. If (e > 0) AND (er < 0) then Heat 0.0 & 0.0 = 0.0
             *      4. If (e < 0) AND (er = 0) then Cool 0.5 & 0.5 = 0.5
             *      5. If (e = 0) AND (er = 0) then No_Chng 0.5 & 0.5 = 0.5
             *      6. If (e > 0) AND (er = 0) then Heat 0.0 & 0.5 = 0.0
             *      7. If (e < 0) AND (er > 0) then Cool 0.5 & 0.5 = 0.5
             *      8. If (e = 0) AND (er > 0) then Cool 0.5 & 0.5 = 0.5
             *      9. If (e > 0) AND (er > 0) then Heat 0.0 & 0.5 = 0.0
             */

            Rule rule = new Rule("Rule 1");

            rule.AddAntecedent(new Clause(tempError, "Is", "Negative"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Negative"));
            rule.Consequent = new Clause(output, "Is", "Cooling");
            rie.AddRule(rule);

            rule = new Rule("Rule 2");
            rule.AddAntecedent(new Clause(tempError, "Is", "Zero"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Negative"));
            rule.Consequent = new Clause(output, "Is", "Heating");
            rie.AddRule(rule);

            rule = new Fuzzy.Rule("Rule 3");
            rule.AddAntecedent(new Clause(tempError, "Is", "Positive"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Negative"));
            rule.Consequent = new Clause(output, "Is", "Heating");
            rie.AddRule(rule);

            rule = new Rule("Rule 4");
            rule.AddAntecedent(new Clause(tempError, "Is", "Negative"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Zero"));
            rule.Consequent = new Clause(output, "Is", "Cooling");
            rie.AddRule(rule);

            rule = new Rule("Rule 5");
            rule.AddAntecedent(new Clause(tempError, "Is", "Zero"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Zero"));
            rule.Consequent = new Clause(output, "Is", "Zero");
            rie.AddRule(rule);

            rule = new Rule("Rule 6");
            rule.AddAntecedent(new Clause(tempError, "Is", "Positive"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Zero"));
            rule.Consequent = new Clause(output, "Is", "Heating");
            rie.AddRule(rule);

            rule = new Rule("Rule 7");
            rule.AddAntecedent(new Clause(tempError, "Is", "Negative"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Positive"));
            rule.Consequent = new Clause(output, "Is", "Cooling");
            rie.AddRule(rule);

            rule = new Rule("Rule 8");
            rule.AddAntecedent(new Clause(tempError, "Is", "Zero"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Positive"));
            rule.Consequent = new Clause(output, "Is", "Cooling");
            rie.AddRule(rule);

            rule = new Rule("Rule 9");
            rule.AddAntecedent(new Clause(tempError, "Is", "Positive"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Positive"));
            rule.Consequent = new Clause(output, "Is", "Heating");
            rie.AddRule(rule);

            tempError.X = -1.0;
            Console.WriteLine("For Temperature Error: {0}", tempError.X);
            Console.WriteLine("Negative: " + tempError.GetMembership("Negative").degree(-1.0));
            Console.WriteLine("Zero: " + tempError.GetMembership("Zero").degree(-1.0));
            Console.WriteLine("Positive: " + tempError.GetMembership("Positive").degree(-1.0));

            tempErrorDot.X = 2.5;
            Console.WriteLine("For Temperature Error Dot: {0}", tempErrorDot.X);
            Console.WriteLine("Negative: " + tempErrorDot.GetMembership("Negative").degree(2.5));
            Console.WriteLine("Zero: " + tempErrorDot.GetMembership("Zero").degree(2.5));
            Console.WriteLine("Positive: " + tempErrorDot.GetMembership("Positive").degree(2.5));

            rie.Infer(output);

            Console.WriteLine("output: " + output.X);



            FrmFuzzySet tempCanvas         = new FrmFuzzySet("Output", output);
            FrmFuzzySet tempErrorCanvas    = new FrmFuzzySet("Temp Error", tempError);
            FrmFuzzySet tempErrorDotCanvas = new FrmFuzzySet("Temp Error Dot", tempErrorDot);

            tempCanvas.Show();
            tempErrorCanvas.Show();
            tempErrorDotCanvas.Show();
        }
Пример #9
0
        public void Run()
        {
            RuleInferenceEngine rie = new RuleInferenceEngine();

            FuzzySet output = new FuzzySet("Output", -100, 100, 1);

            output.AddMembership("Cooling", new FuzzyReverseGrade(-50, 0));
            output.AddMembership("Zero", new FuzzyTriangle(-50, 0, 50));
            output.AddMembership("Heating", new FuzzyGrade(0, 50));
            rie.AddFuzzySet(output.Name, output);

            FuzzySet tempError = new FuzzySet("Temperature Error", -4, 4, 0.05);

            tempError.AddMembership("Negative", new FuzzyReverseGrade(-2, 0));
            tempError.AddMembership("Zero", new FuzzyTriangle(-2, 0, 2));
            tempError.AddMembership("Positive", new FuzzyGrade(0, 2));
            rie.AddFuzzySet(tempError.Name, tempError);


            FuzzySet tempErrorDot = new FuzzySet("Temperature Error", -10, 10, 0.1);

            tempErrorDot.AddMembership("Negative", new FuzzyReverseGrade(-5, 0));
            tempErrorDot.AddMembership("Zero", new FuzzyTriangle(-5, 0, 5));
            tempErrorDot.AddMembership("Positive", new FuzzyGrade(0, 5));
            rie.AddFuzzySet(tempErrorDot.Name, tempErrorDot);

            /*
             *      1. If (e < 0) AND (er < 0) then Cool 0.5 & 0.0 = 0.0
             *      2. If (e = 0) AND (er < 0) then Heat 0.5 & 0.0 = 0.0
             *      3. If (e > 0) AND (er < 0) then Heat 0.0 & 0.0 = 0.0
             *      4. If (e < 0) AND (er = 0) then Cool 0.5 & 0.5 = 0.5
             *      5. If (e = 0) AND (er = 0) then No_Chng 0.5 & 0.5 = 0.5
             *      6. If (e > 0) AND (er = 0) then Heat 0.0 & 0.5 = 0.0
             *      7. If (e < 0) AND (er > 0) then Cool 0.5 & 0.5 = 0.5
             *      8. If (e = 0) AND (er > 0) then Cool 0.5 & 0.5 = 0.5
             *      9. If (e > 0) AND (er > 0) then Heat 0.0 & 0.5 = 0.0
             */

            Rule rule = new Rule("Rule 1");

            rule.AddAntecedent(new Clause(tempError, "Is", "Negative"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Negative"));
            rule.Consequent = new Clause(output, "Is", "Cooling");
            rie.AddRule(rule);

            rule = new Rule("Rule 2");
            rule.AddAntecedent(new Clause(tempError, "Is", "Zero"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Negative"));
            rule.Consequent = new Clause(output, "Is", "Heating");
            rie.AddRule(rule);

            rule = new Rule("Rule 3");
            rule.AddAntecedent(new Clause(tempError, "Is", "Positive"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Negative"));
            rule.Consequent = new Clause(output, "Is", "Heating");
            rie.AddRule(rule);

            rule = new Rule("Rule 4");
            rule.AddAntecedent(new Clause(tempError, "Is", "Negative"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Zero"));
            rule.Consequent = new Clause(output, "Is", "Cooling");
            rie.AddRule(rule);

            rule = new Rule("Rule 5");
            rule.AddAntecedent(new Clause(tempError, "Is", "Zero"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Zero"));
            rule.Consequent = new Clause(output, "Is", "Zero");
            rie.AddRule(rule);

            rule = new Rule("Rule 6");
            rule.AddAntecedent(new Clause(tempError, "Is", "Positive"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Zero"));
            rule.Consequent = new Clause(output, "Is", "Heating");
            rie.AddRule(rule);

            rule = new Rule("Rule 7");
            rule.AddAntecedent(new Clause(tempError, "Is", "Negative"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Positive"));
            rule.Consequent = new Clause(output, "Is", "Cooling");
            rie.AddRule(rule);

            rule = new Rule("Rule 8");
            rule.AddAntecedent(new Clause(tempError, "Is", "Zero"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Positive"));
            rule.Consequent = new Clause(output, "Is", "Cooling");
            rie.AddRule(rule);

            rule = new Rule("Rule 9");
            rule.AddAntecedent(new Clause(tempError, "Is", "Positive"));
            rule.AddAntecedent(new Clause(tempErrorDot, "Is", "Positive"));
            rule.Consequent = new Clause(output, "Is", "Heating");
            rie.AddRule(rule);

            tempError.X = -1.0;
            WriteLine("For Temperature Error: {0}", tempError.X);
            WriteLine("Negative: " + tempError.GetMembership("Negative").degree(-1.0));
            WriteLine("Zero: " + tempError.GetMembership("Zero").degree(-1.0));
            WriteLine("Positive: " + tempError.GetMembership("Positive").degree(-1.0));

            tempErrorDot.X = 2.5;
            WriteLine("For Temperature Error Dot: {0}", tempErrorDot.X);
            WriteLine("Negative: " + tempErrorDot.GetMembership("Negative").degree(2.5));
            WriteLine("Zero: " + tempErrorDot.GetMembership("Zero").degree(2.5));
            WriteLine("Positive: " + tempErrorDot.GetMembership("Positive").degree(2.5));

            rie.Infer(output);

            WriteLine("output: " + output.X);


            /*
             * Application.EnableVisualStyles();
             * Application.SetCompatibleTextRenderingDefault(false);
             *
             *
             * FrmFuzzySet tempCanvas = new FrmFuzzySet("Output", output);
             * FrmFuzzySet tempErrorCanvas = new FrmFuzzySet("Temp Error", tempError);
             * FrmFuzzySet tempErrorDotCanvas = new FrmFuzzySet("Temp Error Dot", tempErrorDot);
             *
             * SimpleUI.Instance.Display(tempCanvas);
             * SimpleUI.Instance.Display(tempErrorCanvas);
             * SimpleUI.Instance.Display(tempErrorDotCanvas);
             *
             * Application.Run(SimpleUI.Instance.View);*/
        }
Пример #10
0
        public ActionResult PostAnswers(QuestionsViewModel model)
        {
            var contentType = TouristicContentType.Tour;

            switch (model.QuestionGroupId.ToString())
            {
            case Service.Constants.TourGuid:
                contentType = TouristicContentType.Tour;
                break;

            case Service.Constants.ExcursionGuid:
                contentType = TouristicContentType.Excursion;
                break;

            case Service.Constants.EventGuid:
                contentType = TouristicContentType.Event;
                break;

            case WebApplication1.Service.Constants.HotelGuid:
                contentType = TouristicContentType.Hotel;
                break;

            case WebApplication1.Service.Constants.RestaurantGuid:
                contentType = TouristicContentType.Restaurant;
                break;
            }

            var dbContext = new ApplicationDbContext();
            var qg        = dbContext.QuestionGroups.Find(model.QuestionGroupId);

            List <Question> questions;
            var             serializerQuestion = new XmlSerializer(typeof(List <Question>));

            using (var reader = new StringReader(qg.QuestionsXml))
            {
                questions = (List <Question>)serializerQuestion.Deserialize(reader);
            }

            var selectedAnswers = new List <Answer>();
            var index           = 0;

            foreach (var q in questions)
            {
                foreach (var a in q.Answers)
                {
                    if (a.AnswerText == model.SelectedAnswers[index])
                    {
                        selectedAnswers.Add(a);
                        break;
                    }
                }
                index++;
            }

            List <Rule> rules;
            var         serializerRule = new XmlSerializer(typeof(List <Rule>));

            using (var reader = new StringReader(qg.RulesXml))
            {
                rules = (List <Rule>)serializerRule.Deserialize(reader);
            }

            var rie = new RuleInferenceEngine();

            rie.Rules = rules;
            foreach (var a in selectedAnswers)
            {
                foreach (var c in a.AnswerFactsList)
                {
                    rie.Facts.AddFact(c);
                }
            }

            rie.Infer();

            var tags =
                rie.WorkingMemory.Facts.Where(f => f.Variable == Service.Constants.ResultTag)
                .ToList()
                .Select(x => x.Value)
                .ToList();

            var contents = dbContext.TouristicContents.Where(x => tags.Contains(x.LogicalTag) && x.TouristicContentType == contentType).ToList();

            var result = string.Empty;
            var userId = User.Identity.GetUserId();

            if (contents.Any())
            {
                var urlHelper         = new UrlHelper(Request.RequestContext);
                var allContentsString = contents.Select(x =>
                                                        $"<a href='{urlHelper.Action("ViewTouristicContent", "Smart", new {id = x.ID})}'>{x.Name}</a>")
                                        .ToList();
                result += $"Рекомендуемые результаты:<br>{string.Join("<br>", allContentsString)}";
            }
            else
            {
                result = "В базе Smart-системы отсутствует подходящий контент, пожалуйста, воспользуйтесь общим каталогом.";
            }

            var interviewResult = dbContext.InterviewResults.Add(new InterviewResult
            {
                UserId  = userId,
                Date    = DateTime.Now,
                Results = result
            });

            dbContext.SaveChanges();

            return(RedirectToAction("SmartResults", new { resultsId = interviewResult.ID }));
        }