Beispiel #1
0
        private void Mutate(IGenome genome, double soften = 1)
        {
            if (new Random().NextDouble() < GenerationParameters.MutateAddNodeProbability / Math.Max(soften, 1))
            {
                genome.MutateAddNode();
            }
            else if (new Random().NextDouble() < GenerationParameters.MutateAddGeneProbability / Math.Max(soften, 1))
            {
                genome.MutateAddGene(Population.Genomes.SelectMany(g => g.Genes));
            }
            else
            {
                if (new Random().NextDouble() < GenerationParameters.MutateAllGeneWeightsProbability)
                {
                    genome.MutateGeneWeights(GeneMutationType.AdjustWeight, GeneParameters.MutateGeneWeightFactor, 1);
                }

                if (new Random().NextDouble() < GenerationParameters.MutateGeneExpressionProbability)
                {
                    genome.MutateGeneExpression();
                }

                if (new Random().NextDouble() < GenerationParameters.MutateGeneReExpressionProbability)
                {
                    genome.MutateGeneReExpress();
                }

                IGene geneCandidate = genome.Genes[new Random().Next(genome.Genes.Count)];

                if (new Random().NextDouble() < GenerationParameters.MutateGeneWeightProbability)
                {
                    geneCandidate.MutateWeight(GeneMutationType.AdjustWeight, GeneParameters.MutateGeneWeightFactor);
                }
            }
        }
Beispiel #2
0
        public void BreedTest()
        {
            IGenome mate = genome.Copy();

            mate.MutateAddNode();
            mate.MutateAddGene(genome.Genes);

            genome.MutateAddNode();

            IGenome offspring = genome.Breed(mate);

            string parentGenes    = string.Join("->", genome.Genes.Select(g => "A" + g.Innovation).ToList());
            string mateGenes      = string.Join("->", mate.Genes.Select(g => "B" + g.Innovation).ToList());
            string offspringGenes = string.Join("->", offspring.Genes.OrderBy(g => g.Innovation).Select(g => "C" + g.Innovation).ToList());

            string dotGraph = "digraph G { " +
                              "    subgraph cluster_0 {                                  " +
                              "                    style = filled;                       " +
                              "                    color = lightgrey;                    " +
                              "                    node[style = filled, color = white];  " +
                              "                    " + parentGenes + ";                  " +
                              "                    label = \"Parent\";                   " +
                              "                }                                         " +
                              "    subgraph cluster_1 {                                  " +
                              "                    style = filled;                       " +
                              "                    color = lightgrey;                    " +
                              "                    node[style = filled, color = white];  " +
                              "                    " + mateGenes + ";                    " +
                              "                    label = \"Mate\"; }                   " +
                              "    subgraph cluster_2 {                                  " +
                              "                    style = filled;                       " +
                              "                    color = lightblue;                    " +
                              "                    node[style = filled, color = white];  " +
                              "                    " + offspringGenes + ";               " +
                              "                    label = \"Offspring\";                " +
                              " }";

            genome.Genes.ForEach(g =>
            {
                dotGraph += string.Format("\"{0}\" [ label=\"{1} {2} {3}\" shape = \"square\" ]", "A" + g.Innovation, g.Innovation, g.NodeOut.Id.ToString().Substring(32) + "->" + g.NodeIn.Id.ToString().Substring(32), Math.Round(g.Weight, 2)) + ";";
            });

            mate.Genes.ForEach(g =>
            {
                dotGraph += string.Format("\"{0}\" [ label=\"{1} {2} {3}\" shape = \"square\" ]", "B" + g.Innovation, g.Innovation, g.NodeOut.Id.ToString().Substring(32) + "->" + g.NodeIn.Id.ToString().Substring(32), Math.Round(g.Weight, 2)) + ";";
            });

            offspring.Genes.ForEach(g =>
            {
                dotGraph += string.Format("\"{0}\" [ label=\"{1} {2} {3}\" shape = \"square\" ]", "C" + g.Innovation, g.Innovation, g.NodeOut.Id.ToString().Substring(32) + "->" + g.NodeIn.Id.ToString().Substring(32), Math.Round(g.Weight, 2)) + ";";
            });

            dotGraph += " rankdir=LR; }";

            Debug.WriteLine(dotGraph);
        }