public VariableNodeEditDialog(ISymbolicExpressionTreeNode node) {
      InitializeComponent();
      oldValueTextBox.TabStop = false; // cannot receive focus using tab key

      NewNode = (VariableTreeNode)node; // will throw an invalid cast exception if node is not of the correct type
      InitializeFields();
    }
Пример #2
0
        public VariableNodeEditDialog(ISymbolicExpressionTreeNode node)
        {
            InitializeComponent();
            oldValueTextBox.TabStop = false;  // cannot receive focus using tab key

            NewNode = (VariableTreeNode)node; // will throw an invalid cast exception if node is not of the correct type
            InitializeFields();
        }
        public static IClassificationSolution CreateLinearDiscriminantAnalysisSolution(IClassificationProblemData problemData)
        {
            var    dataset        = problemData.Dataset;
            string targetVariable = problemData.TargetVariable;
            IEnumerable <string> allowedInputVariables = problemData.AllowedInputVariables;
            IEnumerable <int>    rows = problemData.TrainingIndices;
            int nClasses = problemData.ClassNames.Count();

            double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows);
            if (inputMatrix.Cast <double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
            {
                throw new NotSupportedException("Linear discriminant analysis does not support NaN or infinity values in the input dataset.");
            }

            // change class values into class index
            int           targetVariableColumn = inputMatrix.GetLength(1) - 1;
            List <double> classValues          = problemData.ClassValues.OrderBy(x => x).ToList();

            for (int row = 0; row < inputMatrix.GetLength(0); row++)
            {
                inputMatrix[row, targetVariableColumn] = classValues.IndexOf(inputMatrix[row, targetVariableColumn]);
            }
            int info;

            double[] w;
            alglib.fisherlda(inputMatrix, inputMatrix.GetLength(0), allowedInputVariables.Count(), nClasses, out info, out w);
            if (info < 1)
            {
                throw new ArgumentException("Error in calculation of linear discriminant analysis solution");
            }

            ISymbolicExpressionTree     tree      = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
            ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();

            tree.Root.AddSubtree(startNode);
            ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();

            startNode.AddSubtree(addition);

            int col = 0;

            foreach (string column in allowedInputVariables)
            {
                VariableTreeNode vNode = (VariableTreeNode) new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable().CreateTreeNode();
                vNode.VariableName = column;
                vNode.Weight       = w[col];
                addition.AddSubtree(vNode);
                col++;
            }

            var model = LinearDiscriminantAnalysis.CreateDiscriminantFunctionModel(tree, new SymbolicDataAnalysisExpressionTreeInterpreter(), problemData, rows);
            SymbolicDiscriminantFunctionClassificationSolution solution = new SymbolicDiscriminantFunctionClassificationSolution(model, (IClassificationProblemData)problemData.Clone());

            return(solution);
        }
        private void DfsStringGeneration(VariableTreeNode current, string s, List <string> builtStrings)
        {
            if (current.Children == null)
            {
                builtStrings.Add(s + current.Value);
                return;
            }

            foreach (var node in current.Children)
            {
                DfsStringGeneration(node, s + current.Value, builtStrings);
            }
        }
Пример #5
0
        /**
         * void makeDataTree
         *
         * This function generates a tree based on the variables availible in the simulation.  All input and
         * output variables.  This is used primarily for the Sinter Config GUI.
         *
         * Excel data isn't really organized in a tree, so this function is a little weird
         * The most annoying piece of the process is that I want the Tree to go Sheet->Row->Col
         * but Excel goes Sheet$Col$Row  ie, column major order.  Thanks Excel.
         * * */
        public override void makeDataTree()
        {
            o_dataTree = new VariableTree.VariableTree(parsePath, pathSeperator);
            VariableTreeNode rootNode = new VariableTreeNode("root", "", pathSeperator);

            Excel.Sheets xlWorksheets = (Excel.Sheets)o_xlWorkbook.Worksheets;

            foreach (Excel.Worksheet xlWorksheet in xlWorksheets)
            {
                String           sheetName = xlWorksheet.Name;
                VariableTreeNode sheetNode = new VariableTreeNode(sheetName, sheetName, pathSeperator);

                Excel.Range xlRange = xlWorksheet.UsedRange;
                Excel.Range rows    = xlRange.Rows;
                Excel.Range cols    = xlRange.Columns;

                //Calculate the rows and columns locally, because having Excel do it takes a LONG time.
                String rowStartS = parsePath(((Excel.Range)xlRange.Cells[1, 1]).Address)[0];
                int    rowStartI = Convert.ToInt32(rowStartS);
                String colStartS = parsePath(((Excel.Range)xlRange.Cells[1, 1]).Address)[1];
                int    colStartI = NumberFromExcelColumn(colStartS);

                for (int rr = 0; rr < rows.Count; ++rr)
                {
                    String           rowName = Convert.ToString(rowStartI + rr);
                    String           rowPath = String.Format("{0}$*${1}", sheetName, rowName);
                    VariableTreeNode rowNode = new VariableTreeNode(rowName, rowPath, pathSeperator);
                    sheetNode.addChild(rowNode);

                    for (int cc = 0; cc < cols.Count; ++cc)
                    {
                        String           colName = ExcelColumnFromNumber(colStartI + cc);
                        String           colPath = String.Format("{0}${1}${2}", sheetName, colName, rowName);
                        VariableTreeNode colNode = new VariableTreeNode(colName, colPath, pathSeperator);
                        rowNode.addChild(colNode);
                    }
                }
                rootNode.addChild(sheetNode);
            }

            o_dataTree.rootNode = rootNode;

            //Remove the Dummy Children (those are only required when doing incremental tree building
            rootNode.traverse(rootNode, thisNode =>
            {
                if (thisNode.o_children.ContainsKey("DummyChild"))
                {
                    thisNode.o_children.Remove("DummyChild");
                }
            });
        }
Пример #6
0
 private void explainTreeView_DragDrop(object sender, DragEventArgs e)
 {
     if (e.Data.GetDataPresent("WindowsForms10PersistentObject", false))
     {
         object       data       = e.Data.GetData("WindowsForms10PersistentObject");
         BaseTreeNode sourceNode = data as BaseTreeNode;
         if (sourceNode != null)
         {
             VariableTreeNode variableTreeNode = sourceNode as VariableTreeNode;
             if (variableTreeNode != null)
             {
                 ExpandAndShowVariable(new VariableSelector(variableTreeNode.Item));
             }
         }
     }
 }
        private static void UpdateConstants(ISymbolicExpressionTree tree, double[] constants, bool updateVariableWeights)
        {
            int i = 0;

            foreach (var node in tree.Root.IterateNodesPrefix().OfType <SymbolicExpressionTreeTerminalNode>())
            {
                ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
                VariableTreeNode variableTreeNode = node as VariableTreeNode;
                if (constantTreeNode != null)
                {
                    constantTreeNode.Value = constants[i++];
                }
                else if (updateVariableWeights && variableTreeNode != null)
                {
                    variableTreeNode.Weight = constants[i++];
                }
            }
        }
Пример #8
0
        /// <summary>
        ///     Called when the drop operation is performed on this text box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Editor_DragDropHandler(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("WindowsForms10PersistentObject", false))
            {
                object       data       = e.Data.GetData("WindowsForms10PersistentObject");
                BaseTreeNode sourceNode = data as BaseTreeNode;
                if (sourceNode != null)
                {
                    VariableTreeNode variableNode = sourceNode as VariableTreeNode;
                    if (variableNode != null)
                    {
                        EditionTextBox.SelectedText = SetVariable(variableNode.Item);
                    }
                    else
                    {
                        StructureTreeNode structureTreeNode = sourceNode as StructureTreeNode;
                        if (structureTreeNode != null)
                        {
                            TextualExplanation text = new TextualExplanation();

                            Structure structure = structureTreeNode.Item;
                            CreateDefaultStructureValue(text, structure);
                            EditionTextBox.SelectedText = text.Text;
                        }
                        else
                        {
                            EditionTextBox.SelectedText = StripUseless(sourceNode.Model.FullName, WritingContext());
                        }
                    }
                }

                OLVListItem item = data as OLVListItem;
                if (item != null)
                {
                    IVariable variable = item.RowObject as IVariable;
                    if (variable != null)
                    {
                        EditionTextBox.SelectedText = SetVariable(variable);
                    }
                }
            }
        }
Пример #9
0
        public void DataTreeTest()
        {
            String sinterconf              = Properties.Settings.Default.ExcelSinterJson;
            String defaultsfile            = null;
            String infilename              = Properties.Settings.Default.ExcelSinterInputs;
            String outfilename             = Properties.Settings.Default.ExcelSinterOutputs;
            String canonicalOutputFilename = Properties.Settings.Default.ExcelSinterCanonicalOutputs;

            ISimulation           sim   = startupSim(sinterconf, defaultsfile, infilename);
            sinter_InteractiveSim a_sim = (sinter_InteractiveSim)sim; //Only works on Interactive sims

            {
                String nodePath = "height$C$2";

                VariableTreeNode testNode = sim.dataTree.resolveNode(nodePath);
                Assert.IsTrue(nodePath == testNode.path);
                Assert.IsTrue(testNode.name == "C");

                sinter_Variable tmp = new sinter_Variable();
                tmp.init(a_sim, sinter_Variable.sinter_IOType.si_DOUBLE, new String[] { nodePath });

                Assert.IsTrue((double)tmp.dfault == 74);
                Assert.IsTrue(tmp.type == sinter_Variable.sinter_IOType.si_DOUBLE);
            }

            {
                String nodePath = "height$B$2";

                VariableTreeNode testNode = sim.dataTree.resolveNode(nodePath);
                Assert.IsTrue(nodePath == testNode.path);
                Assert.IsTrue(testNode.name == "B");

                sinter_Variable tmp = new sinter_Variable();
                tmp.init(a_sim, sinter_Variable.sinter_IOType.si_STRING, new String[] { nodePath });

                Assert.IsTrue((String)tmp.dfault == "Leek");
                Assert.IsTrue(tmp.type == sinter_Variable.sinter_IOType.si_STRING);
            }
            sim.closeSim();
        }
        public static double OptimizeConstants(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable <int> rows, bool applyLinearScaling, int maxIterations, bool updateVariableWeights = true, double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue, bool updateConstantsInTree = true)
        {
            List <AutoDiff.Variable> variables     = new List <AutoDiff.Variable>();
            List <AutoDiff.Variable> parameters    = new List <AutoDiff.Variable>();
            List <string>            variableNames = new List <string>();

            AutoDiff.Term func;
            if (!TryTransformToAutoDiff(tree.Root.GetSubtree(0), variables, parameters, variableNames, updateVariableWeights, out func))
            {
                throw new NotSupportedException("Could not optimize constants of symbolic expression tree due to not supported symbols used in the tree.");
            }
            if (variableNames.Count == 0)
            {
                return(0.0);
            }

            AutoDiff.IParametricCompiledTerm compiledFunc = func.Compile(variables.ToArray(), parameters.ToArray());

            List <SymbolicExpressionTreeTerminalNode> terminalNodes = null;

            if (updateVariableWeights)
            {
                terminalNodes = tree.Root.IterateNodesPrefix().OfType <SymbolicExpressionTreeTerminalNode>().ToList();
            }
            else
            {
                terminalNodes = new List <SymbolicExpressionTreeTerminalNode>(tree.Root.IterateNodesPrefix().OfType <ConstantTreeNode>());
            }

            //extract inital constants
            double[] c = new double[variables.Count];
            {
                c[0] = 0.0;
                c[1] = 1.0;
                int i = 2;
                foreach (var node in terminalNodes)
                {
                    ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
                    VariableTreeNode variableTreeNode = node as VariableTreeNode;
                    if (constantTreeNode != null)
                    {
                        c[i++] = constantTreeNode.Value;
                    }
                    else if (updateVariableWeights && variableTreeNode != null)
                    {
                        c[i++] = variableTreeNode.Weight;
                    }
                }
            }
            double[] originalConstants = (double[])c.Clone();
            double   originalQuality   = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);

            alglib.lsfitstate  state;
            alglib.lsfitreport rep;
            int info;

            IDataset ds = problemData.Dataset;

            double[,] x = new double[rows.Count(), variableNames.Count];
            int row = 0;

            foreach (var r in rows)
            {
                for (int col = 0; col < variableNames.Count; col++)
                {
                    x[row, col] = ds.GetDoubleValue(variableNames[col], r);
                }
                row++;
            }
            double[] y = ds.GetDoubleValues(problemData.TargetVariable, rows).ToArray();
            int      n = x.GetLength(0);
            int      m = x.GetLength(1);
            int      k = c.Length;

            alglib.ndimensional_pfunc function_cx_1_func = CreatePFunc(compiledFunc);
            alglib.ndimensional_pgrad function_cx_1_grad = CreatePGrad(compiledFunc);

            try {
                alglib.lsfitcreatefg(x, y, c, n, m, k, false, out state);
                alglib.lsfitsetcond(state, 0.0, 0.0, maxIterations);
                //alglib.lsfitsetgradientcheck(state, 0.001);
                alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, null, null);
                alglib.lsfitresults(state, out info, out c, out rep);
            }
            catch (ArithmeticException) {
                return(originalQuality);
            }
            catch (alglib.alglibexception) {
                return(originalQuality);
            }

            //info == -7  => constant optimization failed due to wrong gradient
            if (info != -7)
            {
                UpdateConstants(tree, c.Skip(2).ToArray(), updateVariableWeights);
            }
            var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, tree, lowerEstimationLimit, upperEstimationLimit, problemData, rows, applyLinearScaling);

            if (!updateConstantsInTree)
            {
                UpdateConstants(tree, originalConstants.Skip(2).ToArray(), updateVariableWeights);
            }
            if (originalQuality - quality > 0.001 || double.IsNaN(quality))
            {
                UpdateConstants(tree, originalConstants.Skip(2).ToArray(), updateVariableWeights);
                return(originalQuality);
            }
            return(quality);
        }
        /// <summary>
        ///     Accepts a drop event
        /// </summary>
        /// <param name="sourceNode"></param>
        public override void AcceptDrop(BaseTreeNode sourceNode)
        {
            {
                if (sourceNode is ShortcutTreeNode)
                {
                    ShortcutTreeNode shortcut = sourceNode as ShortcutTreeNode;

                    if (shortcut.Item.Dictionary == Item.Dictionary)
                    {
                        Shortcut otherShortcut = (Shortcut)shortcut.Item.Duplicate();
                        Item.appendShortcuts(otherShortcut);

                        shortcut.Delete();
                    }
                }
                else if (sourceNode is ShortcutFolderTreeNode)
                {
                    ShortcutFolderTreeNode folder = sourceNode as ShortcutFolderTreeNode;

                    if (folder.Item.Dictionary == Item.Dictionary)
                    {
                        ShortcutFolder otherFolder = (ShortcutFolder)folder.Item.Duplicate();
                        Item.appendFolders(otherFolder);

                        folder.Delete();
                    }
                }
                else if (sourceNode is RuleTreeNode)
                {
                    RuleTreeNode rule = sourceNode as RuleTreeNode;

                    if (rule.Item.Dictionary == Item.Dictionary)
                    {
                        Shortcut shortcut = (Shortcut)acceptor.getFactory().createShortcut();
                        shortcut.CopyFrom(rule.Item);
                        Item.appendShortcuts(shortcut);
                    }
                }
                else if (sourceNode is FunctionTreeNode)
                {
                    FunctionTreeNode function = sourceNode as FunctionTreeNode;

                    if (function.Item.Dictionary == Item.Dictionary)
                    {
                        Shortcut shortcut = (Shortcut)acceptor.getFactory().createShortcut();
                        shortcut.CopyFrom(function.Item);
                        Item.appendShortcuts(shortcut);
                    }
                }
                else if (sourceNode is ProcedureTreeNode)
                {
                    ProcedureTreeNode procedure = sourceNode as ProcedureTreeNode;

                    if (procedure.Item.Dictionary == Item.Dictionary)
                    {
                        Shortcut shortcut = (Shortcut)acceptor.getFactory().createShortcut();
                        shortcut.CopyFrom(procedure.Item);
                        Item.appendShortcuts(shortcut);
                    }
                }
                else if (sourceNode is VariableTreeNode)
                {
                    VariableTreeNode variable = sourceNode as VariableTreeNode;

                    if (variable.Item.Dictionary == Item.Dictionary)
                    {
                        Shortcut shortcut = (Shortcut)acceptor.getFactory().createShortcut();
                        shortcut.CopyFrom(variable.Item);
                        Item.appendShortcuts(shortcut);
                    }
                }
            }
        }
Пример #12
0
 public Symbol(int scope, VariableTreeNode node)
 {
     Scope    = scope;
     TreeNode = node ?? throw new ArgumentNullException(nameof(node));
 }
Пример #13
0
 /** Leftmost name in the path refers to child of "ThisNode"
  */
 private VariableTreeNode findDataTreeNode(IList <String> pathArray, VariableTreeNode thisNode)
 {
     throw new NotImplementedException();
 }
Пример #14
0
        public static ISymbolicRegressionSolution CreateLinearRegressionSolution(IRegressionProblemData problemData, out double rmsError, out double cvRmsError)
        {
            var    dataset        = problemData.Dataset;
            string targetVariable = problemData.TargetVariable;
            IEnumerable <string> allowedInputVariables = problemData.AllowedInputVariables;
            IEnumerable <int>    rows = problemData.TrainingIndices;

            double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows);
            if (inputMatrix.Cast <double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
            {
                throw new NotSupportedException("Linear regression does not support NaN or infinity values in the input dataset.");
            }

            alglib.linearmodel lm = new alglib.linearmodel();
            alglib.lrreport    ar = new alglib.lrreport();
            int nRows             = inputMatrix.GetLength(0);
            int nFeatures         = inputMatrix.GetLength(1) - 1;

            double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant

            int retVal = 1;

            alglib.lrbuild(inputMatrix, nRows, nFeatures, out retVal, out lm, out ar);
            if (retVal != 1)
            {
                throw new ArgumentException("Error in calculation of linear regression solution");
            }
            rmsError   = ar.rmserror;
            cvRmsError = ar.cvrmserror;

            alglib.lrunpack(lm, out coefficients, out nFeatures);

            ISymbolicExpressionTree     tree      = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
            ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();

            tree.Root.AddSubtree(startNode);
            ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();

            startNode.AddSubtree(addition);

            int col = 0;

            foreach (string column in allowedInputVariables)
            {
                VariableTreeNode vNode = (VariableTreeNode) new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable().CreateTreeNode();
                vNode.VariableName = column;
                vNode.Weight       = coefficients[col];
                addition.AddSubtree(vNode);
                col++;
            }

            ConstantTreeNode cNode = (ConstantTreeNode) new Constant().CreateTreeNode();

            cNode.Value = coefficients[coefficients.Length - 1];
            addition.AddSubtree(cNode);

            SymbolicRegressionSolution solution = new SymbolicRegressionSolution(new SymbolicRegressionModel(tree, new SymbolicDataAnalysisExpressionTreeInterpreter()), (IRegressionProblemData)problemData.Clone());

            solution.Model.Name = "Linear Regression Model";
            solution.Name       = "Linear Regression Solution";
            return(solution);
        }
Пример #15
0
        /// <summary>
        ///     Handles a drop event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TimeLineControl_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent("WindowsForms10PersistentObject", false))
            {
                BaseTreeNode sourceNode = e.Data.GetData("WindowsForms10PersistentObject") as BaseTreeNode;
                if (sourceNode != null)
                {
                    VariableTreeNode variableNode = sourceNode as VariableTreeNode;
                    if (variableNode != null)
                    {
                        SubStep subStep = SubStepRelatedToMousePosition();
                        if (subStep != null)
                        {
                            // Create the default value
                            IValue     value        = null;
                            Expression expression   = null;
                            string     defaultValue = variableNode.Item.Default;
                            if (defaultValue != null)
                            {
                                const bool doSemanticalAnalysis = true;
                                const bool silent = true;
                                expression = new Parser().Expression(variableNode.Item, defaultValue,
                                                                     AllMatches.INSTANCE, doSemanticalAnalysis, null, silent);
                            }

                            if (expression != null)
                            {
                                InterpretationContext context = new InterpretationContext {
                                    UseDefaultValue = false
                                };
                                value = expression.GetExpressionValue(context, null);
                            }

                            if (value == null || value is EmptyValue)
                            {
                                Structure structureType = variableNode.Item.Type as Structure;
                                if (structureType != null)
                                {
                                    const bool setDefaultValue = false;
                                    value = new StructureValue(structureType, setDefaultValue);
                                }
                            }

                            // Create the action or the expectation according to the keyboard modifier keys
                            if (value != null)
                            {
                                if ((e.KeyState & Ctrl) != 0)
                                {
                                    Expectation expectation = (Expectation)acceptor.getFactory().createExpectation();
                                    expectation.ExpressionText = variableNode.Item.FullName + " == " + value.FullName;
                                    subStep.appendExpectations(expectation);
                                }
                                else
                                {
                                    Action action = (Action)acceptor.getFactory().createAction();
                                    action.ExpressionText = variableNode.Item.FullName + " <- " + value.FullName;
                                    subStep.appendActions(action);
                                }
                            }
                            else
                            {
                                MessageBox.Show(
                                    Resources
                                    .StaticTimeLineControl_TimeLineControl_DragDrop_Cannot_evaluate_variable_default_value,
                                    Resources.StaticTimeLineControl_TimeLineControl_DragDrop_Cannot_create_event,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                            }
                        }
                    }
                }
            }
        }