예제 #1
0
        public TrainEA RunGA(TrainEA geneticAlgorithm, int maxSameSolutionCount)
        {
            try
            {
                var sameSolutionCount = 0;
                var lastSolutionError = double.MaxValue;
                var iteration         = 1;
                while (sameSolutionCount < maxSameSolutionCount)
                {
                    geneticAlgorithm.Iteration();
                    var currentSolutionError = geneticAlgorithm.Error;
                    if (Math.Abs(lastSolutionError - currentSolutionError) < 1.0)
                    {
                        sameSolutionCount++;
                    }
                    else
                    {
                        sameSolutionCount = 0;
                    }

                    lastSolutionError = currentSolutionError;
                    Console.WriteLine($"{iteration++} Iteration - Error {currentSolutionError}");
                }
                geneticAlgorithm.FinishTraining();
                Console.WriteLine("Good Solutions Found");
                return(geneticAlgorithm);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #2
0
        public int[] Run()
        {
            StringBuilder builder = new StringBuilder();

            IPopulation pop = InitPopulation();


            ICalculateScore score = new FitnessFunction(cities);

            genetic = new TrainEA(pop, score);

            genetic.AddOperation(0.9, new SpliceNoRepeat(cityCount / 3));
            genetic.AddOperation(0.1, new MutateShuffle());

            int    sameSolutionCount = 0;
            int    iteration         = 1;
            double lastSolution      = Double.MaxValue;

            while (sameSolutionCount < maxSameSolition)
            {
                genetic.Iteration();

                double thisSolution = genetic.Error;

                builder.Length = 0;
                builder.Append("Iteration: ");
                builder.Append(iteration++);
                builder.Append(", Best Path Length = ");
                builder.Append(thisSolution);

                Console.WriteLine(builder.ToString());

                if (Math.Abs(lastSolution - thisSolution) < 1.0)
                {
                    sameSolutionCount++;
                }
                else
                {
                    sameSolutionCount = 0;
                }

                lastSolution = thisSolution;
            }

            Console.WriteLine("Good solution found:");

            IntegerArrayGenome best = (IntegerArrayGenome)genetic.BestGenome;


            genetic.FinishTraining();


            return(best.Data);
        }
        /// <summary>
        /// Setup and solve the TSP.
        /// </summary>
        public void Execute(IExampleInterface app)
        {
            this.app = app;

            var builder = new StringBuilder();

            initCities();

            IPopulation pop = initPopulation();

            ICalculateScore score = new TSPScore(cities);

            genetic = new TrainEA(pop, score);

            genetic.AddOperation(0.9, new SpliceNoRepeat(CITIES / 3));
            genetic.AddOperation(0.1, new MutateShuffle());

            int    sameSolutionCount = 0;
            int    iteration         = 1;
            double lastSolution      = Double.MaxValue;

            while (sameSolutionCount < MAX_SAME_SOLUTION)
            {
                genetic.Iteration();

                double thisSolution = genetic.Error;

                builder.Length = 0;
                builder.Append("Iteration: ");
                builder.Append(iteration++);
                builder.Append(", Best Path Length = ");
                builder.Append(thisSolution);

                Console.WriteLine(builder.ToString());

                if (Math.Abs(lastSolution - thisSolution) < 1.0)
                {
                    sameSolutionCount++;
                }
                else
                {
                    sameSolutionCount = 0;
                }

                lastSolution = thisSolution;
            }

            Console.WriteLine("Good solution found:");
            displaySolution();
            genetic.FinishTraining();
        }
예제 #4
0
        /// <summary>
        /// Program entry point.
        /// </summary>
        /// <param name="app">Holds arguments and other info.</param>
        public void Execute(IExampleInterface app)
        {
            IMLDataSet trainingData = GenerationUtil.GenerateSingleDataRange(
                (x) => (3 * Math.Pow(x, 2) + (12 * x) + 4)
                , 0, 100, 1);

            EncogProgramContext context = new EncogProgramContext();

            context.DefineVariable("x");

            StandardExtensions.CreateNumericOperators(context);

            PrgPopulation pop = new PrgPopulation(context, 1000);

            MultiObjectiveFitness score = new MultiObjectiveFitness();

            score.AddObjective(1.0, new TrainingSetScore(trainingData));

            TrainEA genetic = new TrainEA(pop, score);

            genetic.ValidationMode = true;
            genetic.CODEC          = new PrgCODEC();
            genetic.AddOperation(0.5, new SubtreeCrossover());
            genetic.AddOperation(0.25, new ConstMutation(context, 0.5, 1.0));
            genetic.AddOperation(0.25, new SubtreeMutation(context, 4));
            genetic.AddScoreAdjuster(new ComplexityAdjustedScore(10, 20, 10, 20.0));
            genetic.Rules.AddRewriteRule(new RewriteConstants());
            genetic.Rules.AddRewriteRule(new RewriteAlgebraic());
            genetic.Speciation = new PrgSpeciation();

            (new RampedHalfAndHalf(context, 1, 6)).Generate(new EncogRandom(), pop);

            genetic.ShouldIgnoreExceptions = false;

            EncogProgram best = null;

            genetic.ThreadCount = 1;

            try
            {
                for (int i = 0; i < 1000; i++)
                {
                    genetic.Iteration();
                    best = (EncogProgram)genetic.BestGenome;
                    Console.Out.WriteLine(genetic.IterationNumber + ", Error: "
                                          + best.Score + ",Best Genome Size:" + best.Size
                                          + ",Species Count:" + pop.Species.Count + ",best: " + best.DumpAsCommonExpression());
                }

                //EncogUtility.evaluate(best, trainingData);

                Console.Out.WriteLine("Final score:" + best.Score
                                      + ", effective score:" + best.AdjustedScore);
                Console.Out.WriteLine(best.DumpAsCommonExpression());
                //pop.dumpMembers(Integer.MAX_VALUE);
                //pop.dumpMembers(10);
            }
            catch (Exception t)
            {
                Console.Out.WriteLine(t.ToString());
            }
            finally
            {
                genetic.FinishTraining();
                EncogFramework.Instance.Shutdown();
            }
        }