Exemplo n.º 1
0
        public void AddDNA(DNA <T> value)
        {
            Compontents.Add(value);
            FullSequence += value.FullSequence;
            FullLength   += value.FullLength;
            SegmentLength = value.SegmentLength;

            IterationLength = FullSequence.Length / SegmentLength;
        }
Exemplo n.º 2
0
        public void Mutate(double rate, int amount, MutationStrategy strategy, int partition = 1)
        {
            if (amount == 0)
            {
                return;
            }

            int _Treshold = (int)(FullSequence.Length - FullSequence.Length * rate);
            int _Count    = 0;


            switch (strategy)
            {
            case MutationStrategy.BinarySwap:

                for (int i = 0; i < Compontents.Count; i++)
                {
                    if (Globals.GlobalRandom.Next(0, FullSequence.Length) < _Treshold)
                    {
                        continue;
                    }

                    char[] _seq = Compontents[i].FullSequence.ToCharArray();
                    for (int j = 0; j < _seq.Length; j++)
                    {
                        if (Globals.GlobalRandom.Next(0, FullSequence.Length) > _Treshold)
                        {
                            continue;
                        }

                        _seq[j] = _seq[j] == '0' ? '1' : '0';
                        _Count++;
                    }

                    Compontents[i].FullSequence = new string(_seq);
                }

                this.FullSequence = "";

                for (int i = 0; i < Compontents.Count; i++)
                {
                    FullSequence += Compontents[i].FullSequence;
                }


                break;


            case MutationStrategy.SegmentSwap:

                int length = FullSequence.Length;

                if (Compontents.Count == 0)
                {
                    Compontents.Add(this);
                }

                for (int i = 0; i < Compontents.Count; i++)
                {
                    if (Globals.GlobalRandom.Next(0, length) < _Treshold)
                    {
                        continue;
                    }

                    string[] _seq = Compontents[i].FullSequence.GetParts(partition);
                    for (int j = 0; j < _seq.Length; j++)
                    {
                        if (Globals.GlobalRandom.Next(0, length) < _Treshold)
                        {
                            continue;
                        }

                        int    randomIndex = Globals.GlobalRandom.Next(0, _seq.Length);
                        string oldVal      = _seq[randomIndex];
                        _seq[randomIndex] = _seq[j];
                        _seq[j]           = oldVal;
                    }

                    Compontents[i].FullSequence = "";
                    for (int k = 0; k < _seq.Length; k++)
                    {
                        Compontents[i].FullSequence += _seq[k];
                    }
                }



                break;
            }


            Mutate(rate, amount - 1, strategy, partition);
        }