示例#1
0
    /// <summary>
    /// Creates a new connection from this node to target node via a new link
    /// </summary>
    /// <param name="target">TWEANNNode to link to</param>
    /// <param name="weight">Synaptic weight between the nodes</param>
    /// <param name="innovation">Innovation number of new link</param>
    /// <param name="recurrent">Whether or not the link is recurrent</param>
    /// <param name="frozen">Whether or not the link can be changed</param>
    public void Connect(TWEANNNode target, float weight, long innovation, bool recurrent, bool frozen)
    {
        TWEANNLink link = new TWEANNLink(target, weight, innovation, recurrent, frozen);

        //Debug.Log("Adding link from " + GetInnovationID() + " to " + target.GetInnovationID());
        outputs.Add(link);
    }
示例#2
0
    /// <summary>
    /// Create a new random TWEANN
    /// </summary>
    /// <param name="numInputs">Number of input sensors</param>
    /// <param name="numOutputs">Number of policy neurons per node</param>
    /// <param name="featureSelective">If true, nerons are only sparsely connected, fully connected otherwise</param>
    /// <param name="fType">Type of activation function on neurons</param>
    /// <param name="archetypeIndex">Archtype to align with for crossover</param>
    public TWEANN(int numInputs, int numOutputs, bool featureSelective, FTYPE fType, int archetypeIndex)
    {
        this.numInputs  = numInputs;
        this.numOutputs = numOutputs;
        ArchetypeIndex  = archetypeIndex;

        nodes = new List <TWEANNNode>(numInputs + numOutputs);

        long innovation = -1;

        for (int i = 0; i < numInputs; i++)
        {
            TWEANNNode n = new TWEANNNode(fType, NTYPE.INPUT, innovation--);
            nodes.Add(n);
        }
        long linkInnovationBound = innovation - 1;

        for (int j = 0; j < numOutputs; j++)
        {
            nodes.Add(new TWEANNNode(fType, NTYPE.OUTPUT, innovation--));

            int[] inputSources = new int[numInputs]; // HACK making this be fully connected for now
            for (int i = 0; i < numInputs; i++)
            {
                inputSources[i] = i;
            }
            for (int k = 0; k < inputSources.Length; k++)
            {
                nodes[inputSources[k]].Connect(nodes[numInputs + j], RandomGenerator.NextGaussian(), linkInnovationBound - (j * numInputs) - inputSources[k], false, false);
            }
        }
        int outputStart = nodes.Count - numOutputs;
    }
示例#3
0
    public TWEANN(TWEANNGenotype g)
    {
        Running        = true;
        ArchetypeIndex = g.GetArchetypeIndex();
        nodes          = new List <TWEANNNode>(g.Nodes.Count);
        int countIn = 0, countOut = 0;

        if (ArtGallery.DEBUG_LEVEL > ArtGallery.DEBUG.NONE)
        {
            Debug.Log("Starting TWEANNNodes build...");
        }
        foreach (NodeGene node in g.Nodes)
        {
            TWEANNNode tempNode = new TWEANNNode(node.fTYPE, node.nTYPE, node.Innovation, false, node.GetBias());
            nodes.Add(tempNode);
            if (node.nTYPE == NTYPE.INPUT)
            {
                countIn++;
            }
            else if (node.nTYPE == NTYPE.OUTPUT)
            {
                countOut++;
            }
        }
        numInputs  = countIn;
        numOutputs = countOut;
        if (ArtGallery.DEBUG_LEVEL > ArtGallery.DEBUG.NONE)
        {
            Debug.Log("Starting TWEANNLinks build...");
        }
        foreach (LinkGene link in g.Links)
        {
            TWEANNNode source = GetNodeByInnovationID(link.GetSourceInnovation());
            TWEANNNode target = GetNodeByInnovationID(link.GetTargetInnovation());

            if (source == null)
            {
                throw new System.Exception("Source not found with innovation " + link.GetSourceInnovation() + " : " + ToString());
            }
            if (target == null)
            {
                throw new System.Exception("Target not found with innovation " + link.GetTargetInnovation() + " : " + ToString());
            }

            source.Connect(target, link.GetWeight(), link.Innovation, false, false);
        }
        if (ArtGallery.DEBUG_LEVEL > ArtGallery.DEBUG.NONE)
        {
            Debug.Log("TWEANN build from TWEANNGenotype completed");
        }
        Running = false;
    }
示例#4
0
    /// <summary>
    /// Make a new link to target node
    /// </summary>
    /// <param name="target">TWEANNNode that link leads to</param>
    /// <param name="weight">Synaptic weight</param>
    /// <param name="innovation">Innovation number of the link</param>
    /// <param name="recurrent">Whether the link is recurrent</param>
    /// <param name="frozen">Whenter the link is frozen</param>
    public TWEANNLink(TWEANNNode target, float weight, long innovation, bool recurrent, bool frozen)
    {
        if (target != null)
        {
            this.target = target;
        }
        else
        {
            throw new System.ArgumentException("Target can not be null");
        }

        this.weight     = weight;
        this.innovation = innovation;
        this.recurrent  = recurrent;
        this.frozen     = frozen;
    }
示例#5
0
    public TWEANNLink GetLinkToTargetNode(TWEANNNode targetNode)
    {
        TWEANNLink result = null;

        foreach (TWEANNLink link in outputs)
        {
            if (link.GetTarget() == targetNode)
            {
                result = link;
            }
            else
            {
                throw new System.ArgumentException("No link to node found!");
            }
        }
        return(result);
    }
示例#6
0
    private TWEANNNode GetNodeByInnovationID(long innovationID)
    {
        TWEANNNode result = null;

        for (int i = 0; i < nodes.Count; i++)
        {
            if (nodes[i].GetInnovation() == innovationID)
            {
                result = nodes[i];
            }
            else
            {
                //throw new System.ArgumentException("No node found with innovationID " + innovationID);
            }
        }

        return(result);
    }