public void ApplyMutation(ref NetModel net) { var(fromId, toId) = GetRandomNodeIds(net); if (HasConnection(net, fromId, toId) is { } id) { ToggleConnection(ref net, id); return; } // Finds innovation number based on connection locations: from, to var innovation = _innovationController.FetchWithInsertOnFail(new InnovationModel { BatchId = net.BatchId, NodeFromId = fromId, NodeToId = toId, Type = InnovationType.Connection }); // Creates new connections var rnd = new Random(); var connection = new ConnectionModel { FromId = fromId, ToId = toId, InnovationId = innovation.Id, NetworkId = net.Id, Weight = rnd.NextDouble() * 2 - 1 // Range of -1 to 1 }; // Saves and Adds new connection to network _connectionProcessor.Save(ref connection); net.Connections.Add(connection); }
public void ApplyMutation(ref NetModel net) { // There is no connection thus no point of trying to insert node, // because it can only be inserted between two nodes AKA in connection if (net.Connections.Count == 0) { return; } // Finds connection - random var rnd = new Random(); var connection = net.Connections[rnd.Next(0, net.Connections.Count)]; // Gets innovation id for new node var innovation = _innovationController.FetchWithInsertOnFail(new InnovationModel { BatchId = net.BatchId, NodeFromId = connection.FromId, NodeToId = connection.ToId, Type = InnovationType.Node }); // Makes two new connections, thus inserting new node between two others var connFromToNew = new ConnectionModel { FromId = connection.FromId, ToId = innovation.Id, NetworkId = net.Id, InnovationId = _innovationController.FetchWithInsertOnFail(new InnovationModel { BatchId = net.BatchId, NodeFromId = connection.FromId, NodeToId = innovation.Id, Type = InnovationType.Connection }).Id }; _connectionProcessor.Save(ref connFromToNew); net.Connections.Add(connFromToNew); var connNewToTo = new ConnectionModel { FromId = innovation.Id, ToId = connection.ToId, NetworkId = net.Id, InnovationId = _innovationController.FetchWithInsertOnFail(new InnovationModel { BatchId = net.BatchId, NodeFromId = innovation.Id, NodeToId = connection.ToId, Type = InnovationType.Connection }).Id }; _connectionProcessor.Save(ref connNewToTo); net.Connections.Add(connNewToTo); // Disables old connection connection.Enabled = false; _connectionProcessor.Update(connection); // Create new node only if it doesn't exist if (net.Nodes.Any(n => n.InnovationId == innovation.Id)) { return; } var nodeModel = new NodeModel { InnovationId = innovation.Id, NetworkId = net.Id }; _nodeProcessor.Save(ref nodeModel); net.Nodes.Add(nodeModel); }