Esempio n. 1
0
    // reset phenotype networks after each turn
    public void removePhenotypeNetworks()
    {
        bool          removed  = false;
        List <string> toRemove = new List <string>();

        foreach (string netKey in networks[0].Keys)
        {
            if (netKey.StartsWith("phenotypeNet"))
            {
                toRemove.Add(netKey);
                removed = true;
            }
        }
        for (int i = 0; i < toRemove.Count; i++)
        {
            networks[0].Remove(toRemove[i]);
        }

        // remove extra nodes as well

        // for every output network
        if (removed)
        {
            // remove extra nodes
            foreach (OutputNetwork net in networks[networks.Count - 1].Values)
            {
                List <Node> removeNodes = new List <Node>();
                // for every inner input node in first layer, delete if temp
                foreach (Node node in net.net[0])
                {
                    if (node.GetType().Name == "InnerInputNode")
                    {
                        InnerInputNode iiNode = (InnerInputNode)node;
                        if (iiNode.temp)
                        {
                            removeNodes.Add(node);
                        }
                    }
                }
                foreach (Node node in removeNodes)
                {
                    net.net[0].Remove(node);
                }

                // reassign previous nodes after deletion
                for (int i = 1; i < net.net.Count; i++)
                {
                    for (int j = 0; j < net.net[i].Count; j++)
                    {
                        NonInputNode niNode = (NonInputNode)net.net[i][j];
                        niNode.assignPrevNodes();
                    }
                }
            }
        }
    }
Esempio n. 2
0
    public static Node getNewNode(Node oldNode, Creature creatureCopy, Network parentNet)
    {
        if (oldNode.GetType().Name == "SensoryInputNode")
        {
            SensoryInputNode newNode = (SensoryInputNode)oldNode.clone();
            newNode.creature = creatureCopy;
            return(newNode);
        }
        else if (oldNode.GetType().Name == "InternalResourceInputNode")
        {
            InternalResourceInputNode newNode = (InternalResourceInputNode)oldNode.clone();
            newNode.creature = creatureCopy;
            return(newNode);
        }
        else if (oldNode.GetType().Name == "OutputNode")
        {
            OutputNode oldNode2 = (OutputNode)oldNode;
            OutputNode newNode  = (OutputNode)oldNode.clone();
            newNode.resetRand();
            newNode.parentNet      = parentNet;
            newNode.parentCreature = creatureCopy;
            newNode.action         = getNewAction(newNode.action);
            //newNode.setActivBehavior(new LogisticActivBehavior());
            newNode.prevNodes = new List <Node>();
            newNode.assignPrevNodes();
            newNode.weights = new List <float>();
            for (int i = 0; i < oldNode2.weights.Count; i++)
            {
                newNode.weights.Add(oldNode2.weights[i]);
            }
            newNode.extraWeights = new List <float>();
            for (int i = 0; i < oldNode2.extraWeights.Count; i++)
            {
                newNode.extraWeights.Add(oldNode2.extraWeights[i]);
            }
            return(newNode);
        }
        else if (oldNode.GetType().Name == "NonInputNode")
        {
            NonInputNode oldNode2 = (NonInputNode)oldNode;
            NonInputNode newNode  = (NonInputNode)oldNode.clone();
            newNode.resetRand();
            newNode.parentNet      = parentNet;
            newNode.parentCreature = creatureCopy;
            //newNode.setActivBehavior(new LogisticActivBehavior());
            newNode.prevNodes = new List <Node>();
            newNode.assignPrevNodes();
            newNode.weights = new List <float>();
            for (int i = 0; i < oldNode2.weights.Count; i++)
            {
                newNode.weights.Add(oldNode2.weights[i]);
            }
            newNode.extraWeights = new List <float>();
            for (int i = 0; i < oldNode2.extraWeights.Count; i++)
            {
                newNode.extraWeights.Add(oldNode2.extraWeights[i]);
            }
            return(newNode);
        }
        else if (oldNode.GetType().Name == "BiasNode")
        {
            BiasNode oldNode2 = (BiasNode)oldNode;
            BiasNode newNode  = oldNode2.clone();
            return(newNode);
        }

        else if (oldNode.GetType().Name == "InnerInputNode")
        {
            InnerInputNode oldNode2 = (InnerInputNode)oldNode;
            InnerInputNode newNode  = oldNode2.clone();
            newNode.parentCreature = creatureCopy;

            /*
             * Debug.Log("linked network layer number: " + oldNode2.linkedNodeNetworkLayer);
             * Debug.Log("net name: " + oldNode2.linkedNetName);
             * Debug.Log("node index: " + newNode.linkedNodeIndex);
             */
            Network linkedNetwork = creatureCopy.networks[oldNode2.linkedNodeNetworkLayer][oldNode2.linkedNetName];
            newNode.linkedNode = linkedNetwork.net[linkedNetwork.net.Count - 1][newNode.linkedNodeIndex];
            return(newNode);
        }
        // called when template is being copied
        else if (oldNode.GetType().Name == "PhenotypeInputNode")
        {
            PhenotypeInputNode oldNode2 = (PhenotypeInputNode)oldNode;
            PhenotypeInputNode newNode  = (PhenotypeInputNode)oldNode.clone();
            // copy phenotype
            newNode.parentCreat = creatureCopy;
            int length = oldNode2.phenotype.Length;
            newNode.phenotype = new bool[length];
            for (int i = 0; i < newNode.phenotype.Length; i++)
            {
                newNode.phenotype[i] = oldNode2.phenotype[i];
            }
            return(newNode);
        }

        else if (oldNode.GetType().Name == "CommInputNode")
        {
            //TODO
            return(null);
        }
        else if (oldNode.GetType().Name == "MemoryInputNode")
        {
            //TODO
            return(null);
        }
        else
        {
            Debug.LogError("Didn't find correct type of node to add");
            return(null);
        }
    }