Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/14/18 11:47:37 (6 years ago)
Author:
abeham
Message:

#2817: updated to trunk r16140

Location:
branches/2817-BinPackingSpeedup
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2817-BinPackingSpeedup

  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views

  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionVariableImpactsView.cs

    r16140 r16141  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Threading;
    2526using System.Threading.Tasks;
    2627using HeuristicLab.Common;
     
    3233  [Content(typeof(IRegressionSolution))]
    3334  public partial class RegressionSolutionVariableImpactsView : DataAnalysisSolutionEvaluationView {
     35    private CancellationTokenSource cancellationToken = new CancellationTokenSource();
    3436    private enum SortingCriteria {
    3537      ImpactValue,
     
    3739      VariableName
    3840    }
    39 
    40     private Dictionary<string, double> rawVariableImpacts = new Dictionary<string, double>();
     41    private List<Tuple<string, double>> rawVariableImpacts = new List<Tuple<string, double>>();
    4142
    4243    public new IRegressionSolution Content {
     
    5152      InitializeComponent();
    5253
    53       //Little workaround. If you fill the ComboBox-Items in the other partial class, the UI-Designer will moan.
    54       this.sortByComboBox.Items.AddRange(Enum.GetValues(typeof(SortingCriteria)).Cast<object>().ToArray());
     54      //Set the default values
     55      this.dataPartitionComboBox.SelectedIndex = 0;
     56      this.replacementComboBox.SelectedIndex = 3;
     57      this.factorVarReplComboBox.SelectedIndex = 0;
    5558      this.sortByComboBox.SelectedItem = SortingCriteria.ImpactValue;
    56 
    57       this.dataPartitionComboBox.SelectedIndex = 0;
    58       this.replacementComboBox.SelectedIndex = 0;
    59       this.factorVarReplComboBox.SelectedIndex = 0;
    60     }
    61 
    62     #region events
     59    }
     60
    6361    protected override void RegisterContentEvents() {
    6462      base.RegisterContentEvents();
     
    8684        variableImactsArrayView.Content = null;
    8785      } else {
    88         UpdateVariableImpacts();
    89       }
     86        UpdateVariableImpact();
     87      }
     88    }
     89
     90    private void RegressionSolutionVariableImpactsView_VisibleChanged(object sender, EventArgs e) {
     91      cancellationToken.Cancel();
    9092    }
    9193
    9294
    9395    private void dataPartitionComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    94       UpdateVariableImpacts();
     96      UpdateVariableImpact();
    9597    }
    9698
    9799    private void replacementComboBox_SelectedIndexChanged(object sender, EventArgs e) {
    98       UpdateVariableImpacts();
     100      UpdateVariableImpact();
    99101    }
    100102
     
    102104      //Update the default ordering (asc,desc), but remove the eventHandler beforehand (otherwise the data would be ordered twice)
    103105      ascendingCheckBox.CheckedChanged -= ascendingCheckBox_CheckedChanged;
    104       switch ((SortingCriteria)sortByComboBox.SelectedItem) {
    105         case SortingCriteria.ImpactValue:
    106           ascendingCheckBox.Checked = false;
    107           break;
    108         case SortingCriteria.Occurrence:
    109           ascendingCheckBox.Checked = true;
    110           break;
    111         case SortingCriteria.VariableName:
    112           ascendingCheckBox.Checked = true;
    113           break;
    114         default:
    115           throw new NotImplementedException("Ordering for selected SortingCriteria not implemented");
    116       }
     106      ascendingCheckBox.Checked = (SortingCriteria)sortByComboBox.SelectedItem != SortingCriteria.ImpactValue;
    117107      ascendingCheckBox.CheckedChanged += ascendingCheckBox_CheckedChanged;
    118108
    119       UpdateDataOrdering();
     109      UpdateOrdering();
    120110    }
    121111
    122112    private void ascendingCheckBox_CheckedChanged(object sender, EventArgs e) {
    123       UpdateDataOrdering();
    124     }
    125     #endregion
    126 
    127     #region Helper Methods         
    128     private void UpdateVariableImpacts() {
     113      UpdateOrdering();
     114    }
     115
     116
     117    private async void UpdateVariableImpact() {
     118      IProgress progress;
     119
     120      //Check if the selection is valid
    129121      if (Content == null) { return; }
    130122      if (replacementComboBox.SelectedIndex < 0) { return; }
     
    132124      if (factorVarReplComboBox.SelectedIndex < 0) { return; }
    133125
    134       variableImactsArrayView.Caption = Content.Name + " Variable Impacts";
    135 
     126      //Prepare arguments
    136127      var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;
    137128      var replMethod = (RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum)replacementComboBox.Items[replacementComboBox.SelectedIndex];
     
    139130      var dataPartition = (RegressionSolutionVariableImpactsCalculator.DataPartitionEnum)dataPartitionComboBox.SelectedItem;
    140131
    141       Task.Factory.StartNew(() => {
    142         try {
    143           mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name);
    144 
    145           //Remember the original ordering of the variables
    146           var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod);
    147           var problemData = Content.ProblemData;
    148           var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
    149           var originalVariableOrdering = problemData.Dataset.VariableNames.Where(v => inputvariables.Contains(v)).Where(problemData.Dataset.VariableHasType<double>).ToList();
    150           rawVariableImpacts.Clear();
    151           originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(v, impacts.First(vv => vv.Item1 == v).Item2));
    152           UpdateDataOrdering();
    153         } finally {
    154           mainForm.RemoveOperationProgressFromView(this);
    155         }
    156       });
     132      variableImactsArrayView.Caption = Content.Name + " Variable Impacts";
     133      progress = mainForm.AddOperationProgressToView(this, "Calculating variable impacts for " + Content.Name);
     134      progress.ProgressValue = 0;
     135
     136      cancellationToken = new CancellationTokenSource();
     137      //Remember the original ordering of the variables
     138      try {
     139        var impacts = await Task.Run(() => RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content, dataPartition, replMethod, factorReplMethod,
     140          (i, s) => {
     141            progress.ProgressValue = i;
     142            progress.Status = s;
     143            return cancellationToken.Token.IsCancellationRequested;
     144          }), cancellationToken.Token);
     145
     146        if (cancellationToken.Token.IsCancellationRequested) { return; }
     147        var problemData = Content.ProblemData;
     148        var inputvariables = new HashSet<string>(problemData.AllowedInputVariables.Union(Content.Model.VariablesUsedForPrediction));
     149        var originalVariableOrdering = problemData.Dataset.VariableNames
     150          .Where(v => inputvariables.Contains(v))
     151          .Where(v => problemData.Dataset.VariableHasType<double>(v) || problemData.Dataset.VariableHasType<string>(v))
     152          .ToList();
     153
     154        rawVariableImpacts.Clear();
     155        originalVariableOrdering.ForEach(v => rawVariableImpacts.Add(new Tuple<string, double>(v, impacts.First(vv => vv.Item1 == v).Item2)));
     156        UpdateOrdering();
     157      } finally {
     158        ((MainForm.WindowsForms.MainForm)MainFormManager.MainForm).RemoveOperationProgressFromView(this);
     159      }
    157160    }
    158161
     
    161164    /// The default is "Descending" by "VariableImpact" (as in previous versions)
    162165    /// </summary>
    163     private void UpdateDataOrdering() {
     166    private void UpdateOrdering() {
    164167      //Check if valid sortingCriteria is selected and data exists
    165168      if (sortByComboBox.SelectedIndex == -1) { return; }
     
    170173      bool ascending = ascendingCheckBox.Checked;
    171174
    172       IEnumerable<KeyValuePair<string, double>> orderedEntries = null;
     175      IEnumerable<Tuple<string, double>> orderedEntries = null;
    173176
    174177      //Sort accordingly
    175178      switch (selectedItem) {
    176179        case SortingCriteria.ImpactValue:
    177           orderedEntries = rawVariableImpacts.OrderBy(v => v.Value);
     180          orderedEntries = rawVariableImpacts.OrderBy(v => v.Item2);
    178181          break;
    179182        case SortingCriteria.Occurrence:
     
    181184          break;
    182185        case SortingCriteria.VariableName:
    183           orderedEntries = rawVariableImpacts.OrderBy(v => v.Key, new NaturalStringComparer());
     186          orderedEntries = rawVariableImpacts.OrderBy(v => v.Item1, new NaturalStringComparer());
    184187          break;
    185188        default:
     
    190193
    191194      //Write the data back
    192       var impactArray = new DoubleArray(orderedEntries.Select(i => i.Value).ToArray()) {
    193         ElementNames = orderedEntries.Select(i => i.Key)
     195      var impactArray = new DoubleArray(orderedEntries.Select(i => i.Item2).ToArray()) {
     196        ElementNames = orderedEntries.Select(i => i.Item1)
    194197      };
    195       variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
    196     }
    197     #endregion
     198
     199      //Could be, if the View was closed
     200      if (!variableImactsArrayView.IsDisposed) {
     201        variableImactsArrayView.Content = (DoubleArray)impactArray.AsReadOnly();
     202      }
     203    }
    198204  }
    199205}
Note: See TracChangeset for help on using the changeset viewer.