Esempio n. 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);
        }
Esempio n. 2
0
        public static ConstNode randomizeNode()
        {
            float     rndConst = Rng.Rnd.Next(-5, 5);
            ConstNode cn       = new ConstNode(rndConst + (float)Rng.Rnd.NextDouble());

            return(cn);
        }
Esempio n. 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);
        }
Esempio n. 4
0
        public static ConstNode deepCopy(ConstNode other)
        {
            ConstNode res = new ConstNode(other._value);

            res._color = other._color;
            return(res);
        }
Esempio n. 5
0
        public static ConstNode shallowCopy(ConstNode other)
        {
            ConstNode res = new ConstNode(other._value);

            res._children = other._children;
            res._color    = other._color;
            return(res);
        }
Esempio n. 6
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);
        }
Esempio n. 7
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());
            }
        }
Esempio n. 8
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);
        }