Пример #1
0
        public override object Clone()
        {
            var c = (MutateAllNodesPipeline)(base.Clone());

            // deep-cloned stuff
            c.NodeSelect = (IGPNodeSelector)(NodeSelect.Clone());
            return(c);
        }
Пример #2
0
        public override object Clone()
        {
            var c = (MutateOneNodePipeline)base.Clone();

            // deep-cloned stuff
            c.NodeSelect = (IGPNodeSelector)NodeSelect.Clone();
            return(c);
        }
Пример #3
0
        public override void Setup(IEvolutionState state, IParameter paramBase)
        {
            base.Setup(state, paramBase);

            var def = DefaultBase;
            var p   = paramBase.Push(P_NODESELECTOR).Push("" + 0);
            var d   = def.Push(P_NODESELECTOR).Push("" + 0);

            NodeSelect = (IGPNodeSelector)(state.Parameters.GetInstanceForParameter(p, d, typeof(IGPNodeSelector)));
            NodeSelect.Setup(state, p);

            p = paramBase.Push(P_BUILDER).Push("" + 0);
            d = def.Push(P_BUILDER).Push("" + 0);

            Builder = (GPNodeBuilder)(state.Parameters.GetInstanceForParameter(p, d, typeof(GPNodeBuilder)));
            Builder.Setup(state, p);

            NumTries = state.Parameters.GetInt(paramBase.Push(P_NUM_TRIES), def.Push(P_NUM_TRIES), 1);
            if (NumTries == 0)
            {
                state.Output.Fatal("Mutation Pipeline has an invalid number of tries (it must be >= 1).",
                                   paramBase.Push(P_NUM_TRIES), def.Push(P_NUM_TRIES));
            }

            MaxDepth = state.Parameters.GetInt(paramBase.Push(P_MAXDEPTH), def.Push(P_MAXDEPTH), 1);
            if (MaxDepth == 0)
            {
                state.Output.Fatal("The Mutation Pipeline " + paramBase + "has an invalid maximum depth (it must be >= 1).",
                                   paramBase.Push(P_MAXDEPTH), def.Push(P_MAXDEPTH));
            }

            MaxSize = NO_SIZE_LIMIT;
            if (state.Parameters.ParameterExists(paramBase.Push(P_MAXSIZE), def.Push(P_MAXSIZE)))
            {
                MaxSize = state.Parameters.GetInt(paramBase.Push(P_MAXSIZE), def.Push(P_MAXSIZE), 1);
                if (MaxSize < 1)
                {
                    state.Output.Fatal("Maximum tree size, if defined, must be >= 1");
                }
            }

            EqualSize = state.Parameters.GetBoolean(paramBase.Push(P_EQUALSIZE), def.Push(P_EQUALSIZE), false);

            Tree = TREE_UNFIXED;
            if (state.Parameters.ParameterExists(paramBase.Push(P_TREE).Push("" + 0), def.Push(P_TREE).Push("" + 0)))
            {
                Tree = state.Parameters.GetInt(paramBase.Push(P_TREE).Push("" + 0), def.Push(P_TREE).Push("" + 0), 0);
                if (Tree == -1)
                {
                    state.Output.Fatal("Tree fixed value, if defined, must be >= 0");
                }
            }
        }
Пример #4
0
        public override void Setup(IEvolutionState state, IParameter paramBase)
        {
            base.Setup(state, paramBase);

            var def = DefaultBase;

            var p = paramBase.Push(P_NODESELECTOR).Push("" + 0);

            NodeSelect = (IGPNodeSelector)(state.Parameters.GetInstanceForParameter(p, def.Push(P_NODESELECTOR).Push("" + 0), typeof(IGPNodeSelector)));
            NodeSelect.Setup(state, p);

            Tree = TREE_UNFIXED;
            if (state.Parameters.ParameterExists(paramBase.Push(P_TREE).Push("" + 0), def.Push(P_TREE).Push("" + 0)))
            {
                Tree = state.Parameters.GetInt(paramBase.Push(P_TREE).Push("" + 0), def.Push(P_TREE).Push("" + 0), 0);
                if (Tree == -1)
                {
                    state.Output.Fatal("Tree fixed value, if defined, must be >= 0");
                }
            }
        }
Пример #5
0
    private NodeRoot GetNodeRoot(NodeValue nodeValue)
    {
        NodeRoot nodeRoot = null;

        switch (nodeValue.NodeType)
        {
        case NodeType.Select:        // 选择节点
            nodeRoot = new NodeSelect();
            break;

        case NodeType.Sequence:      // 顺序节点
            nodeRoot = new NodeSequence();
            break;

        case NodeType.Decorator:     // 修饰节点
            nodeRoot = new NodeDecorator();
            break;

        case NodeType.Random:        // 随机节点
            nodeRoot = new NodeRandom();
            break;

        case NodeType.Parallel:      // 并行节点
            nodeRoot = new NodeParallel();
            break;

        case NodeType.Condition:     // 条件节点
            nodeRoot = GetLeafNode(nodeValue);
            break;

        case NodeType.Action:        // 行为节点
            nodeRoot = GetLeafNode(nodeValue);
            break;
        }

        return(nodeRoot);
    }
Пример #6
0
        public override int Produce(
            int min,
            int max,
            int subpop,
            IList <Individual> inds,
            IEvolutionState state,
            int thread,
            IDictionary <string, object> misc)
        {
            int start = inds.Count;

            // grab n individuals from our source and stick 'em right into inds.
            // we'll modify them from there
            var n = Sources[0].Produce(min, max, subpop, inds, state, thread, misc);

            // should we bother?
            if (!state.Random[thread].NextBoolean(Likelihood))
            {
                return(n);
            }

            var initializer = (GPInitializer)state.Initializer;

            // now let's mutate 'em
            for (var q = start; q < n + start; q++)
            {
                var i = (GPIndividual)inds[q];

                if (Tree != TREE_UNFIXED && (Tree < 0 || Tree >= i.Trees.Length))
                {
                    // uh oh
                    state.Output.Fatal("MutateOneNodePipeline attempted to fix tree.0 to a value which was out of bounds"
                                       + " of the array of the individual's trees.  Check the pipeline's fixed tree values"
                                       + " -- they may be negative or greater than the number of trees in an individual");
                }

                int t;
                // pick random tree
                if (Tree == TREE_UNFIXED)
                {
                    t = i.Trees.Length > 1 ? state.Random[thread].NextInt(i.Trees.Length) : 0;
                }
                else
                {
                    t = Tree;
                }

                // prepare the NodeSelector
                NodeSelect.Reset();

                // pick a node

                // pick a node in individual 1
                var p1 = NodeSelect.PickNode(state, subpop, thread, i, i.Trees[t]);

                // generate a tree with a new root but the same children,
                // which we will replace p1 with

                var type = p1.ParentType(initializer);

                var p2 = PickCompatibleNode(p1, i.Trees[t].Constraints(initializer).FunctionSet, state, type, thread).LightClone();

                // if it's an ERC, let it set itself up
                p2.ResetNode(state, thread);

                // p2's parent and ArgPosition will be set automatically below

                p1.ReplaceWith(p2);
                i.Evaluated = false;

                // add the new individual, replacing its previous source
                inds[q] = i;
            }
            return(n);
        }
Пример #7
0
    /// <summary>
    /// 初始化添加行为树节点
    /// </summary>
    private void Init()
    {
        // 顺序节点 1
        NodeSequence nodeSequence_1 = new NodeSequence();

        // 顺序节点 1 添加到根节点
        rootNode.AddNode(nodeSequence_1);

        #region 是否饿了
        // 条件节点 1.1
        NodeConditionHungry nodeConditionHungry_1_1 = new NodeConditionHungry();
        nodeConditionHungry_1_1.SetStudent(this);
        // 条件节点 1.1 添加到 顺序节点1
        nodeSequence_1.AddNode(nodeConditionHungry_1_1);
        #endregion

        #region  择节点 1.2
        {
            // 选择节点1.2
            NodeSelect nodeSelect_1_2 = new NodeSelect();
            nodeSequence_1.AddNode(nodeSelect_1_2);

            // 节点 1.2.1 是否有饭
            NodeConditionHasFood nodeConditionHasFood_1_2_1 = new NodeConditionHasFood();
            nodeConditionHasFood_1_2_1.SetStudent(this);
            nodeSelect_1_2.AddNode(nodeConditionHasFood_1_2_1);

            // 顺序节点 1.2.2
            NodeSequence nodeSequence_1_2_2 = new NodeSequence();
            nodeSelect_1_2.AddNode(nodeSequence_1_2_2);

            // 行为节点 1.2.2.1 走到厨房
            NodeActionMove nodeActionMove_1_2_2_1 = new NodeActionMove();
            nodeActionMove_1_2_2_1.SetStudent(this);
            nodeActionMove_1_2_2_1.SetTarget("Cooking");
            nodeActionMove_1_2_2_1.SetTr(transform);
            nodeSequence_1_2_2.AddNode(nodeActionMove_1_2_2_1);

            // 行为节点 1.2.2.2 做饭
            NodeActionCooking nodeActionCooking_1_2_2_2 = new NodeActionCooking();
            nodeActionCooking_1_2_2_2.SetStudent(this);
            nodeSequence_1_2_2.AddNode(nodeActionCooking_1_2_2_2);
        }
        #endregion

        #region 走到餐桌
        {
            // 行为节点 1.3 走到餐桌
            NodeActionMove nodeActionMove_1_3 = new NodeActionMove();
            nodeActionMove_1_3.SetStudent(this);
            nodeActionMove_1_3.SetTarget("Food");
            nodeActionMove_1_3.SetTr(transform);
            nodeSequence_1.AddNode(nodeActionMove_1_3);
        }
        #endregion

        #region 吃饭
        {
            // 行为节点 1.4 吃饭
            NodeActionEat nodeActionEat_1_4 = new NodeActionEat();
            nodeActionEat_1_4.SetStudent(this);
            nodeSequence_1.AddNode(nodeActionEat_1_4);
        }
        #endregion
    }
Пример #8
0
        public override int Produce(
            int min,
            int max,
            int subpop,
            IList <Individual> inds,
            IEvolutionState state,
            int thread,
            IDictionary <string, object> misc)
        {
            int start = inds.Count;

            // grab n individuals from our source and stick 'em right into inds.
            // we'll modify them from there
            var n = Sources[0].Produce(min, max, subpop, inds, state, thread, misc);

            // should we bother?
            if (!state.Random[thread].NextBoolean(Likelihood))
            {
                return(n);
            }

            IntBag[] parentparents   = null;
            IntBag[] preserveParents = null;
            if (misc != null && misc.ContainsKey(KEY_PARENTS))
            {
                preserveParents   = (IntBag[])misc[KEY_PARENTS];
                parentparents     = new IntBag[2];
                misc[KEY_PARENTS] = parentparents;
            }

            var initializer = ((GPInitializer)state.Initializer);

            // now let's mutate 'em
            for (var q = start; q < n + start; q++)
            {
                var i = (GPIndividual)inds[q];

                if (Tree != TREE_UNFIXED && (Tree < 0 || Tree >= i.Trees.Length))
                {
                    // uh oh
                    state.Output.Fatal("MutateAllNodesPipeline attempted to fix Tree.0 to a value which was out of bounds of the array of the individual's Trees.  Check the pipeline's fixed tree values -- they may be negative or greater than the number of trees in an individual");
                }

                int t;
                // pick random tree
                if (Tree == TREE_UNFIXED)
                {
                    t = i.Trees.Length > 1 ? state.Random[thread].NextInt(i.Trees.Length) : 0;
                }
                else
                {
                    t = Tree;
                }

                // prepare the NodeSelector
                NodeSelect.Reset();

                // pick a node

                // pick a node in individual 1
                var p1 = NodeSelect.PickNode(state, subpop, thread, i, i.Trees[t]);

                // generate a tree with a new root but the same children,
                // which we will replace p1 with

                var type = p1.ParentType(initializer);

                var p2 = GenerateCompatibleTree(p1, i.Trees[t].Constraints(initializer).FunctionSet, state, type, thread);
                // we'll need to set p2.ArgPosition and p2.Parent further down


                p2.Parent      = p1.Parent;
                p2.ArgPosition = p1.ArgPosition;
                if (p2.Parent is GPNode)
                {
                    ((GPNode)(p2.Parent)).Children[p2.ArgPosition] = p2;
                }
                else
                {
                    ((GPTree)(p2.Parent)).Child = p2;
                }
                i.Evaluated = false;     // we've modified it

                // add the new individual, replacing its previous source
                inds[q] = i;
                if (preserveParents != null)
                {
                    parentparents[0].AddAll(parentparents[1]);
                    preserveParents[q] = new IntBag(parentparents[0]);
                }
            }
            return(n);
        }
Пример #9
0
    void Init()
    {
        NodeSequence nodeSequence_1 = new NodeSequence();

        rootNode.AddNode(nodeSequence_1);

        #region 条件 1.1 是否饿了
        {
            NodeConditionHungry cond_1_1 = new NodeConditionHungry();
            cond_1_1.SetActor(this);
            nodeSequence_1.AddNode(cond_1_1);
        }
        #endregion

        #region  择条件 1.2 是否有饭
        {
            NodeSelect sel_1_2 = new NodeSelect();
            nodeSequence_1.AddNode(sel_1_2);

            // 1.2.1 是否有饭
            NodeConditionHasFood hasFood_1_2_1 = new NodeConditionHasFood();
            hasFood_1_2_1.SetActor(this);
            sel_1_2.AddNode(hasFood_1_2_1);

            // 没有饭的话, 走去厨房 => 做饭
            // 1.2.2 顺序节点
            NodeSequence sequence_1_2_2 = new NodeSequence();
            sel_1_2.AddNode(sequence_1_2_2);
            // 1.2.2.1 走厨房
            NodeActionMove move_1_2_2_1 = new NodeActionMove();
            move_1_2_2_1.SetActor(this);
            move_1_2_2_1.myTransform     = this.transform;
            move_1_2_2_1.targetTransform = doCookingTransform;
            sequence_1_2_2.AddNode(move_1_2_2_1);
            // 1.2.2.2 做饭
            NodeActionCooking cooking_1_2_2_2 = new NodeActionCooking();
            cooking_1_2_2_2.SetActor(this);
            sequence_1_2_2.AddNode(cooking_1_2_2_2);
        }
        #endregion
        #region 1.3 去餐桌
        {
            NodeActionMove move_1_3 = new NodeActionMove();
            move_1_3.SetActor(this);
            move_1_3.myTransform     = this.transform;
            move_1_3.targetTransform = eatFoodTransform;

            nodeSequence_1.AddNode(move_1_3);
        }
        #endregion

        #region 1.4 吃饭
        {
            NodeActionEat eat_1_4 = new NodeActionEat();
            eat_1_4.SetActor(this);
            nodeSequence_1.AddNode(eat_1_4);
        }
        #endregion

        #region 2
        // 并行节点
        NodeParallel p_2 = new NodeParallel();
        rootNode.AddNode(p_2);

        // 2.1 是否有钱
        NodeConditionHasMoney hasMoney_2_1 = new NodeConditionHasMoney();
        p_2.AddNode(hasMoney_2_1);

        // 2.2 走去酒馆
        NodeSequence seq_saloon = new NodeSequence();
        p_2.AddNode(seq_saloon);

        NodeActionMove move_2_2 = new NodeActionMove();
        move_2_2.myTransform     = this.transform;
        move_2_2.targetTransform = saloonTransform;
        seq_saloon.AddNode(move_2_2);
        // 2.3 随机点一份酒水
        NodeRandom r_2_3 = new NodeRandom();
        seq_saloon.AddNode(r_2_3);
        // 2.3.1 喝红酒
        r_2_3.AddNode(new NodeActionDrinkRedWine());
        // 2.3.2 喝白开水
        r_2_3.AddNode(new NodeActionDrinkWater());
        #endregion
    }
Пример #10
0
        public override int Produce(
            int min,
            int max,
            int subpop,
            IList <Individual> inds,
            IEvolutionState state,
            int thread,
            IDictionary <string, object> misc)
        {
            int start = inds.Count;

            // grab n individuals from our source and stick 'em right into inds.
            // we'll modify them from there
            var n = Sources[0].Produce(min, max, subpop, inds, state, thread, misc);

            // should we bother?
            if (!state.Random[thread].NextBoolean(Likelihood))
            {
                return(n);
            }

            // now let's mutate 'em
            for (var q = start; q < n + start; q++)
            {
                var i = (GPIndividual)inds[q];

                if (Tree != TREE_UNFIXED && (Tree < 0 || Tree >= i.Trees.Length))
                {
                    // uh oh
                    state.Output.Fatal("MutateERCPipeline attempted to fix tree.0 to a value which was"
                                       + " out of bounds of the array of the individual's trees. "
                                       + " Check the pipeline's fixed tree values -- they may be"
                                       + " negative or greater than the number of trees in an individual");
                }

                int t;
                // pick random tree
                if (Tree == TREE_UNFIXED)
                {
                    t = i.Trees.Length > 1 ? state.Random[thread].NextInt(i.Trees.Length) : 0;
                }
                else
                {
                    t = Tree;
                }

                i.Evaluated = false;

                // prepare the NodeSelector
                NodeSelect.Reset();

                // Now pick a random node

                var p = NodeSelect.PickNode(state, subpop, thread, i, i.Trees[t]);

                // mutate all the ERCs in p1's subtree

                MutateERCs(p, state, thread);

                // add the new individual, replacing its previous source
                inds[q] = i;
            }
            return(n);
        }
Пример #11
0
        public override int Produce(
            int min,
            int max,
            int subpop,
            IList <Individual> inds,
            IEvolutionState state,
            int thread,
            IDictionary <string, object> misc)
        {
            int start = inds.Count;

            // grab individuals from our source and stick 'em right into inds.
            // we'll modify them from there
            var n = Sources[0].Produce(min, max, subpop, inds, state, thread, misc);

            // should we bother?
            if (!state.Random[thread].NextBoolean(Likelihood))
            {
                return(n);
            }

            var initializer = ((GPInitializer)state.Initializer);

            // now let's mutate 'em
            for (var q = start; q < n + start; q++)
            {
                var i = (GPIndividual)inds[q];

                if (Tree != TREE_UNFIXED && (Tree < 0 || Tree >= i.Trees.Length))
                {
                    // uh oh
                    state.Output.Fatal("GP Mutation Pipeline attempted to fix Tree.0 to a value which was out of bounds"
                                       + " of the array of the individual's Trees.  Check the pipeline's fixed Tree values"
                                       + " -- they may be negative or greater than the number of Trees in an individual");
                }


                int t;
                // pick random Tree
                if (Tree == TREE_UNFIXED)
                {
                    if (i.Trees.Length > 1)
                    {
                        t = state.Random[thread].NextInt(i.Trees.Length);
                    }
                    else
                    {
                        t = 0;
                    }
                }
                else
                {
                    t = Tree;
                }

                // validity result...
                var res = false;

                // prepare the NodeSelector
                NodeSelect.Reset();

                // pick a node

                GPNode p1 = null; // the node we pick
                GPNode p2 = null;

                for (var x = 0; x < NumTries; x++)
                {
                    // pick a node in individual 1
                    p1 = NodeSelect.PickNode(state, subpop, thread, i, i.Trees[t]);

                    // generate a Tree swap-compatible with p1's position


                    var size = GPNodeBuilder.NOSIZEGIVEN;
                    if (EqualSize)
                    {
                        size = p1.NumNodes(GPNode.NODESEARCH_ALL);
                    }

                    p2 = Builder.NewRootedTree(state, p1.ParentType(initializer), thread, p1.Parent,
                                               i.Trees[t].Constraints(initializer).FunctionSet, p1.ArgPosition, size);

                    // check for depth and swap-compatibility limits
                    res = VerifyPoints(p2, p1); // p2 can fit in p1's spot  -- the order is important!

                    // did we get something that had both nodes verified?
                    if (res)
                    {
                        break;
                    }
                }

                if (res)
                // we're in business
                {
                    p2.Parent      = p1.Parent;
                    p2.ArgPosition = p1.ArgPosition;
                    if (p2.Parent is GPNode)
                    {
                        ((GPNode)p2.Parent).Children[p2.ArgPosition] = p2;
                    }
                    else
                    {
                        ((GPTree)p2.Parent).Child = p2;
                    }
                    i.Evaluated = false; // we've modified it
                }

                // add the new individual, replacing its previous source
                inds[q] = i;
            }
            return(n);
        }