Exemplo n.º 1
0
        public Qaib(MainWindow window)
        {
            //Load test bytes
            test = load();

            //truncate test bytes
            byte[] truncatedTest = new byte[256];
            for (int i = 0; i < truncatedTest.Length; i++)
            {
                truncatedTest[i] = test[i];
            }
            test = truncatedTest;

            test = CharToSymbol(test);

            basicPredictTablePrint(test);

            //print Test bytes metrics
            Console.WriteLine("Test text loaded with: " + test.Length + " Bytes.");
            Console.WriteLine("Test text loaded with: " + test.Length / 1024f + " KBytes.");
            Console.WriteLine("Test text loaded with: " + (test.Length / 1024f) / 1014f + " MBytes.");

            //Set char alphabet as test for translation display
            //test = new byte[256];
            //for (int i = 0; i < 256; i++)
            //    test[i] = (byte)i;

            bool printTestStrings = false;

            if (printTestStrings)
            {
                string inputText = "";
                for (int i = 0; i < test.Length; i++)
                {
                    inputText += (char)test[i];
                }
                Console.WriteLine("\nTest string:");
                Console.WriteLine(inputText);

                test = CharToSymbol(test);
                string symbolBytes = "";
                for (int i = 0; i < test.Length; i++)
                {
                    symbolBytes += ("" + (int)test[i]).PadLeft(4);
                }
                Console.WriteLine("\nTest symbol bytes:");
                Console.WriteLine(symbolBytes);

                test = SymbolToChar(test);
                string symbolText = "";
                for (int i = 0; i < test.Length; i++)
                {
                    symbolText += (char)test[i];
                }
                Console.WriteLine("\nTest symbol texted:");
                Console.WriteLine(symbolText);
            }

            //test = load();
            //test = CharToSymbol(test);

            TablePredictor f0 = TablePredictor.LoadFromFile("AAABakef0.tp");

            byte[] g0 = f0.predictStates(test);
            Console.WriteLine(f0.getStats());
            test        = g0;
            symbolCount = 64;

            //more layers
            TablePredictor f1 = TablePredictor.LoadFromFile("aaap.tp");

            byte[] g1 = f1.predictStates(g0);
            Console.WriteLine(f1.getStats());

            //TablePredictor f2 = TablePredictor.LoadFromFile("Bakef2.tp");
            //byte[] g2 = f2.predictStates(g1);
            //Console.WriteLine(f2.getStats());

            //TablePredictor f3 = TablePredictor.LoadFromFile("Bakef3.tp");
            //byte[] g3 = f3.predictStates(g2);
            //Console.WriteLine(f3.getStats());

            byte[] display;
            //display = f3.project(g3);
            //display = f2.project(g2);
            display = f1.project(g1);
            display = f0.project(display);
            display = SymbolToChar(display);
            string os = "";

            for (int i = 0; i < display.Length; i++)
            {
                os += (char)display[i];
            }
            Console.WriteLine("\nTest symbol texted:");
            Console.WriteLine(os);

            //Setup2();
        }
Exemplo n.º 2
0
        void VFLearner()
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            Random rdm = new Random(0);

            TablePredictor testTable = new TablePredictor(symbolCount, population[0].stateSize);
            TablePredictor temp;

            int printRun = 100000;

            double crossRatio        = 0.0;
            double randomRatio       = 0.1;
            double mutatedRatio      = 1 - (crossRatio + randomRatio);
            double mutationIntensity = 0.5;
            int    runs = int.MaxValue;
            double decision;
            int    parent;

            for (int i = 0; i < runs; i++)
            {
                cycle++;

                if (i % printRun == 0)
                {
                    population[0].SaveToFile("Run0-" + (i / printRun) + "v0.tp");
                    Console.WriteLine("m.i.: " + ("" + mutationIntensity).PadRight(10).Substring(0, 6));
                    printPopulation(1);
                }

                decision = rdm.NextDouble();
                if (decision < mutatedRatio)
                {
                    //Mutation
                    parent = rdm.Next(populationCount);
                    TablePredictor.mutateOverride(testTable, population[parent], rdm, mutationIntensity);
                    testTable.birthTime = cycle;
                }
                else if (decision < mutatedRatio + crossRatio)
                {
                    throw new Exception("NO CROSS ALLOWED");
                    //Cross
                    int parent1 = rdm.Next(populationCount);
                    int parent2 = rdm.Next(populationCount);
                    TablePredictor.crossOverride(testTable, population[parent1], population[parent2], rdm);
                    testTable.birthTime = cycle;

                    if (testTable.species == population[parent1].species)
                    {
                        parent = parent1;
                    }
                    else
                    {
                        parent = parent2;
                    }
                }
                else
                {
                    TablePredictor.randomOverride(testTable, rdm);
                    parent = -1;
                }

                testTable.testPredict(test);

                if (testTable.accuracy > population[populationCount - 1].accuracy)
                {
                    if (parent != -1)
                    {
                        //Children analysis
                        if (population[parent].accuracy < testTable.accuracy)
                        {
                            //Parent beat
                            //Soft change for sucessful mutation intensity (x2 because of random average behaviour)
                            mutationIntensity = 0.8 * mutationIntensity + 0.2 * (2 * testTable.mutationIntensity);
                            if (mutationIntensity > 0.9)
                            {
                                mutationIntensity = 0.9; //maximum allowed mutation intensity
                            }
                            temp = population[parent];
                            population[parent] = testTable;
                            testTable          = temp;

                            //Reorder
                            for (int j = parent; j > 0; j--)
                            {
                                if (population[j - 1].accuracy < population[j].accuracy)
                                {
                                    temp = population[j - 1];
                                    population[j - 1] = population[j];
                                    population[j]     = temp;
                                }
                            }
                        }
                    }
                    else
                    {
                        //Random win, order
                        for (int j = 0; j < populationCount; j++)
                        {
                            if (testTable.accuracy > population[j].accuracy)
                            {
                                temp          = population[j];
                                population[j] = testTable;
                                testTable     = temp;
                            }
                        }
                    }
                }
            }

            stopwatch.Stop();
            long elapsedTime = stopwatch.ElapsedMilliseconds;

            Console.WriteLine("Genetic Algorithms runtime at: \nMiliseconds: " + elapsedTime + "\nSeconds: " + elapsedTime / 1000f + "\nMinutes" + (elapsedTime / 1000f) / 60f);

            printPopulation(populationCount);

            TablePredictor best = population[0];

            Console.WriteLine("Rank : 0 " + best.getStats());


            best.SaveToFile("Potato.tp");

            TablePredictor lel = TablePredictor.LoadFromFile("Potato.tp");

            lel.testPredict(test);
            Console.WriteLine("Loaded: " + lel.getStats());
        }