[13939] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
[13963] | 26 | using System.Windows.Forms;
|
---|
[13939] | 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Random;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.Instances.DataAnalysis {
|
---|
| 32 | public class VariableNetwork : ArtificialRegressionDataDescriptor {
|
---|
| 33 | private int nTrainingSamples;
|
---|
| 34 | private int nTestSamples;
|
---|
| 35 |
|
---|
| 36 | private int numberOfFeatures;
|
---|
| 37 | private double noiseRatio;
|
---|
| 38 | private IRandom random;
|
---|
| 39 |
|
---|
| 40 | public override string Name { get { return string.Format("VariableNetwork-{0:0%} ({1} dim)", noiseRatio, numberOfFeatures); } }
|
---|
| 41 | private string networkDefinition;
|
---|
| 42 | public string NetworkDefinition { get { return networkDefinition; } }
|
---|
| 43 | public override string Description {
|
---|
| 44 | get {
|
---|
| 45 | return "The data are generated specifically to test methods for variable network analysis.";
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public VariableNetwork(int numberOfFeatures, double noiseRatio,
|
---|
| 50 | IRandom rand)
|
---|
| 51 | : this(250, 250, numberOfFeatures, noiseRatio, rand) { }
|
---|
| 52 |
|
---|
| 53 | public VariableNetwork(int nTrainingSamples, int nTestSamples,
|
---|
| 54 | int numberOfFeatures, double noiseRatio, IRandom rand) {
|
---|
| 55 | this.nTrainingSamples = nTrainingSamples;
|
---|
| 56 | this.nTestSamples = nTestSamples;
|
---|
| 57 | this.noiseRatio = noiseRatio;
|
---|
| 58 | this.random = rand;
|
---|
| 59 | this.numberOfFeatures = numberOfFeatures;
|
---|
| 60 | // default variable names
|
---|
| 61 | variableNames = Enumerable.Range(1, numberOfFeatures)
|
---|
| 62 | .Select(i => string.Format("X{0:000}", i))
|
---|
| 63 | .ToArray();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | private string[] variableNames;
|
---|
| 67 | protected override string[] VariableNames {
|
---|
| 68 | get {
|
---|
| 69 | return variableNames;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | // there is no specific target variable in variable network analysis but we still need to specify one
|
---|
| 74 | protected override string TargetVariable { get { return VariableNames.Last(); } }
|
---|
| 75 |
|
---|
| 76 | protected override string[] AllowedInputVariables {
|
---|
| 77 | get {
|
---|
| 78 | return VariableNames.Take(numberOfFeatures - 1).ToArray();
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | protected override int TrainingPartitionStart { get { return 0; } }
|
---|
| 83 | protected override int TrainingPartitionEnd { get { return nTrainingSamples; } }
|
---|
| 84 | protected override int TestPartitionStart { get { return nTrainingSamples; } }
|
---|
| 85 | protected override int TestPartitionEnd { get { return nTrainingSamples + nTestSamples; } }
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | protected override List<List<double>> GenerateValues() {
|
---|
| 89 | // var shuffledIdx = Enumerable.Range(0, numberOfFeatures).Shuffle(random).ToList();
|
---|
| 90 |
|
---|
| 91 | // variable names are shuffled in the beginning (and sorted at the end)
|
---|
| 92 | variableNames = variableNames.Shuffle(random).ToArray();
|
---|
| 93 |
|
---|
| 94 | // a third of all variables are independen vars
|
---|
| 95 | List<List<double>> lvl0 = new List<List<double>>();
|
---|
| 96 | int numLvl0 = (int)Math.Ceiling(numberOfFeatures * 0.33);
|
---|
| 97 |
|
---|
| 98 | List<string> description = new List<string>(); // store information how the variable is actually produced
|
---|
[13963] | 99 | List<string[]> inputVarNames = new List<string[]>(); // store information to produce graphviz file
|
---|
[13939] | 100 |
|
---|
| 101 | var nrand = new NormalDistributedRandom(random, 0, 1);
|
---|
| 102 | for (int c = 0; c < numLvl0; c++) {
|
---|
| 103 | var datai = Enumerable.Range(0, TestPartitionEnd).Select(_ => nrand.NextDouble()).ToList();
|
---|
[13963] | 104 | inputVarNames.Add(new string[] { });
|
---|
[13939] | 105 | description.Add("~ N(0, 1)");
|
---|
| 106 | lvl0.Add(datai);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | // lvl1 contains variables which are functions of vars in lvl0 (+ noise)
|
---|
| 110 | List<List<double>> lvl1 = new List<List<double>>();
|
---|
| 111 | int numLvl1 = (int)Math.Ceiling(numberOfFeatures * 0.33);
|
---|
| 112 | for (int c = 0; c < numLvl1; c++) {
|
---|
[13963] | 113 | string[] selectedVarNames;
|
---|
| 114 | var x = GenerateRandomFunction(random, lvl0, out selectedVarNames);
|
---|
[13939] | 115 | var sigma = x.StandardDeviation();
|
---|
| 116 | var noisePrng = new NormalDistributedRandom(random, 0, sigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio)));
|
---|
| 117 | lvl1.Add(x.Select(t => t + noisePrng.NextDouble()).ToList());
|
---|
[13963] | 118 |
|
---|
| 119 | inputVarNames.Add(selectedVarNames);
|
---|
| 120 | var desc = string.Format("f({0})", string.Join(",", selectedVarNames));
|
---|
[13939] | 121 | description.Add(string.Format(" ~ N({0}, {1:N3})", desc, noisePrng.Sigma));
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | // lvl2 contains variables which are functions of vars in lvl0 and lvl1 (+ noise)
|
---|
| 125 | List<List<double>> lvl2 = new List<List<double>>();
|
---|
| 126 | int numLvl2 = (int)Math.Ceiling(numberOfFeatures * 0.2);
|
---|
| 127 | for (int c = 0; c < numLvl2; c++) {
|
---|
[13963] | 128 | string[] selectedVarNames;
|
---|
| 129 | var x = GenerateRandomFunction(random, lvl0.Concat(lvl1).ToList(), out selectedVarNames);
|
---|
[13939] | 130 | var sigma = x.StandardDeviation();
|
---|
| 131 | var noisePrng = new NormalDistributedRandom(random, 0, sigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio)));
|
---|
| 132 | lvl2.Add(x.Select(t => t + noisePrng.NextDouble()).ToList());
|
---|
[13963] | 133 |
|
---|
| 134 | inputVarNames.Add(selectedVarNames);
|
---|
| 135 | var desc = string.Format("f({0})", string.Join(",", selectedVarNames));
|
---|
[13939] | 136 | description.Add(string.Format(" ~ N({0}, {1:N3})", desc, noisePrng.Sigma));
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | // lvl3 contains variables which are functions of vars in lvl0, lvl1 and lvl2 (+ noise)
|
---|
| 140 | List<List<double>> lvl3 = new List<List<double>>();
|
---|
| 141 | int numLvl3 = numberOfFeatures - numLvl0 - numLvl1 - numLvl2;
|
---|
| 142 | for (int c = 0; c < numLvl3; c++) {
|
---|
[13963] | 143 | string[] selectedVarNames;
|
---|
| 144 | var x = GenerateRandomFunction(random, lvl0.Concat(lvl1).Concat(lvl2).ToList(), out selectedVarNames);
|
---|
[13939] | 145 | var sigma = x.StandardDeviation();
|
---|
| 146 | var noisePrng = new NormalDistributedRandom(random, 0, sigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio)));
|
---|
| 147 | lvl3.Add(x.Select(t => t + noisePrng.NextDouble()).ToList());
|
---|
[13963] | 148 |
|
---|
| 149 | inputVarNames.Add(selectedVarNames);
|
---|
| 150 | var desc = string.Format("f({0})", string.Join(",", selectedVarNames));
|
---|
[13939] | 151 | description.Add(string.Format(" ~ N({0}, {1:N3})", desc, noisePrng.Sigma));
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[13963] | 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 + "}";
|
---|
| 165 |
|
---|
[13939] | 166 | // return a random permutation of all variables
|
---|
| 167 | var allVars = lvl0.Concat(lvl1).Concat(lvl2).Concat(lvl3).ToList();
|
---|
| 168 | var orderedVars = allVars.Zip(variableNames, Tuple.Create).OrderBy(t => t.Item2).Select(t => t.Item1).ToList();
|
---|
| 169 | variableNames = variableNames.OrderBy(n => n).ToArray();
|
---|
| 170 | return orderedVars;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | // sample the input variables that are actually used and sample from a Gaussian process
|
---|
[13963] | 174 | private IEnumerable<double> GenerateRandomFunction(IRandom rand, List<List<double>> xs, out string[] selectedVarNames) {
|
---|
[13939] | 175 | double r = -Math.Log(1.0 - rand.NextDouble()) * 2.0; // r is exponentially distributed with lambda = 2
|
---|
| 176 | int nl = (int)Math.Floor(1.5 + r); // number of selected vars is likely to be between three and four
|
---|
| 177 | if (nl > xs.Count) nl = xs.Count; // limit max
|
---|
| 178 |
|
---|
| 179 | var selectedIdx = Enumerable.Range(0, xs.Count).Shuffle(random)
|
---|
| 180 | .Take(nl).ToArray();
|
---|
| 181 |
|
---|
| 182 | var selectedVars = selectedIdx.Select(i => xs[i]).ToArray();
|
---|
[13963] | 183 | selectedVarNames = selectedIdx.Select(i => VariableNames[i]).ToArray();
|
---|
[13939] | 184 | return SampleGaussianProcess(random, selectedVars);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | private IEnumerable<double> SampleGaussianProcess(IRandom random, List<double>[] xs) {
|
---|
| 188 | int nl = xs.Length;
|
---|
| 189 | int nRows = xs.First().Count;
|
---|
| 190 | double[,] K = new double[nRows, nRows];
|
---|
| 191 |
|
---|
| 192 | // sample length-scales
|
---|
| 193 | var l = Enumerable.Range(0, nl)
|
---|
[13963] | 194 | .Select(_ => random.NextDouble() * 2 + 0.5)
|
---|
[13939] | 195 | .ToArray();
|
---|
| 196 | // calculate covariance matrix
|
---|
| 197 | for (int r = 0; r < nRows; r++) {
|
---|
| 198 | double[] xi = xs.Select(x => x[r]).ToArray();
|
---|
| 199 | for (int c = 0; c <= r; c++) {
|
---|
| 200 | double[] xj = xs.Select(x => x[c]).ToArray();
|
---|
| 201 | double dSqr = xi.Zip(xj, (xik, xjk) => (xik - xjk))
|
---|
| 202 | .Select(dk => dk * dk)
|
---|
| 203 | .Zip(l, (dk, lk) => dk / lk)
|
---|
| 204 | .Sum();
|
---|
| 205 | K[r, c] = Math.Exp(-dSqr);
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | // add a small diagonal matrix for numeric stability
|
---|
| 210 | for (int i = 0; i < nRows; i++) {
|
---|
| 211 | K[i, i] += 1.0E-7;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | // decompose
|
---|
| 215 | alglib.trfac.spdmatrixcholesky(ref K, nRows, false);
|
---|
| 216 |
|
---|
| 217 | // sample u iid ~ N(0, 1)
|
---|
| 218 | var u = Enumerable.Range(0, nRows).Select(_ => NormalDistributedRandom.NextDouble(random, 0, 1)).ToArray();
|
---|
| 219 |
|
---|
| 220 | // calc y = Lu
|
---|
| 221 | var y = new double[u.Length];
|
---|
| 222 | alglib.ablas.rmatrixmv(nRows, nRows, K, 0, 0, 0, u, 0, ref y, 0);
|
---|
| 223 |
|
---|
| 224 | return y;
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | }
|
---|