Esempio n. 1
0
 /// <summary>
 /// Mutates a gene by applying boundary mutation.
 /// </summary>
 /// <param name="gene">An IntegerGene or DoubleGene to be mutated.</param>
 /// <returns></returns>
 public static Gene Mutate(Gene gene)
 {
     if ( gene is IntegerGene )
     {
         IntegerGene ig = (IntegerGene)gene;
         if ( genX.Utils.Rand.NextDouble() < 0.5 )
         {
             ig.Value = ig.Descriptor.MaxValue;
         }
         else
         {
             ig.Value = ig.Descriptor.MinValue;
         }
     }
     else if ( gene is DoubleGene )
     {
         DoubleGene dg = (DoubleGene)gene;
         if ( genX.Utils.Rand.NextDouble() < 0.5 )
         {
             dg.Value = dg.Descriptor.MaxValue;
         }
         else
         {
             dg.Value = dg.Descriptor.MinValue;
         }
     }
     else
     {
         throw new ArgumentException(
             "Boundary mutation may only apply to IntegerGene or DoubleGene genes.",
             "gene"
             );
     }
     return gene;
 }
Esempio n. 2
0
 /// <summary>
 /// Mutates a gene by bitwise mutation.
 /// </summary>
 /// <param name="gene"></param>
 /// <returns></returns>
 public static Gene Mutate(Gene gene)
 {
     if ( gene is BinaryGene )
     {
         BinaryGene g = (BinaryGene) gene.Clone();
         g.Value = !(BinaryGene)gene;
         return g;
     }
     else if ( gene is DoubleGene )
     {
         DoubleGene g = (DoubleGene) gene.Clone();
         byte[] bytes = BitConverter.GetBytes(g.Value);
         BitArray ba = new BitArray(bytes);
         int p = Utils.Rand.Next( ba.Length );
         ba.Set(p, !ba[p]);
         ba.CopyTo(bytes, 0);
         g.Value = BitConverter.ToDouble(bytes,0);
         return g;
     }
     else if ( gene is IntegerGene )
     {
         IntegerGene g = (IntegerGene) gene.Clone();
         byte[] bytes = BitConverter.GetBytes(g.Value);
         BitArray ba = new BitArray(bytes);
         int p = Utils.Rand.Next( ba.Length );
         ba.Set(p, !ba[p]);
         ba.CopyTo(bytes, 0);
         g.Value = BitConverter.ToInt32(bytes,0);
         return g;
     }
     return (Gene) gene.Clone(); // default
 }
Esempio n. 3
0
 /// <summary>
 /// Mutates a gene.
 /// </summary>
 /// <param name="g"></param>
 /// <returns></returns>
 public virtual Gene Mutate(Gene g)
 {
     if (Mutator != null)
     {
         return(Mutator(g));
     }
     else
     {
         Gene g2 = GetRandomAllele();
         g2.Label = g.Label;
         return(g2);
     }
 }
Esempio n. 4
0
File: ga.cs Progetto: mykwillis/genX
        void Mutate(Chromosome[] cs)
        {
            foreach (Chromosome c in cs)
            {
                //
                // Mutate a gene's value in this chromosome.
                //
                if (Utils.Rand.NextDouble() < GeneMutationProbability)
                {
                    int  mutationPoint = Utils.Rand.Next(c.Genes.Length);
                    Gene gene          = c.Genes[mutationPoint];

                    //
                    // A globally installed valuemutator takes precedence over the
                    // default gene mutator.
                    //
                    if (ValueMutator != null)
                    {
                        c.Genes[mutationPoint] = ValueMutator(gene);
                    }
                    else
                    {
                        // this is the Custom case
                        c.Genes[mutationPoint] = gene.Descriptor.Mutate(gene);
                    }

                    //
                    // Raise the Mutated event.
                    //
                    MutatedEventArgs e = new MutatedEventArgs();
                    e.Chromosome    = c;
                    e.MutationPoint = mutationPoint;
                    e.OldGene       = gene;
                    e.NewGene       = c.Genes[mutationPoint];

                    OnMutated(e);
                }
#if NOTDEF
                if (Utils.Rand.NextDouble() < OrderMutationProbability)
                {
                    if (OrderMutator != null)
                    {
                        OrderMutator(c);
                    }
                }
#endif
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Initializes a chromosome with a given set of genes.
 /// </summary>
 /// <param name="genes">
 /// An array of Genes with which to initailize.  Each Gene in the array
 /// may be of any derived type.
 /// </param>
 public Chromosome(Gene[] genes)
 {
     this.Genes = genes;
     this.Parents = new Chromosome[2];
 }
Esempio n. 6
0
 /// <summary>
 /// Mutates a gene.
 /// </summary>
 /// <param name="g"></param>
 /// <returns></returns>
 public virtual Gene Mutate(Gene g)
 {
     if ( Mutator != null )
     {
         return Mutator(g);
     }
     else
     {
         Gene g2 = GetRandomAllele();
         g2.Label = g.Label;
         return g2;
     }
 }