コード例 #1
0
    public void createNodePanel(NonInputNode node)
    {
        nodePanel.SetActive(true);
        NodePanelPopulator panelPop = nodePanel.GetComponent <NodePanelPopulator>();

        panelPop.setNode(node);
    }
コード例 #2
0
    public void updateWeightsByCreature()
    {
        weightsByCreature = new List <List <float> >(); // reset for new set of creatures
        // store weights of all creatures in 2D list
        // for the ith creature
        for (int i = 0; i < creatures.Count; i++)
        {
            int creatureWeightIndex = 0; // the index of the the weight out of all weights in a creature
            // add a list for every creature
            weightsByCreature.Add(new List <float>());

            // for every node
            for (int j = 0; j < creatures[i].networks.Count; j++)
            {
                foreach (string key in creatures[i].networks[j].Keys)
                {
                    // if it's not a phenotype network
                    if (!key.StartsWith("phenotypeNet"))
                    {
                        for (int k = 0; k < creatures[i].networks[j][key].net.Count; k++)
                        {
                            for (int l = 0; l < creatures[i].networks[j][key].net[k].Count; l++)
                            {
                                bool         castWorked = true;
                                NonInputNode node       = null;
                                // get node and convert it to Non-Input node if possible
                                try
                                {
                                    node = (NonInputNode)creatures[i].networks[j][key].net[k][l];
                                }
                                catch (InvalidCastException e)
                                {
                                    castWorked = false;
                                }
                                // if it is a non-input node
                                if (castWorked)
                                {
                                    // reset creature weights indicies in node
                                    node.creatureWeightsIndicies = new List <int>();

                                    // for every weight
                                    for (int m = 0; m < node.weights.Count; m++)
                                    {
                                        // add the weight to our weights by creature list
                                        weightsByCreature[i].Add(node.weights[m]); // store weights by creature and weight index
                                                                                   // add index of where weights are stored in population to node
                                        node.creatureWeightsIndicies.Add(creatureWeightIndex);
                                        creatureWeightIndex++;
                                    }
                                    //Debug.Log(node.creatureWeightsIndicies.Count);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
コード例 #3
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();
                    }
                }
            }
        }
    }
コード例 #4
0
    // TODO : test
    public void addPhenotypeNetworks()
    {
        // add network for every neighbor with a creature
        for (int i = 1; i < neighborLands.Length; i++)
        {
            if (neighborLands[i].creatureIsOn())
            {
                // get a copy of the template
                // Debug.Log("template first layer length: " + phenotypeNetTemplate.net[0].Count);
                PhenotypeNetwork phenotypeNet = (PhenotypeNetwork)Copier.copyNetwork(phenotypeNetTemplate, this);

                // set the phenotype used in the template
                phenotypeNet.setInputNodes(neighborLands[i].creatureOn.phenotype, i);
                // add the network to the creatures networks
                string phenoNetName = "phenotypeNet" + i;
                networks[0].Add(phenoNetName, phenotypeNet);

                // for every output node in the phenotype network
                int length = phenotypeNet.net.Count;
                for (int j = 0; j < phenotypeNet.net[length - 1].Count; j++)
                {
                    OutputNode outNode = (OutputNode)phenotypeNet.net[length - 1][j];

                    // for every output network, add an inner-input node for the phenotype node, if applicable
                    foreach (OutputNetwork net in networks[networks.Count - 1].Values)
                    {
                        // output network must match action of phenotype output node
                        if (net.outputAction.name.Equals(outNode.action.name))
                        {
                            // create a new inner-input node
                            InnerInputNode node = new InnerInputNode();

                            // set linked node
                            node.parentCreature = this;
                            node.setLinkedNode(this, 0, phenoNetName, phenotypeNet.net.Count - 1, j);
                            node.temp = true;

                            // add inner-input node to first layer of output network
                            net.net[0].Add(node);
                            // add previous node for all nodes in second layer of output network
                            for (int k = 0; k < net.net[1].Count; k++)
                            {
                                NonInputNode niNode = (NonInputNode)net.net[1][k];
                                niNode.appendPrevNode(net.net[0][net.net[0].Count - 1]);
                            }
                        }
                    }
                }
            }
        }
    }
コード例 #5
0
    public void loadWeightsFromFile()
    {
        List <float> weightAverages = CreatureAveragesIO.loadAverages();
        int          avgsIndex      = 0;

        // for every node
        for (int j = 0; j < creature.networks.Count; j++)
        {
            foreach (string key in creature.networks[j].Keys)
            {
                for (int k = 0; k < creature.networks[j][key].net.Count; k++)
                {
                    for (int l = 0; l < creature.networks[j][key].net[k].Count; l++)
                    {
                        bool         castWorked = true;
                        NonInputNode node       = null;
                        // get node and convert it to Non-Input node if possible
                        try
                        {
                            node = (NonInputNode)creature.networks[j][key].net[k][l];
                        }
                        catch (InvalidCastException e)
                        {
                            castWorked = false;
                        }
                        // if it is a non-input node
                        if (castWorked)
                        {
                            for (int i = 0; i < node.weights.Count; i++)
                            {
                                try
                                {
                                    node.weights[i] = weightAverages[avgsIndex];
                                    avgsIndex++;
                                }
                                catch (ArgumentOutOfRangeException e)
                                {
                                    Debug.LogError("Tried to read weight from file, but weight array length didn't match current creature.");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
コード例 #6
0
    public void setNode(NonInputNode _node)
    {
        int x = 147;
        int y = -80;

        node = _node;
        Debug.Log(node.value);

        List <float> averages = node.getWeightAverages();
        List <float> sDs      = node.getWeightSDs();

        Debug.Log(averages.Count);

        for (int i = 0; i < node.weights.Count; i++)
        {
            GameObject created = GameObject.Instantiate(textObj);
            Text       txt     = created.GetComponent <Text>();

            double rounded = System.Math.Round((double)node.weights[i], 2);

            // case for phenotype network nodes
            if (averages.Count == node.weights.Count)
            {
                double rounded2 = System.Math.Round((double)averages[i], 2);
                double rounded3 = System.Math.Round((double)sDs[i], 2);
                txt.text = rounded.ToString() + "          " + rounded2 + "           " + rounded3;
            }
            else
            {
                txt.text = rounded.ToString();
            }



            created.transform.SetParent(canvas.transform, false);
            RectTransform transform = created.GetComponent <RectTransform>();
            transform.anchoredPosition = new Vector2(x, y);
            y -= 20;
            if (i % 10 == 0 && i != 0)
            {
                y  = -80;
                x += 70;
            }
            instantiated.Add(created);
        }
    }
コード例 #7
0
 public HiddenNodeEditor(NonInputNode node, int nodeLayer)
 {
     this.hiddenNode = node;
     this.nodeLayer  = nodeLayer;
 }
コード例 #8
0
    public void setNetwork(Network _net)
    {
        int x = 130;
        int y = -50;

        net = _net;
        List <List <Node> > layers = net.net;

        for (int i = 0; i < layers.Count; i++)
        {
            for (int j = 0; j < layers[i].Count; j++)
            {
                GameObject created = Instantiate(nodePrefab);

                Text   title = created.transform.GetChild(1).gameObject.GetComponent <Text>();
                Text   value = created.transform.GetChild(2).gameObject.GetComponent <Text>();
                Button btn   = created.transform.GetChild(3).gameObject.GetComponent <Button>();
                //btn.name = j.ToString();

                string type = layers[i][j].GetType().Name;
                //Debug.Log("type " + type);
                bool         castWorked = true;
                NonInputNode nodeArg    = null;
                try
                {
                    nodeArg = (NonInputNode)layers[i][j];
                }
                catch (InvalidCastException e)
                {
                    castWorked = false;
                }
                if (castWorked)
                {
                    btn.onClick.AddListener(() => { createNodePanel(nodeArg); });
                }


                string titleText = "";
                switch (type)
                {
                case "SensoryInputNode":
                    SensoryInputNode siNode = (SensoryInputNode)layers[i][j];
                    titleText = "sense " + siNode.sensedResource + " " + Helper.neighborIndexToWord(siNode.neighborLandIndex);
                    break;

                case "OutputNode":
                    OutputNode outNode = (OutputNode)layers[i][j];
                    titleText = outNode.action.name;
                    break;

                case "InternalResourceInputNode":
                    InternalResourceInputNode internNode = (InternalResourceInputNode)layers[i][j];
                    titleText = internNode.sensedResource + " stored";
                    break;

                case "InnerInputNode":
                    InnerInputNode innerNode = (InnerInputNode)layers[i][j];
                    titleText = innerNode.linkedNetName;
                    break;

                case "PhenotypeInputNode":
                    PhenotypeInputNode phenoNode = (PhenotypeInputNode)layers[i][j];
                    titleText = "Pheno: " + phenoNode.neighborIndex;
                    break;

                case "BiasNode":
                    titleText = "Bias Node";
                    break;

                default:
                    titleText = "";
                    break;
                }
                double rounded = System.Math.Round((double)layers[i][j].value, 2);
                value.text = rounded.ToString();
                title.text = titleText;

                created.transform.SetParent(canvas.transform, false);
                RectTransform transform = created.GetComponent <RectTransform>();
                transform.anchoredPosition = new Vector2(x, y);
                y -= 40;
                if (j % 8 == 0 && j != 0)
                {
                    x += 70;
                    y  = -50;
                }
                instantiated.Add(created);
            }
            x += 120;
            y  = -50;
        }
    }
コード例 #9
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);
        }
    }