private ISymbolicExpressionTreeNode ParseFactor(Queue <Token> tokens)
        {
            Token tok = tokens.Dequeue();

            Debug.Assert(tok.StringValue == "FACTOR");
            FactorVariableTreeNode t = (FactorVariableTreeNode)(new FactorVariable()).CreateTreeNode(); // create a new symbol each time on purpose
            var varNameTok           = tokens.Dequeue();

            Debug.Assert(tok.Symbol == TokenSymbol.SYMB);
            t.VariableName = varNameTok.StringValue;

            var weights = new List <double>();

            while (tokens.Peek().Symbol == TokenSymbol.NUMBER)
            {
                weights.Add(tokens.Dequeue().DoubleValue);
            }

            t.Weights = weights.ToArray();

            // create a set of (virtual) values to match the number of weights
            t.Symbol.VariableNames  = new string[] { t.VariableName };
            t.Symbol.VariableValues = new[]
            { new KeyValuePair <string, Dictionary <string, int> >(
                  t.VariableName,
                  weights.Select((_, i) => Tuple.Create(_, i)).ToDictionary(tup => "X" + tup.Item2, tup => tup.Item2)) };
            return(t);
        }
Ejemplo n.º 2
0
 private FactorVariableTreeNode(FactorVariableTreeNode original, Cloner cloner)
     : base(original, cloner)
 {
     variableName = original.variableName;
     if (original.weights != null)
     {
         this.weights = new double[original.Weights.Length];
         Array.Copy(original.Weights, weights, weights.Length);
     }
 }
Ejemplo n.º 3
0
 private void FormatFactor(int level, FactorVariableTreeNode node, StringBuilder strBuilder)
 {
     strBuilder.AppendLine("( ");
     strBuilder.AppendLineIndented(level + 1, string.Format("CASE {0}", VariableName2Identifier(node.VariableName)));
     foreach (var name in node.Symbol.GetVariableValues(node.VariableName))
     {
         strBuilder.AppendLineIndented(level + 2, string.Format("WHEN '{0}' THEN {1}", name, node.GetValue(name).ToString(CultureInfo.InvariantCulture)));
     }
     strBuilder.AppendLineIndented(level + 1, "ELSE NULL END");
     strBuilder.AppendIndented(level, ")");
 }
        protected IEnumerable <double> CalculateReplacementValues(ISymbolicExpressionTreeNode node, ISymbolicExpressionTree sourceTree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
                                                                  IDataset dataset, IEnumerable <int> rows)
        {
            //optimization: constant nodes return always the same value
            ConstantTreeNode             constantNode     = node as ConstantTreeNode;
            BinaryFactorVariableTreeNode binaryFactorNode = node as BinaryFactorVariableTreeNode;
            FactorVariableTreeNode       factorNode       = node as FactorVariableTreeNode;

            if (constantNode != null)
            {
                yield return(constantNode.Value);
            }
            else if (binaryFactorNode != null)
            {
                // valid replacements are either all off or all on
                yield return(0);

                yield return(1);
            }
            else if (factorNode != null)
            {
                foreach (var w in factorNode.Weights)
                {
                    yield return(w);
                }
                yield return(0.0);
            }
            else
            {
                var rootSymbol  = new ProgramRootSymbol().CreateTreeNode();
                var startSymbol = new StartSymbol().CreateTreeNode();
                rootSymbol.AddSubtree(startSymbol);
                startSymbol.AddSubtree((ISymbolicExpressionTreeNode)node.Clone());

                var tempTree = new SymbolicExpressionTree(rootSymbol);
                // clone ADFs of source tree
                for (int i = 1; i < sourceTree.Root.SubtreeCount; i++)
                {
                    tempTree.Root.AddSubtree((ISymbolicExpressionTreeNode)sourceTree.Root.GetSubtree(i).Clone());
                }
                yield return(interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Median());

                yield return(interpreter.GetSymbolicExpressionTreeValues(tempTree, dataset, rows).Average()); // TODO perf
            }
        }
Ejemplo n.º 5
0
 private void FormatFactor(FactorVariableTreeNode node, StringBuilder strBuilder)
 {
     strBuilder.AppendFormat("EvaluateFactor({0}, new [] {{ {1} }}, new [] {{ {2} }})", VariableName2Identifier(node.VariableName),
                             string.Join(",", node.Symbol.GetVariableValues(node.VariableName).Select(name => "\"" + name + "\"")), string.Join(",", node.Weights.Select(v => v.ToString(CultureInfo.InvariantCulture))));
 }