Пример #1
0
        public override bool Equals(Object obj)
        {
            // If parameter is null return false.
            if (obj == null)
            {
                return(false);
            }

            // If parameter cannot be cast to Point return false.
            Creature c = obj as Creature;

            if ((Object)c == null)
            {
                return(false);
            }

            // Return true if the fields match:
            for (int i = 0; i < Genes.Count(); i++)
            {
                if (c.Genes.Count() - 1 < i)
                {
                    return(false);
                }

                if (!Genes[i].Equals(c.Genes[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #2
0
        /// <summary>
        /// Mutates creature's genes. Returns true if the creature did mutate false otherwise.
        /// </summary>
        /// <returns>true if the creature did mutate false otherwise</returns>
        public bool Mutate()
        {
            bool didMutate = false;

            for (int i = 0; i < Genes.Count(); i++)                              // for each gene
            {
                if (Tools.rdm.NextDouble() < Properties.GeneMutationProbability) // if gene has to mutate
                {
                    didMutate = true;
                    Genes.ElementAt(i).Value.Mutate();

                    switch (Genes.ElementAt(i).Key)
                    {
                    case "force":
                        UpdateForce();
                        break;

                    case "energy":
                    case "colorH":
                    case "colorS":
                    case "colorV":
                        UpdateColor();
                        break;
                    }
                }
            }
            return(didMutate);
        }
Пример #3
0
        public virtual Individuo Cruce(Individuo Padre)
        {
            var genesPadre = Padre.Genes;
            int n          = Genes.Count();
            var genes      = (n % 2 == 0) ?
                             Genes.Take(n / 2).Concat(genesPadre.Skip(n / 2))
                : Genes.Take((n / 2) + 1).Concat(genesPadre.Skip(n / 2));

            return(getNuevoIndividuo(genes.ToList()));
        }
Пример #4
0
        private double CalculateFenotype()
        {
            double sum = 0;
            int    i   = Genes.Count() - 1;

            foreach (var item in Genes)
            {
                sum += Convert.ToInt16(item) * Math.Pow(2, i);
                i--;
            }

            return(sum);
        }
        public double GetTravelDistance()
        {
            var travelingDistance = 0d;

            for (var i = 0; i < Genes.Count() - 1; i++)
            {
                var currentCity = Genes.ElementAt(i);
                var nextCity    = Genes.ElementAt(i + 1);

                travelingDistance += currentCity.DistanceTo(nextCity);
            }

            return(travelingDistance);
        }
Пример #6
0
        public double CalculateFenotype()
        {
            double suma = 0;
            int i = Genes.Count() - 1;

            foreach (var item in Genes)
            {
                if (item == true)
                {
                    suma += Math.Pow(2, i);
                }
                i--;
            }

            return suma;
        }
        public override bool Equals(object obj)
        {
            Solution other = obj as Solution;

            if (other == null)
            {
                return(false);
            }
            for (int i = 0; i < Genes.Count(); ++i)
            {
                if (!Genes[i].Equals(other.Genes[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #8
0
        /// <summary>
        /// Reproduces this creature with other. Creates a newborn creature with genes from its parents.
        /// </summary>
        /// <param name="other">significant other</param>
        public Creature Cross(Creature other)
        {
            Creature newborn = new Creature(this.canvas, this.map, true)
                               .WithPosition(new Point(Position.X + Math.Abs(Position.X - other.Position.X) / 2,
                                                       Position.Y + Math.Abs(Position.Y - other.Position.Y) / 2));

            for (int i = 0; i < Genes.Count(); i++) // for each gene
            {
                Gene selfGene  = Genes.ElementAt(i).Value;
                Gene otherGene = other.Genes.ElementAt(i).Value;

                String name  = selfGene.Name;
                uint   mask  = selfGene.Mask;
                uint   value = 0;

                if (Tools.rdm.NextDouble() < Properties.CrossoverKeepAverageProbability) // average between self and other
                {
                    uint   avg       = selfGene.Value / 2 + otherGene.Value / 2;
                    double rdmOffset = Tools.rdm.NextDouble() - 0.5;                                           // random between -0.5 and 0.5
                    rdmOffset *= Math.Floor((double)Math.Abs((int)selfGene.Value - (int)otherGene.Value) / 2); // multiply random by difference to get the offset from avg's value
                    value      = avg + (uint)Math.Floor(rdmOffset);                                            // apply offset to average
                }
                else
                {
                    if (Tools.rdm.NextDouble() < Properties.CrossoverKeepOtherProbability) // keep other
                    {
                        value = otherGene.Value;
                    }
                    else   // keep self
                    {
                        value = selfGene.Value;
                    }
                }

                newborn.AddGene(name, value, mask);
            }
            newborn.UpdateColor();
            newborn.UpdateForce();
            return(newborn);
        }
        public override void Repair()
        {
            var items    = CandidateItem.Count;
            var selected = Genes.Count(p => p);

            while (CandidateItem.Zip(Genes, (item, b) => new { item, b }).Where(p => p.b).Sum(p => p.item.Size) > Capacity)
            {
                var deslect = _randomizer.IntLessThan(selected);
                for (int i = 0; i < items; i++)
                {
                    if (Genes[i])
                    {
                        if (deslect == 0)
                        {
                            Genes[i] = false;
                            selected--;
                            break;
                        }

                        deslect--;
                    }
                }
            }
        }
Пример #10
0
 public int getIndividualSize()
 {
     return(Genes.Count());
 }
Пример #11
0
 public void Mutate()
 {
     Genes[_rnd.Next(Genes.Count())].Mutate();
 }