Ejemplo n.º 1
0
        static Logger _log = new Logger("ABuilder", InternalTraceLevel.Default, TextWriter.Null);        // LogManager.GetLogger("ABuilder");
        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.");
                }
            } while (!trainer.isTrainingstoped() && epoch++ < MaxIterations);


            ANFIS fis = new ANFIS(ruleBase);

            _log.Info("Done");
            return(fis);
        }
Ejemplo n.º 2
0
        private static void subTestOptimization1(ITraining bprop, double[][] x, double[][] y, double[][] tx, double[][] ty)
        {
            GaussianRule2[] terms = new GaussianRule2[] { new GaussianRule2() };
            terms[0].Init(
                new double[] { 0.5, 0.3 },
                new double[] { 0 },
                new double[] { 0.0, 0.0 });

            int    epoch    = 0;
            int    maxit    = 1000;
            double trnError = 0.0;
            double tstError = 0.0;

            do
            {
                trnError = bprop.Iteration(x, y, terms);
                tstError = bprop.Error(tx, ty, terms);
            } while (!bprop.isTrainingstoped() && epoch++ < maxit);

            Trace.WriteLine(string.Format("Epochs {0} - Error {1}/{2}", epoch, trnError, tstError), "training");
            Assert.IsFalse(tstError > 1e-2);
            Assert.IsFalse(trnError > 1e-2);
            Assert.AreEqual(terms[0].Z[0], 1.0, 1e-2);
        }