Example #1
0
    /// <summary>
    /// Update this visualisation
    /// </summary>
    /// <param name="node">The node for which this is a visualisation of</param>
    public void SetVisualisation(NeatNode node, bool isHidden = false)
    {
        netNode = node;

        // Disable visual for input nodes
        gameObject.SetActive(!isHidden);
    }
Example #2
0
    /// <summary>
    /// Randomly place a node in the middle of an existing gene
    /// </summary>
    private void AddMutatedNode()
    {
        if (genes.Count == 0)
        {
            return;
        }

        // 30 attempts to create a node
        for (int i = 0; i < 30; ++i)
        {
            int      geneId = Random.Range(0, genes.Count);
            NeatGene gene   = genes[geneId];

            // Only add node in enabled genes
            if (!gene.isEnabled)
            {
                continue;
            }

            // Add the node
            NeatNode newNode = new NeatNode(nodes.Count, NeatNode.NodeType.Hidden, this);
            nodes.Add(newNode);

            // Disable old gene and add connected genes to new node
            gene.isEnabled = false;
            CreateGene(gene.fromNodeId, newNode.ID).weight = 1.0f;             // Use 1.0 to avoid initial impact of adding the node
            CreateGene(newNode.ID, gene.toNodeId).weight   = gene.weight;
            return;
        }
    }
Example #3
0
    /// <summary>
    /// Calculate the value for this node be reading incoming connections
    /// </summary>
    /// <param name="network">List of all the nodes in the network</param>
    /// <returns>The activated value for this node</returns>
    public float CalculateValue()
    {
        if (type == NodeType.Input || workingValueFinal)
        {
            return(workingValue); // Value is set or cached for us
        }
        else if (workingOutValue) // Can only really return approximation (Occurs in event of loop)
        {
            return((float)Math.Tanh(workingValue));
        }


        // Haven't worked out the value yet, so need to work it out now
        workingValue    = 0.0f;
        workingOutValue = true;

        foreach (NeatGene input in inputGenes)
        {
            // Only considered enabled genes
            if (!input.isEnabled)
            {
                continue;
            }

            NeatNode inNode = network.nodes[input.fromNodeId];
            workingValue += inNode.CalculateValue() * input.weight;
        }


        // Pass summation through activation function and cache until net inputs
        workingValue      = (float)Math.Tanh(workingValue);
        workingValueFinal = true;
        workingOutValue   = false;

        return(workingValue);
    }