public override CrossoverOperation Calc(CrossoverOperation Operation)
        {
            var rand = RandomFactory.Rand;

            if (rand.NextDouble() > CrossoverRate || Operation.IsEqual())
            {
                return(Operation);
            }
            var babymom     = CrossoverOperation.Copy(Operation.Mom);
            var babydad     = CrossoverOperation.Copy(Operation.Dad);
            var listmom     = Operation.Mom.ListNodes;
            var listdad     = Operation.Dad.ListNodes;
            var listbabymom = babymom.ListNodes;
            var listbabydad = babydad.ListNodes;
            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 CrossoverOperation Calc(CrossoverOperation Operation)
        {
            var rand = RandomFactory.Rand;

            if (rand.NextDouble() > CrossoverRate || Operation.IsEqual())
            {
                return(Operation);
            }
            var babymom      = CrossoverOperation.Copy(Operation.Mom);
            var babydad      = CrossoverOperation.Copy(Operation.Dad);
            var lstPositions = new List <int>();
            var listmom      = Operation.Mom.ListNodes;
            var listdad      = Operation.Dad.ListNodes;
            var minindex     = Math.Min(listmom.Count, listdad.Count);

            for (int i = 0; i < minindex; i++)
            {
                babymom.ListNodes[i] = babydad.ListNodes[i] = new Node(-1, -1, DirectionMovement.None);
            }
            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.ListNodes[lstPositions[pos]] = listmom[lstPositions[pos]];
                babydad.ListNodes[lstPositions[pos]] = listdad[lstPositions[pos]];
            }
            int c1, c2;

            c1 = c2 = 0;
            for (int pos = 0; pos < minindex; pos++)
            {
                while (c2 < minindex && babydad.ListNodes[c2].X > -1)
                {
                    ++c2;
                }
                if (c2 < babydad.ListNodes.Count)
                {
                    if (!babydad.ListNodes.Exists(i => i.EqualsAll(listmom[pos])))
                    {
                        babydad.ListNodes[c2] = listmom[pos];
                    }
                }
                while (c1 < minindex && babymom.ListNodes[c1].X > -1)
                {
                    ++c1;
                }
                if (c1 < babymom.ListNodes.Count)
                {
                    if (!babymom.ListNodes.Exists(i => i.EqualsAll(listdad[pos])))
                    {
                        babymom.ListNodes[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 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));
        }
        public override CrossoverOperation Calc(CrossoverOperation Operation)
        {
            var rand = RandomFactory.Rand;

            if (rand.NextDouble() > CrossoverRate || Operation.IsEqual())
            {
                return(Operation);
            }
            var babymom       = CrossoverOperation.Copy(Operation.Mom);
            var babydad       = CrossoverOperation.Copy(Operation.Dad);
            var lstTempCities = new List <Node>();
            var lstPositions  = new List <int>();
            var listmom       = Operation.Mom.ListNodes;
            var listdad       = Operation.Dad.ListNodes;
            var minindex      = Math.Min(listmom.Count, listdad.Count);
            var pos           = rand.Next(0, minindex - 1);

            while (pos < minindex)
            {
                lstPositions.Add(pos);
                lstTempCities.Add(listmom[pos]);
                pos += rand.Next(1, minindex - pos);
            }
            var cPos = 0;

            for (int cit = 0; cit < minindex; ++cit)
            {
                for (int i = 0; i < lstTempCities.Count; ++i)
                {
                    if (babydad.ListNodes[cit].EqualsAll(lstTempCities[i]))
                    {
                        if (lstTempCities.Count < cPos)
                        {
                            babydad.ListNodes[cit] = lstTempCities[cPos];
                        }
                        ++cPos;
                        break;
                    }
                }
            }
            lstTempCities.Clear();
            cPos = 0;
            for (int i = 0; i < lstPositions.Count; ++i)
            {
                var x = lstPositions[i];
                lstTempCities.Add(listdad[x]);
            }
            for (int cit = 0; cit < minindex; ++cit)
            {
                for (int i = 0; i < lstTempCities.Count; ++i)
                {
                    if (babymom.ListNodes[cit].EqualsAll(lstTempCities[i]))
                    {
                        if (lstTempCities.Count < cPos)
                        {
                            babymom.ListNodes[cit] = lstTempCities[cPos];
                        }
                        ++cPos;
                        break;
                    }
                }
            }
            return(new CrossoverOperation(babymom, babydad));
        }
Exemplo n.º 6
0
 public abstract CrossoverOperation Calc(CrossoverOperation Operation);