Example #1
0
        public void Run()
        {
            var input  = new BitArray(25, RndInit);
            var output = new BitArray(4);
            var W      = new BitArray(100, RndInit);

            BinaryNN.XnorAndActivate(W, input, output, BinaryNN.SignHigh);
            Console.WriteLine($"Input:  {input}");
            Console.WriteLine($"W:      {W}");
            Console.WriteLine($"Output: {output}");
        }
Example #2
0
        public void Run( )
        {
            var szIn  = 0;
            var szHid = 0;
            var szOut = 0;

            Dictionary <int, int> truthTable = new Dictionary <int, int>();


            if (SmallToy == 0)
            {
                szIn  = 2;
                szHid = 3;
                szOut = 1;
                truthTable.Add(0b11, 0b0);
                truthTable.Add(0b10, 0b1);
                truthTable.Add(0b01, 0b1);
                truthTable.Add(0b00, 0b0);
            }
            else if (SmallToy == 1)
            {
                szIn  = 5;
                szHid = 11;
                szOut = 1;

                //NOR
                truthTable.Add(0b00011, 0b0);
                truthTable.Add(0b00010, 0b0);
                truthTable.Add(0b00001, 0b0);
                truthTable.Add(0b00000, 0b1);

                //XOR
                truthTable.Add(0b00111, 0b0);
                truthTable.Add(0b00110, 0b1);
                truthTable.Add(0b00101, 0b1);
                truthTable.Add(0b00100, 0b0);

                //NXOR
                truthTable.Add(0b01011, 0b1);
                truthTable.Add(0b01010, 0b0);
                truthTable.Add(0b01001, 0b0);
                truthTable.Add(0b01000, 0b1);

                //OR
                truthTable.Add(0b01111, 0b1);
                truthTable.Add(0b01110, 0b1);
                truthTable.Add(0b01101, 0b1);
                truthTable.Add(0b01100, 0b0);


                //AND
                truthTable.Add(0b10011, 0b1);
                truthTable.Add(0b10010, 0b0);
                truthTable.Add(0b10001, 0b0);
                truthTable.Add(0b10000, 0b0);
            }



            BitArray[] I = new BitArray[truthTable.Count];
            BitArray[] O = new BitArray[truthTable.Count];
            BitArray[] H = new BitArray[truthTable.Count];
            IEnumerable <BitArray>[] w = new IEnumerable <BitArray> [truthTable.Count];
            int iTT = 0;

            foreach (var tti in truthTable)
            {
                I[iTT] = new BitArray(new int[] { tti.Key }, szIn);
                O[iTT] = new BitArray(new int[] { tti.Value }, szOut);
                H[iTT] = new BitArray(szHid);

                iTT++;
            }

            BitArray WA     = null;
            BitArray WB     = new BitArray(szHid * szOut, RndInit);
            var      output = new BitArray(szOut);
            int      loops  = 0;

            while (loops < int.MaxValue)
            {
                loops++;

                //Get random weights for hidden layer
                WA = new BitArray(szIn * szHid, RndInit);

                //Generate hidden layer output values for each truth table entry
                for (int i = 0; i < I.Length; i++)
                {
                    BinaryNN.XnorAndActivate(WA, I[i], H[i], BinaryNN.SignMid);
                }

                //Make sure hidden layers produce unique values for all the inputs in the truth table
                if (H.Distinct().Count() < H.Length)
                {
                    continue;
                }

                //Find all the weights which produces a valid output for the hidden layer for each truth table entry (this is effectively the back propagation step)
                for (int i = 0; i < I.Length; i++)
                {
                    w[i] = BinaryNN.FindWeights(H[i], O[i], BinaryNN.SignMid);
                }

                //find a set of weights common to all the thruth table entries
                var wDistinct = w[0].Join(w[1], l => l.AsString(), r => r.AsString(), (l, r) => l);
                for (int i = 2; i < w.Length; i++)
                {
                    wDistinct = wDistinct.Join(w[i], l => l.AsString(), r => r.AsString(), (l, r) => l);
                }


                if (wDistinct.Count() > 0)
                {
                    WB = wDistinct.First();

                    foreach (var tti in truthTable)
                    {
                        var input  = new BitArray(new int[] { tti.Key }, szIn);
                        var hidden = new BitArray(szHid);
                        BinaryNN.XnorAndActivate(WA, input, hidden, BinaryNN.SignMid);
                        BinaryNN.XnorAndActivate(WB, hidden, output, BinaryNN.SignMid);
                        Console.WriteLine($"{input} -> {output} (Should be {new BitArray(new int[] { tti.Value }, output.Length)})");
                    }

                    Console.WriteLine($"WA: {WA}");
                    Console.WriteLine($"WB: {WB}");
                    Console.WriteLine($"Loop {loops}");

                    Console.ReadLine();
                }
                else
                {
                    if (loops % 100 == 0)
                    {
                        Console.Clear();
                        Console.WriteLine($"Nothing found for WA: {WA} (Loop {loops})");
                    }

                    //Console.WriteLine($"Nothing found for WA: {WA}");
                }
            }
        }
Example #3
0
        public void Run()
        {
            var csvFile = "Results.csv";

            if (File.Exists(csvFile))
            {
                File.Delete(csvFile);
            }

            var table = new ConsoleTable("Input Size", "Output Size", "Search Size", "Solutions Found", "% Of Space", "Search Time");

            File.AppendAllText(csvFile, "Input Size,Output Size,Search Size,Solutions Found,% Of Space,Search Time\r\n");
            int sizeFomMax = 36;

            var searchJobs = new List <SearchTask>();

            for (int szFrom = 1; szFrom <= sizeFomMax; szFrom++)
            {
                for (int szTo = 1; szTo <= szFrom && szFrom * szTo <= sizeFomMax; szTo++)
                {
                    //if (szFrom == 17 && szTo == 2)
                    //if (szFrom * szTo > 32)
                    searchJobs.Add(new SearchTask {
                        SzFrom = szFrom, SzTo = szTo
                    });
                }
            }

            var jobs = new ConcurrentQueue <SearchTask>();

            foreach (var searchJob in searchJobs.OrderBy(o => o.SzSearchSpace))
            {
                jobs.Enqueue(searchJob);
            }

            Parallel.Invoke(Progress, RunJob, RunJob);//, RunJob, RunJob, RunJob, RunJob);

            Console.ReadLine();

            void Progress()
            {
                while (true)
                {
                    Console.Clear();

                    lock (csvFile)
                        table.Write();

                    var compJobs    = searchJobs.Where(j => j.Elapsed != null).Count();
                    var waitingJobs = searchJobs.Where(j => j.Elapsed == null && j.Progress == 0).Count();
                    var busyJobs    = searchJobs.Where(j => j.Elapsed == null && j.Progress > 0);

                    foreach (var busyJob in busyJobs)
                    {
                        Console.WriteLine($"Searching... (szFrom={busyJob.SzFrom}, szTo={busyJob.SzTo}, searchSpace={busyJob.SzSearchSpace:E}, numSolutions={busyJob.NumSolutions}, progress={busyJob.Progress * 1f / busyJob.SzSearchSpace:P0})");
                    }
                    if (busyJobs.Count() == 0 && waitingJobs == 0)
                    {
                        break;
                    }

                    Thread.Sleep(5000);
                }
            }

            void RunJob()
            {
OUTERLOOP:
                while (jobs.TryDequeue(out SearchTask searchJob))
                {
                    try
                    {
                        var mapFrom     = new BitArray(searchJob.SzFrom, RndInit);
                        var mapToTarget = new BitArray(searchJob.SzTo, RndInit);
                        var mapToActual = new BitArray(mapToTarget.Length);
                        var W1          = new BitArray(mapFrom.Length * mapToTarget.Length);


                        long numSolutions = 0;
                        var  sw           = Stopwatch.StartNew();
                        var  prevStr      = "";
                        for (long iLoop = 0; iLoop < searchJob.SzSearchSpace; iLoop++)
                        {
                            searchJob.Progress = iLoop;

                            BinaryNN.XnorAndActivate(W1, mapFrom, mapToActual, BinaryNN.SignHigh);

                            if (mapToTarget == mapToActual)
                            {
                                numSolutions++;
                            }

                            try
                            {
                                W1.Inc();
                            }
                            catch (OverflowException ove)
                            {
                                File.AppendAllText($"IntError-OVE.txt", $"SzFrom={searchJob.SzFrom},SzTo={searchJob.SzTo},SzSearchSpace={searchJob.SzSearchSpace},i={iLoop},prevStr={prevStr}\r\n{ove}\r\n");
                                goto OUTERLOOP;
                            }

                            if (iLoop % 10000 == 0)
                            {
                                prevStr = $"@{iLoop} => {W1}";
                            }

                            searchJob.NumSolutions = numSolutions;
                        }
                        sw.Stop();
                        searchJob.Elapsed = sw.Elapsed;

                        lock (csvFile)
                        {
                            File.AppendAllText(csvFile, $"{mapFrom.Length},{mapToTarget.Length},{searchJob.SzSearchSpace},{numSolutions},{ numSolutions * 1f / searchJob.SzSearchSpace:P1},\"{sw.Elapsed}\"\r\n");
                            table.AddRow(mapFrom.Length, mapToTarget.Length, searchJob.SzSearchSpace, numSolutions, $"{numSolutions * 1f / searchJob.SzSearchSpace:P1}", sw.Elapsed);
                        }
                    }
                    catch (Exception e)
                    {
                        File.AppendAllText($"IntError.txt", $"{e}\r\n");
                    }
                }
            }
        }