Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15353


Ignore:
Timestamp:
09/07/17 15:32:26 (7 years ago)
Author:
pfleck
Message:

#1666:

  • Changed Progress interface (single parameterless ctor, overloads for Start-method).
  • Passed Impact-Calculator per ctor argument instead of virtual properties.
  • Created virtual OptimizeConstants-method instead of making the button-click event-handler virtual.
Location:
branches/SimplifierViewsProgress
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/SimplifierViewsProgress/HeuristicLab.MainForm.WindowsForms/3.3/MainForms/MainForm.cs

    r15320 r15353  
    371371        contentViews = contentViews.Where(v => v.Content == content);
    372372
    373       var progress = new Progress(progressMessage, ProgressState.Started, progressValue);
     373      var progress = new Progress();
     374      progress.Start(progressMessage, progressValue);
    374375      foreach (var contentView in contentViews) {
    375376        progressViews.Add(new ProgressView(contentView, progress));
     
    384385    /// </summary>
    385386    public IProgress AddOperationProgressToView(Control control, string progressMessage) {
    386       var progress = new Progress(progressMessage, ProgressState.Started);
     387      var progress = new Progress();
     388      progress.Start(progressMessage);
    387389      AddOperationProgressToView(control, progress);
    388390      return progress;
  • branches/SimplifierViewsProgress/HeuristicLab.MainForm/3.3/Interfaces/IProgress.cs

    r15320 r15353  
    6363    /// Starts or restarts a Progress.
    6464    /// </summary>
    65     void Start(string status = null, double progressValue = -1.0);
     65    void Start();
     66    void Start(string status);
     67    void Start(string status, double progressValue);
    6668
    6769    /// <summary>
  • branches/SimplifierViewsProgress/HeuristicLab.MainForm/3.3/Progress.cs

    r15320 r15353  
    6868    }
    6969
    70     public Progress(string status = null, ProgressState state = ProgressState.Finished, double progressValue = -1)
    71       : base() {
    72       this.status = status;
    73       this.progressState = state;
    74       this.progressValue = progressValue;
     70    public Progress() {
     71      progressState = ProgressState.Finished;
     72      canBeCanceled = false;
    7573    }
    7674
     
    8583    }
    8684
    87     public void Start(string status = null, double progressValue = -1.0) {
     85    public void Start() {
     86      Start(string.Empty);
     87    }
     88    public void Start(string status) {
     89      Start(status, -1.0);
     90    }
     91    public void Start(string status, double progressValue) {
    8892      ProgressState = ProgressState.Started;
    89       Status = status;
     93      Status = status ?? string.Empty;
    9094      ProgressValue = progressValue;
    9195    }
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views/3.4/InteractiveSymbolicClassificationSolutionSimplifierViewBase.cs

    r15321 r15353  
    2020#endregion
    2121
    22 using System;
    2322using HeuristicLab.Common;
    2423using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     
    2726namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification.Views {
    2827  public abstract partial class InteractiveSymbolicClassificationSolutionSimplifierViewBase : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
    29     private readonly SymbolicClassificationSolutionImpactValuesCalculator calculator;
    30 
    3128    public new ISymbolicClassificationSolution Content {
    3229      get { return (ISymbolicClassificationSolution)base.Content; }
     
    3431    }
    3532
    36     protected override ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactCalculator {
    37       get { return calculator; }
    38     }
    39 
    40     public InteractiveSymbolicClassificationSolutionSimplifierViewBase()
    41       : base() {
     33    protected InteractiveSymbolicClassificationSolutionSimplifierViewBase()
     34      : base(new SymbolicClassificationSolutionImpactValuesCalculator()) {
    4235      InitializeComponent();
    4336      this.Caption = "Interactive Classification Solution Simplifier";
    44 
    45       calculator = new SymbolicClassificationSolutionImpactValuesCalculator();
    4637    }
    4738
     
    6152      return model;
    6253    }
    63 
    64     protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {
    65 
    66     }
    6754  }
    6855}
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views/3.4/InteractiveSymbolicRegressionSolutionSimplifierView.cs

    r15321 r15353  
    2121
    2222using System;
    23 using System.Threading.Tasks;
    2423using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     24using HeuristicLab.MainForm;
    2525using HeuristicLab.Problems.DataAnalysis.Symbolic.Views;
    2626
    2727namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Views {
    2828  public partial class InteractiveSymbolicRegressionSolutionSimplifierView : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
    29     private readonly SymbolicRegressionSolutionImpactValuesCalculator calculator;
    30 
    3129    public new SymbolicRegressionSolution Content {
    3230      get { return (SymbolicRegressionSolution)base.Content; }
     
    3432    }
    3533
    36     protected override ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactCalculator {
    37       get { return calculator; }
    38     }
    39 
    4034    public InteractiveSymbolicRegressionSolutionSimplifierView()
    41       : base() {
     35      : base(new SymbolicRegressionSolutionImpactValuesCalculator()) {
    4236      InitializeComponent();
    4337      this.Caption = "Interactive Regression Solution Simplifier";
    44       calculator = new SymbolicRegressionSolutionImpactValuesCalculator();
    4538    }
    4639
     
    5144    }
    5245
    53     protected override async void btnOptimizeConstants_Click(object sender, EventArgs e) {
     46    protected override ISymbolicExpressionTree OptimizeConstants(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData, IProgress progress) {
    5447      const int constOptIterations = 50;
    55       var model = Content.Model;
    56       Progress.Start("Optimizing Constants ...", 0);
    57       await Task.Run(() => {
    58         SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(model.Interpreter, model.SymbolicExpressionTree, Content.ProblemData, Content.ProblemData.TrainingIndices,
    59           applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true, lowerEstimationLimit: model.LowerEstimationLimit, upperEstimationLimit: model.UpperEstimationLimit,
    60           iterationReport: (args, func, iter) => {
    61             Progress.ProgressValue = (double)iter / (constOptIterations + 1); // (maxIterations + 1) iterations are reported
    62           });
    63       });
    64       UpdateModel(Content.Model.SymbolicExpressionTree); // UpdateModel calls Progress.Finish
     48      var regressionModelModel = (ISymbolicDataAnalysisModel)model.Clone();
     49      var regressionProblemData = (IRegressionProblemData)problemData;
     50      SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(regressionModelModel.Interpreter, regressionModelModel.SymbolicExpressionTree, regressionProblemData, regressionProblemData.TrainingIndices,
     51        applyLinearScaling: true, maxIterations: constOptIterations, updateVariableWeights: true, lowerEstimationLimit: regressionModelModel.LowerEstimationLimit, upperEstimationLimit: regressionModelModel.UpperEstimationLimit,
     52        iterationCallback: (args, func, obj) => {
     53          // (maxIterations + 2) iterations are reported; Math.Min neccecary because of floating-point inaccuracies
     54          progress.ProgressValue = Math.Min(progress.ProgressValue + 1.0 / (constOptIterations + 2), 1);
     55        });
     56      return model.SymbolicExpressionTree;
    6557    }
    6658  }
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs

    r15319 r15353  
    156156      int maxIterations, bool updateVariableWeights = true,
    157157      double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue,
    158       bool updateConstantsInTree = true, Action<double[], double, int> iterationReport = null) {
     158      bool updateConstantsInTree = true, Action<double[], double, object> iterationCallback = null) {
    159159
    160160      // numeric constants in the tree become variables for constant opt
     
    210210      alglib.ndimensional_pfunc function_cx_1_func = CreatePFunc(func);
    211211      alglib.ndimensional_pgrad function_cx_1_grad = CreatePGrad(func_grad);
    212 
    213       int i = 0;
    214       var xrep = new alglib.ndimensional_rep((a, f, o) => {
    215         if (iterationReport != null) iterationReport(a, f, i++);
    216       });
     212      alglib.ndimensional_rep xrep = null;
     213      if (iterationCallback != null)
     214        xrep = new alglib.ndimensional_rep(iterationCallback);
    217215
    218216      try {
    219217        alglib.lsfitcreatefg(x, y, c, n, m, k, false, out state);
    220218        alglib.lsfitsetcond(state, 0.0, 0.0, maxIterations);
    221         alglib.lsfitsetxrep(state, iterationReport != null);
     219        alglib.lsfitsetxrep(state, iterationCallback != null);
    222220        //alglib.lsfitsetgradientcheck(state, 0.001);
    223221        alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, xrep, null);
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views/3.4/InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView.cs

    r15321 r15353  
    2020#endregion
    2121
    22 using System;
    2322using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2423using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
     
    2726namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views {
    2827  public partial class InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView : InteractiveSymbolicDataAnalysisSolutionSimplifierView {
    29     private readonly SymbolicRegressionSolutionImpactValuesCalculator calculator;
    30 
    3128    public new SymbolicTimeSeriesPrognosisSolution Content {
    3229      get { return (SymbolicTimeSeriesPrognosisSolution)base.Content; }
     
    3431    }
    3532
    36     protected override ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactCalculator {
    37       get { return calculator; }
    38     }
    39 
    4033    public InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView()
    41       : base() {
     34      : base(new SymbolicRegressionSolutionImpactValuesCalculator()) {
    4235      InitializeComponent();
    4336      this.Caption = "Interactive Time-Series Prognosis Solution Simplifier";
    44       calculator = new SymbolicRegressionSolutionImpactValuesCalculator();
    4537    }
    4638
     
    5042      Content.Model = model;
    5143    }
    52 
    53     protected override void btnOptimizeConstants_Click(object sender, EventArgs e) {
    54 
    55     }
    5644  }
    5745}
  • branches/SimplifierViewsProgress/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs

    r15321 r15353  
    3838    private Dictionary<ISymbolicExpressionTreeNode, double> nodeImpacts;
    3939
    40     protected IProgress Progress = new Progress();
     40    private readonly ISymbolicDataAnalysisSolutionImpactValuesCalculator impactCalculator;
     41
     42    private readonly IProgress progress = new Progress();
    4143
    4244    private enum TreeState { Valid, Invalid }
    4345    private TreeState treeState;
    4446
    45     protected InteractiveSymbolicDataAnalysisSolutionSimplifierView() {
     47    protected InteractiveSymbolicDataAnalysisSolutionSimplifierView(ISymbolicDataAnalysisSolutionImpactValuesCalculator impactCalculator) {
    4648      InitializeComponent();
    4749      foldedNodes = new Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>();
     
    4951      nodeImpacts = new Dictionary<ISymbolicExpressionTreeNode, double>();
    5052      this.Caption = "Interactive Solution Simplifier";
     53      this.impactCalculator = impactCalculator;
    5154
    5255      // initialize the tree modifier that will be used to perform edit operations over the tree
     
    149152      Content.ProblemDataChanged += Content_Changed;
    150153      treeChart.Repainted += treeChart_Repainted;
    151       MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(grpSimplify, Progress);
     154      MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>().AddOperationProgressToView(grpSimplify, progress);
    152155    }
    153156    protected override void DeregisterContentEvents() {
     
    180183      treeChart.Tree = tree.Root.SubtreeCount > 1 ? new SymbolicExpressionTree(tree.Root) : new SymbolicExpressionTree(tree.Root.GetSubtree(0).GetSubtree(0));
    181184
    182       Progress.Start("Calculate Impact and Replacement Values ...", 0);
     185      progress.Start("Calculate Impact and Replacement Values ...", 0);
    183186      var impactAndReplacementValues = await Task.Run(() => CalculateImpactAndReplacementValues(tree));
    184187      var replacementValues = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item2);
     
    187190      }
    188191      nodeImpacts = impactAndReplacementValues.ToDictionary(x => x.Key, x => x.Value.Item1);
    189       Progress.Finish();
     192      progress.Finish();
    190193      PaintNodeImpacts();
    191194    }
     
    195198      foreach (var node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix()) {
    196199        double impactValue, replacementValue, newQualityForImpactsCalculation;
    197         ImpactCalculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation);
    198         Progress.ProgressValue += 1.0 / (tree.Length - 2);
     200        impactCalculator.CalculateImpactAndReplacementValues(Content.Model, node, Content.ProblemData, Content.ProblemData.TrainingIndices, out impactValue, out replacementValue, out newQualityForImpactsCalculation);
     201        progress.ProgressValue += 1.0 / (tree.Length - 2);
    199202        impactAndReplacementValues.Add(node, new Tuple<double, double>(impactValue, replacementValue));
    200203      }
     
    204207    protected abstract void UpdateModel(ISymbolicExpressionTree tree);
    205208
    206     protected abstract ISymbolicDataAnalysisSolutionImpactValuesCalculator ImpactCalculator { get; }
     209    protected virtual ISymbolicExpressionTree OptimizeConstants(ISymbolicDataAnalysisModel model, IDataAnalysisProblemData problemData, IProgress progress) {
     210      return model.SymbolicExpressionTree;
     211    }
    207212
    208213    private static ConstantTreeNode MakeConstantTreeNode(double value) {
     
    290295    }
    291296
    292     protected abstract void btnOptimizeConstants_Click(object sender, EventArgs e);
     297    private async void btnOptimizeConstants_Click(object sender, EventArgs e) {
     298      progress.Start("Optimizing Constants ...", 0);
     299      var newTree = await Task.Run(() => OptimizeConstants(Content.Model, Content.ProblemData, progress));
     300      UpdateModel(newTree); // UpdateModel calls Progress.Finish (via Content_Changed)
     301    }
    293302  }
    294303}
Note: See TracChangeset for help on using the changeset viewer.