Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/GaussianProcessDemo/Form1.cs @ 12897

Last change on this file since 12897 was 10756, checked in by gkronber, 11 years ago

#1967 adaptations to work with current trunk version

File size: 4.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Globalization;
7using System.Linq;
8using System.Text;
9using System.Threading.Tasks;
10using System.Windows.Forms;
11using HeuristicLab.Algorithms.DataAnalysis;
12using HeuristicLab.Core;
13using HeuristicLab.Data;
14using HeuristicLab.Problems.DataAnalysis;
15using HeuristicLab.Problems.Instances.DataAnalysis;
16using HeuristicLab.Random;
17
18namespace 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();
31      var t = new CovarianceSquaredExponentialIso();
32      sum.Terms.Add(t);
33      //sum.Terms.Add(new CovarianceNoise());
34      this.covFunction = sum;
35      UpdateSliders();
36
37      InitData();
38      UpdateChart();
39    }
40
41    private void UpdateSliders() {
42      flowLayoutPanel1.Controls.Clear();
43      flowLayoutPanel1.Controls.Add(dataButton);
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() {
55      int n = 5000;
56      data = new List<List<double>>();
57      data.Add(Enumerable.Range(0, n).Select(e => e / 200.0).ToList());
58
59      // sample from GP
60      var normalRand = new NormalDistributedRandom(random, 0, 1);
61      alpha = (from i in Enumerable.Range(0, n)
62               select normalRand.NextDouble()).ToArray();
63    }
64
65    private void UpdateChart() {
66      var hyp = GetSliderValues();
67      var cov = covFunction.GetParameterizedCovarianceFunction(hyp, Enumerable.Range(0, data.Count));
68      var y = HeuristicLab.Problems.Instances.DataAnalysis.Util.SampleGaussianProcess(random, cov, data, alpha);
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
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);
101      //}
102    }
103
104    private double[] GetSliderValues() {
105      var hyp = new List<double>();
106      foreach (var slider in flowLayoutPanel1.Controls.OfType<TrackBar>()) {
107        Console.Write(slider.Value / 10.0 + " ");
108        hyp.Add(slider.Value / 10.0);
109      }
110      Console.WriteLine();
111
112      return hyp.ToArray();
113    }
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();
127      foreach (var p in chart1.Series[0].Points) {
128        str.AppendLine(p.XValue.ToString(CultureInfo.InvariantCulture) + "\t" + p.YValues.First().ToString(CultureInfo.InvariantCulture));
129      }
130      return str.ToString();
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.