Changeset 15319


Ignore:
Timestamp:
08/10/17 13:49:26 (10 days ago)
Author:
pfleck
Message:

#1666 Made constant optimization and impact/replacement-values calculation async and added progress indication.

Location:
branches/SimplifierViewsProgress
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs

    r14826 r15319  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Linq;
     24using System.Threading.Tasks;
    2525using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2626using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
     
    5454        double impactValue, replacementValue, newQualityForImpactsCalculation;
    5555        calculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation);
     56        Progress.ProgressValue += 1.0 / (tree.Length - 2);
    5657        impactAndReplacementValues.Add(node, new Tuple<double, double>(impactValue, replacementValue));
    5758      }
     
    5960    }
    6061
    61     protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {
     62    protected override async void btnOptimizeConstants_Click(object sender, EventArgs e) {
     63      const int constOptIterations = 50;
    6264      var model = Content.Model;
    63       SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(model.Interpreter, model.SymbolicExpressionTree, Content.ProblemData, Content.ProblemData.TrainingIndices,
    64         applyLinearScaling: true, maxIterations: 50, updateVariableWeights: true, lowerEstimationLimit: model.LowerEstimationLimit, upperEstimationLimit: model.UpperEstimationLimit);
    65       UpdateModel(Content.Model.SymbolicExpressionTree);
     65      Progress.Start("Optimizing Constants ...", 0);
     66      await Task.Run(() => {
     67        SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(model.Interpreter, model.SymbolicExpressionTree, Content.ProblemData, Content.ProblemData.TrainingIndices,
     68          applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true, lowerEstimationLimit: model.LowerEstimationLimit, upperEstimationLimit: model.UpperEstimationLimit,
     69          iterationReport: (args, func, iter) => {
     70            Progress.ProgressValue = (double)iter / (constOptIterations + 1); // (maxIterations + 1) iterations are reported
     71          });
     72      });
     73      UpdateModel(Content.Model.SymbolicExpressionTree); // UpdateModel calls Progress.Finish
    6674    }
    6775  }
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/SymbolicRegressionSolutionView.cs

    r14185 r15319  
    5151    private void btn_SimplifyModel_Click(object sender, EventArgs e) {
    5252      var view = new InteractiveSymbolicRegressionSolutionSimplifierView();
     53      view.Show(); // open view first that a progress can be displayed when setting the content
    5354      view.Content = (SymbolicRegressionSolution)this.Content.Clone();
    54       view.Show();
    5555    }
    5656
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs

    r14951 r15319  
    156156      int maxIterations, bool updateVariableWeights = true,
    157157      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue,
    158       bool updateConstantsInTree = true) {
     158      bool updateConstantsInTree = true, Action<double[], double, int> iterationReport = null) {
    159159
    160160      // numeric constants in the tree become variables for constant opt
     
    211211      alglib.ndimensional_pgrad function_cx_1_grad = CreatePGrad(func_grad);
    212212
     213      int i = 0;
     214      var xrep = new alglib.ndimensional_rep((a, f, o) => {
     215        if (iterationReport != null) iterationReport(a, f, i++);
     216      });
     217
    213218      try {
    214219        alglib.lsfitcreatefg(x, y, c, n, m, k, false, out state);
    215220        alglib.lsfitsetcond(state, 0.0, 0.0, maxIterations);
     221        alglib.lsfitsetxrep(state, iterationReport != null);
    216222        //alglib.lsfitsetgradientcheck(state, 0.001);
    217         alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, null, null);
     223        alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, xrep, null);
    218224        alglib.lsfitresults(state, out retVal, out c, out rep);
    219       }
    220       catch (ArithmeticException) {
     225      } catch (ArithmeticException) {
    221226        return originalQuality;
    222       }
    223       catch (alglib.alglibexception) {
     227      } catch (alglib.alglibexception) {
    224228        return originalQuality;
    225229      }
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs

    r14949 r15319  
    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
     
    3537    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> changedNodes;
    3638    private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
     39
     40    protected IProgress Progress = new Progress();
    3741
    3842    private enum TreeState { Valid, Invalid }
     
    145149      Content.ProblemDataChanged += Content_Changed;
    146150      treeChart.Repainted += treeChart_Repainted;
     151      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(grpSimplify, Progress);
    147152    }
    148153    protected override void DeregisterContentEvents() {
     
    151156      Content.ProblemDataChanged -= Content_Changed;
    152157      treeChart.Repainted -= treeChart_Repainted;
     158      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(grpSimplify, false);
    153159    }
    154160
     
    169175    }
    170176
    171     private void UpdateView() {
     177    private async void UpdateView() {
    172178      if (Content == null || Content.Model == null || Content.ProblemData == null) return;
    173179      var tree = Content.Model.SymbolicExpressionTree;
    174180      treeChart.Tree = tree.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(tree.Root) : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0));
    175181
    176       var impactAndReplacementValues = CalculateImpactAndReplacementValues(tree);
     182      Progress.Start("Calculate Impact and Replacement Values ...", 0);
     183      var impactAndReplacementValues = await Task.Run(() => CalculateImpactAndReplacementValues(tree));
    177184      var replacementValues = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item2);
    178185      foreach (var pair in replacementValues.Where(pair => !(pair.Key is ConstantTreeNode))) {
     
    180187      }
    181188      nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1);
     189      Progress.Finish();
    182190      PaintNodeImpacts();
    183191    }
Note: See TracChangeset for help on using the changeset viewer.