Пример #1
0
        public IFitness CreateFitness()
        {
            var db = "";

            foreach (var file in Directory.GetFiles(@"C:\Users\giacomelli\Downloads\wn3.1.dict\dict\dbfiles", "*.*"))
            {
                db += File.ReadAllText(file).ToUpperInvariant();
            }

            var f = new GhostwriterFitness();

            f.EvaluateFunc = (text) =>
            {
                //Console.WriteLine("Evaluating text: {0}", text);
                var words         = text.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                var points        = 0.0;
                var wordsNotFound = 0.0;

                foreach (var word in words)
                {
                    try
                    {
                        if (db.Contains(word))
                        {
                            points += word.Length;
                        }
                        else
                        {
                            wordsNotFound += word.Length * 2;
                        }
                    }
                    catch
                    {
                        points--;
                        continue;
                    }
                }

                if (points > 100)
                {
                    points = 100;
                }
                else if (points < 0)
                {
                    points = 0;
                }

                var fitness = (points / wordsNotFound) / 100.0;

                //onsole.WriteLine("Fitness: {0}", fitness);

                return(fitness);
            };

            return(f);
        }
Пример #2
0
        public IFitness CreateFitness()
        {
            var f = new GhostwriterFitness();

            f.EvaluateFunc = (text) =>
            {
                var minDistance = m_quotes.Min(q => LevenshteinDistance(q, text));

                return(1 - (minDistance / 100f));
            };

            return(f);
        }
Пример #3
0
        public void Evolve_ManyGenerations_Fast()
        {
            var selection  = new EliteSelection();
            var crossover  = new UniformCrossover();
            var mutation   = new UniformMutation(true);
            var chromosome = new GhostwriterChromosome(4, new string[] { "The", "C#", "Genetic", "Algorithm", "library" });
            var fitness    = new GhostwriterFitness((t) => t.Length);

            var population = new Population(10, 10, chromosome);
            var ga         = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);

            ga.Termination = new GenerationNumberTermination(5);
            ga.Start();

            Assert.NotNull(ga.BestChromosome);
        }