Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9124 was 9124, checked in by gkronber, 12 years ago

#1967 implemented utility app to draw random samples using a GP prior

File size: 3.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows.Forms;
10using HeuristicLab.Algorithms.DataAnalysis;
11using HeuristicLab.Core;
12using HeuristicLab.Problems.DataAnalysis;
13using HeuristicLab.Problems.Instances.DataAnalysis;
14using HeuristicLab.Random;
15
16namespace GaussianProcessDemo {
17  public partial class Form1 : Form {
18    private IRandom random;
19    private ICovarianceFunction covFunction;
20    private List<List<double>> data;
21    private double[] alpha;
22
23
24    public Form1() {
25      InitializeComponent();
26      this.random = new MersenneTwister();
27
28      var sum = new CovarianceSum();
29      sum.Terms.Add(new CovarianceSquaredExponentialIso());
30      sum.Terms.Add(new CovariancePeriodic());
31      sum.Terms.Add(new CovarianceNoise());
32      this.covFunction = sum;
33      UpdateSliders();
34
35      InitData();
36      UpdateChart();
37    }
38
39    private void UpdateSliders() {
40      flowLayoutPanel1.Controls.Clear();
41      for (int i = 0; i < covFunction.GetNumberOfParameters(1); i++) {
42        var sliderControl = new TrackBar();
43        sliderControl.Minimum = -50;
44        sliderControl.Maximum = 50;
45        sliderControl.Value = 0;
46        sliderControl.ValueChanged += (sender, args) => UpdateChart();
47        flowLayoutPanel1.Controls.Add(sliderControl);
48      }
49    }
50
51    private void InitData() {
52      int n = 100;
53      data = new List<List<double>>();
54      data.Add(ValueGenerator.GenerateSteps(0, 1, 1.0 / n).ToList());
55
56      // sample from GP
57      var normalRand = new NormalDistributedRandom(random, 0, 1);
58      alpha = (from i in Enumerable.Range(0, n + 1)
59               select normalRand.NextDouble()).ToArray();
60    }
61
62    private void UpdateChart() {
63      var hyp = GetSliderValues();
64      var cov = covFunction.GetParameterizedCovarianceFunction(hyp, null);
65      var y = Util.SampleGaussianProcess(random, cov, data, alpha);
66
67      chart1.Series[0].Points.Clear();
68      foreach (var p in y.Zip(data[0], (t, x) => new { t, x })) {
69        chart1.Series[0].Points.AddXY(p.x, p.t);
70      }
71
72      var allData = new List<List<double>>();
73      allData.Add(y);
74      allData.Add(data[0]);
75      var variableNames = new string[] { "y", "x" };
76      var ds = new Dataset(variableNames, allData);
77      var rows = Enumerable.Range(0, data[0].Count);
78      var correctModel = new GaussianProcessModel(ds, variableNames.First(), variableNames.Skip(1), rows, hyp, new MeanZero(),
79                                                (ICovarianceFunction)covFunction.Clone());
80      var yPred = correctModel.GetEstimatedValues(ds, rows);
81      chart1.Series[1].Points.Clear();
82      foreach (var p in yPred.Zip(data[0], (t, x) => new { t, x })) {
83        chart1.Series[1].Points.AddXY(p.x, p.t);
84      }
85    }
86
87    private double[] GetSliderValues() {
88      var hyp = new List<double>();
89      foreach (var slider in flowLayoutPanel1.Controls.Cast<TrackBar>()) {
90        Console.Write(slider.Value / 10.0 + " ");
91        hyp.Add(slider.Value / 10.0);
92      }
93      Console.WriteLine();
94
95      return hyp.ToArray();
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.