コード例 #1
0
ファイル: LogicGate.cs プロジェクト: SteffenBlake/AIExample
        public static void Run()
        {
            var service = new DeepThink(1, 2, 4, 1);

            var trainingData = new Dictionary <double[], double[]>
            {
                { new[] { 0D, 0D }, new[] { 0D } },
                { new[] { 0D, 1D }, new[] { 1D } },
                { new[] { 1D, 0D }, new[] { 0D } },
                { new[] { 1D, 1D }, new[] { 1D } }
            };


            var currentError = 1D;

            for (var n = 0; currentError > Errorthresh; n++)
            {
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("Training - Sets: {0} - Error threshold: {1:P}", n, Errorthresh);
                var errorList = new List <double>();
                foreach (var trainer in trainingData)
                {
                    service.Train(trainer.Key, trainer.Value);
                    errorList.Add(service.LastError);
                }
                currentError = Math.Sqrt(errorList.Sum(e => e * e));
            }

            Console.WriteLine("Error: {0:P}", currentError);

            foreach (var trainer in trainingData)
            {
                Console.WriteLine("Inputs: {0}", string.Join("-", trainer.Key));
                Console.WriteLine("Expected Out: {0}", string.Join("-", trainer.Value));

                var outputs = service.Run(trainer.Key);
                Console.WriteLine("Actual Out: {0}\n", string.Join("-", outputs));
            }
        }
コード例 #2
0
ファイル: Mover.cs プロジェクト: SteffenBlake/AIExample
        public static void Run()
        {
            var service = new DeepThink(220, 221, 4, 4);
            var x       = 15;
            var y       = 7;

            DrawScreen(x, y, "");

            var lastOutputs = new double[] { };

            var reinforcing = false;

            while (true)
            {
                var keyDown = (int)Console.ReadKey(true).Key;
                switch (keyDown)
                {
                case Plus:
                    service.Reward();
                    DrawScreen(x, y, "Rewarded");
                    break;

                case Minus:
                    service.Punish();
                    DrawScreen(x, y, "Punished");
                    break;

                case Enter:
                    reinforcing = true;
                    DrawScreen(x, y, "Training... Press a key to reinforce it.");
                    break;

                default:
                    service.Reset();
                    var inputs = new double[220];
                    for (var n = 0; n < 220; n++)
                    {
                        inputs[n] = 0D;
                    }
                    inputs[keyDown] = 1D;

                    if (reinforcing)
                    {
                        service.Train(inputs, lastOutputs);
                        reinforcing = false;
                        DrawScreen(x, y, "Trained");
                    }
                    else
                    {
                        lastOutputs = service.Run(inputs, false).Select(v => Math.Round(v)).ToArray();

                        if (lastOutputs[0] == 1)
                        {
                            x -= 1;
                        }
                        if (lastOutputs[1] == 1)
                        {
                            y -= 1;
                        }
                        if (lastOutputs[2] == 1)
                        {
                            x += 1;
                        }
                        if (lastOutputs[3] == 1)
                        {
                            y += 1;
                        }

                        if (x < 1)
                        {
                            x = 30;
                        }
                        if (x > 30)
                        {
                            x = 1;
                        }
                        if (y < 1)
                        {
                            y = 15;
                        }
                        if (y > 15)
                        {
                            y = 1;
                        }

                        Console.Clear();
                        DrawScreen(x, y, "");
                    }
                    break;
                }
            }
        }
コード例 #3
0
        public static void Run()
        {
            var dir    = Directory.GetCurrentDirectory().Replace("bin\\Debug", "dictionary.txt");
            var reader = new StreamReader(dir);

            var wordList = new List <string>();


            while (reader.Peek() != 0)
            {
                var nextLine = reader.ReadLine();
                if (nextLine == null)
                {
                    break;
                }
                wordList.Add(nextLine.Trim().ToLower());
            }

            var wordArray = wordList.Select(s => s.Select(ToSimple).ToArray()).ToArray();

            var maxLength = wordList.Select(a => a.Length).Max();

            var service = new DeepThink(1, maxLength, maxLength + 1, 1);

            //  B      C      D      F      G      H      J       K       L      M        N       P      Q        R       S      T       V       W        X        Y     Z
            var consanents = new List <double>()
            {
                2 / 26D, 3 / 26D, 4 / 26D, 6 / 26D, 7 / 26D, 8 / 26D, 10 / 26D, 11 / 26D, 12 / 26D, 13 / 26D, 14 / 26D, 16 / 26D, 17 / 26D, 18 / 26D, 19 / 26D, 20 / 26D, 21 / 26D, 22 / 26D, 23 / 26D, 24 / 26D, 1D
            }.ToArray();
            var rnd = new Random();

            for (int n = 0, max = wordArray.Length; n < max; n++)
            {
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("Iterations: {0}/{1}", n, max);
                var word = wordArray[n];
                service.Train(word, new[] { 1D });
                var error1 = service.LastError;

                var length   = word.Length;
                var badArray = new double[length];
                for (var y = 0; y < length; y++)
                {
                    badArray[y] = consanents[rnd.Next(0, 21)];
                }
                var output = service.Run(badArray);
                service.Train(badArray, new[] { 0D });
                var error2     = service.LastError;
                var totalError = Math.Sqrt(error1 * error1 + error2 * error2);
                Console.WriteLine("Last Error: {0:P}", totalError);
            }

            Console.WriteLine("Ready! Give me words to try and guess! Type 'Exit' to end");
            while (true)
            {
                var input = Console.ReadLine().Trim().ToLower();
                if (input == "exit")
                {
                    break;
                }

                if (input.All(char.IsLetter))
                {
                    var charArray = input.Select(ToSimple).ToArray();
                    var output    = service.Run(charArray).First();
                    Console.WriteLine("Confidence: {0:P}", output);
                }
                else
                {
                    Console.WriteLine("Now I know that's definitely not a word! Try again!");
                }
            }
        }