public void ApplyMutation(ref NetModel net) { if (net.Connections.Count == 0) { return; } // Shifts weight var rnd = new Random(); var connectionId = rnd.Next(0, net.Connections.Count); net.Connections[connectionId].Weight *= rnd.NextDouble(); // Saves change _connectionProcessor.Update(net.Connections[connectionId]); }
/// <summary> /// Changes enabled/disabled state of specific connection /// </summary> /// <param name="net">Network that contains connection</param> /// <param name="connId">Connections id int list (not db id and not innovation id)</param> private void ToggleConnection(ref NetModel net, int connId) { net.Connections[connId].Enabled = !net.Connections[connId].Enabled; _connectionProcessor.Update(net.Connections[connId]); }
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); }