コード例 #1
0
        /// <summary>
        ///     Generate a random genome.
        /// </summary>
        /// <param name="rnd">Random number generator.</param>
        /// <param name="eval">The evaluator.</param>
        /// <returns>The random genome.</returns>
        private TreeGenome RandomGenome(IGenerateRandom rnd, EvaluateExpression eval)
        {
            var result = new TreeGenome(eval);

            result.Root = eval.Grow(rnd, 5);
            return(result);
        }
コード例 #2
0
        /// <summary>
        ///     Create an initial random population.
        /// </summary>
        /// <param name="rnd">A random number generator.</param>
        /// <param name="eval">The expression evaluator.</param>
        /// <returns>The new population.</returns>
        private IPopulation InitPopulation(IGenerateRandom rnd, EvaluateExpression eval)
        {
            IPopulation result = new BasicPopulation(PopulationSize, null);

            var defaultSpecies = new BasicSpecies();

            defaultSpecies.Population = result;
            for (int i = 0; i < PopulationSize; i++)
            {
                TreeGenome genome = RandomGenome(rnd, eval);
                defaultSpecies.Add(genome);
            }
            result.GenomeFactory = new TreeGenomeFactory(eval);
            result.Species.Add(defaultSpecies);

            return(result);
        }
コード例 #3
0
        /// <summary>
        ///     Process the specified file.
        /// </summary>
        /// <param name="filename">The filename to process.</param>
        public void Process(String filename)
        {
            // read the data from the resources
            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream   res      = assembly.GetManifestResourceStream("AIFH_Vol2.Resources.simple-poly.csv");

            // did we fail to read the resouce
            if (res == null)
            {
                Console.WriteLine("Can't read iris data from embedded resources.");
                return;
            }

            // load the data
            var     istream = new StreamReader(res);
            DataSet ds      = DataSet.Load(istream);

            istream.Close();


            // Extract supervised training.
            IList <BasicData> training = ds.ExtractSupervised(0, 1, 1, 1);


            IGenerateRandom rnd   = new MersenneTwisterGenerateRandom();
            var             eval  = new EvaluateExpression(rnd);
            IPopulation     pop   = InitPopulation(rnd, eval);
            IScoreFunction  score = new ScoreSmallExpression(training, 30);

            IEvolutionaryAlgorithm genetic = new BasicEA(pop, score);

            genetic.AddOperation(0.3, new MutateTree(3));
            genetic.AddOperation(0.7, new CrossoverTree());
            genetic.ShouldIgnoreExceptions = false;


            int    sameSolutionCount = 0;
            int    iteration         = 1;
            double lastSolution      = double.MaxValue;
            var    builder           = new StringBuilder();

            while (sameSolutionCount < MaxSameSolution && iteration < 1000)
            {
                genetic.Iteration();

                double thisSolution = genetic.LastError;

                builder.Length = 0;
                builder.Append("Iteration: ");
                builder.Append(iteration++);
                builder.Append(", Current error = ");
                builder.Append(thisSolution);
                builder.Append(", Best Solution Length = ");
                builder.Append(genetic.BestGenome.Count);

                Console.WriteLine(builder.ToString());

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

                lastSolution = thisSolution;
            }

            Console.WriteLine("Good solution found:");
            var best = (TreeGenome)genetic.BestGenome;

            Console.WriteLine(eval.DisplayExpressionNormal(best.Root));
            genetic.FinishTraining();
        }
コード例 #4
0
ファイル: FindEquation.cs プロジェクト: Raghavendra1509/aifh
        /// <summary>
        ///     Create an initial random population.
        /// </summary>
        /// <param name="rnd">A random number generator.</param>
        /// <param name="eval">The expression evaluator.</param>
        /// <returns>The new population.</returns>
        private IPopulation InitPopulation(IGenerateRandom rnd, EvaluateExpression eval)
        {
            IPopulation result = new BasicPopulation(PopulationSize, null);

            var defaultSpecies = new BasicSpecies();
            defaultSpecies.Population = result;
            for (int i = 0; i < PopulationSize; i++)
            {
                TreeGenome genome = RandomGenome(rnd, eval);
                defaultSpecies.Add(genome);
            }
            result.GenomeFactory = new TreeGenomeFactory(eval);
            result.Species.Add(defaultSpecies);

            return result;
        }
コード例 #5
0
ファイル: FindEquation.cs プロジェクト: Raghavendra1509/aifh
 /// <summary>
 ///     Generate a random genome.
 /// </summary>
 /// <param name="rnd">Random number generator.</param>
 /// <param name="eval">The evaluator.</param>
 /// <returns>The random genome.</returns>
 private TreeGenome RandomGenome(IGenerateRandom rnd, EvaluateExpression eval)
 {
     var result = new TreeGenome(eval);
     result.Root = eval.Grow(rnd, 5);
     return result;
 }
コード例 #6
0
ファイル: FindEquation.cs プロジェクト: Raghavendra1509/aifh
        /// <summary>
        ///     Process the specified file.
        /// </summary>
        /// <param name="filename">The filename to process.</param>
        public void Process(String filename)
        {
            // read the data from the resources
            Assembly assembly = Assembly.GetExecutingAssembly();
            Stream res = assembly.GetManifestResourceStream("AIFH_Vol2.Resources.simple-poly.csv");

            // did we fail to read the resouce
            if (res == null)
            {
                Console.WriteLine("Can't read iris data from embedded resources.");
                return;
            }

            // load the data
            var istream = new StreamReader(res);
            DataSet ds = DataSet.Load(istream);
            istream.Close();


            // Extract supervised training.
            IList<BasicData> training = ds.ExtractSupervised(0, 1, 1, 1);


            IGenerateRandom rnd = new MersenneTwisterGenerateRandom();
            var eval = new EvaluateExpression(rnd);
            IPopulation pop = InitPopulation(rnd, eval);
            IScoreFunction score = new ScoreSmallExpression(training, 30);

            IEvolutionaryAlgorithm genetic = new BasicEA(pop, score);
            genetic.AddOperation(0.3, new MutateTree(3));
            genetic.AddOperation(0.7, new CrossoverTree());
            genetic.ShouldIgnoreExceptions = false;


            int sameSolutionCount = 0;
            int iteration = 1;
            double lastSolution = double.MaxValue;
            var builder = new StringBuilder();

            while (sameSolutionCount < MaxSameSolution && iteration < 1000)
            {
                genetic.Iteration();

                double thisSolution = genetic.LastError;

                builder.Length = 0;
                builder.Append("Iteration: ");
                builder.Append(iteration++);
                builder.Append(", Current error = ");
                builder.Append(thisSolution);
                builder.Append(", Best Solution Length = ");
                builder.Append(genetic.BestGenome.Count);

                Console.WriteLine(builder.ToString());

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

                lastSolution = thisSolution;
            }

            Console.WriteLine("Good solution found:");
            var best = (TreeGenome) genetic.BestGenome;
            Console.WriteLine(eval.DisplayExpressionNormal(best.Root));
            genetic.FinishTraining();
        }