Ejemplo n.º 1
0
        // Makes crossover unstable
        public void MutateNode_RearrangeNetworkTest()
        {
            if (ConnectionCollection.Count <= 0)
            {
                return;
            }

            var connection = GetRandomConnection();

            connection.Enabled = false;
            DebugConnection(connection);

            var newNode = Evaluator.CreateHiddenNode(connection.Output);
            var oldNode = NodeCollection.Nodes[connection.Output];

            NodeCollection.Nodes.Remove(oldNode.Id);
            oldNode.Id = Evaluator.GetNextNodeId();
            NodeCollection.Nodes[newNode.Id] = newNode;
            NodeCollection.Nodes[oldNode.Id] = oldNode;

            // swap old connection to shifted node
            (int, int)oldCon  = (connection.Input, connection.Output);
            connection.Output = newNode.Id;
            ConnectionCollection.UpdateInOut(oldCon, connection);

            foreach (var con in ConnectionCollection.Connections.Where(x => x.Value.Input == newNode.Id || x.Value.Output == newNode.Id).ToList())
            {
                (int, int)oldKey = (con.Value.Input, con.Value.Output);
                if (con.Value.Input == newNode.Id)
                {
                    con.Value.Input = oldNode.Id;
                }

                if (con.Value.Output == newNode.Id)
                {
                    con.Value.Output = oldNode.Id;
                }

                ConnectionCollection.UpdateInOut(oldKey, con.Value);
            }

            var con1 = Evaluator.CreateConnection(this, Evaluator.GetNextInnovationId(), connection.Input, newNode.Id, Config.MutateNodeWeightInitialValue);
            var con2 = Evaluator.CreateConnection(this, Evaluator.GetNextInnovationId(), newNode.Id, oldNode.Id, connection.Weight);

            bool addedCon1       = Evaluator.AddConnection(con1);
            bool addedCon2       = Evaluator.AddConnection(con2);
            bool addedconnection = Evaluator.AddConnection(connection);

            bool addedConLocal1 = ConnectionCollection.AddNew(con1);
            bool addedConLocal2 = ConnectionCollection.AddNew(con2);

            NodeCollection.Nodes.Remove(newNode.Id);
            NodeCollection.AddNew(oldNode);
            NodeCollection.AddNew(newNode);
        }
Ejemplo n.º 2
0
        public bool AddConnection(int inputKey, int outputKey)
        {
            var input  = NodeCollection.Nodes[inputKey];
            var output = NodeCollection.Nodes[outputKey];

            if (NodeCollection.Nodes.Count == 0)
            {
                return(false);
            }

            bool geneValid = !Config.FeedForwardNetwork || !((input.Type & output.Type) == NodeGeneType.Sensor || (input.Type & output.Type) == NodeGeneType.Output || input.Id == output.Id);

            (int, int)key = (input.Id, output.Id);

            if (geneValid)
            {
                if (ConnectionCollection.Exists(key))
                {
                    return(false);
                }

                ConnectionGene connection;
                if (!Evaluator.Connections.Exists(key))
                {
                    connection = new ConnectionGene
                    {
                        Enabled          = true,
                        Input            = input.Id,
                        Output           = output.Id,
                        Weight           = Evaluator.GetRandomNumber(-Config.WeightMutationPower, Config.WeightMutationPower),
                        InnovationNumber = Evaluator.GetNextInnovationId(),
                        Genome           = this
                    };
                    Evaluator.AddConnection(connection);
                }
                else
                {
                    // old connection gets copied
                    connection        = Evaluator.Connections.Get(key).Copy(this);
                    connection.Weight = Evaluator.GetRandomNumber(-Config.WeightMutationPower,
                                                                  Config.WeightMutationPower);

                    if (connection.Input != key.Item1 || connection.Output != key.Item2)
                    {
                        throw new InvalidOperationException();
                    }

                    connection.Enabled = true;
                }

                ConnectionCollection.AddNew(connection);
                return(true);
            }
            return(false);
        }
Ejemplo n.º 3
0
        public void MutateNode()
        {
            if (ConnectionCollection.Count <= 0)
            {
                return;
            }

            var connection = GetRandomConnection();

            connection.Enabled = false;
            DebugConnection(connection);

            var node = Evaluator.CreateHiddenNode(Evaluator.GetNextNodeId());

            var con1 = Evaluator.CreateConnection(this, Evaluator.GetNextInnovationId(), connection.Input, node.Id, Config.MutateNodeWeightInitialValue);
            var con2 = Evaluator.CreateConnection(this, Evaluator.GetNextInnovationId(), node.Id, connection.Output, connection.Weight);

            bool addedCon1 = Evaluator.AddConnection(con1);
            bool addedCon2 = Evaluator.AddConnection(con2);

            bool addedConLocal1 = ConnectionCollection.AddNew(con1);
            bool addedConLocal2 = ConnectionCollection.AddNew(con2);

            bool nodeAdded = NodeCollection.AddNew(node);

            if (!nodeAdded)
            {
                throw new InvalidOperationException();
            }


            if (NodeCollection.Get(con1.Input) == null)
            {
                throw new InvalidOperationException();
            }
        }
Ejemplo n.º 4
0
        public void MutateConnection()
        {
            bool geneAdded = false;

            while (!geneAdded)
            {
                if (NodeCollection.Nodes.Count == 0)
                {
                    break;
                }

                bool geneValid = true;

                int indexInput  = Evaluator.Random.Next(0, NodeCollection.Count);
                int indexOutput = Evaluator.Random.Next(0, NodeCollection.Count);
                var input       = NodeCollection[indexInput];
                var output      = NodeCollection[indexOutput];

                //if (input.Id >= output.Id || (input.Type & output.Type) == NodeGeneType.Sensor)
                //    geneValid = false;

                if (Config.FeedForwardNetwork && ((input.Type & output.Type) == NodeGeneType.Sensor ||
                                                  (input.Type & output.Type) == NodeGeneType.Output ||
                                                  input.Id == output.Id) ||
                    //(output.Type == NodeGeneType.Output && input.Type == NodeGeneType.Hidden) ||
                    //(input.Type == NodeGeneType.Output && output.Type == NodeGeneType.Sensor) ||
                    input.Type == NodeGeneType.Output || output.Type == NodeGeneType.Sensor)
                {
                    geneValid = false;
                }

                (int, int)key = (input.Id, output.Id);

                if (geneValid)
                {
                    if (ConnectionCollection.Exists(key))
                    {
                        geneValid = false;
                        break;
                    }

                    ConnectionGene connection;
                    if (!Evaluator.Connections.Exists(key))
                    {
                        connection = new ConnectionGene
                        {
                            Enabled          = true,
                            Input            = input.Id,
                            Output           = output.Id,
                            Weight           = Evaluator.GetRandomNumber(-Config.WeightMutationPower, Config.WeightMutationPower),
                            InnovationNumber = Evaluator.GetNextInnovationId(),
                            Genome           = this
                        };
                        Evaluator.AddConnection(connection);
                    }
                    else
                    {
                        // old connection gets copied
                        connection        = Evaluator.Connections.Get(key).Copy(this);
                        connection.Weight = Evaluator.GetRandomNumber(-Config.WeightMutationPower,
                                                                      Config.WeightMutationPower);

                        if (connection.Input != key.Item1 || connection.Output != key.Item2)
                        {
                            throw new InvalidOperationException();
                        }

                        connection.Enabled = true;
                    }

                    geneAdded = true;
                    ConnectionCollection.AddNew(connection);
                }
            }
        }