Пример #1
0
        public static OperatorNode deepCopy(OperatorNode other)
        {
            OperatorNode res = new OperatorNode(other._operatorType, other._childNum);

            res._color = other._color;
            for (int i = 0; i < res._childNum; i++)
            {
                switch (other._children._children[i])
                {
                case OperatorNode op:
                    res.addChild(OperatorNode.deepCopy(op));
                    break;

                case ConstNode cn:
                    res.addChild(ConstNode.deepCopy(cn));
                    break;

                case VariableNode vn:
                    res.addChild(VariableNode.deepCopy(vn));
                    break;

                case MultipleNode mn:
                    res.addChild(MultipleNode.deepCopy(mn));
                    break;

                default:
                    break;
                }
            }
            return(res);
        }
Пример #2
0
        public static OperatorNode randomizeNode(int branching)
        {
            int sumW = 1;
            int mulW = 1;
            int invW = 1;
            int notW = 1;
            NodeOperatorType type;
            int idx = Rng.Rnd.Next(sumW + mulW + invW + notW);

            if (idx < sumW)
            {
                type = NodeOperatorType.ADD;
            }
            else if (idx < sumW + mulW)
            {
                type = NodeOperatorType.MUL;
            }
            else if (idx < sumW + mulW + invW)
            {
                type = NodeOperatorType.INV;
            }
            else
            {
                type = NodeOperatorType.NOT;
            }

            uint         maxChildNum    = (uint)NodeDictionary.OPERATOR_NUM_ARGS[type];
            uint         actualChildNum = (uint)Math.Min(Rng.Rnd.Next(3, branching), maxChildNum);
            OperatorNode op             = new OperatorNode((NodeOperatorType)type, actualChildNum);

            return(op);
        }
Пример #3
0
        public static MultipleNode deepCopy(MultipleNode other)
        {
            MultipleNode res = new MultipleNode(other._value);

            res._color = other._color;
            switch (other._children._children[0])
            {
            case OperatorNode op:
                res.addChild(OperatorNode.deepCopy(op));
                break;

            case ConstNode cn:
                res.addChild(ConstNode.deepCopy(cn));
                break;

            case VariableNode vn:
                res.addChild(VariableNode.deepCopy(vn));
                break;

            case MultipleNode mn:
                res.addChild(MultipleNode.deepCopy(mn));
                break;

            default:
                break;
            }
            return(res);
        }
Пример #4
0
        public static OperatorNode shallowCopy(OperatorNode other)
        {
            OperatorNode res = new OperatorNode(other._operatorType, other._childNum);

            res._children = other._children;
            res._color    = other._color;
            return(res);
        }
Пример #5
0
        private GeneticNode _fixTreeAux(GeneticNode current, int currentDepth)
        {
            current._childNum = (uint)Math.Min(_maxBranching, current._childNum);
            current._children.Resize((int)current._childNum);
            GeneticNode res;

            if (currentDepth >= _maxDepth)
            {
                res        = randomNode(0, 4, 1);
                res._color = current._color;
                return(res);
            }

            switch (current)
            {
            case OperatorNode on:
                res = OperatorNode.shallowCopy(on);
                break;

            case VariableNode vn:
                res = VariableNode.shallowCopy(vn);
                break;

            case ConstNode cn:
                res = ConstNode.shallowCopy(cn);
                break;

            case MultipleNode mn:
                res = MultipleNode.shallowCopy(mn);
                break;

            default:
                res = current;
                break;
            }

            for (int i = 0; i < res._childNum; i++)
            {
                res._children[i] = _fixTreeAux(res._children[i], currentDepth + 1);
            }

            return(res);
        }
Пример #6
0
        public GeneticNode randomNode(double opWeight, double varWeight, double constWeight)
        {
            if (opWeight < 0)
            {
                opWeight = 0;
            }
            if (varWeight < 0)
            {
                varWeight = 0;
            }
            if (constWeight < 0)
            {
                constWeight = 0;
            }

            double sum       = varWeight + opWeight + constWeight;
            double opCentil  = opWeight / sum;
            double varCentil = opCentil + varWeight / sum;

            double centil = _RND.NextDouble();

            if (centil < opCentil)
            {
                int det = Rng.Rnd.Next(0, (int)OPERATOR_NODES_NUM + (int)MULTIPLE_NODES_NUM);
                if (det < MULTIPLE_NODES_NUM)
                {
                    return(MultipleNode.randomizeNode());
                }
                else
                {
                    return(OperatorNode.randomizeNode((int)_maxBranching));
                }
            }
            else if (centil < varCentil)
            {
                return(VariableNode.randomizeNode());
            }
            else
            {
                return(ConstNode.randomizeNode());
            }
        }
Пример #7
0
        public static GeneticTree deepCopy(GeneticTree other)
        {
            GeneticTree res = new GeneticTree(other._maxDepth, other._maxBranching);

            res._nodesColor  = other._nodesColor;
            res._id          = other._id;
            res._nodeIndices = new List <string>(other._nodeIndices);
            switch (other._root)
            {
            case OperatorNode on:
                res._root = OperatorNode.deepCopy((OperatorNode)other._root);
                break;

            case VariableNode vn:
                res._root = VariableNode.deepCopy((VariableNode)other._root);
                break;

            case ConstNode cn:
                res._root = ConstNode.deepCopy((ConstNode)other._root);
                break;

            case MultipleNode mn:
                res._root = MultipleNode.deepCopy((MultipleNode)other._root);
                break;

            default:
                break;
            }
            res.getRPN();
            res._nodeStatsDictionary = other._nodeStatsDictionary;
            res._firstParentId       = other._firstParentId;
            res._secondParentId      = other._secondParentId;

            //res.setColor(other._nodesColor.r, other._nodesColor.g, other._nodesColor.b);
            return(res);
        }