コード例 #1
0
        public Hypothesis Crossover(Hypothesis hypothesisA, Hypothesis hypothesisB)
        {
            var minLength = Math.Min(hypothesisA.String.Length, hypothesisB.String.Length);

            if (minLength == 0)
            {
                return(hypothesisA.String.Length > hypothesisB.String.Length ? hypothesisA : hypothesisB);
            }
            var        index = Program.MainRandom.Next(0, minLength);
            Hypothesis hypA;
            Hypothesis hypB;

            if (Program.MainRandom.NextDouble() > 0.5)
            {
                hypA = hypothesisA;
                hypB = hypothesisB;
            }
            else
            {
                hypA = hypothesisB;
                hypB = hypothesisA;
            }

            var firstPart     = hypA.String.Substring(0, index);
            var secondPart    = hypB.String.Substring(index, hypB.String.Length - index);
            var newString     = "" + firstPart + secondPart;
            var newHypothesis = new Hypothesis {
                String = newString
            };

            return(newHypothesis);
        }
コード例 #2
0
        public static Hypothesis CreateRandomHypothesis()
        {
            var newHypotheis = new Hypothesis();

            var length = Convert.ToInt32(Program.MainRandom.NextDouble() * (Math.Pow(Program.MainRandom.NextDouble() * 10, (Program.MainRandom.NextDouble() * 3))));

            newHypotheis.String = CreateRandomString(length);
            return(newHypotheis);
        }
コード例 #3
0
        private static void MutatePopulation(List <Hypothesis> hypothesisList, double percentToMutate, double mutationIntensity)
        {
            var populationToMutate = Math.Max(1, Convert.ToInt32(hypothesisList.Count * percentToMutate));

            for (var i = 0; i < populationToMutate; i++)
            {
                var index = Program.MainRandom.Next(0, hypothesisList.Count);
                var hypothesisToMutate = hypothesisList[index];
                hypothesisList.RemoveAt(index);
                hypothesisList.Add(Hypothesis.CreateMutatedHypothesis(hypothesisToMutate, 1 - mutationIntensity));
            }
        }
コード例 #4
0
        private static int ExecuteExperiment(Experiment experiment)
        {
            //returns milliseconds to get target string
            const int timesToTest = 20;

            var tests = 0;
            //var generationTotal = 0;
            var timeTotal = 0;

            while (tests < timesToTest)
            {
                var time = DateTime.Now;
                tests++;
                var hypothesisList = new List <Hypothesis>();
                for (var i = 0; i < experiment.PopulationSize; i++)
                {
                    hypothesisList.Add(Hypothesis.CreateRandomHypothesis());
                }

                var generations = 0;
                while (true)
                {
                    SortAndCull(Program.Comparer, experiment.PopulationSize, experiment.Evaluator, hypothesisList);
                    var bestHypothesis = hypothesisList.FirstOrDefault();
                    if (bestHypothesis != null)
                    {
                        var bestString = bestHypothesis.String;
                        if (bestString.Equals(experiment.TargetString))
                        {
                            break;
                        }
                    }
                    var newHypothesisList = experiment.CrossoverHandler.CrossoverPopulation(hypothesisList, experiment.PercentToCrossover, experiment.ChildPerPair);
                    MutatePopulation(newHypothesisList, experiment.PercentToMutate, experiment.MutationIntensity);
                    hypothesisList.AddRange(newHypothesisList);

                    generations++;
                }

                var timeElapsed = (DateTime.Now - time).Milliseconds;
                //generationTotal += generations;
                timeTotal += timeElapsed;
                //Console.WriteLine("Test "+tests+" Found in: " + generations + " generations, "+timeElapsed+" ms");
            }

            //Console.WriteLine("Average to find the goal over "+timesToTest+" trials: "+(generationTotal/timesToTest)+" generations, "+(timeTotal/timesToTest)+" ms");
            //Console.ReadLine();
            return(timeTotal / timesToTest);
        }
コード例 #5
0
        public int EvaluateHypothesis(Hypothesis h)
        {
            if (h.Evaluated)
            {
                return(h.Fitness);
            }
            var fitness   = 0;
            var minLength = Math.Min(TargetString.Length, h.String.Length);

            for (var i = 0; i < minLength; i++)
            {
                if (h.String[i] == TargetString[i])
                {
                    fitness += 5;
                }
            }
            fitness    -= Math.Abs(TargetString.Length - h.String.Length);
            h.Evaluated = true;
            return(fitness);
        }
コード例 #6
0
        public static Hypothesis CreateMutatedHypothesis(Hypothesis parent, double mutationIntensity)
        {
            var newHypothesis = new Hypothesis();
            var newString     = new string(parent.String.ToCharArray());

            while (Program.MainRandom.NextDouble() > mutationIntensity)
            {
                var index = Program.MainRandom.Next(0, newString.Length);
                var type  = Program.MainRandom.Next(0, 3);

                if (type != 0 && newString.Length > 0)
                {
                    newString = newString.Remove(index, 1);
                }
                if (type != 1)
                {
                    newString = newString.Insert(index, GetRandomChar());
                }
            }
            newHypothesis.String = newString;
            return(newHypothesis);
        }
コード例 #7
0
        public int EvaluateHypothesis(Hypothesis h)
        {
            if (h.Evaluated)
            {
                return(h.Fitness);
            }
            var fitness   = 0;
            var minLength = Math.Min(TargetString.Length, h.String.Length);

            for (var i = 0; i < minLength; i++)
            {
                if (h.String[i] == TargetString[i])
                {
                    fitness += 20;
                }
                else
                {
                    for (var j = 1; j < 3; j++)
                    {
                        if (h.String[Math.Min(h.String.Length - 1, i + j)] == TargetString[i])
                        {
                            fitness += 10 - j;
                            break;
                        }
                        if (h.String[Math.Max(0, i - j)] != TargetString[i])
                        {
                            continue;
                        }
                        fitness += 10 - j;
                        break;
                    }
                }
            }
            fitness    -= Math.Abs(TargetString.Length - h.String.Length) * 2;
            h.Evaluated = true;
            return(fitness);
        }
コード例 #8
0
        public int EvaluateHypothesis(Hypothesis h)
        {
            if (h.Evaluated)
            {
                return(h.Fitness);
            }
            var fitness = 10000;
            var hIndex  = 0;
            var endScan = false;

            if (h.String.Length > 0)
            {
                foreach (var charToFind in TargetString)
                {
                    while (h.String[hIndex] != charToFind)
                    {
                        hIndex++;
                        fitness -= 1;
                        if (hIndex < h.String.Length)
                        {
                            continue;
                        }
                        endScan = true;
                        break;
                    }
                    if (endScan)
                    {
                        break;
                    }
                    fitness += 20;
                }
            }
            fitness    -= Math.Abs(TargetString.Length - h.String.Length);
            h.Evaluated = true;
            return(fitness);
        }