Ejemplo n.º 1
0
        private static void subtestIris(double[][] x, double[][] y, double[][] tx, double[][] ty, ITraining bprop)
        {
            KMEANSExtractorI extractor = new KMEANSExtractorI(15);
            var   timer = Stopwatch.StartNew();
            ANFIS fis   = ANFISBuilder <GaussianRule2> .Build(x, y, extractor, bprop, 1000);

            timer.Stop();

            double err = bprop.Error(tx, ty, fis.RuleBase);

            double correctClass = 0;

            for (int i = 0; i < tx.Length; i++)
            {
                double[] o = fis.Inference(tx[i]);
                for (int j = 0; j < ty[i].Length; j++)
                {
                    if (ty[i][j] == 1.0 && o[j] == o.Max())
                    {
                        correctClass++;
                    }
                }
            }

            Trace.WriteLine(string.Format("[{1}]\tIris Dataset Error {0} Classification Error {4}\tElapsed {2}\tRuleBase {3}", err, bprop.GetType().Name, timer.Elapsed, fis.RuleBase.Length, 1.0 - correctClass / ty.Length), "training");
            Assert.IsFalse(ty.Length - correctClass > 2);
        }
Ejemplo n.º 2
0
        private static void subtestLogisticsMap <T>(double[][] x, double[][] y, double[][] tx, double[][] ty, ITraining bprop) where T : IRule, new()
        {
            KMEANSExtractorIO extractor = new KMEANSExtractorIO(10);
            var   timer = Stopwatch.StartNew();
            ANFIS fis   = ANFISBuilder <T> .Build(x, y, extractor, bprop, 1000);

            timer.Stop();

            double err = bprop.Error(tx, ty, fis.RuleBase);



            Trace.WriteLine(string.Format("[{1} - {4}]\tLogistic map Error {0}\tElapsed {2}\tRuleBase {3}", err, bprop.GetType().Name, timer.Elapsed, fis.RuleBase.Length, typeof(T).Name), "training");
            Assert.IsFalse(err > 1e-2);
        }
Ejemplo n.º 3
0
        private void Solve(double[][] x, double[][] y, double[][] tx, double[][] ty, ITraining bprop)
        {
            KMEANSExtractorI extractor = new KMEANSExtractorI(int.Parse(txtbxRulesCount.Text));
            var timer = Stopwatch.StartNew();
            var fis   = ANFISBuilder <GaussianRule2> .Build(x, y, extractor, bprop, int.Parse(txtbxMaxIterCount.Text));

            timer.Stop();

            double err  = bprop.Error(tx, ty, fis.RuleBase);
            string line = "";

            double correctClass = 0;

            for (int i = 0; i < tx.Length; i++)
            {
                double[] o = fis.Inference(tx[i]);
                if (tx[i].Length == 4 && o.Length == 3)
                {
                    line = $"input: [{tx[i][0]}, {tx[i][1]}, {tx[i][2]}, {tx[i][3]}] output:[{o[0].ToString("F2")}, {o[1].ToString("F2")}, {o[2].ToString("F2")}] expected output: [{ty[i][0]}, {ty[i][1]}, {ty[i][2]}]";
                }
                for (int j = 0; j < ty[i].Length; j++)
                {
                    if (ty[i][j] == 1.0 && o[j] == o.Max())
                    {
                        correctClass++;
                        line += " OK";
                    }
                }
                if (tx[i].Length == 4 && o.Length == 3)
                {
                    InMemoryLogger.PrintMessage(line);
                }
            }

            InMemoryLogger.PrintMessage(string.Format("Correct answers {5}\tClassification Error {4}\tElapsed {2}\tRuleBase {3}", err, bprop.GetType().Name, timer.Elapsed, fis.RuleBase.Length, 1.0 - correctClass / ty.Length, correctClass));
        }
Ejemplo n.º 4
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);
        }