Пример #1
0
 /// <summary>
 /// Retrieves the id of a matching innovation.
 /// </summary>
 /// <param name="innovInfo">The information to search for.</param>
 /// <returns>The id of the first matching innovation. (-1) if not found.</returns>
 public int FindByInnovation(InnovationInformation innovInfo)
 {
     for (int i = 0; i < Size(); i++)
     {
         if (innovInfo.IsSameAs(Pool[i]))
         {
             return(i);
         }
     }
     return(-1);
 }
Пример #2
0
        /// <summary>
        /// Mutates a genome by breaking a connection up into two separate connections.
        /// </summary>
        /// <param name="genome">The genome to be modified.</param>
        /// <param name="innovationsSeen">A list of previously seen innovations.</param>
        /// <param name="rando">A random number generator.</param>
        public static void MutateAddNode(Genome genome, InnovationPool innovationsSeen, Random rando)
        {
            List <Gene> possibleConnections = new List <Gene>(genome.Genes);

            possibleConnections.RemoveAll(x => x.Frozen || NodePool.FindById(x.link.InNode).Type == NodeType.BIAS);

            if (possibleConnections.Count == 0)
            {
                return;
            }

            //TODO: Note in original algorithm saying uniform distribution is not optimal here.
            Gene geneToSplit = possibleConnections[rando.Next(possibleConnections.Count)];

            geneToSplit.Frozen = true;

            ActivationStyle       style      = ActivationFunctions.ChooseActivationStyle(rando);
            InnovationInformation innovation = new InnovationInformation(geneToSplit.link, style);

            int firstConId  = -1;
            int secondConId = -1;

            int registeredInnovationId = innovationsSeen.FindByInnovation(innovation);

            if (registeredInnovationId == -1)
            {
                int newNodeId = NodePool.Add(new NodeInformation(NodeType.HIDDEN, style));

                ConnectionInformation firstConnect = new ConnectionInformation(geneToSplit.link.InNode, newNodeId);
                firstConId = ConnectionPool.Add(firstConnect);

                ConnectionInformation secondConnect = new ConnectionInformation(newNodeId, geneToSplit.link.OutNode);
                secondConId = ConnectionPool.Add(secondConnect);

                innovation.NewNodeDetails.NewNodeId          = newNodeId;
                innovation.NewNodeDetails.FirstConnectionId  = firstConId;
                innovation.NewNodeDetails.SecondConnectionId = secondConId;
                innovationsSeen.Add(innovation);
            }
            else
            {
                InnovationInformation registeredInnovation = innovationsSeen.FindById(registeredInnovationId);
                firstConId  = registeredInnovation.NewNodeDetails.FirstConnectionId;
                secondConId = registeredInnovation.NewNodeDetails.SecondConnectionId;
            }

            genome.Genes.Add(new Gene(ConnectionPool.FindById(firstConId), firstConId, 1.0, false));
            genome.Genes.Add(new Gene(ConnectionPool.FindById(secondConId), secondConId, geneToSplit.Weight, false));
        }
Пример #3
0
        /// <summary>
        /// Checks if the two innovations are equivalent.
        /// </summary>
        /// <param name="other">The innovation to compare against.</param>
        /// <returns>True if they are the same.</returns>
        public bool IsSameAs(InnovationInformation other)
        {
            if (InnovationType == MutationStyle.AddConnection)
            {
                return(InnovationType == other.InnovationType &&
                       ConnectionInformation.IsSameAs(other.ConnectionInformation));
            }
            else if (InnovationType == MutationStyle.AddNode)
            {
                return(InnovationType == other.InnovationType &&
                       ConnectionInformation.IsSameAs(other.ConnectionInformation) &&
                       Style == other.Style);
            }

            return(false);
        }
Пример #4
0
        /// <summary>
        /// Creates an initial population.
        /// </summary>
        /// <param name="nodes">A list of NodeType-ActivationStyle pairing. The node Id will be the position in the list.</param>
        /// <param name="connections">A list of InNodeId-OutNodeId-Weight tuples.</param>
        /// <param name="populationSize">The size of the population to test.</param>
        /// <param name="rando">A random number generator for perturbing the weights.</param>
        public Population(List <Tuple <NodeType, ActivationStyle> > nodes, List <Tuple <int, int, double> > connections, int populationSize, Random rando)
        {
            TargetPopulationSize = populationSize;
            ValidateConstructorParameters(nodes.Count, connections);

            for (int i = 0; i < nodes.Count; i++)
            {
                NodePool.Add(new NodeInformation(nodes[i].Item1, nodes[i].Item2));
            }

            List <Gene> startGenes = new List <Gene>();

            foreach (var tupe in connections)
            {
                ConnectionInformation ci = new ConnectionInformation(tupe.Item1, tupe.Item2);
                startGenes.Add(new Gene(ci, ConnectionPool.Size(), tupe.Item3, false));

                InnovationInformation info = new InnovationInformation(ci);
                info.NewConnectionDetails.ConnectionId = ConnectionPool.Size();
                GenerationalInnovations.Add(info);

                ConnectionPool.Add(ci);
            }

            Genome        adam     = new Genome(startGenes);
            List <Genome> firstGen = new List <Genome>()
            {
                adam
            };

            for (int i = 1; i < TargetPopulationSize; i++)
            {
                Genome copy = new Genome(adam);
                Mutation.MutateTryAllNonStructural(copy, rando);
                firstGen.Add(copy);
            }

            SpeciateNewGeneration(firstGen);
        }
Пример #5
0
        /// <summary>
        /// Mutates a given genome by adding a connection.
        /// Connection is guaranteed to not be 'into' a sensor or bias.
        /// </summary>
        /// <param name="genome">The genome to be modified.</param>
        /// <param name="innovationsSeen">A list of previously seen innovations.</param>
        /// <param name="rando">A random number generator.</param>
        public static void MutateAddConnection(Genome genome, InnovationPool innovationsSeen, Random rando)
        {
            //TODO: I'm getting the node information, but I only need that to construct allNodesNotInput...

            Dictionary <int, NodeInformation> allNodes         = genome.GetAllNodeInformation(true);
            Dictionary <int, NodeInformation> allNodesNotInput = new Dictionary <int, NodeInformation>(allNodes);

            //TODO: Witnessed a bug where allNodes.Count == 0.
            //      Could be a node where there are only frozen links connecting.

            foreach (var id in allNodesNotInput.Where(kvp => kvp.Value.IsInput()).ToList())
            {
                allNodesNotInput.Remove(id.Key);
            }

            //TODO: Gotta be a better way than a tryCount...
            int tryCount   = 0;
            int nodeFromId = -1;
            int nodeToId   = -1;

            while (tryCount < 20)
            {
                nodeFromId = allNodes.Keys.ToList()[rando.Next(allNodes.Count)];
                nodeToId   = allNodesNotInput.Keys.ToList()[rando.Next(allNodesNotInput.Count)];

                if (!genome.ContainsConnection(nodeFromId, nodeToId))
                {
                    break;
                }

                tryCount++;
            }

            if (tryCount == 20)
            {
                return;
            }

            ConnectionInformation connectInfo = new ConnectionInformation(nodeFromId, nodeToId);
            InnovationInformation innovation  = new InnovationInformation(connectInfo);

            int connectId = -1;
            //TODO: Pull inital weight setting out of here.
            double weight = rando.NextDouble() * 2.0 - 1.0;

            int registeredInnovationId = innovationsSeen.FindByInnovation(innovation);

            if (registeredInnovationId == -1)
            {
                connectId = ConnectionPool.Add(connectInfo);

                innovation.NewConnectionDetails.ConnectionId = connectId;
                innovationsSeen.Add(innovation);
            }
            else
            {
                connectId = innovationsSeen.FindById(registeredInnovationId).NewConnectionDetails.ConnectionId;
            }

            genome.Genes.Add(new Gene(ConnectionPool.FindById(connectId), connectId, weight, false));
        }
Пример #6
0
 /// <summary>
 /// Adds a new innovation to the pool.
 /// </summary>
 /// <param name="innovInfo">The information uniquely identifying the innovation.</param>
 /// <returns>The position in the Pool (unique Id).</returns>
 public int Add(InnovationInformation innovInfo)
 {
     Pool.Add(innovInfo);
     return(Size() - 1);
 }