当前位置: 首页>>代码示例>>C#>>正文


C# INode.GetNodes方法代码示例

本文整理汇总了C#中INode.GetNodes方法的典型用法代码示例。如果您正苦于以下问题:C# INode.GetNodes方法的具体用法?C# INode.GetNodes怎么用?C# INode.GetNodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在INode的用法示例。


在下文中一共展示了INode.GetNodes方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SimplifyNode

        private static void SimplifyNode(ref INode result)
        {
            // Check for an InputVariableNode in result
            if (result.GetNodes().Any(n => n.GetType() == typeof(InputVariableNode) || n.GetType() == typeof(InputSizeNode)
                || n.GetType() == typeof(CollectionSizeNode) || n.GetType() == typeof(SumNode)))
                return;

            // We don't have an InputVariableNode, so we can simplify into a single constant
            double constant = result.Calculate(new List<double>(), new List<List<double>>());
            result = new ConstantNode(result.Context, constant);
        }
开发者ID:geoffsmith,项目名称:Genetic-Programming,代码行数:11,代码来源:NodeFactory.cs

示例2: Reproduce

        /// <summary>
        /// A very simple reproduction algorithm. It starts with parent A and takes a single node
        /// from each parent and performs a crossover with those nodes.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public INode Reproduce(INode a, INode b)
        {
            // Clone parent A to start with
            INode child = (INode) a.Clone();

            // Get all the nodes in the child and parent b
            List<INode> childNodes = child.GetNodes();
            List<INode> bNodes = b.GetNodes();

            // Get a random node from the child and b
            INode childCrossoverNode = childNodes[RandomUtil.Random.Next(childNodes.Count - 1)];
            List<INode> bCandidates = FindNodeWithMatchingContext(childCrossoverNode, bNodes);
            if (bCandidates.Count == 0) return child;
            INode bCrossoverNode = bCandidates[RandomUtil.Random.Next(bCandidates.Count)];

            childCrossoverNode.ReplaceChild((INode) bCrossoverNode.Clone());
            return child;
        }
开发者ID:geoffsmith,项目名称:Genetic-Programming,代码行数:25,代码来源:SimpleReproduction.cs

示例3: CalculateFitness

        public double CalculateFitness(INode individual)
        {
            // Don't allow nodes to get too big
            if (individual.GetNodes().Count > 30) return 1000;

            if (this.perfectNode == null) this.CreatePerfectNode();

            // Keep track the average error
            double totalError = 0;
            const int numberTests = 50;

            for (int i = 0; i < numberTests; ++i)
            {
                int top = RandomUtil.Random.Next(1000);
                int size = RandomUtil.Random.Next(10);
                if (size == 0) size = 1;
                double expectedTotal = 0.0;
                List<double> input = new List<double>();
                for (int j = 0; j < size; ++j)
                {
                    double expectedItem = RandomUtil.Random.Next(top);
                    input.Add(expectedItem);
                    expectedTotal += expectedItem;
                }
                double expectedResult = expectedTotal/(double) size;

                List<List<double>> inputs = new List<List<double>>();
                inputs.Add(input);

                double error = expectedResult - individual.Calculate(new List<double>(), inputs);
                error = Math.Abs(error);
                error = Math.Abs(Math.Sqrt(0.1*error));
                totalError += error;

                // Add some error if the error is too large taking account of the size
                //if (error > 0.3) totalError += individual.GetNodes().Count/100.0;

            }

            return totalError/(double)(numberTests);
        }
开发者ID:geoffsmith,项目名称:Genetic-Programming,代码行数:41,代码来源:AverageFitnessFunction.cs

示例4: Mutate

        public void Mutate(ref INode node, NodeContext zeroContext)
        {
            // Only do a mutation 1/2 the time
            int doMutation = RandomUtil.Random.Next(100);
            if (doMutation < this.mutationRate) return;

            // Mutate a single node, by replacing one of its children
            var nodes = node.GetNodes();
            if (nodes.Count == 1)
            {
                node = NodeFactory.GenerateNode(zeroContext);
            }
            else
            {
                INode mutationNode = nodes[RandomUtil.Random.Next(nodes.Count)];
                INode newNode = NodeFactory.GenerateNode(mutationNode.Context);
                mutationNode.ReplaceChild(newNode);

                //Console.WriteLine("Before {0} after {1}", nodes.Count, node.GetNodes().Count);
            }
        }
开发者ID:geoffsmith,项目名称:Genetic-Programming,代码行数:21,代码来源:SimpleNodeMutator.cs

示例5: CalculateFitness

        public double CalculateFitness(INode individual)
        {
            // Don't allow nodes to get too big
            if (individual.GetNodes().Count > 30) return 1000;

            // We don't allow an individual to take more than 1 second to test
            DateTime startTime = DateTime.Now;

            // Keep track the average error
            double totalError = 0;

            // Keep track of results to make sure that we have some non-zero
            bool haveNonZero = false;

            //INode ideal = MakeVarianceNode();

            foreach (TestCase testCase in this.testCases)
            {
                double calculate = individual.Calculate(new List<double>(), testCase.Inputs);

                if (calculate != 0) haveNonZero = true;

                double error = testCase.ExpectedResult - calculate;

                //error = Math.Abs(Math.Sqrt(0.1*error));
                if (testCase.ExpectedResult != 0) error = error/testCase.ExpectedResult;
                totalError += Math.Abs(error);

                // Add some error if the error is too large taking account of the size
                //if (error > 0.3) totalError += individual.GetNodes().Count/100.0;

                // If we've gone over a second, stop with a high error
                if ((DateTime.Now - startTime).TotalMilliseconds > StandardDeviationFitnessFunction.maximumRunLength)
                    return Double.MaxValue;
            }

            if (!haveNonZero) return Double.MaxValue;

            return totalError/(double)(this.testCases.Count);
        }
开发者ID:geoffsmith,项目名称:Genetic-Programming,代码行数:40,代码来源:StandardDeviationFitnessFunction.cs


注:本文中的INode.GetNodes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。