/// <summary> /// Copy an existing NodeGene. Only the id and the type will be copied. /// </summary> /// <param name="nodeGene"></param> public NodeGene(NodeGene nodeGene) { _id = nodeGene.ID; _type = nodeGene.Type; _typeOfFunction = nodeGene.TypeOfActivationFunction; _xValue = nodeGene.XValue; _inputs = new List <ConnectionGene>(); }
/// <summary> /// Create a new node gene with an id and a type /// </summary> /// <param name="id">of the node gene. Must be unique</param> /// <param name="type">of the node gene</param> public NodeGene(int id, NodeGeneType type, float xValue) { _id = id; _type = type; _xValue = xValue; _inputs = new List <ConnectionGene>(); _typeOfFunction = ActivationFunctionHelper.Function.SIGMOID; }
/// <summary> /// Create a new node gene with an id and a type /// </summary> /// <param name="id">of the node gene. Must be unique</param> /// <param name="type">of the node gene</param> /// <param name="function">the type of activation function</param> public NodeGene(int id, NodeGeneType type, float yValue, ActivationFunctionHelper.Function function) { _id = id; _type = type; _typeOfFunction = function; _xValue = yValue; _inputs = new List <ConnectionGene>(); }
/// <summary> /// Mutate the genome and replaces an existing connection with a new node and two new connections. /// The existing connection will be disabled and a new node will be created. The new node will /// be set between the input and output node of the old connection. /// Two connections will be created. The first one is from the disabled connections input node to the newly created node with a weight of 1. /// The second is from the newly created input node the the disabled connections output node with the weight if the disabled connection. /// If the connection is already disabled, null will be returned /// </summary> /// <param name="connection">the existing connection that will be replaced</param> /// <returns>an array with 4 objects. /// Index 0: the disabled connection /// Index 1: the newly created Node /// Index 2: the newly created Connection between the Input and the newly created node and a weight of 1 /// Index 3: the newly created Connection between the newly created node and the output node with the weight of the given connection. /// Return null, if the connection is already disabled</returns> public System.Object[] AddNodeMutation(ConnectionGene connection, int nodeID, int inToNewNodeInnovationNumber, int newToOutNodeInnovationNumber) { //Test if the connection is available if (!IsNodePossible(connection)) { return(null); } NodeGene inNode = _nodes[connection.InNode]; NodeGene outNode = _nodes[connection.OutNode]; //Disable the connection connection.Expressed = false; //TODO new float yValue = inNode.XValue + ((outNode.XValue - inNode.XValue) / 2); //Get the activation function ActivationFunctionHelper.Function type = Random.Range(-1f, 1f) >= 0 ? inNode.TypeOfActivationFunction : outNode.TypeOfActivationFunction; NodeGene newNode = new NodeGene(nodeID, NodeGeneType.HIDDEN, yValue, type); ConnectionGene inToNewNodeConnection = new ConnectionGene(inNode.ID, newNode.ID, 1f, true, inToNewNodeInnovationNumber); ConnectionGene newToOutNodeConnection = new ConnectionGene(newNode.ID, outNode.ID, connection.Weight, true, newToOutNodeInnovationNumber); _nodes.Add(newNode.ID, newNode); _connections.Add(inToNewNodeConnection.InnovationNumber, inToNewNodeConnection); _connections.Add(newToOutNodeConnection.InnovationNumber, newToOutNodeConnection); System.Object[] changedValues = new System.Object[4]; changedValues[0] = connection; changedValues[1] = newNode; changedValues[2] = inToNewNodeConnection; changedValues[3] = newToOutNodeConnection; return(changedValues); }