Changeset 13963


Ignore:
Timestamp:
06/30/16 16:13:39 (16 months ago)
Author:
gkronber
Message:

#2618: added graphviz output for the variable network instances

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/VariableNetworks/VariableNetwork.cs

    r13939 r13963  
    2424using System.Linq;
    2525using System.Text;
     26using System.Windows.Forms;
    2627using HeuristicLab.Common;
    2728using HeuristicLab.Core;
     
    9697
    9798      List<string> description = new List<string>(); // store information how the variable is actually produced
     99      List<string[]> inputVarNames = new List<string[]>(); // store information to produce graphviz file
    98100
    99101      var nrand = new NormalDistributedRandom(random, 0, 1);
    100102      for (int c = 0; c < numLvl0; c++) {
    101103        var datai = Enumerable.Range(0, TestPartitionEnd).Select(_ => nrand.NextDouble()).ToList();
     104        inputVarNames.Add(new string[] { });
    102105        description.Add("~ N(0, 1)");
    103106        lvl0.Add(datai);
     
    108111      int numLvl1 = (int)Math.Ceiling(numberOfFeatures * 0.33);
    109112      for (int c = 0; c < numLvl1; c++) {
    110         string desc;
    111         var x = GenerateRandomFunction(random, lvl0, out desc);
     113        string[] selectedVarNames;
     114        var x = GenerateRandomFunction(random, lvl0, out selectedVarNames);
    112115        var sigma = x.StandardDeviation();
    113116        var noisePrng = new NormalDistributedRandom(random, 0, sigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio)));
    114117        lvl1.Add(x.Select(t => t + noisePrng.NextDouble()).ToList());
     118
     119        inputVarNames.Add(selectedVarNames);
     120        var desc = string.Format("f({0})", string.Join(",", selectedVarNames));
    115121        description.Add(string.Format(" ~ N({0}, {1:N3})", desc, noisePrng.Sigma));
    116122      }
     
    120126      int numLvl2 = (int)Math.Ceiling(numberOfFeatures * 0.2);
    121127      for (int c = 0; c < numLvl2; c++) {
    122         string desc;
    123         var x = GenerateRandomFunction(random, lvl0.Concat(lvl1).ToList(), out desc);
     128        string[] selectedVarNames;
     129        var x = GenerateRandomFunction(random, lvl0.Concat(lvl1).ToList(), out selectedVarNames);
    124130        var sigma = x.StandardDeviation();
    125131        var noisePrng = new NormalDistributedRandom(random, 0, sigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio)));
    126132        lvl2.Add(x.Select(t => t + noisePrng.NextDouble()).ToList());
     133
     134        inputVarNames.Add(selectedVarNames);
     135        var desc = string.Format("f({0})", string.Join(",", selectedVarNames));
    127136        description.Add(string.Format(" ~ N({0}, {1:N3})", desc, noisePrng.Sigma));
    128137      }
     
    132141      int numLvl3 = numberOfFeatures - numLvl0 - numLvl1 - numLvl2;
    133142      for (int c = 0; c < numLvl3; c++) {
    134         string desc;
    135         var x = GenerateRandomFunction(random, lvl0.Concat(lvl1).Concat(lvl2).ToList(), out desc);
     143        string[] selectedVarNames;
     144        var x = GenerateRandomFunction(random, lvl0.Concat(lvl1).Concat(lvl2).ToList(), out selectedVarNames);
    136145        var sigma = x.StandardDeviation();
    137146        var noisePrng = new NormalDistributedRandom(random, 0, sigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio)));
    138147        lvl3.Add(x.Select(t => t + noisePrng.NextDouble()).ToList());
     148
     149        inputVarNames.Add(selectedVarNames);
     150        var desc = string.Format("f({0})", string.Join(",", selectedVarNames));
    139151        description.Add(string.Format(" ~ N({0}, {1:N3})", desc, noisePrng.Sigma));
    140152      }
     153
     154      networkDefinition = string.Join(Environment.NewLine, variableNames.Zip(description, (n, d) => n + d));
     155      // for graphviz
     156      networkDefinition += Environment.NewLine + "digraph G {";
     157      foreach (var t in variableNames.Zip(inputVarNames, Tuple.Create).OrderBy(t => t.Item1)) {
     158        var name = t.Item1;
     159        var selectedVarNames = t.Item2;
     160        foreach (var selectedVarName in selectedVarNames) {
     161          networkDefinition += Environment.NewLine + selectedVarName + " -> " + name;
     162        }
     163      }
     164      networkDefinition += Environment.NewLine + "}";
    141165
    142166      // return a random permutation of all variables
    143167      var allVars = lvl0.Concat(lvl1).Concat(lvl2).Concat(lvl3).ToList();
    144       networkDefinition = string.Join(Environment.NewLine, variableNames.Zip(description, (n, d) => n + d));
    145168      var orderedVars = allVars.Zip(variableNames, Tuple.Create).OrderBy(t => t.Item2).Select(t => t.Item1).ToList();
    146169      variableNames = variableNames.OrderBy(n => n).ToArray();
     
    149172
    150173    // sample the input variables that are actually used and sample from a Gaussian process
    151     private IEnumerable<double> GenerateRandomFunction(IRandom rand, List<List<double>> xs, out string desc) {
    152       int nRows = xs.First().Count;
    153 
     174    private IEnumerable<double> GenerateRandomFunction(IRandom rand, List<List<double>> xs, out string[] selectedVarNames) {
    154175      double r = -Math.Log(1.0 - rand.NextDouble()) * 2.0; // r is exponentially distributed with lambda = 2
    155176      int nl = (int)Math.Floor(1.5 + r); // number of selected vars is likely to be between three and four
     
    160181
    161182      var selectedVars = selectedIdx.Select(i => xs[i]).ToArray();
    162       desc = string.Format("f({0})", string.Join(",", selectedIdx.Select(i => VariableNames[i])));
     183      selectedVarNames = selectedIdx.Select(i => VariableNames[i]).ToArray();
    163184      return SampleGaussianProcess(random, selectedVars);
    164185    }
     
    171192      // sample length-scales
    172193      var l = Enumerable.Range(0, nl)
    173         .Select(_ => random.NextDouble()*2+0.5)
     194        .Select(_ => random.NextDouble() * 2 + 0.5)
    174195        .ToArray();
    175196      // calculate covariance matrix
Note: See TracChangeset for help on using the changeset viewer.