Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16295


Ignore:
Timestamp:
11/13/18 15:22:18 (6 years ago)
Author:
bburlacu
Message:

#2288: Refactor code (use HL impacts calculator instead of manually calculating impacts, various fixes and improvements)

Location:
branches/2288_HeuristicLab.VariableInteractionNetworks
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2288_HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/RunCollectionVariableInteractionNetworkView.Designer.cs

    r14275 r16295  
    2626      this.components = new System.ComponentModel.Container();
    2727      this.settingsGroupBox = new System.Windows.Forms.GroupBox();
     28      this.impactThresholdLabel = new System.Windows.Forms.Label();
    2829      this.impactThresholdTrackBar = new System.Windows.Forms.TrackBar();
    2930      this.exportImpactsMatrixButton = new System.Windows.Forms.Button();
     
    3839      this.label1 = new System.Windows.Forms.Label();
    3940      this.layoutOptionsGroupBox = new System.Windows.Forms.GroupBox();
     41      this.relayoutGraphButton = new System.Windows.Forms.Button();
    4042      this.idealEdgeLengthTextBox = new System.Windows.Forms.TextBox();
    4143      this.edgeRoutingComboBox = new System.Windows.Forms.ComboBox();
     
    4345      this.label3 = new System.Windows.Forms.Label();
    4446      this.errorProvider = new System.Windows.Forms.ErrorProvider(this.components);
    45       this.impactThresholdLabel = new System.Windows.Forms.Label();
    46       this.relayoutGraphButton = new System.Windows.Forms.Button();
    4747      this.graphChart = new HeuristicLab.VariableInteractionNetworks.Views.DirectedGraphChart();
    4848      this.settingsGroupBox.SuspendLayout();
     
    5454      // settingsGroupBox
    5555      //
    56       this.settingsGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     56      this.settingsGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    5757            | System.Windows.Forms.AnchorStyles.Left)));
    5858      this.settingsGroupBox.Controls.Add(this.impactThresholdLabel);
     
    7575      this.settingsGroupBox.Text = "Network Configuration";
    7676      //
     77      // impactThresholdLabel
     78      //
     79      this.impactThresholdLabel.Location = new System.Drawing.Point(118, 92);
     80      this.impactThresholdLabel.Name = "impactThresholdLabel";
     81      this.impactThresholdLabel.Size = new System.Drawing.Size(204, 23);
     82      this.impactThresholdLabel.TabIndex = 8;
     83      this.impactThresholdLabel.Text = "0";
     84      this.impactThresholdLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
     85      //
    7786      // impactThresholdTrackBar
    7887      //
     
    186195      // layoutOptionsGroupBox
    187196      //
    188       this.layoutOptionsGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    189             | System.Windows.Forms.AnchorStyles.Left)));
     197      this.layoutOptionsGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
    190198      this.layoutOptionsGroupBox.Controls.Add(this.relayoutGraphButton);
    191199      this.layoutOptionsGroupBox.Controls.Add(this.idealEdgeLengthTextBox);
     
    193201      this.layoutOptionsGroupBox.Controls.Add(this.label4);
    194202      this.layoutOptionsGroupBox.Controls.Add(this.label3);
    195       this.layoutOptionsGroupBox.Location = new System.Drawing.Point(4, 245);
     203      this.layoutOptionsGroupBox.Location = new System.Drawing.Point(4, 360);
    196204      this.layoutOptionsGroupBox.Name = "layoutOptionsGroupBox";
    197       this.layoutOptionsGroupBox.Size = new System.Drawing.Size(331, 217);
     205      this.layoutOptionsGroupBox.Size = new System.Drawing.Size(331, 102);
    198206      this.layoutOptionsGroupBox.TabIndex = 2;
    199207      this.layoutOptionsGroupBox.TabStop = false;
    200208      this.layoutOptionsGroupBox.Text = "Layout Options";
     209      //
     210      // relayoutGraphButton
     211      //
     212      this.relayoutGraphButton.Location = new System.Drawing.Point(10, 69);
     213      this.relayoutGraphButton.Name = "relayoutGraphButton";
     214      this.relayoutGraphButton.Size = new System.Drawing.Size(149, 23);
     215      this.relayoutGraphButton.TabIndex = 4;
     216      this.relayoutGraphButton.Text = "Recalculate Layout";
     217      this.relayoutGraphButton.UseVisualStyleBackColor = true;
     218      this.relayoutGraphButton.Click += new System.EventHandler(this.relayoutGraphButton_Click);
    201219      //
    202220      // idealEdgeLengthTextBox
     
    246264      this.errorProvider.ContainerControl = this;
    247265      //
    248       // impactThresholdLabel
    249       //
    250       this.impactThresholdLabel.Location = new System.Drawing.Point(118, 92);
    251       this.impactThresholdLabel.Name = "impactThresholdLabel";
    252       this.impactThresholdLabel.Size = new System.Drawing.Size(204, 23);
    253       this.impactThresholdLabel.TabIndex = 8;
    254       this.impactThresholdLabel.Text = "0";
    255       this.impactThresholdLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
    256       //
    257       // relayoutGraphButton
    258       //
    259       this.relayoutGraphButton.Location = new System.Drawing.Point(10, 69);
    260       this.relayoutGraphButton.Name = "relayoutGraphButton";
    261       this.relayoutGraphButton.Size = new System.Drawing.Size(149, 23);
    262       this.relayoutGraphButton.TabIndex = 4;
    263       this.relayoutGraphButton.Text = "Recalculate Layout";
    264       this.relayoutGraphButton.UseVisualStyleBackColor = true;
    265       this.relayoutGraphButton.Click += new System.EventHandler(this.relayoutGraphButton_Click);
    266       //
    267266      // graphChart
    268267      //
    269       this.graphChart.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
    270             | System.Windows.Forms.AnchorStyles.Left)
     268      this.graphChart.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
     269            | System.Windows.Forms.AnchorStyles.Left) 
    271270            | System.Windows.Forms.AnchorStyles.Right)));
    272271      this.graphChart.BackColor = System.Drawing.SystemColors.Control;
     272      this.graphChart.DefaultEdgeLength = 50D;
    273273      this.graphChart.Graph = null;
    274       this.graphChart.DefaultEdgeLength = 50D;
    275274      this.graphChart.Location = new System.Drawing.Point(341, 4);
    276275      this.graphChart.Mode = null;
  • branches/2288_HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/RunCollectionVariableInteractionNetworkView.cs

    r15421 r16295  
    111111      var targetImpacts = new Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>>();
    112112
     113      var groups = runs.GroupBy(run => {
     114        var sol = (IRegressionSolution)run.Results.Values.Single(x => x is IRegressionSolution);
     115        return Concatenate(sol.ProblemData.AllowedInputVariables) + sol.ProblemData.TargetVariable;
     116      });
     117
    113118      if (useBest) {
    114119        // build network using only the best run for each target
     120        foreach (var group in groups) {
     121          var solutions = group.Select(run => Tuple.Create(run, (IRegressionSolution)run.Results.Values.Single(sol => sol is IRegressionSolution)));
     122          var best = solutions.OrderBy(x => x.Item2.TrainingRSquared).Last();
     123          var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(best.Item2, RegressionSolutionVariableImpactsCalculator.DataPartitionEnum.All, RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum.Shuffle).ToDictionary(x => x.Item1, x => x.Item2);
     124
     125          targetImpacts[best.Item2.ProblemData.TargetVariable] = Tuple.Create(new[] { best.Item1 }.AsEnumerable(), impacts);
     126        }
    115127      } else {
    116         var groups = runs.GroupBy(run => {
    117           var sol = (IRegressionSolution)run.Results.Values.Single(x => x is IRegressionSolution);
    118           return Concatenate(sol.ProblemData.AllowedInputVariables) + sol.ProblemData.TargetVariable;
    119         });
    120 
    121128        foreach (var group in groups) {
    122129          // calculate average impacts
     
    131138            }
    132139            var md = dataset.ToModifiable();
    133             var impacts = CalculateImpacts(sol, md, originalValues, medians, estimationLimits);
    134             foreach (var pair in impacts) {
    135               if (averageImpacts.ContainsKey(pair.Key))
    136                 averageImpacts[pair.Key] += pair.Value;
     140
     141            var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(sol, RegressionSolutionVariableImpactsCalculator.DataPartitionEnum.All, RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum.Shuffle);
     142            foreach (var t in impacts) {
     143              if (averageImpacts.ContainsKey(t.Item1))
     144                averageImpacts[t.Item1] += t.Item2;
    137145              else {
    138                 averageImpacts[pair.Key] = pair.Value;
     146                averageImpacts[t.Item1] = t.Item2;
    139147              }
    140148            }
    141149          }
     150
    142151          var count = group.Count();
    143           var keys = averageImpacts.Keys.ToList();
    144           foreach (var v in keys) {
     152          foreach (var v in averageImpacts.Keys) {
    145153            averageImpacts[v] /= count;
    146154          }
    147155
    148           targetImpacts[solution.ProblemData.TargetVariable] = new Tuple<IEnumerable<IRun>, Dictionary<string, double>>(group, averageImpacts);
     156          targetImpacts[solution.ProblemData.TargetVariable] = Tuple.Create(group.AsEnumerable(), averageImpacts);
    149157        }
    150158      }
    151159      return targetImpacts;
    152     }
    153 
    154     private static Dictionary<string, double> CalculateImpacts(IRegressionSolution solution, ModifiableDataset dataset,
    155       Dictionary<string, List<double>> originalValues, Dictionary<string, List<double>> medianValues, DoubleLimit estimationLimits = null) {
    156       var impacts = new Dictionary<string, double>();
    157 
    158       var model = solution.Model;
    159       var pd = solution.ProblemData;
    160 
    161       var rows = pd.TrainingIndices.ToList();
    162       var targetValues = pd.Dataset.GetDoubleValues(pd.TargetVariable, rows).ToList();
    163 
    164 
    165       foreach (var v in pd.AllowedInputVariables) {
    166         dataset.ReplaceVariable(v, medianValues[v]);
    167 
    168         var estimatedValues = model.GetEstimatedValues(dataset, rows);
    169         if (estimationLimits != null)
    170           estimatedValues = estimatedValues.LimitToRange(estimationLimits.Lower, estimationLimits.Upper);
    171 
    172         OnlineCalculatorError error;
    173         var r = OnlinePearsonsRCalculator.Calculate(targetValues, estimatedValues, out error);
    174         var newQuality = error == OnlineCalculatorError.None ? r * r : double.NaN;
    175         var originalQuality = solution.TrainingRSquared;
    176         impacts[v] = originalQuality - newQuality;
    177 
    178         dataset.ReplaceVariable(v, originalValues[v]);
    179       }
    180       return impacts;
    181160    }
    182161
    183162    public static Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>> CalculateVariableImpactsFromRunResults(RunCollection runs,
    184163      string qualityResultName, bool maximization, string impactsResultName, bool useBestRunsPerTarget = false) {
    185       var targets = runs.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList();
     164
     165      Func<IRun, double> getQuality = run => ((DoubleValue)run.Results[qualityResultName]).Value;
     166      var targetGroups = runs.GroupBy(x => ((IRegressionProblemData)x.Parameters["ProblemData"]).TargetVariable).ToList();
    186167      var targetImpacts = new Dictionary<string, Tuple<IEnumerable<IRun>, Dictionary<string, double>>>();
    187168
    188       Func<IRun, double> getQualityValue = run => ((DoubleValue)run.Results[qualityResultName]).Value;
    189 
    190169      if (useBestRunsPerTarget) {
    191         var bestRunsPerTarget = maximization
    192           ? targets.Select(x => x.OrderBy(getQualityValue).Last())
    193           : targets.Select(x => x.OrderBy(getQualityValue).First());
    194 
    195         foreach (var run in bestRunsPerTarget) {
    196           var pd = (IRegressionProblemData)run.Parameters["ProblemData"];
    197           var target = pd.TargetVariable;
    198           var impacts = (DoubleMatrix)run.Results[impactsResultName];
    199           targetImpacts[target] = new Tuple<IEnumerable<IRun>, Dictionary<string, double>>(new[] { run }, impacts.RowNames.Select((x, i) => new { Name = x, Index = i }).ToDictionary(x => x.Name, x => impacts[x.Index, 0]));
     170        foreach (var group in targetGroups) {
     171          var ordered = group.OrderBy(getQuality);
     172          var best = maximization ? ordered.Last() : ordered.First();
     173          var pd = (IRegressionProblemData)best.Parameters["ProblemData"];
     174          var target = group.Key;
     175          var impacts = (DoubleMatrix)best.Results[impactsResultName];
     176          targetImpacts[target] = Tuple.Create((IEnumerable<IRun>)new[] { best }, impacts.RowNames.Select((x, i) => new { x, i }).ToDictionary(x => x.x, x => impacts[x.i, 0]));
    200177        }
    201178      } else {
    202         foreach (var target in targets) {
     179        foreach (var target in targetGroups) {
    203180          var averageImpacts = CalculateAverageImpacts(new RunCollection(target), impactsResultName);
    204181          targetImpacts[target.Key] = new Tuple<IEnumerable<IRun>, Dictionary<string, double>>(target, averageImpacts);
     
    460437      worker.DoWork += (o, e) => {
    461438        ControlsEnable(false);
    462         var impacts = CalculateVariableImpactsOnline(Content, false);
     439        var impacts = CalculateVariableImpactsOnline(Content, impactAggregationComboBox.SelectedIndex == 0);
    463440        variableInteractionNetwork = CreateNetwork(impacts);
    464441        var threshold = impactThresholdTrackBar.Minimum + (double)impactThresholdTrackBar.Value / impactThresholdTrackBar.Maximum;
  • branches/2288_HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/HeuristicLab.VariableInteractionNetworks-3.3.csproj

    r15929 r16295  
    111111      <SpecificVersion>False</SpecificVersion>
    112112      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.dll</HintPath>
    113       <Private>False</Private>
    114     </Reference>
    115     <Reference Include="HeuristicLab.MainForm-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    116       <SpecificVersion>False</SpecificVersion>
    117       <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.MainForm-3.3.dll</HintPath>
    118       <Private>False</Private>
    119     </Reference>
    120     <Reference Include="HeuristicLab.MainForm.WindowsForms-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
    121       <SpecificVersion>False</SpecificVersion>
    122       <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.MainForm.WindowsForms-3.3.dll</HintPath>
    123113      <Private>False</Private>
    124114    </Reference>
Note: See TracChangeset for help on using the changeset viewer.