[9124] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.ComponentModel;
|
---|
| 4 | using System.Data;
|
---|
| 5 | using System.Drawing;
|
---|
[10422] | 6 | using System.Globalization;
|
---|
[9124] | 7 | using System.Linq;
|
---|
| 8 | using System.Text;
|
---|
| 9 | using System.Threading.Tasks;
|
---|
| 10 | using System.Windows.Forms;
|
---|
| 11 | using HeuristicLab.Algorithms.DataAnalysis;
|
---|
| 12 | using HeuristicLab.Core;
|
---|
[9338] | 13 | using HeuristicLab.Data;
|
---|
[9124] | 14 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 15 | using HeuristicLab.Problems.Instances.DataAnalysis;
|
---|
| 16 | using HeuristicLab.Random;
|
---|
| 17 |
|
---|
| 18 | namespace GaussianProcessDemo {
|
---|
| 19 | public partial class Form1 : Form {
|
---|
| 20 | private IRandom random;
|
---|
| 21 | private ICovarianceFunction covFunction;
|
---|
| 22 | private List<List<double>> data;
|
---|
| 23 | private double[] alpha;
|
---|
| 24 |
|
---|
| 25 |
|
---|
| 26 | public Form1() {
|
---|
| 27 | InitializeComponent();
|
---|
| 28 | this.random = new MersenneTwister();
|
---|
| 29 |
|
---|
| 30 | var sum = new CovarianceSum();
|
---|
[10422] | 31 | var t = new CovarianceSquaredExponentialIso();
|
---|
[9338] | 32 | sum.Terms.Add(t);
|
---|
[10422] | 33 | //sum.Terms.Add(new CovarianceNoise());
|
---|
[9124] | 34 | this.covFunction = sum;
|
---|
| 35 | UpdateSliders();
|
---|
| 36 |
|
---|
| 37 | InitData();
|
---|
| 38 | UpdateChart();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | private void UpdateSliders() {
|
---|
| 42 | flowLayoutPanel1.Controls.Clear();
|
---|
[9338] | 43 | flowLayoutPanel1.Controls.Add(dataButton);
|
---|
[9124] | 44 | for (int i = 0; i < covFunction.GetNumberOfParameters(1); i++) {
|
---|
| 45 | var sliderControl = new TrackBar();
|
---|
| 46 | sliderControl.Minimum = -50;
|
---|
| 47 | sliderControl.Maximum = 50;
|
---|
| 48 | sliderControl.Value = 0;
|
---|
| 49 | sliderControl.ValueChanged += (sender, args) => UpdateChart();
|
---|
| 50 | flowLayoutPanel1.Controls.Add(sliderControl);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | private void InitData() {
|
---|
[10422] | 55 | int n = 5000;
|
---|
[9124] | 56 | data = new List<List<double>>();
|
---|
[10422] | 57 | data.Add(Enumerable.Range(0, n).Select(e => e / 200.0).ToList());
|
---|
[9124] | 58 |
|
---|
| 59 | // sample from GP
|
---|
| 60 | var normalRand = new NormalDistributedRandom(random, 0, 1);
|
---|
[10422] | 61 | alpha = (from i in Enumerable.Range(0, n)
|
---|
[9124] | 62 | select normalRand.NextDouble()).ToArray();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | private void UpdateChart() {
|
---|
| 66 | var hyp = GetSliderValues();
|
---|
[9387] | 67 | var cov = covFunction.GetParameterizedCovarianceFunction(hyp, Enumerable.Range(0, data.Count));
|
---|
[10756] | 68 | var y = HeuristicLab.Problems.Instances.DataAnalysis.Util.SampleGaussianProcess(random, cov, data, alpha);
|
---|
[9124] | 69 |
|
---|
| 70 | chart1.Series[0].Points.Clear();
|
---|
| 71 | foreach (var p in y.Zip(data[0], (t, x) => new { t, x })) {
|
---|
| 72 | chart1.Series[0].Points.AddXY(p.x, p.t);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[10422] | 75 | //var trainingData = new List<List<double>>();
|
---|
| 76 | //var trainingIndices = RandomEnumerable.SampleRandomWithoutRepetition(Enumerable.Range(0, y.Count), random, 10);
|
---|
| 77 | //var trainingY = trainingIndices.Select(i => y[i]).ToList();
|
---|
| 78 | //var trainingX = trainingIndices.Select(i => data[0][i]).ToList();
|
---|
| 79 | //trainingData.Add(trainingY);
|
---|
| 80 | //trainingData.Add(trainingX);
|
---|
| 81 | //
|
---|
| 82 | ////chart1.Series[2].Points.Clear();
|
---|
| 83 | ////foreach (var p in trainingY.Zip(trainingX, (t, x) => new { t, x })) {
|
---|
| 84 | //// chart1.Series[2].Points.AddXY(p.x, p.t);
|
---|
| 85 | ////}
|
---|
| 86 | //
|
---|
| 87 | //var allData = new List<List<double>>();
|
---|
| 88 | //allData.Add(y);
|
---|
| 89 | //allData.Add(data[0]);
|
---|
| 90 | //var variableNames = new string[] { "y", "x" };
|
---|
| 91 | //var fullDataSet = new Dataset(variableNames, allData);
|
---|
| 92 | //var trainingDataSet = new Dataset(variableNames, trainingData);
|
---|
| 93 | //var trainingRows = Enumerable.Range(0, trainingIndices.Count());
|
---|
| 94 | //var fullRows = Enumerable.Range(0, data[0].Count);
|
---|
| 95 | //var correctModel = new GaussianProcessModel(fullDataSet, variableNames.First(), variableNames.Skip(1), fullRows, hyp, new MeanZero(),
|
---|
| 96 | // (ICovarianceFunction)covFunction.Clone());
|
---|
| 97 | //var yPred = correctModel.GetEstimatedValues(fullDataSet, fullRows);
|
---|
| 98 | //chart1.Series[1].Points.Clear();
|
---|
| 99 | //foreach (var p in yPred.Zip(data[0], (t, x) => new { t, x })) {
|
---|
| 100 | // chart1.Series[1].Points.AddXY(p.x, p.t);
|
---|
[9338] | 101 | //}
|
---|
[9124] | 102 | }
|
---|
| 103 |
|
---|
| 104 | private double[] GetSliderValues() {
|
---|
| 105 | var hyp = new List<double>();
|
---|
[9338] | 106 | foreach (var slider in flowLayoutPanel1.Controls.OfType<TrackBar>()) {
|
---|
[9124] | 107 | Console.Write(slider.Value / 10.0 + " ");
|
---|
| 108 | hyp.Add(slider.Value / 10.0);
|
---|
| 109 | }
|
---|
| 110 | Console.WriteLine();
|
---|
| 111 |
|
---|
| 112 | return hyp.ToArray();
|
---|
| 113 | }
|
---|
[9338] | 114 |
|
---|
| 115 | private void dataButton_Click(object sender, EventArgs e) {
|
---|
| 116 | var dataForm = new Form();
|
---|
| 117 | var dataTextField = new TextBox();
|
---|
| 118 | dataTextField.Multiline = true;
|
---|
| 119 | dataTextField.Text = DataToText();
|
---|
| 120 | dataTextField.Dock = DockStyle.Fill;
|
---|
| 121 | dataForm.Controls.Add(dataTextField);
|
---|
| 122 | dataForm.ShowDialog();
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | private string DataToText() {
|
---|
| 126 | var str = new StringBuilder();
|
---|
[10422] | 127 | foreach (var p in chart1.Series[0].Points) {
|
---|
| 128 | str.AppendLine(p.XValue.ToString(CultureInfo.InvariantCulture) + "\t" + p.YValues.First().ToString(CultureInfo.InvariantCulture));
|
---|
[9338] | 129 | }
|
---|
| 130 | return str.ToString();
|
---|
| 131 | }
|
---|
[9124] | 132 | }
|
---|
| 133 | }
|
---|