/// <summary> /// Create the start genome /// </summary> private void SetStartGenome() { _nodeCounter = new GeneCounter(0); _connectionCounter = new GeneCounter(0); _startGenome = new Genome(); int amountPlayerInputs = PlayerController.GetAmountOfInputs(_levelViewWidht, _levelViewHeight); List <NodeGene> tmpInputNodes = new List <NodeGene>(); //Crate input nodes for (int i = 0; i < amountPlayerInputs; i++) { NodeGene node = new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.SIGMOID); _startGenome.AddNodeGene(node); tmpInputNodes.Add(node); } //Create output nodes NodeGene outputNode1 = new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.OUTPUT, 1f, ActivationFunctionHelper.Function.SIGMOID); NodeGene outputNode2 = new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.OUTPUT, 1f, ActivationFunctionHelper.Function.SIGMOID); _startGenome.AddNodeGene(outputNode1); _startGenome.AddNodeGene(outputNode2); //Create connections for (int i = 0; i < amountPlayerInputs; i++) { _startGenome.AddConnectionGene(new ConnectionGene(tmpInputNodes[i].ID, outputNode1.ID, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); _startGenome.AddConnectionGene(new ConnectionGene(tmpInputNodes[i].ID, outputNode2.ID, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); } }
public void MutateAgent_Weight_Test() { MutationLog mutationLog = new MutationLog(); AgentObject customAgent = new CustomAgent(populationManager, parent2Genome, 10); Assert.AreEqual(6, customAgent.Genome.Nodes.Count); Assert.AreEqual(9, customAgent.Genome.Connections.Count); Dictionary <int, double> weights = new Dictionary <int, double>(); foreach (int key in customAgent.Genome.Connections.Keys) { weights.Add(key, customAgent.Genome.Connections[key].Weight); } //Mutate weights populationManager.MutateAgent(customAgent, 0f, 0f, 1f, nodeInnovationCounter, connectionInnovationCounter, mutationLog); //Nodes should not have changed Assert.AreEqual(6, customAgent.Genome.Nodes.Count); Assert.AreEqual(9, customAgent.Genome.Connections.Count); Assert.AreEqual(14, connectionInnovationCounter.GetNewNumber()); Assert.AreEqual(11, nodeInnovationCounter.GetNewNumber()); foreach (int key in weights.Keys) { Assert.AreNotEqual(weights[key], customAgent.Genome.Connections[key].Weight); } }
/// <summary> /// Create the start genome /// </summary> private void SetStartGenome() { _nodeCounter = new GeneCounter(0); _connectionCounter = new GeneCounter(0); _startGenome = new Genome(); //5 input nodes + 1 bias node _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID)); _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID)); _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID)); _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID)); _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID)); _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.INPUT, 0f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID)); //OutputNodes _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.OUTPUT, 1f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID)); _startGenome.AddNodeGene(new NodeGene(_nodeCounter.GetNewNumber(), NodeGeneType.OUTPUT, 1f, ActivationFunctionHelper.Function.STEEPENED_SIGMOID)); //Add connections to first output nde _startGenome.AddConnectionGene(new ConnectionGene(0, 6, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); _startGenome.AddConnectionGene(new ConnectionGene(1, 6, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); _startGenome.AddConnectionGene(new ConnectionGene(2, 6, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); _startGenome.AddConnectionGene(new ConnectionGene(3, 6, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); _startGenome.AddConnectionGene(new ConnectionGene(4, 6, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); _startGenome.AddConnectionGene(new ConnectionGene(5, 6, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); //Connection to second ouztputNode _startGenome.AddConnectionGene(new ConnectionGene(0, 7, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); _startGenome.AddConnectionGene(new ConnectionGene(1, 7, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); _startGenome.AddConnectionGene(new ConnectionGene(2, 7, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); _startGenome.AddConnectionGene(new ConnectionGene(3, 7, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); _startGenome.AddConnectionGene(new ConnectionGene(4, 7, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); _startGenome.AddConnectionGene(new ConnectionGene(5, 7, Random.Range(-1f, 1f), true, _connectionCounter.GetNewNumber())); }
/// <summary> /// Get an InnovationNumber for the connection between the given nodes. /// If there is already and entry for the connection, the existing InnovationNumber will be returned /// If it is a new connection, a new number will be generated with the connectionMutationCounter and after that directly stored /// </summary> /// <param name="inNodeID">the in node of the new connection</param> /// <param name="outNodeID">the out node of the new connection</param> /// <param name="connectionMutationCounter">GeneCounter to generate a new number if necessary</param> /// <returns>the InnovationNumber for the connection</returns> public int GetConnectionInnovationNumber(int inNodeID, int outNodeID, GeneCounter connectionMutationCounter) { int innovationNumber = IsConnectionExisting(inNodeID, outNodeID); //If node is existing return the value if (innovationNumber != -1) { return(innovationNumber); } innovationNumber = connectionMutationCounter.GetNewNumber(); //Store new mutation AddConnectionMutation(inNodeID, outNodeID, innovationNumber); return(innovationNumber); }
/* * * /// <summary> * /// Get an ID for the a new node that will be splaced between the given connection. * /// If there is already an entry for the node, the existing id will be returned. * /// If it is a new node a new numer will be generated with the GeneCounter and after that directly stored. * /// </summary> * /// <param name="connectionInnovationNumber">innovationNumber of the conenction that will be splitted</param> * /// <param name="nodeMutationCounter">to generate a new node ID if necessary</param> * /// <returns>an ID for the node</returns> * public int GetNodeID(int connectionInnovationNumber, GeneCounter nodeMutationCounter) * { * int nodeID = IsNodeExisting(connectionInnovationNumber); * * //If node is existing return the value * if (nodeID != -1) return nodeID; * * nodeID = nodeMutationCounter.GetNewNumber(); * * //Add node to the list * AddNodeMutation(connectionInnovationNumber, nodeID); * * return nodeID; * * } * */ /// <summary> /// Get an id for a new node. /// If the mutation has already occured, an existing id will be returned. /// If the mutation is new, a new id will be generated and stored in the log /// </summary> /// <param name="connectionInnovationNumber">the innovation number of the splitted connection</param> /// <param name="nodeIDs">a list with all node ids' of the genome</param> /// <param name="nodeMutationCounter">a node mutation counter to generate a new id if necessary</param> /// <returns>an id for the node</returns> public int GetNodeID(int connectionInnovationNumber, List <int> nodeIDs, GeneCounter nodeMutationCounter) { List <int> existingNodeIDs = IsNodeExisting(connectionInnovationNumber); foreach (int existingID in existingNodeIDs) { //If the node Id list does not contain one of the existing nodes, break the loop if (!nodeIDs.Contains(existingID)) { return(existingID); } } //If the id is new, add an entry in the log int newID = nodeMutationCounter.GetNewNumber(); AddNodeMutation(connectionInnovationNumber, newID); return(newID); }