示例#1
0
        static void Main(string[] args)
        {
            int[] nodesPerLayer = new int[] { 2, 10, 10, 1 };

            NetworkStructure networkStructure = new NetworkStructure(nodesPerLayer);

            Random r = new Random();

            int numNetworks = 100;
            int generations = 100;

            Network[] networks = new Network[numNetworks];
            for (int i = 0; i < networks.Length; i++)
            {
                Weights weights = new Weights(nodesPerLayer);
                networks[i] = new Network(networkStructure, weights);
            }
            MutateNetworks mutateNetworks = new MutateNetworks(0.05f, 10, 2, nodesPerLayer);


            for (int i = 0; i < generations; i++)
            {
                for (int j = 0; j < networks.Length; j++)
                {
                    networks[j].ResetFitness();
                    float[] output = networks[j].EvaluateNetwork(new float[] { 1, 0 });

                    networks[j].AddFitness(1 - Math.Abs(output[0]));

                    Console.WriteLine(output[0]);
                }
                mutateNetworks.Mutate(networks);
            }

            for (int i = 0; i < networks.Length; i++)
            {
                Console.WriteLine("Fitness: " + networks[i].Fitness);
            }

            Console.WriteLine();

            Network bestNetwork = SortNetworks.GetBestNetwork(networks);

            Console.WriteLine(bestNetwork.EvaluateNetwork(new float[] { 1, 0 })[0]);

            Console.ReadKey();
        }
示例#2
0
        static void Main(string[] args)
        {
            int[] nodesPerLayer = new int[] { 2, 3, 1 };

            float[][] inputs = new float[][] { new float[] { 0, 0 },
                                               new float[] { 1, 0 },
                                               new float[] { 0, 1 },
                                               new float[] { 1, 1 } };

            float[] outputs = new float[] { 0, 1, 1, 0 };

            NetworkStructure networkStructure = new NetworkStructure(nodesPerLayer, new SigmoidActivation());

            Random r = new Random();

            int numNetworks = 100;

            Network[] networks = new Network[numNetworks];

            bool fromFile = false;

            Console.Write("Load from file ? (y/n) ");
            if (Console.ReadKey().Key == ConsoleKey.Y)
            {
                fromFile = true;
            }

            for (int i = 0; i < networks.Length; i++)
            {
                if (fromFile)
                {
                    networks[i] = new Network(networkStructure, WEIGHTS_FILE);
                }
                else
                {
                    networks[i] = new Network(networkStructure, nodesPerLayer);
                }
            }
            MutateNetworks mutateNetworks = new MutateNetworks(0.05f, 10, 2, nodesPerLayer);

            for (int i = 0; ; i++)
            {
                Console.WriteLine("Generation " + i + ": ");
                for (int j = 0; j < networks.Length; j++)
                {
                    networks[j].ResetFitness();
                    for (int k = 0; k < inputs.Length; k++)
                    {
                        float[] output = networks[j].EvaluateNetwork(inputs[k]);
                        networks[j].AddFitness(1 - Math.Abs(output[0] - outputs[k]));
                    }
                }



                Network bestNetwork = SortNetworks.GetBestNetwork(networks);
                Console.WriteLine("Fitness: " + bestNetwork.Fitness);
                for (int a = 0; a < inputs.Length; a++)
                {
                    Console.WriteLine(bestNetwork.EvaluateNetwork(inputs[a])[0]);
                    if (bestNetwork.Fitness > 3.8f)
                    {
                        goto generationLoop;
                    }
                }

                mutateNetworks.Mutate(networks);
            }

generationLoop:

            for (int i = 0; i < networks.Length; i++)
            {
                Console.WriteLine("Fitness: " + networks[i].Fitness);
            }

            Console.WriteLine();

            //Network bestNetwork = SortNetworks.GetBestNetwork(networks);
            //Console.WriteLine(bestNetwork.EvaluateNetwork(new float[] { 1, 0 })[0]);

            Console.Write("Save Weights? (y/n)");
            if (Console.ReadKey().Key == ConsoleKey.Y)
            {
                Network bestNetwork = SortNetworks.GetBestNetwork(networks);
                bestNetwork.SaveNetwork(WEIGHTS_FILE);
            }

            Console.WriteLine();
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
示例#3
0
文件: Program.cs 项目: quinv/SimpleAI
        static void Main(string[] args)
        {
            int[]            nodesPerLayer    = new int[] { 400, 200, 100, 50, 30, 13 };
            NetworkStructure networkStructure = new NetworkStructure(nodesPerLayer, new TanHActivation());

            Network[] networks = new Network[100];

            bool fromFile = false;

            fromFile = true;
            string fileName = WEIGHTS_FILE;

            //Console.Write("Load from file (empty if using no file): ");
            //string fileName = Console.ReadLine();
            //if(fileName != "")
            //{
            //    fromFile = true;
            //}
            //else
            //{
            //    fileName = WEIGHTS_FILE;
            //}

            for (int i = 0; i < networks.Length; i++)
            {
                if (fromFile)
                {
                    networks[i] = new Network(networkStructure, fileName);
                }
                else
                {
                    networks[i] = new Network(networkStructure, nodesPerLayer);
                }
            }
            MutateNetworks mutateNetworks = new MutateNetworks(0.05f, 10, 2, nodesPerLayer);

            int CurrentGeneration = 1;

            //infinite loop for training
            for (; ;)
            {
                Console.WriteLine("generation " + CurrentGeneration++ + ": ");
                //testing each networks
                for (int i = 0; i < networks.Length; i++)
                {
                    networks[i].ResetFitness();
                    Console.Write(".");
                    //testing 130 images
                    for (int j = 0; j < 130; j++)
                    {
                        PositionData evaluatedImage = ImageLoading.LoadImage(j / 10, TRAINIG_DATA_PATH);
                        float[]      output         = networks[i].EvaluateNetwork(evaluatedImage.imageInfo);
                        for (int k = 0; k < output.Length; k++)
                        {
                            if (evaluatedImage.expectedResult[k] == 1)
                            {
                                networks[i].AddFitness(output[k] * 10);
                            }
                            else
                            {
                                networks[i].AddFitness(-Math.Abs(output[k]));
                            }
                        }
                    }
                }

                Network bestNetwork = SortNetworks.GetBestNetwork(networks);

                Console.WriteLine();
                Console.WriteLine("Fitness: " + bestNetwork.Fitness);

                for (int j = 0; j < 13; j++)
                {
                    PositionData evaluatedImage = ImageLoading.LoadImage(j, TRAINIG_DATA_PATH);
                    float[]      output         = bestNetwork.EvaluateNetwork(evaluatedImage.imageInfo);
                    float        maxValue       = output.Max();
                    Console.WriteLine(output.ToList().IndexOf(maxValue));
                }

                bestNetwork.SaveNetwork(fileName);

                mutateNetworks.Mutate(networks);
            }
        }