Example #1
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);
        }
Example #2
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());
            }
        }
Example #3
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);
        }