Example #1
0
        public void InitializeFromCrossover(IEnumerable <NodeGene> nodes, IEnumerable <ConnectionGene> connections)
        {
            //foreach (var nodeGene in nodes)
            //{
            //    NodeCollection.AddNew(nodeGene);
            //}
            foreach (var connectionGene in connections)
            {
                var node1 = new NodeGene
                {
                    Type = NodeGeneType.Hidden,
                    Id   = connectionGene.Input,
                };

                var node2 = new NodeGene
                {
                    Type = NodeGeneType.Hidden,
                    Id   = connectionGene.Output
                };

                NodeCollection.AddNew(node1);
                NodeCollection.AddNew(node2);
                connectionGene.Genome = this;
                ConnectionCollection.AddNew(connectionGene);
            }
        }
Example #2
0
 public static NodeGene Copy(NodeGene node)
 {
     return(new NodeGene
     {
         Id = node.Id,
         Type = node.Type,
     });
 }
Example #3
0
 public bool AddNew(NodeGene node)
 {
     if (!Nodes.ContainsKey(node.Id))
     {
         Nodes[node.Id] = node;
         NodeList.Add(node);
         return(true);
     }
     return(false);
 }
Example #4
0
        public NodeGene CreateHiddenNode(int nodeId)
        {
            var node = new NodeGene
            {
                Id   = nodeId,
                Type = NodeGeneType.Hidden
            };

            return(node);
        }
Example #5
0
        private IEnumerable <NodeGene> CreateInitNodes()
        {
            NodeGene node;

            for (var i = 0; i < Inputs; i++)
            {
                node = new NodeGene {
                    Id = GetNextNodeId(), Type = NodeGeneType.Sensor
                };
                yield return(node);
            }

            for (var i = 0; i < Outputs; i++)
            {
                node = new NodeGene {
                    Id = GetNextNodeId(), Type = NodeGeneType.Output
                };
                yield return(node);
            }
        }
Example #6
0
        public void CalculateGenome(NodeGene node)
        {
            if (!node.Evaluated && node.Type != NodeGeneType.Sensor)
            {
                node.Evaluated = true;
                var    connections = GetConnections(node.Id).ToList();
                double sum         = 0;
                foreach (var connection in connections)
                {
                    if (connection.Enabled)
                    {
                        var input = NodeCollection.Get(connection.Input);
                        if (!input.Evaluated)
                        {
                            CalculateGenome(input);
                        }
                        sum += input.Value * connection.Weight;
                        //node.Calculate(Observer.NeatConfig.GetActivation(), input.Value, connection.Weight);
                    }
                }

                node.Value = (float)Evaluator.NeatConfig.GetActivation().Invoke(sum);
            }
        }