Ejemplo n.º 1
0
        private double CalcAdjFitness(Genom genom)
        {
            int count = AllNets.Count(g => g.GetDistance(genom, C1, C2, C3) <= DistThreshold);


            return(genom.Fitness / count);
        }
Ejemplo n.º 2
0
        public Genom Clone()
        {
            Genom clone = ObjectCopier.Clone(this);

            clone.Fitness = 0;
            return(clone);
        }
Ejemplo n.º 3
0
        public Population(int inCount, int outCount, bool bias)
        {
            if (bias)
            {
                inCount++;
            }

            for (int i = 0; i < Amount; i++)
            {
                Genom genom = new Genom(inCount, outCount);
                for (int k = 0; k < inCount; k++)
                {
                    if (k == inCount - 1 && bias)
                    {
                        genom.Inputs[k].OldValue = 1;
                        continue;
                    }

                    for (int j = 0; j < outCount; j++)
                    {
                        genom.AddLink(genom.Inputs[k], genom.Outputs[j], rnd.NextDouble() * 2 - 1);
                    }
                }

                AllNets.Add(genom);
            }

            Speciate();
        }
Ejemplo n.º 4
0
        public Genom Mate(Genom other, Random rnd)
        {
            Genom child = new Genom(Inputs.Count, Outputs.Count);

            bool        thisGeneBetter = Fitness > other.Fitness;
            List <Link> bestGenes      = thisGeneBetter ? LocalLinks : other.LocalLinks;
            List <Link> worstGenes     = !thisGeneBetter ? LocalLinks : other.LocalLinks;

            if (Fitness == other.Fitness)
            {
                worstGenes.ForEach(g =>
                {
                    if (!bestGenes.Contains(g))
                    {
                        bestGenes.Add(g);
                    }
                });
            }

            foreach (Link link in bestGenes)
            {
                Node input = new Node(link.Input.Id);
                if (!child.LocalNodes.Contains(input))
                {
                    child.Hidden.Add(input);
                    child.LocalNodes.Add(input);
                }
                else
                {
                    input = child.LocalNodes.Find(n => n.Equals(input));
                }

                Node output = new Node(link.Output.Id);
                if (!child.LocalNodes.Contains(output))
                {
                    child.Hidden.Add(output);
                    child.LocalNodes.Add(output);
                }
                else
                {
                    output = child.LocalNodes.Find(n => n.Equals(output));
                }

                double weight = link.Output.Ancestors[link.Input];
                if (rnd.Next(2) == 0 && worstGenes.Contains(link))
                {
                    Link worseLink = worstGenes.Find(l => l.Equals(link));
                    weight = worseLink.Output.Ancestors[worseLink.Input];
                }

                child.AddLink(input, output, weight);
            }

            return(child);
        }
Ejemplo n.º 5
0
        public double GetDistance(Genom other, double c1, double c2, double c3)
        {
            double N     = Math.Max(other.LocalLinks.Count, LocalLinks.Count);
            double delW  = 0;
            int    match = 0;
            int    exc   = 0;
            int    dis   = 0;

            List <Link> locLinks      = new List <Link>(LocalLinks);
            List <Link> otherLocLinks = new List <Link>(other.LocalLinks);

            locLinks.Sort(Link.InnNumberComparer);
            otherLocLinks.Sort(Link.InnNumberComparer);
            if (locLinks.Count < 40 && otherLocLinks.Count < 40)
            {
                N = 1;
            }

            int biggestLocalInn    = locLinks.Count == 0 ? -1 : locLinks[locLinks.Count - 1].InnNumber;
            int biggestOtherInn    = otherLocLinks.Count == 0 ? -1 : otherLocLinks[otherLocLinks.Count - 1].InnNumber;
            int smallestBiggestInn = Math.Min(biggestLocalInn, biggestOtherInn);

            List <Link> listToTrim = biggestLocalInn > biggestOtherInn ? locLinks : otherLocLinks;

            while (listToTrim.Count > 0 && listToTrim[listToTrim.Count - 1].InnNumber > smallestBiggestInn)
            {
                listToTrim.RemoveAt(listToTrim.Count - 1);
                exc++;
            }

            for (int i = 0; i < locLinks.Count; i++)
            {
                Link otherLink = otherLocLinks.Find(l => l.InnNumber == locLinks[i].InnNumber);
                if (otherLink != null)
                {
                    match++;
                    delW += Math.Abs(locLinks[i].Output.Ancestors[locLinks[i].Input] -
                                     otherLink.Output.Ancestors[otherLink.Input]);
                    locLinks.RemoveAt(i);
                    otherLocLinks.Remove(otherLink);
                    i--;
                }
            }

            dis = locLinks.Count + otherLocLinks.Count;
            if (match == 0)
            {
                match = 1;
            }

            return((c1 * exc) / N + (c2 * dis) / N + c3 * (delW / match));
        }
Ejemplo n.º 6
0
        public Genom GetBest(List <Genom> group)
        {
            Genom max = new Genom(0, 0);

            max.Fitness = 0;
            foreach (Genom genom in group)
            {
                if (genom.Fitness > max.Fitness)
                {
                    max = genom;
                }
            }

            return(max);
        }
Ejemplo n.º 7
0
        private void Speciate()
        {
            Dictionary <Genom, List <Genom> > newSpecies = new Dictionary <Genom, List <Genom> >();

            foreach (List <Genom> oldSpicie in _species.Values)
            {
                if (oldSpicie.Count == 0)
                {
                    continue;
                }

                Genom representative = oldSpicie[rnd.Next(oldSpicie.Count)];
                newSpecies.Add(representative, new List <Genom>());
                //newSpecies[representative].Add(representative);
            }

            bool isUniq;

            foreach (Genom genom in AllNets)
            {
                isUniq = true;
                foreach (Genom specieRepresentative in newSpecies.Keys)
                {
                    if (genom.GetDistance(specieRepresentative, C1, C2, C3) <= DistThreshold)
                    {
                        newSpecies[specieRepresentative].Add(genom);
                        isUniq = false;
                        break;
                    }
                }

                if (isUniq)
                {
                    newSpecies.Add(genom, new List <Genom>());
                    newSpecies[genom].Add(genom);
                }
            }

            _species = new Dictionary <Genom, List <Genom> >(newSpecies);
            foreach (Genom key in newSpecies.Keys)
            {
                if (newSpecies[key].Count == 0)
                {
                    _species.Remove(key);
                }
            }
        }