Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            PyDrawGraph.pyGraphDrawerFilePath = pyFitnessGraphPath;
            GARandomManager.Random            = new RandomClassic((int)DateTime.Now.Ticks);

            var salesman = new Salesman(GetLocations());

            var gif = new GifDrawer(
                salesman.Locations,
                () => salesman.GetBestPath(),
                gifSlowMotionGradient: 1.02f,
                gifSlowMotionCount: 0.8f);

            var fitnessGrph = new GraphDataCollector();

            for (var i = 1; i < maxIterations; i++)
            {
                salesman.Evolve();

                // Debugging stuff...
                var bestDist = salesman.CurrentBestDist();
                Console.WriteLine(String.Format(
                                      "{0}) dist = {1}", i, bestDist));
                fitnessGrph.Tick(i, bestDist);

                if (drawGif)
                {
                    gif.Tick(i);
                }
            }

            if (drawGif)
            {
                gif.ShowGif(maxPos);
            }

            if (drawResult)
            {
                TSGraph.DrawGrapghResult(
                    maxPos.X, maxPos.Y,
                    salesman.Locations, salesman.GetBestPath());

                fitnessGrph.Draw("Distance", "b-");
            }
        }
        static void Main(string[] args)
        {
            NeuralNetDrawer.pyGraphDrawerPath = pyNeuralNetGraphDrawerPath;
            PyDrawGraph.pyGraphDrawerFilePath = pyFitnessGraphPath;

            GARandomManager.Random = new RandomClassic((int)DateTime.Now.Ticks);
            var neuralNetDrawer  = new NeuralNetDrawer(false);
            var fitnessCollector = new GraphDataCollector();

            NeuralGenomeToJSONExtension.distBetweenNodes *= 5;
            NeuralGenomeToJSONExtension.randomPosTries    = 10;
            NeuralGenomeToJSONExtension.xPadding          = 0.03f;
            NeuralGenomeToJSONExtension.yPadding          = 0.03f;

            var program = new Program();
            var watch   = new Stopwatch();

            watch.Start();

            for (var i = 0; i < maxIterations; i++)
            {
                //program.EvaluateAsync(25).Wait();
                program.Evaluate();
                //program.EvaluateAsyncEach().Wait();

                if (i % 10 == 0)
                {
                    watch.Stop();
                    Console.WriteLine(watch.Elapsed);
                    watch.Restart();


                    var fintessSum = program.geneticManager
                                     .GenerationManager
                                     .CurrentGeneration
                                     .Genomes.Sum(x => x.Fitness);
                    var best = program.geneticManager
                               .GenerationManager
                               .CurrentGeneration
                               .BestGenome as NeuralGenome;

                    //fitnessCollector.Tick(i, best.Fitness);
                    Console.WriteLine(String.Format(
                                          "{0}) Best:{1:0.00} Sum:{2:0.00}",
                                          i,
                                          best.Fitness,
                                          fintessSum));

                    //neuralNetDrawer.QueueNeuralNetJson(program.GetBestJson());

                    if (best.Fitness > -0.01f)
                    {
                        generationsWithTargetReachedCount++;
                        if (generationsWithTargetReachedCount >= generationsWithTargetReached)
                        {
                            targetReached = true;

                            for (var j = 0; j < Math.Pow(2, inputs) - 1; j++)
                            {
                                best.FeedNeuralNetwork(GetBits(j).Select(x => (float)x).ToArray());
                                var expectedOutput = GetBits(j + 1);

                                var output = BitsToInt(best.Outputs.Select(x => x.Value).ToArray());
                                Console.WriteLine(String.Format("{0:0.00} | {1}", output, j));
                            }
                            break;
                        }
                    }
                    else
                    {
                        generationsWithTargetReachedCount = 0;
                    }

                    //neuralNetDrawer.QueueNeuralNetJson(program.GetBestJson());
                }

                program.Evolve();
            }

            neuralNetDrawer.QueueNeuralNetJson(program.GetBestJson());
            //fitnessCollector.Draw();
        }