Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/16/18 11:55:13 (5 years ago)
Author:
chaider
Message:

#2956: Added intermediate of a-priori knowledge

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2956_apriori_knowledge/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs

    r15583 r16303  
    2626using System.Threading.Tasks;
    2727using System.Windows.Forms;
     28using HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration.Interpreter;
     29using HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration.IntervalArithmetic;
     30using HeuristicLab.Algorithms.DataAnalysis.KnowledgeIntegration.Util;
    2831using HeuristicLab.Common;
    2932using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    3639    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> foldedNodes;
    3740    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> changedNodes;
     41    //private Dictionary<ISymbolicExpressionTreeNode, Interval> intervals;
     42    private Dictionary<ISymbolicExpressionTreeNode, Interval> intervals;
    3843    private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
    3944
     
    5055      changedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
    5156      nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
     57      //intervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
    5258      this.Caption = "Interactive Solution Simplifier";
    5359      this.impactCalculator = impactCalculator;
     
    185191      progress.Start("Calculate Impact and Replacement Values ...");
    186192      var impactAndReplacementValues = await Task.Run(() => CalculateImpactAndReplacementValues(tree));
     193      var interpreter = new SymbolicDataAnalysisIntervalArithmeticInterpreter();
     194
     195      //var intervalParam = ((IRegressionProblemData)Content.ProblemData).IntervalVariable;
     196      var customIntervals = new Dictionary<String, Interval>();
     197      var parser = new FormulationParser();
     198      //foreach (var ci in intervalParam) {
     199      //var inter = parser.Parse(ci.Formulation.GetValue());
     200      //customIntervals.Add(ci.Variable.GetValue(), new Interval(0, 1));
     201      //}
     202      var resultInterval = interpreter.GetSymbolicExressionTreeIntervals(tree, Content.ProblemData.Dataset, Content.ProblemData.TrainingIndices, out intervals);
    187203      await Task.Delay(500); // wait for progressbar to finish animation
    188204      var replacementValues = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item2);
     
    190206        foldedNodes[pair.Key] = MakeConstantTreeNode(pair.Value);
    191207      }
     208      //Instruction currentInstr = instructions.NextInstruction();
     209      //while (instructions.HasNextInstruction()) {
     210      //  //intervals[currentInstr.dynamicNode] = (Interval)currentInstr.data;
     211      //  intervals.Add(Tuple.Create(currentInstr.dynamicNode, (Interval)currentInstr.data));
     212      //  currentInstr = instructions.NextInstruction();
     213      //}
     214     
     215     
     216
     217      //intervals.Add(Tuple.Create(currentInstr.dynamicNode, (Interval)currentInstr.data));
    192218      nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1);
    193219      progress.Finish();
    194       PaintNodeImpacts();
     220      PaintIntervals();
     221      //PaintNodeImpacts();
    195222    }
    196223
     
    253280    }
    254281
     282    private void PaintIntervals() {
     283      int node = 0;
     284      var impacts = nodeImpacts.Values;
     285      double max = impacts.Max();
     286      double min = impacts.Min();
     287
     288      foreach (ISymbolicExpressionTreeNode treeNode in Content.Model.SymbolicExpressionTree.IterateNodesPrefix()) {
     289        VisualTreeNode<ISymbolicExpressionTreeNode> visualTree = treeChart.GetVisualSymbolicExpressionTreeNode(treeNode);
     290
     291        if (visualTree != null) {
     292          visualTree.ToolTip = visualTree.Content.ToString();
     293          //visualTree.ToolTip += String.Format($"{Environment.NewLine}Intervals: [{intervals[node].Item2.LowerBound:E4} ... {intervals[node].Item2.UpperBound:E4}]{Environment.NewLine}");
     294          visualTree.ToolTip += String.Format($"{Environment.NewLine}Intervals: [{intervals[treeNode].LowerBound:E4} ... {intervals[treeNode].UpperBound:E4}]{Environment.NewLine}]");
     295          node++;
     296         
     297          if (!(treeNode is ConstantTreeNode) && nodeImpacts.ContainsKey(treeNode)) {
     298            double impact = nodeImpacts[treeNode];
     299
     300            // impact = 0 if no change
     301            // impact < 0 if new solution is better
     302            // impact > 0 if new solution is worse
     303            if (impact < 0.0) {
     304              visualTree.FillColor = Color.FromArgb((int)(impact / min * 255), Color.Red);
     305            } else if (impact.IsAlmost(0.0)) {
     306              visualTree.FillColor = Color.White;
     307            } else {
     308              // max is guaranteed to be > 0
     309              visualTree.FillColor = Color.FromArgb((int)(impact / max * 255), Color.Green);
     310            }
     311            visualTree.ToolTip += String.Format($"{Environment.NewLine} Node impact: {impact:E4}");
     312            var constantReplacementNode = foldedNodes[treeNode] as ConstantTreeNode;
     313            if (constantReplacementNode != null) {
     314              visualTree.ToolTip += String.Format($"{Environment.NewLine} Replacement value: {constantReplacementNode.Value:E4}");
     315            }
     316          }
     317          if (changedNodes.ContainsKey(treeNode)) {
     318            visualTree.LineColor = Color.DodgerBlue;
     319          } else if (treeNode is ConstantTreeNode && foldedNodes.ContainsKey(treeNode)) {
     320            visualTree.LineColor = Color.DarkOrange;
     321          }
     322        }
     323      }
     324      treeChart.RepaintNodes();
     325    }
     326
    255327    private void PaintNodeImpacts() {
    256328      var impacts = nodeImpacts.Values;
Note: See TracChangeset for help on using the changeset viewer.