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);
        }