Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/27/16 17:17:00 (9 years ago)
Author:
bburlacu
Message:

#2288: Remove TinySet.cs in favor of a more general method for generating k-combinations. Improve target variation experiment generation. Refactored code and avoided some corner case exceptions.

Location:
branches/HeuristicLab.VariableInteractionNetworks
Files:
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/DirectedGraphChart.cs

    r13789 r13806  
    9090      get { return graph; }
    9191      set {
    92         if (value == null || graph == value) return;
     92        if (value == null || graph == value || !value.Vertices.Any()) return;
    9393        graph = value;
    9494        Draw();
  • branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks.Views/3.3/RunCollectionVariableInteractionNetworkView.cs

    r13789 r13806  
    269269    }
    270270
     271    private static VariableInteractionNetwork ApplyThreshold(VariableInteractionNetwork originalNetwork, double threshold) {
     272      var arcs = originalNetwork.Arcs.Where(x => x.Weight >= threshold).ToList();
     273      if (!arcs.Any()) return originalNetwork;
     274      var filteredNetwork = new VariableInteractionNetwork();
     275      var cloner = new Cloner();
     276      var vertices = arcs.SelectMany(x => new[] { x.Source, x.Target }).Select(cloner.Clone).Distinct(); // arcs are not cloned
     277      filteredNetwork.AddVertices(vertices);
     278      filteredNetwork.AddArcs(arcs.Select(x => (IArc)x.Clone(cloner)));
     279
     280      var unusedJunctions = filteredNetwork.Vertices.Where(x => x.InDegree == 0 && x is JunctionNetworkNode).ToList();
     281      filteredNetwork.RemoveVertices(unusedJunctions);
     282      var orphanedNodes = filteredNetwork.Vertices.Where(x => x.Degree == 0).ToList();
     283      filteredNetwork.RemoveVertices(orphanedNodes);
     284      return filteredNetwork.Vertices.Any() ? filteredNetwork : originalNetwork;
     285    }
     286
    271287    private static double CalculateAverageQuality(RunCollection runs) {
    272288      var pd = (IRegressionProblemData)runs.First().Parameters["ProblemData"];
     
    374390      var tb = (TextBox)sender;
    375391      errorProvider.SetError(tb, string.Empty);
    376       var network = ApplyThreshold(variableInteractionNetwork, double.Parse(tb.Text));
     392      double impact;
     393      if (!double.TryParse(tb.Text, out impact))
     394        impact = 0.1;
     395      var network = ApplyThreshold(variableInteractionNetwork, impact);
    377396      graphChart.Graph = network;
    378     }
    379 
    380     private static VariableInteractionNetwork ApplyThreshold(VariableInteractionNetwork originalNetwork, double threshold) {
    381       var arcs = originalNetwork.Arcs.Where(x => x.Weight >= threshold).ToList();
    382       if (!arcs.Any()) return originalNetwork;
    383       var filteredNetwork = new VariableInteractionNetwork();
    384       var cloner = new Cloner();
    385       var vertices = arcs.SelectMany(x => new[] { x.Source, x.Target }).Select(cloner.Clone).Distinct(); // arcs are not cloned
    386       filteredNetwork.AddVertices(vertices);
    387       filteredNetwork.AddArcs(arcs.Select(x => (IArc)x.Clone(cloner)));
    388 
    389       var unusedJunctions = filteredNetwork.Vertices.Where(x => x.InDegree == 0 && x is JunctionNetworkNode).ToList();
    390       filteredNetwork.RemoveVertices(unusedJunctions);
    391       var orphanedNodes = filteredNetwork.Vertices.Where(x => x.Degree == 0).ToList();
    392       filteredNetwork.RemoveVertices(orphanedNodes);
    393       return filteredNetwork;
    394397    }
    395398
  • branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/CreateTargetVariationExperiment.cs

    r13728 r13806  
    2626using HeuristicLab.Optimization;
    2727using HeuristicLab.Optimizer;
     28using HeuristicLab.Problems.DataAnalysis;
    2829
    2930namespace HeuristicLab.VariableInteractionNetworks {
     
    4748    protected override void OnActiveViewChanged(object sender, EventArgs e) {
    4849      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
    49       ToolStripItem.Enabled = (activeView != null) && (activeView.Content != null) && (activeView.Content is IOptimizer) && !activeView.Locked;
     50      bool enabled = false;
     51      if (activeView != null && !activeView.Locked && activeView.Content != null) {
     52        var algorithm = activeView.Content as IAlgorithm;
     53        if (algorithm != null) {
     54          var problem = algorithm.Problem;
     55          // make sure the problem data specifies a target variable
     56          enabled = problem is IRegressionProblem || problem is IClassificationProblem;
     57        }
     58      }
     59      ToolStripItem.Enabled = enabled;
    5060    }
    5161
  • branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/CreateTargetVariationExperimentDialog.Designer.cs

    r13788 r13806  
    3333      this.cancelButton = new System.Windows.Forms.Button();
    3434      this.okButton = new System.Windows.Forms.Button();
    35       this.binomialCoeffNumericUpDown = new System.Windows.Forms.NumericUpDown();
     35      this.combinationGroupSizeNumericUpDown = new System.Windows.Forms.NumericUpDown();
    3636      this.label1 = new System.Windows.Forms.Label();
    37       this.label2 = new System.Windows.Forms.Label();
     37      this.warningLabel = new System.Windows.Forms.Label();
    3838      ((System.ComponentModel.ISupportInitialize)(this.repetitionsNumericUpDown)).BeginInit();
    39       ((System.ComponentModel.ISupportInitialize)(this.binomialCoeffNumericUpDown)).BeginInit();
     39      ((System.ComponentModel.ISupportInitialize)(this.combinationGroupSizeNumericUpDown)).BeginInit();
    4040      this.SuspendLayout();
    4141      //
     
    9898      this.okButton.Click += new System.EventHandler(this.okButton_Click);
    9999      //
    100       // binomialCoeffNumericUpDown
     100      // combinationGroupSizeNumericUpDown
    101101      //
    102       this.binomialCoeffNumericUpDown.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
     102      this.combinationGroupSizeNumericUpDown.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
    103103            | System.Windows.Forms.AnchorStyles.Right)));
    104       this.binomialCoeffNumericUpDown.Location = new System.Drawing.Point(128, 38);
    105       this.binomialCoeffNumericUpDown.Maximum = new decimal(new int[] {
     104      this.combinationGroupSizeNumericUpDown.Location = new System.Drawing.Point(128, 38);
     105      this.combinationGroupSizeNumericUpDown.Maximum = new decimal(new int[] {
    106106            1000000,
    107107            0,
    108108            0,
    109109            0});
    110       this.binomialCoeffNumericUpDown.Minimum = new decimal(new int[] {
     110      this.combinationGroupSizeNumericUpDown.Minimum = new decimal(new int[] {
    111111            1,
    112112            0,
    113113            0,
    114114            0});
    115       this.binomialCoeffNumericUpDown.Name = "binomialCoeffNumericUpDown";
    116       this.binomialCoeffNumericUpDown.Size = new System.Drawing.Size(185, 20);
    117       this.binomialCoeffNumericUpDown.TabIndex = 4;
    118       this.binomialCoeffNumericUpDown.ThousandsSeparator = true;
    119       this.binomialCoeffNumericUpDown.Value = new decimal(new int[] {
     115      this.combinationGroupSizeNumericUpDown.Name = "combinationGroupSizeNumericUpDown";
     116      this.combinationGroupSizeNumericUpDown.Size = new System.Drawing.Size(185, 20);
     117      this.combinationGroupSizeNumericUpDown.TabIndex = 4;
     118      this.combinationGroupSizeNumericUpDown.ThousandsSeparator = true;
     119      this.combinationGroupSizeNumericUpDown.Value = new decimal(new int[] {
    120120            10,
    121121            0,
    122122            0,
    123123            0});
    124       this.binomialCoeffNumericUpDown.Validated += new System.EventHandler(this.binomialCoefficientNumericUpDown_Validated);
     124      this.combinationGroupSizeNumericUpDown.ValueChanged += new System.EventHandler(this.combinationGroupSizeNumericUpDown_Validated);
     125      this.combinationGroupSizeNumericUpDown.Validating += new System.ComponentModel.CancelEventHandler(this.combinationGroupSizeNumericUpDown_Validating);
     126      this.combinationGroupSizeNumericUpDown.Validated += new System.EventHandler(this.combinationGroupSizeNumericUpDown_Validated);
    125127      //
    126128      // label1
     
    129131      this.label1.Location = new System.Drawing.Point(26, 40);
    130132      this.label1.Name = "label1";
    131       this.label1.Size = new System.Drawing.Size(79, 13);
     133      this.label1.Size = new System.Drawing.Size(60, 13);
    132134      this.label1.TabIndex = 5;
    133       this.label1.Text = "&Binomial coeff.:";
     135      this.label1.Text = "&Group size:";
    134136      //
    135       // label2
     137      // warningLabel
    136138      //
    137       this.label2.ForeColor = System.Drawing.Color.Red;
    138       this.label2.Location = new System.Drawing.Point(26, 67);
    139       this.label2.Name = "label2";
    140       this.label2.Size = new System.Drawing.Size(287, 50);
    141       this.label2.TabIndex = 5;
    142       this.label2.Text = "Warning: depending on the binomial coefficient and the number of variables, this " +
    143     "can create very large experiments. ";
     139      this.warningLabel.ForeColor = System.Drawing.Color.Red;
     140      this.warningLabel.Location = new System.Drawing.Point(26, 67);
     141      this.warningLabel.Name = "warningLabel";
     142      this.warningLabel.Size = new System.Drawing.Size(287, 50);
     143      this.warningLabel.TabIndex = 5;
     144      this.warningLabel.TextAlign = System.Drawing.ContentAlignment.TopCenter;
    144145      //
    145146      // CreateTargetVariationExperimentDialog
     
    152153      this.Controls.Add(this.okButton);
    153154      this.Controls.Add(this.cancelButton);
    154       this.Controls.Add(this.label2);
     155      this.Controls.Add(this.warningLabel);
    155156      this.Controls.Add(this.label1);
    156157      this.Controls.Add(this.repetitionsLabel);
    157       this.Controls.Add(this.binomialCoeffNumericUpDown);
     158      this.Controls.Add(this.combinationGroupSizeNumericUpDown);
    158159      this.Controls.Add(this.repetitionsNumericUpDown);
    159160      this.Name = "CreateTargetVariationExperimentDialog";
    160161      this.Text = "Create Target Variation Experiment";
    161162      ((System.ComponentModel.ISupportInitialize)(this.repetitionsNumericUpDown)).EndInit();
    162       ((System.ComponentModel.ISupportInitialize)(this.binomialCoeffNumericUpDown)).EndInit();
     163      ((System.ComponentModel.ISupportInitialize)(this.combinationGroupSizeNumericUpDown)).EndInit();
    163164      this.ResumeLayout(false);
    164165      this.PerformLayout();
     
    172173        private System.Windows.Forms.Button cancelButton;
    173174        private System.Windows.Forms.Button okButton;
    174     private System.Windows.Forms.NumericUpDown binomialCoeffNumericUpDown;
     175    private System.Windows.Forms.NumericUpDown combinationGroupSizeNumericUpDown;
    175176    private System.Windows.Forms.Label label1;
    176     private System.Windows.Forms.Label label2;
     177    private System.Windows.Forms.Label warningLabel;
    177178  }
    178179}
  • branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/CreateTargetVariationExperimentDialog.cs

    r13788 r13806  
    2424using System.Linq;
    2525using System.Windows.Forms;
     26using HeuristicLab.Common;
    2627using HeuristicLab.MainForm;
    2728using HeuristicLab.Optimization;
     
    3233    public Experiment Experiment { get; private set; }
    3334    private int repetitions;
    34     private int binomialCoefficient;
     35    private int combinationGroupSize;
    3536
    3637    public CreateTargetVariationExperimentDialog() : this(null) { }
     
    3839      InitializeComponent();
    3940      repetitions = (int)repetitionsNumericUpDown.Value;
     41      combinationGroupSize = (int)combinationGroupSizeNumericUpDown.Value;
     42
     43      var problem = alg.Problem as IDataAnalysisProblem;
     44      var inputCount = problem.ProblemData.AllowedInputVariables.Count();
     45      if (combinationGroupSize > inputCount)
     46        combinationGroupSize = inputCount;
     47      warningLabel.Text = string.Format("Warning: this will create {0} combinations.", EnumerableExtensions.BinomialCoefficient(inputCount, combinationGroupSize));
    4048
    4149      Experiment = null;
    4250      okButton.Enabled = alg != null;
    43     }
    44 
    45     private void repetitionsNumericUpDown_Validated(object sender, EventArgs e) {
    46       if (repetitionsNumericUpDown.Text == string.Empty)
    47         repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
    48       repetitions = (int)repetitionsNumericUpDown.Value;
    49     }
    50 
    51     private void binomialCoefficientNumericUpDown_Validated(object sender, EventArgs e) {
    52       if (binomialCoeffNumericUpDown.Text == string.Empty)
    53         binomialCoeffNumericUpDown.Text = binomialCoeffNumericUpDown.Value.ToString();
    54       binomialCoefficient = (int)binomialCoeffNumericUpDown.Value;
    5551    }
    5652
     
    6157        throw new ArgumentNullException("The algorithm parameter must be a valid IAlgorithm instance.");
    6258
    63       var regressionProblem = algorithm.Problem as IRegressionProblem;
    64       if (regressionProblem == null)
    65         throw new ArgumentNullException("The algorithm does not contain a valid problem instance.");
     59      var problem = (IDataAnalysisProblem)algorithm.Problem;
     60      var problemData = problem.ProblemData;
    6661
    67       var problemData = regressionProblem.ProblemData;
    68       var variables = problemData.Dataset.DoubleVariables.ToList();
     62      var variables = problemData.AllowedInputVariables.ToList();
    6963
    7064      for (int i = 0; i < variables.Count; i++) {
    7165        var target = variables[i];
    7266        var inputs = variables.Where(x => x != target).ToList();
    73         var combinations = Util.Combinations(inputs, binomialCoefficient);
     67        if (combinationGroupSize > inputs.Count)
     68          combinationGroupSize = inputs.Count;
     69
     70        var combinations = inputs.Combinations(combinationGroupSize);
    7471
    7572        foreach (var combination in combinations) {
    7673          var h = new HashSet<string>(combination);
    7774          var alg = (IAlgorithm)algorithm.Clone();
    78           var pd = ((IRegressionProblem)alg.Problem).ProblemData;
    79           pd.TargetVariable = target;
    80           alg.Name += "{Target = " + target + ", Inputs = (" + combination.Aggregate((a, b) => a + "," + b) + ")}";
    81 
    82           foreach (var item in pd.InputVariables) {
    83             pd.InputVariables.SetItemCheckedState(item, h.Contains(item.Value));
     75          alg.Name += " {Target = " + target + ", Inputs = (" + combination.Aggregate((a, b) => a + "," + b) + ")}";
     76          problem = (IDataAnalysisProblem)alg.Problem;
     77          problemData = problem.ProblemData;
     78          foreach (var item in problemData.InputVariables) {
     79            problemData.InputVariables.SetItemCheckedState(item, h.Contains(item.Value));
    8480          }
     81          SetTargetName(problemData, target);
    8582
    8683          var batchrun = new BatchRun(string.Format("Batchrun {0}", i + 1)) {
     
    9592    }
    9693
     94    private static string GetTargetName(IDataAnalysisProblemData problemData) {
     95      var regressionProblemData = problemData as IRegressionProblemData;
     96      if (regressionProblemData != null)
     97        return regressionProblemData.TargetVariable;
     98      var classificationProblemData = problemData as IClassificationProblemData;
     99      if (classificationProblemData != null)
     100        return classificationProblemData.TargetVariable;
     101      throw new ArgumentException("The provided argument must be either a regression or classification problem data.");
     102    }
     103
     104    private static void SetTargetName(IDataAnalysisProblemData problemData, string targetName) {
     105      var regressionProblemData = problemData as IRegressionProblemData;
     106      if (regressionProblemData != null) {
     107        regressionProblemData.TargetVariable = targetName;
     108        return;
     109      }
     110      var classificationProblemData = problemData as IClassificationProblemData;
     111      if (classificationProblemData != null) {
     112        classificationProblemData.TargetVariable = targetName;
     113        return;
     114      }
     115      throw new ArgumentException("The provided argument must be either a regression or classification problem data.");
     116    }
     117
     118    #region events
    97119    private void okButton_Click(object sender, EventArgs e) {
    98120      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
    99       if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IOptimizer) && !activeView.Locked) {
     121      if ((activeView != null) && (activeView.Content != null) && (activeView.Content is IAlgorithm) && !activeView.Locked) {
    100122        Experiment = CreateVariableCombinations((IAlgorithm)activeView.Content);
    101123        DialogResult = DialogResult.OK;
     
    103125      }
    104126    }
     127
     128    private void combinationGroupSizeNumericUpDown_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
     129      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
     130      if (activeView == null) return;
     131      var algorithm = (IAlgorithm)activeView.Content;
     132      var problem = algorithm.Problem as IDataAnalysisProblem;
     133      if (problem == null) return;
     134      var inputCount = problem.ProblemData.AllowedInputVariables.Count();
     135      if (combinationGroupSizeNumericUpDown.Value > inputCount)
     136        combinationGroupSizeNumericUpDown.Value = inputCount;
     137    }
     138
     139    private void repetitionsNumericUpDown_Validated(object sender, EventArgs e) {
     140      if (repetitionsNumericUpDown.Text == string.Empty)
     141        repetitionsNumericUpDown.Text = repetitionsNumericUpDown.Value.ToString();
     142      repetitions = (int)repetitionsNumericUpDown.Value;
     143    }
     144
     145    private void combinationGroupSizeNumericUpDown_Validated(object sender, EventArgs e) {
     146      if (combinationGroupSizeNumericUpDown.Text == string.Empty)
     147        combinationGroupSizeNumericUpDown.Text = combinationGroupSizeNumericUpDown.Value.ToString();
     148      combinationGroupSize = (int)combinationGroupSizeNumericUpDown.Value;
     149      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
     150      if (activeView == null) return;
     151      var algorithm = (IAlgorithm)activeView.Content;
     152      var problem = (IDataAnalysisProblem)algorithm.Problem;
     153      var inputCount = problem.ProblemData.AllowedInputVariables.Count();
     154      warningLabel.Text = string.Format("Warning: this will create {0} combinations.", EnumerableExtensions.BinomialCoefficient(inputCount, combinationGroupSize));
     155    }
     156    #endregion
    105157  }
    106158}
  • branches/HeuristicLab.VariableInteractionNetworks/HeuristicLab.VariableInteractionNetworks/3.3/HeuristicLab.VariableInteractionNetworks-3.3.csproj

    r13789 r13806  
    166166    <Compile Include="Properties\AssemblyInfo.cs" />
    167167    <Compile Include="SymbolicDataAnalysisVariableImpactsAnalyzer.cs" />
    168     <Compile Include="TinySet.cs" />
    169168    <Compile Include="VariableInteractionNetwork.cs" />
    170169  </ItemGroup>
Note: See TracChangeset for help on using the changeset viewer.