Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1967: worked on Gaussian Process evolution problem

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 CovariancePeriodic());
30      sum.Terms.Add(new CovarianceNoise());
31      this.covFunction = sum;
32      UpdateSliders();
33
34      InitData();
35      UpdateChart();
36    }
37
38    private void UpdateSliders() {
39      flowLayoutPanel1.Controls.Clear();
40      for (int i = 0; i < covFunction.GetNumberOfParameters(1); i++) {
41        var sliderControl = new TrackBar();
42        sliderControl.Minimum = -50;
43        sliderControl.Maximum = 50;
44        sliderControl.Value = 0;
45        sliderControl.ValueChanged += (sender, args) => UpdateChart();
46        flowLayoutPanel1.Controls.Add(sliderControl);
47      }
48    }
49
50    private void InitData() {
51      int n = 20;
52      data = new List<List<double>>();
53      data.Add(ValueGenerator.GenerateSteps(0, 1, 1.0 / n).ToList());
54
55      // sample from GP
56      var normalRand = new NormalDistributedRandom(random, 0, 1);
57      alpha = (from i in Enumerable.Range(0, n + 1)
58               select normalRand.NextDouble()).ToArray();
59    }
60
61    private void UpdateChart() {
62      var hyp = GetSliderValues();
63      var cov = covFunction.GetParameterizedCovarianceFunction(hyp, null);
64      var y = Util.SampleGaussianProcess(random, cov, data, alpha);
65
66      chart1.Series[0].Points.Clear();
67      foreach (var p in y.Zip(data[0], (t, x) => new { t, x })) {
68        chart1.Series[0].Points.AddXY(p.x, p.t);
69      }
70
71      var allData = new List<List<double>>();
72      allData.Add(y);
73      allData.Add(data[0]);
74      var variableNames = new string[] { "y", "x" };
75      var ds = new Dataset(variableNames, allData);
76      var rows = Enumerable.Range(0, data[0].Count);
77      var correctModel = new GaussianProcessModel(ds, variableNames.First(), variableNames.Skip(1), rows, hyp, new MeanZero(),
78                                                (ICovarianceFunction)covFunction.Clone());
79      var yPred = correctModel.GetEstimatedValues(ds, rows);
80      chart1.Series[1].Points.Clear();
81      foreach (var p in yPred.Zip(data[0], (t, x) => new { t, x })) {
82        chart1.Series[1].Points.AddXY(p.x, p.t);
83      }
84    }
85
86    private double[] GetSliderValues() {
87      var hyp = new List<double>();
88      foreach (var slider in flowLayoutPanel1.Controls.Cast<TrackBar>()) {
89        Console.Write(slider.Value / 10.0 + " ");
90        hyp.Add(slider.Value / 10.0);
91      }
92      Console.WriteLine();
93
94      return hyp.ToArray();
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.