Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/06/17 13:38:16 (6 years ago)
Author:
pfleck
Message:

#1666: Merged 15371-15372,15390,15400,15402 to stable

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Views

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs

    r15145 r15406  
    2424using System.Drawing;
    2525using System.Linq;
     26using System.Threading.Tasks;
    2627using System.Windows.Forms;
    2728using HeuristicLab.Common;
    2829using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2930using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
     31using HeuristicLab.MainForm;
    3032using HeuristicLab.MainForm.WindowsForms;
    3133
     
    3638    private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
    3739
     40    private readonly ISymbolicDataAnalysisSolutionImpactValuesCalculator impactCalculator;
     41
     42    private readonly IProgress progress = new Progress();
     43
    3844    private enum TreeState { Valid, Invalid }
    3945    private TreeState treeState;
    4046
    41     protected InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
     47    protected InteractiveSymbolicDataAnalysisSolutionSimplifierView(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactCalculator) {
    4248      InitializeComponent();
    4349      foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
     
    4551      nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
    4652      this.Caption = "Interactive Solution Simplifier";
     53      this.impactCalculator = impactCalculator;
    4754
    4855      // initialize the tree modifier that will be used to perform edit operations over the tree
     
    145152      Content.ProblemDataChanged += Content_Changed;
    146153      treeChart.Repainted += treeChart_Repainted;
     154      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(grpSimplify, progress);
    147155    }
    148156    protected override void DeregisterContentEvents() {
     
    151159      Content.ProblemDataChanged -= Content_Changed;
    152160      treeChart.Repainted -= treeChart_Repainted;
     161      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(grpSimplify, false);
    153162    }
    154163
     
    169178    }
    170179
    171     private void UpdateView() {
     180    private async void UpdateView() {
    172181      if (Content == null || Content.Model == null || Content.ProblemData == null) return;
    173182      var tree = Content.Model.SymbolicExpressionTree;
    174183      treeChart.Tree = tree.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(tree.Root) : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0));
    175184
    176       var impactAndReplacementValues = CalculateImpactAndReplacementValues(tree);
     185      progress.Start("Calculate Impact and Replacement Values ...");
     186      var impactAndReplacementValues = await Task.Run(() => CalculateImpactAndReplacementValues(tree));
     187      await Task.Delay(500); // wait for progressbar to finish animation
    177188      var replacementValues = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item2);
    178189      foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) {
     
    180191      }
    181192      nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1);
     193      progress.Finish();
    182194      PaintNodeImpacts();
    183195    }
    184196
    185     protected abstract Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree);
     197    protected virtual Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>> CalculateImpactAndReplacementValues(ISymbolicExpressionTree tree) {
     198      var impactAndReplacementValues = new Dictionary<ISymbolicExpressionTreeNode, Tuple<double, double>>();
     199      foreach (var node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
     200        double impactValue, replacementValue, newQualityForImpactsCalculation;
     201        impactCalculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation);
     202        double newProgressValue = progress.ProgressValue + 1.0 / (tree.Length - 2);
     203        progress.ProgressValue = Math.Min(newProgressValue, 1);
     204        impactAndReplacementValues.Add(node, new Tuple<double, double>(impactValue, replacementValue));
     205      }
     206      return impactAndReplacementValues;
     207    }
     208
    186209    protected abstract void UpdateModel(ISymbolicExpressionTree tree);
     210
     211    protected virtual ISymbolicExpressionTree OptimizeConstants(ISymbolicExpressionTree tree, IProgress progress) {
     212      return tree;
     213    }
    187214
    188215    private static ConstantTreeNode MakeConstantTreeNode(double value) {
     
    270297    }
    271298
    272     protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e);
     299    private async void btnOptimizeConstants_Click(object sender, EventArgs e) {
     300      progress.Start("Optimizing Constants ...");
     301      var tree = (ISymbolicExpressionTree)Content.Model.SymbolicExpressionTree.Clone();
     302      var newTree = await Task.Run(() => OptimizeConstants(tree, progress));
     303      await Task.Delay(500); // wait for progressbar to finish animation
     304      UpdateModel(newTree); // UpdateModel calls Progress.Finish (via Content_Changed)
     305    }
    273306  }
    274307}
Note: See TracChangeset for help on using the changeset viewer.