Ejemplo n.º 1
0
        public IGenome Select(IList <IGenome> lstgenes)
        {
            var rand = GAResolver.Resolve <IRandom>();
            var ind  = rand.Next(0, lstgenes.Count);

            return(lstgenes[ind]);
        }
Ejemplo n.º 2
0
        public override IGenome Calc(IGenome baby)
        {
            var rand = GAResolver.Resolve <IRandom>();

            if (rand.NextDouble() > MutationRate)
            {
                return(baby);
            }

            int listcount = baby.Gens.Count;
            // Ignora o inicial
            var pos1 = rand.Next(0, listcount);
            var pos2 = pos1;

            while (pos1 == pos2)
            {
                pos2 = rand.Next(0, listcount); // Ignora o inicial
            }
            var temp = baby.Gens[pos1];

            baby.Gens[pos1] = baby.Gens[pos2];
            baby.Gens[pos2] = temp;

            return(baby);
        }
Ejemplo n.º 3
0
        public override IGenome Calc(IGenome baby)
        {
            var rand = GAResolver.Resolve <IRandom>();

            if (rand.NextDouble() > MutationRate)
            {
                return(baby);
            }

            int       listcount   = baby.Gens.Count;
            const int minSpanSize = 3;

            if (listcount <= minSpanSize)
            {
                return(baby);
            }

            int beg, end;

            beg = end = 0;
            var spanSize = rand.Next(minSpanSize, listcount);

            beg = rand.Next(1, listcount - spanSize);
            end = beg + spanSize;

            var lstTemp = new List <int>();

            for (int i = beg; i < end; i++)
            {
                lstTemp.Add(baby.Gens[beg]);
                baby.Gens.RemoveAt(beg);
            }

            var numberOfSwaprsRequired = lstTemp.Count;

            while (numberOfSwaprsRequired != 0)
            {
                var no1 = rand.Next(1, lstTemp.Count);
                var no2 = rand.Next(1, lstTemp.Count);

                var temp = lstTemp[no1];
                lstTemp[no1] = lstTemp[no2];
                lstTemp[no2] = temp;

                --numberOfSwaprsRequired;
            }

            var insertLocation = rand.Next(1, baby.Gens.Count);
            var count          = 0;

            for (int i = insertLocation; count < lstTemp.Count; i++)
            {
                baby.Gens.Insert(i, lstTemp[count]);
                count++;
            }

            return(baby);
        }
Ejemplo n.º 4
0
        public override CrossoverOperation Calc(CrossoverOperation Operation)
        {
            var rand = GAResolver.Resolve <IRandom>();

            if (rand.NextDouble() > CrossoverRate || Operation.IsEqual())
            {
                return(Operation);
            }

            var babymom = Operation.Copy(Operation.Mom);
            var babydad = Operation.Copy(Operation.Dad);

            var listmom = Operation.Mom.Gens;
            var listdad = Operation.Dad.Gens;

            var listbabymom = babymom.Gens;
            var listbabydad = babydad.Gens;


            var minindex = Math.Min(listmom.Count, listdad.Count);

            var beg = rand.Next(0, minindex - 1);

            var end = beg;

            while (end < beg)
            {
                end = rand.Next(0, minindex);
            }

            for (int pos = beg; pos <= end; ++pos)
            {
                var genemom = listmom[pos];
                var genedad = listdad[pos];

                if (!genemom.Equals(genedad))
                {
                    var temp = listbabymom[pos];
                    listbabymom[pos] = listdad[pos];
                    listdad[pos]     = temp;
                }
            }

            return(new CrossoverOperation(babymom, babydad));
        }
        public override IGenome Calc(IGenome baby)
        {
            var rand = GAResolver.Resolve <IRandom>();

            if (rand.NextDouble() > MutationRate)
            {
                return(baby);
            }

            var i = (int)Math.Floor(rand.NextDouble() * baby.Gens.Count());

            // choose the bit to swap
            var qtdBits = (int)Math.Floor(8 * rand.NextDouble());

            baby.Gens[i] ^= 1 << qtdBits;

            return(baby);
        }
Ejemplo n.º 6
0
        public override IGenome Calc(IGenome baby)
        {
            var rand = GAResolver.Resolve <IRandom>();

            if (rand.NextDouble() > MutationRate)
            {
                return(baby);
            }

            int       listcount   = baby.Gens.Count;
            const int minSpanSize = 3;

            if (listcount <= minSpanSize)
            {
                return(baby);
            }

            int beg, end;

            beg = end = 0;

            var spanSize = rand.Next(minSpanSize, listcount);

            beg = rand.Next(1, listcount - spanSize);
            end = beg + spanSize;

            var lstTemp = new List <int>();

            for (int i = beg; i < end; i++)
            {
                lstTemp.Add(baby.Gens[beg]);
                baby.Gens.RemoveAt(beg);
            }
            lstTemp.Reverse();
            var count = 0;

            for (int i = beg; i < end; i++)
            {
                baby.Gens.Insert(i, lstTemp[count]);
                count++;
            }
            return(baby);
        }
Ejemplo n.º 7
0
        public override IGenome Calc(IGenome baby)
        {
            var rand = GAResolver.Resolve <IRandom>();

            if (rand.NextDouble() > MutationRate)
            {
                return(baby);
            }

            int       listcount   = baby.Gens.Count;
            const int minSpanSize = 3;

            if (listcount <= minSpanSize)
            {
                return(baby);
            }

            int beg, end;

            beg = end = 0;

            var spanSize = rand.Next(minSpanSize, listcount);

            beg = rand.Next(1, listcount - spanSize);
            end = beg + spanSize;

            var span = end - beg;
            var numberOfSwaprsRequired = span;

            while (numberOfSwaprsRequired != 0)
            {
                var no1 = rand.Next(beg, end);
                var no2 = rand.Next(beg, end);

                var temp = baby.Gens[no1];
                baby.Gens[no1] = baby.Gens[no2];
                baby.Gens[no2] = temp;

                --numberOfSwaprsRequired;
            }

            return(baby);
        }
Ejemplo n.º 8
0
        public override IGenome Calc(IGenome baby)
        {
            var rand = GAResolver.Resolve <IRandom>();

            if (rand.NextDouble() > MutationRate)
            {
                return(baby);
            }

            int listcount   = baby.Gens.Count;
            var randomPoint = rand.Next(1, listcount);
            var tempNumber  = baby.Gens[randomPoint];

            baby.Gens.RemoveAt(randomPoint);
            var insertAt = rand.Next(1, listcount);

            baby.Gens.Insert(insertAt, tempNumber);

            return(baby);
        }
Ejemplo n.º 9
0
        public IGenome Select(IList <IGenome> lstgens)
        {
            var    rand           = GAResolver.Resolve <IRandom>();
            double slice          = rand.NextDouble() * lstgens.Sum(e => e.Fitness + 1);
            double total          = 0;
            int    selectedGenome = 0;

            for (int i = 0; i < lstgens.Count(); i++)
            {
                total += (lstgens[i].Fitness + 1);

                if (total > slice)
                {
                    selectedGenome = i;
                    break;
                }
            }

            return(lstgens[selectedGenome]);
        }
        public override CrossoverOperation Calc(CrossoverOperation Operation)
        {
            var rand = GAResolver.Resolve <IRandom>();

            if (rand.NextDouble() > CrossoverRate || Operation.IsEqual())
            {
                return(Operation);
            }

            var babymom = Operation.Copy(Operation.Mom);
            var babydad = Operation.Copy(Operation.Dad);

            var lstPositions = new List <int>();

            var listmom = Operation.Mom.Gens;
            var listdad = Operation.Dad.Gens;

            var minindex = Math.Min(listmom.Count, listdad.Count);

            for (int i = 0; i < minindex; i++)
            {
                babymom.Gens[i] = babydad.Gens[i] = -1;
            }

            var Pos = rand.Next(0, minindex - 1);

            while (Pos < minindex)
            {
                lstPositions.Add(Pos);
                Pos += rand.Next(1, minindex - Pos);
            }

            for (int pos = 0; pos < lstPositions.Count; ++pos)
            {
                babymom.Gens[lstPositions[pos]] = listmom[lstPositions[pos]];
                babydad.Gens[lstPositions[pos]] = listdad[lstPositions[pos]];
            }

            int c1, c2;

            c1 = c2 = 0;

            for (int pos = 0; pos < minindex; pos++)
            {
                while (c2 < minindex && babydad.Gens[c2] > -1)
                {
                    ++c2;
                }

                if (c2 < babydad.Gens.Count)
                {
                    if (!babydad.Gens.ToList().Exists(i => i == (listmom[pos])))
                    {
                        babydad.Gens[c2] = listmom[pos];
                    }
                }

                while (c1 < minindex && babymom.Gens[c1] > -1)
                {
                    ++c1;
                }

                if (c1 < babymom.Gens.Count)
                {
                    if (!babymom.Gens.ToList().Exists(i => i == (listdad[pos])))
                    {
                        babymom.Gens[c1] = listdad[pos];
                    }
                }
            }

            return(new CrossoverOperation(babymom, babydad));
        }
        public override CrossoverOperation Calc(CrossoverOperation Operation)
        {
            var rand = GAResolver.Resolve <IRandom>();

            if (rand.NextDouble() > CrossoverRate || Operation.IsEqual())
            {
                return(Operation);
            }

            var babymom = Operation.Copy(Operation.Mom);
            var babydad = Operation.Copy(Operation.Dad);

            var listmom = Operation.Mom.Gens;
            var listdad = Operation.Dad.Gens;

            var lstTemp      = new List <int>();
            var lstPositions = new List <int>();


            int pos = rand.Next(0, listmom.Count - 1);

            while (pos < listmom.Count)
            {
                lstPositions.Add(pos);
                lstTemp.Add(listmom[pos]);
                pos += rand.Next(1, listmom.Count - pos);
            }

            int cPos = 0;

            for (int cit = 0; cit < babydad.Gens.Count; ++cit)
            {
                for (int i = 0; i < lstTemp.Count; ++i)
                {
                    if (babydad.Gens[cit] == lstTemp[i])
                    {
                        babydad.Gens[cit] = lstTemp[cPos];
                        ++cPos;
                        break;
                    }
                }
            }

            lstTemp.Clear();
            cPos = 0;

            for (int i = 0; i < lstPositions.Count - 1; ++i)
            {
                lstTemp.Add(listdad[lstPositions[i]]);
            }

            for (int cit = 0; cit < babymom.Gens.Count; ++cit)
            {
                cPos = 0;
                for (int i = 0; i < lstTemp.Count; ++i)
                {
                    if (babymom.Gens[cit] == lstTemp[i])
                    {
                        babymom.Gens[cit] = lstTemp[cPos];
                        ++cPos;
                        break;
                    }
                }
            }


            return(new CrossoverOperation(babymom, babydad));
        }