示例#1
0
        //private static TransferFunction Transfer = new ReLUFunction();
        //private static TransferFunction Transfer = new SigmoidFunction();

        private static void Main(string[] args)
        {
            var data = DataReader.ReadFromFile("data/iris.data").OrderByDescending(i => i.SepalWidth).ToArray();

            data = NormalizeData(data, Transfer.XMin, Transfer.XMax).ToArray();
            var networks = GenerateNetworks().ToArray();
            var genetic  = new Genetic.Genetic(new ConstantParamsProvider(0.02F, 0.001F).GetParams, NextFloat);
            var indivs   = new Individual[NetworkCount];

            int   genIndx = 0;
            float avgFitness;
            float bestFitness;
            var   startT = Environment.TickCount;

            do
            {
                var t = Environment.TickCount;
                Console.Write($"\nGen: {genIndx}");
                Parallel.For(0, NetworkCount, (n) => {
                    var curNet = networks[n];
                    var grade  = Grade(curNet, data);
                    indivs[n]  = curNet.ToIndividual(grade);
                });
                var res = genetic.Apply(indivs).ToArray();
                for (int n = 0; n < NetworkCount; n++)
                {
                    networks[n].SetWeights(res[n]);
                }
                avgFitness  = indivs.Average(ind => ind.Fitness);
                bestFitness = indivs.Max(ind => ind.Fitness);
                Console.Write($" - Fitness: avg={avgFitness:N3} max={bestFitness:N3}, variety: {indivs.AvgVariety():N3}, time {Environment.TickCount - t}Ms");
                genIndx++;
            } while(bestFitness < 0.96);

            Console.Write($"\n\nDone in {Environment.TickCount - startT}Ms");
            Console.ReadKey();
        }