示例#1
0
        public void DrawGenome(INeuralGenome genome)
        {
            RemoveAllNodes();

            if (autoAdjustMaxWeight)
            {
                maxWeight = GetAverageWeight(genome.Genes);
            }

            int count = 0;

            foreach (var neuron in genome.NeuronLst)
            {
                var newNode = Instantiate(nodePrefab, nodesParent)
                              .GetComponent <RectTransform>();
                newNode.name = "Node " + count;

                var nodeText = neuron.innovNb.ToString();
                if (neuron.neurType == ENeurType.bias)
                {
                    nodeText = "bias" + nodeText;
                }
                else if (neuron.neurType == ENeurType.input)
                {
                    nodeText = "in" + nodeText;
                }
                else if (neuron.neurType == ENeurType.output)
                {
                    nodeText = "out" + nodeText;
                }

                newNode.GetChild(0).GetComponent <Text>().text = nodeText;

                nodes.Add(neuron, newNode);
                count++;
            }

            var inputNeurons = NeuralUtils.GetNeurons(
                genome, ENeurType.input);
            var outputNeurons = NeuralUtils.GetNeurons(
                genome, ENeurType.output);
            var bias = NeuralUtils.GetBias(genome);

            foreach (var kv in nodes)
            {
                ConnectNode(kv.Value, kv.Key, genome);
                if (inputNeurons.Contains(kv.Key))
                {
                    int i = inputNeurons.IndexOf(kv.Key) + 1;
                    kv.Value.localPosition =
                        inputNodesPos.localPosition +
                        Vector3.down * distBetweenNodes * i;
                }
                else if (outputNeurons.Contains(kv.Key))
                {
                    int i = outputNeurons.IndexOf(kv.Key);
                    kv.Value.localPosition =
                        outputNodesPos.localPosition +
                        Vector3.down * distBetweenNodes * i;
                }
                else if (bias == kv.Key)
                {
                    kv.Value.localPosition = inputNodesPos.localPosition;
                }
                else
                {
                    kv.Value.localPosition = GetPosOfNewNode();
                }
            }
        }