Example #1
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);
        }
Example #2
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
        }
Example #3
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);
            }
        }
Example #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);
        }
Example #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;
            }));
        }