コード例 #1
0
        /// <summary>
        /// Creates a fully initialized node of given type. If the node requires successors, these successors will also be created and assigned to the node. Successors will be default-value-nodes of required types.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Node createRandomNode(NodeType type, NodeCreationConstrains constrains = null)
        {
            Node n;

            if (type == NodeType.BoolConstant)
            {
                n = new BoolConstant(r.NextDouble() < 0.5);
                return(n);
            }
            if (type == NodeType.NumConst)
            {
                n = new NumericConstant(r.Next(10));
                return(n);
            }
            if (type == NodeType.NumInput)
            {
                n = new InputNode(r.Next(10));
                return(n);
            }
            n = createPrototypeNode(type);

            for (int i = 0; i < n.successors.count; i++)
            {
                n.setSuccessor(createDefaultNode(n.successors.GetSlot(i).argumentClass, constrains), i);
            }
            return(n);
        }
コード例 #2
0
        /// <summary>
        /// Returns all posible node of given type. If the node requires successors, these successors will also be created and assigned to the node. Successors will be default-value-nodes of required types.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IEnumerable <Node> enumerateAllNodes(NodeType type, NodeCreationConstrains constrains = null)
        {
            Node n;
            int  constantsLimit = 2;

            switch (type)
            {
            case NodeType.BoolConstant:
                //yield return new BoolConstant(false);	//boolean constants are no longer supported
                //yield return new BoolConstant(true);
                break;

            case NodeType.NumConst:
                for (int i = 0; i < constantsLimit; i++)
                {
                    yield return(new NumericConstant(i));
                }
                break;

            case NodeType.NumInput:
                for (int i = 0; i < constantsLimit; i++)
                {
                    yield return(new InputNode(i));
                }
                break;

            default:
                n = createPrototypeNode(type);
                yield return(n);

                break;
            }
        }
コード例 #3
0
        /// <summary>
        /// Creates a new node that is "default" for given class. For "directive" the default value is TerminalDirective, for "boolean" the default is BoolConstant with value "false", and for numeric the default is "NumericConstant" with value 0.
        /// If NodeCreationConstrains are given, then other result might be returned. E.g. if the constrains forbid constants, it will return input-reading-node instead of a constant one, etc.
        /// </summary>
        /// <param name="cl"></param>
        /// <returns></returns>
        public static Node createDefaultNode(NodeClass cl, NodeCreationConstrains constrains = null)
        {
            switch (cl)
            {
            case NodeClass.directive:
                return(new directiveTerminal());

            case NodeClass.numeric:
                if (constrains?.Contains(NodeCreationContrainsOption.cantBeConstant) == true)
                {
                    return(new InputNode(0));
                }
                return(new NumericConstant(0));

            case NodeClass.boolean:
                if (constrains?.Contains(NodeCreationContrainsOption.cantBeConstant) == true)
                {
                    return(new BoolEqualsOperator(new InputNode(0), new NumericConstant(0)));
                }
                return(new BoolConstant(false));

            default:
                throw new Exception();
            }
        }
コード例 #4
0
        /// <summary>
        /// Generates a random node type based on a distribution computed from current frequencies.
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public virtual NodeType getRandomNodeType(NodeClass c, NodeType?parrentType, NodeCreationConstrains constrains = null)
        {
            if (parrentType == null || !relativizedProfiles.ContainsKey(parrentType.Value))
            {
                return(simpleProfile.getRandomNodeType(c, constrains));
            }

            return(relativizedProfiles[parrentType.Value].getRandomNodeType(c, constrains));
        }
コード例 #5
0
        /// <summary>
        /// Generates a random node type based on a distribution computed from current frequencies.
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public virtual NodeType getRandomNodeType(NodeClass c, NodeCreationConstrains constrains = null)
        {
            int rouletteWheelIndex = 0;
            int sampledNumber      = r.Next(sumsOfFrequenciesByClasses[c]);

            foreach (var cummulativeSum in getCummulativeFrequencies(c))
            {
                if (sampledNumber < cummulativeSum)
                {
                    return(typesByClasses[c][rouletteWheelIndex]);
                }

                rouletteWheelIndex++;
            }
            throw new Exception();
        }
コード例 #6
0
 public override NodeType getRandomNodeType(NodeClass c, NodeCreationConstrains constrains = null)
 {
     return(simpleProfile.getRandomNodeType(c, constrains));
 }
コード例 #7
0
 private static IEnumerable <Node> fillSlotsWithAllPossibilities(Node n, int depth, int slotIndex, NodeCreationConstrains constrains = null)
 {
     if (slotIndex >= n.successors.count)
     {
         yield return(n.createDeepCopy());
     }
     else
     {
         var slot = n.successors.GetSlot(slotIndex);
         foreach (var subtree in enumerateAllTrees(slot.argumentClass, depth, constrains))
         {
             slot.setNodeConnectedToSlot(subtree.createDeepCopy());
             foreach (var rest in fillSlotsWithAllPossibilities(n, depth, slotIndex + 1, constrains))
             {
                 yield return(rest.createDeepCopy());
             }
         }
     }
 }
コード例 #8
0
        public static IEnumerable <Node> enumerateAllTrees(NodeClass rootNodeClass, int depth, NodeCreationConstrains constrains = null)
        {
            if (depth <= 0)             //it should not get here anyway. Unless it was already called with depth = 0;
            {
                yield return(createDefaultNode(rootNodeClass, constrains));
            }

            if (depth == 1)
            {
                //foreach (var type in EnumUtils.getTerminalTypes(rootNodeClass))
                //{
                foreach (var node in enumerateAllNonConstantTerminalNodes(rootNodeClass, constrains))
                {
                    yield return(node);
                    //TODO!!
                }
                //}
            }

            else
            {
                foreach (var type in EnumUtils.getTerminalTypes(rootNodeClass))
                {
                    foreach (var node in enumerateAllNodes(type, constrains))
                    {
                        yield return(node);
                    }
                }
                foreach (var type in EnumUtils.getNonterminalTypes(rootNodeClass))
                {
                    foreach (var node in enumerateAllNodes(type, constrains))
                    {
                        foreach (var tree in fillSlotsWithAllPossibilities(node, depth - 1, 0, constrains))
                        {
                            yield return(tree);
                        }
                    }
                }
            }
        }
コード例 #9
0
        /// <summary>
        /// Creates a tree of fully instantiated nodes with given depth. Nodes at the leaf level will be default-value-nodes.
        /// </summary>
        /// <param name="cl"></param>
        /// <param name="profile"></param>
        /// <param name="depth"></param>
        /// <returns></returns>
        public static Node createRandomTree(NodeClass cl, NodeTypeRelativizedFrequencyProfile profile, int depth, NodeType?parrentType = null, NodeCreationConstrains contrains = null)
        {
            if (depth == 0)
            {
                return(createDefaultNode(cl, contrains));
            }
            Node root = createRandomNode(cl, parrentType, profile, contrains);

            foreach (var slot in root.successors.getSlots())
            {
                slot.setNodeConnectedToSlot(createRandomTree(slot.argumentClass, profile, depth - 1, root?.type, contrains));
            }
            return(root);
        }
コード例 #10
0
        /// <summary>
        /// Creates a new fully initialized node whose type is selected randomly based on distribution in given NodeTypeFrequencyProfile object. Node that require value to be set (like numeric constants) will receive a random number between 0 and 10 as their value.
        /// If the node requires successors to be specified, such successors will be created and default-value-node will be used.
        /// </summary>
        /// <param name="cl"></param>
        /// <param name="profile"></param>
        /// <returns></returns>
        public static Node createRandomNode(NodeClass cl, NodeType?parrentType, NodeTypeRelativizedFrequencyProfile profile, NodeCreationConstrains constrains = null)
        {
            NodeType t = profile.getRandomNodeType(cl, parrentType, constrains);

            return(createRandomNode(t, constrains));
        }
コード例 #11
0
        /// <summary>
        /// Returns all posible non-constant terminal nodes of given type.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IEnumerable <Node> enumerateAllNonConstantTerminalNodes(NodeClass cl, NodeCreationConstrains constrains = null)
        {
            Node n;
            int  limit = 2;

            switch (cl)
            {
            case NodeClass.boolean:
                //yield return new BoolConstant(false);	//boolean constants are no longer supported
                //yield return new BoolConstant(true);
                break;

            case NodeClass.numeric:
                for (int i = 0; i < limit; i++)
                {
                    yield return(new ValueGetter(new NumericConstant(i)));
                }

                break;

            case NodeClass.directive:
                break;
            }
        }