Esempio 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);
        }
Esempio 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);
        }
Esempio n. 3
0
        public void ANFIS_OutputFromDataSet()
        {
            var sampleSize = RobotArmDataSet.Input.Length - 1;
            //StochasticBatch sprop = new StochasticBatch(sampleSize, 1e-5);
            //sprop.UnknownCaseFaced += AddRule<GaussianRule2>;
            //var sprop = new Backprop(1e-1);
            var sprop     = new StochasticQprop(sampleSize);
            var extractor = new KMEANSExtractorIO(25);

            //ANFIS fis = ANFISBuilder<GaussianRule2>.Build(RobotArmDataSet.Input, RobotArmDataSet.OutputTheta1, extractor, sprop, 150);
            ANFIS fis = ANFISBuilder <GaussianRule> .Build(RobotArmDataSet.Input, RobotArmDataSet.OutputTheta1, extractor, sprop, 150);

            var output1 = fis.Inference(new[] { 1.10413546487088, 2.81104319371924 }).FirstOrDefault();             // 1.1
            var output2 = fis.Inference(new[] { 2.31665592712393, 1.9375717475909 }).FirstOrDefault();              // 0.6
            var output3 = fis.Inference(new[] { 2.88944142930409, 16.7526454098038 }).FirstOrDefault();             // 1.4
        }
Esempio n. 4
0
        void Awake()
        {
            //Check if there is already an instance of SoundManager
            if (instance == null)
            {
                //if not, set it to this.
                instance = this;
            }
            //If instance already exists:
            else if (instance != this)
            {
                //Destroy this, this enforces our singleton pattern so there can only be one instance of SoundManager.
                Destroy(gameObject);
            }

            //Set SoundManager to DontDestroyOnLoad so that it won't be destroyed when reloading our scene.
            DontDestroyOnLoad(gameObject);
            Backprop          bprop     = new Backprop(1e-2);
            KMEANSExtractorIO extractor = new KMEANSExtractorIO(5);

            fis = ANFISBuilder <GaussianRule> .Build(gameParams, musicParams, extractor, bprop, 5);
        }
Esempio n. 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ruleNumber"></param>
        /// <param name="maxIterations"></param>
        /// <returns></returns>
        public Task <bool> TrainANFIS(int ruleNumber, int maxIterations, bool useAnalicitalOutcomeForTraining = false)
        {
            return(Task.Run(() => {
                if (!IsDataSetCalculated)
                {
                    throw new ApplicationException("DataSet is not calculated or provided.");
                }

                var sampleSize = Positions.Count() - 1;
                var dynamicObj = useAnalicitalOutcomeForTraining ? Positions.Select(x => new { Point = x, KinematicOutCome = CalculateArmJoint(x).GetAwaiter().GetResult().FirstOrDefault() }) : null;

                var input = useAnalicitalOutcomeForTraining
                                                                ? dynamicObj.Select(x => x.Point).ConvertToANFISParameter()
                                                                : Positions.ConvertToANFISParameter();
                var theta1ANFIS = Task.Run(() => {
                    var sPropTheta1 = new StochasticQprop(sampleSize);
                    var extractorForTheta1 = new KMEANSExtractorIO(ruleNumber);
                    var expectedOutcome = useAnalicitalOutcomeForTraining
                                                                                        ? dynamicObj.Select(x => new[] { x.KinematicOutCome.Theta1.ConvertRadiansToDegrees() }).ToArray()
                                                                                        : AnglesGrid.First().ConvertToANFISParameter();
                    Theta1ANFIS = ANFISBuilder <GaussianRule> .Build(input,
                                                                     expectedOutcome, extractorForTheta1, sPropTheta1, maxIterations);
                });
                var theta2ANFIS = Task.Run(() => {
                    var sPropTheta2 = new StochasticQprop(sampleSize);
                    var extractorForTheta2 = new KMEANSExtractorIO(ruleNumber);
                    var expectedOutcome2 = useAnalicitalOutcomeForTraining
                                                                                ? dynamicObj.Select(x => new[] { x.KinematicOutCome.Theta2.ConvertRadiansToDegrees() }).ToArray()
                                                                                : AnglesGrid.Last().ConvertToANFISParameter();

                    Theta2ANFIS = ANFISBuilder <GaussianRule> .Build(input,
                                                                     expectedOutcome2, extractorForTheta2, sPropTheta2, maxIterations);
                });

                Task.WaitAll(theta1ANFIS, theta2ANFIS);
                IsANFISTrained = true;
                return true;
            }));
        }
Esempio n. 6
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));
        }