Пример #1
0
        public static ANFIS Build(double[][] input, double[][] output, IRuleExtractor RuleExtractor, ITraining trainer, int MaxIterations)
        {
            InMemoryLogger.PrintMessage("Start...");
            InMemoryLogger.PrintMessage($"Constructing initial rule set with [{RuleExtractor.GetType().Name}]");
            var ruleBase = RuleSetFactory <R> .Build(input, output, RuleExtractor).Select(z => z as IRule).ToList();

            InMemoryLogger.PrintMessage($"Get {ruleBase.Count} initial rules.");
            int epoch = 0;

            double trnError = 0.0;

            Console.WriteLine();
            Console.WriteLine();
            do
            {
                trnError = trainer.Iteration(input, output, ruleBase);
                InMemoryLogger.PrintMessage($"Epoch {epoch}, training error {trnError}");

                if (double.IsNaN(trnError))
                {
                    InMemoryLogger.PrintMessage("Failure! Training error is NAN.");
                    throw new Exception("Failure! Bad system design.");
                }
            } while (!trainer.isTrainingstoped() && epoch++ < MaxIterations);


            ANFIS fis = new ANFIS(ruleBase);

            InMemoryLogger.PrintMessage("Done");
            return(fis);
        }
Пример #2
0
        public static IRuleSet LoadRuleSet()
        {
            var rules = Directory.GetFiles(_basePath, "*.xml", SearchOption.AllDirectories)
                        .Select(x => new RuleSetFactory.RuleFile(Path.GetFileName(x), null, File.ReadAllBytes(x)));
            var rs = RuleSetFactory.FromRuleFiles(rules);

            return(rs);
        }
Пример #3
0
        private static IRuleSet BuildRuleset()
        {
            // getting file paths
            string[] paths = { "SampleValidationRule.xml", "SampleProcedureRule.xml" };

            // building RuleFile based on the path
            var files = paths.Select(f => new RuleSetFactory.RuleFile(f, string.Empty, File.ReadAllBytes(f)));

            // creating ruleset
            return(RuleSetFactory.FromRuleFiles(files));
        }
Пример #4
0
        public void ReturnsExpectedRuleSetForRuleSetTypeEnum(RuleSetType ruleSetType, Type expectedType)
        {
            _ruleSetsProvider.GetRuleSets().Returns(new Dictionary <RuleSetType, IRuleSet>()
            {
                { RuleSetType.Default, new DefaultRuleSet() },
                { RuleSetType.Relaxed, new RelaxedRuleSet() }
            });

            var ruleSetFactory = new RuleSetFactory(_ruleSetsProvider);

            var ruleSet = ruleSetFactory.GetRuleSet(ruleSetType);

            ruleSet.ShouldBeOfType(expectedType);
        }
Пример #5
0
        public Simulation(SimulationConfiguration config, IPresenter presenter, ITimer timer)
        {
            _generationCount = 0;
            _config          = config;
            _presenter       = presenter;
            _timer           = timer;

            var gridFactory = new GridFactory();
            var grid        = gridFactory.Create(config.GridType, config.Height, config.Width);

            var ruleSetFactory = new RuleSetFactory();
            var ruleset        = ruleSetFactory.Create(config.RuleSetType);

            _board = new Board(ruleset, grid);
        }
        public PhpBotController(IChatBotService chatBotService, IChatSessionService chatSessionService, IExerciseService exerciseService) : base(chatBotService, chatSessionService)
        {
            _ruleSet = RuleSetFactory.GetRuleSet("phpCourseRuleSet");

            _chatBotName     = "PhpChatBot";
            _relativeRoute   = "/api/php-bot";
            _exerciseService = exerciseService;

            AddRuleSet(RuleSetFactory.GetRuleSet("jokeRuleSet"));
            AddRuleSet(RuleSetFactory.GetRuleSet("greetingsRuleSet"));
            AddRuleSet(RuleSetFactory.GetRuleSet("goodbyeRuleSet"));

            _chatBot = new RestChatBot(_ruleSet.Rules);
            _chatBot.AddExerciseService(_exerciseService);
        }
Пример #7
0
        public void TestRulesetGeneration()
        {
            int trainingSamples = 100;

            double[][] x = new double[trainingSamples][];
            double[][] y = new double[trainingSamples][];

            Random rnd = new Random();

            for (int i = 0; i < trainingSamples; i++)
            {
                bool   isRigth = i % 2 == 0;
                double valx    = (isRigth ? 1 : -1) + (0.5 - rnd.NextDouble());

                x[i] = new double[] { valx, valx };
                y[i] = new double[] { isRigth ? 1 : 0, isRigth ? 0 : 1 };
            }

            KMEANSExtractorIO   extractor = new KMEANSExtractorIO(2);
            List <GaussianRule> ruleBase  = RuleSetFactory <GaussianRule> .Build(x, y, extractor);

            if (ruleBase[0].Z[0] > 0.5)
            {
                Assert.AreEqual(ruleBase[0].Z[0], 1, 1e-2);
                Assert.AreEqual(ruleBase[0].Z[1], 0, 1e-2);
                Assert.AreEqual(ruleBase[1].Z[1], 1, 1e-2);
                Assert.AreEqual(ruleBase[1].Z[0], 0, 1e-2);
                Assert.AreEqual(ruleBase[0].Parameters[0], 1, 1e-1);
                Assert.AreEqual(ruleBase[0].Parameters[1], 1, 1e-1);
                Assert.AreEqual(ruleBase[1].Parameters[0], -1, 1e-1);
                Assert.AreEqual(ruleBase[1].Parameters[1], -1, 1e-1);
            }
            else
            {
                Assert.AreEqual(ruleBase[0].Z[1], 1, 1e-2);
                Assert.AreEqual(ruleBase[0].Z[0], 0, 1e-2);
                Assert.AreEqual(ruleBase[1].Z[0], 1, 1e-2);
                Assert.AreEqual(ruleBase[1].Z[1], 0, 1e-2);
                Assert.AreEqual(ruleBase[1].Parameters[0], 1, 1e-1);
                Assert.AreEqual(ruleBase[1].Parameters[1], 1, 1e-1);
                Assert.AreEqual(ruleBase[0].Parameters[0], -1, 1e-1);
                Assert.AreEqual(ruleBase[0].Parameters[1], -1, 1e-1);
            }
        }
Пример #8
0
        private static void RunFromRuleSet()
        {
            // get list of lofic files
            var list = new[] { "CarsDiscountFlow.xml", "YearDiscount.xml" };

            // create a set of file
            var ruleFiles = list.Select(x => new RuleSetFactory.RuleFile(x, null, File.ReadAllBytes(x)));

            var rs     = RuleSetFactory.FromRuleFiles(ruleFiles);
            var engine = RuntimeEngine.FromRuleSet(rs, "CarsDiscountFlow.xml");

            var result = engine.Run(ConnectionString);
            var cars   = result.Context.VariableContainer["cars"] as IEnumerable;

            foreach (IDictionary <string, object> car in cars)
            {
                Console.WriteLine("{0} {1} {2} Discount: {3}", car["Made"], car["Model"], car["Year"], car["Discount"]);
            }
        }
Пример #9
0
        public static ANFIS Build(double[][] input, double[][] output, IRuleExtractor RuleExtractor, ITraining trainer, int MaxIterations)
        {
            _log.Info("Start...");
            _log.Info($"Constructing initial rule set with [{RuleExtractor.GetType().Name}]");
            var ruleBase = RuleSetFactory <R> .Build(input, output, RuleExtractor).Select(z => z as IRule).ToList();

            _log.Info($"Get {ruleBase.Count} initial rules.");
            int epoch = 0;

            double trnError = 0.0;

            Console.WriteLine();
            Console.WriteLine("Старт обучения");
            do
            {
                trnError = trainer.Iteration(input, output, ruleBase);
                _log.Info($"Epoch {epoch}, training error {trnError}");

                if (double.IsNaN(trnError))
                {
                    _log.Info("Failure! Training error is NAN.");
                    throw new Exception("Failure! Bad system design.");
                }

                if (epoch % 100 == 0)
                {
                    Console.WriteLine("Обучение: " + epoch.ToString() + " итерация. Ошибка: " + trnError);
                }
            } while (!trainer.isTrainingstoped() && epoch++ < MaxIterations);


            ANFIS fis = new ANFIS(ruleBase);

            Console.WriteLine("Завершение обучения");
            _log.Info("Done");
            return(fis);
        }