using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using HeuristicLab.Algorithms.DataAnalysis; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Problems.DataAnalysis; using HeuristicLab.Problems.Instances.DataAnalysis; using HeuristicLab.Random; namespace GaussianProcessDemo { public partial class Form1 : Form { private IRandom random; private ICovarianceFunction covFunction; private List> data; private double[] alpha; public Form1() { InitializeComponent(); this.random = new MersenneTwister(); var sum = new CovarianceSum(); var t = new CovarianceNeuralNetwork(); sum.Terms.Add(t); sum.Terms.Add(new CovarianceNoise()); this.covFunction = sum; UpdateSliders(); InitData(); UpdateChart(); } private void UpdateSliders() { flowLayoutPanel1.Controls.Clear(); flowLayoutPanel1.Controls.Add(dataButton); for (int i = 0; i < covFunction.GetNumberOfParameters(1); i++) { var sliderControl = new TrackBar(); sliderControl.Minimum = -50; sliderControl.Maximum = 50; sliderControl.Value = 0; sliderControl.ValueChanged += (sender, args) => UpdateChart(); flowLayoutPanel1.Controls.Add(sliderControl); } } private void InitData() { int n = 200; data = new List>(); data.Add(ValueGenerator.GenerateSteps(0, 1, 1.0 / n).ToList()); // sample from GP var normalRand = new NormalDistributedRandom(random, 0, 1); alpha = (from i in Enumerable.Range(0, n + 1) select normalRand.NextDouble()).ToArray(); } private void UpdateChart() { var hyp = GetSliderValues(); var cov = covFunction.GetParameterizedCovarianceFunction(hyp, Enumerable.Range(0, data.Count)); var y = Util.SampleGaussianProcess(random, cov, data, alpha); chart1.Series[0].Points.Clear(); foreach (var p in y.Zip(data[0], (t, x) => new { t, x })) { chart1.Series[0].Points.AddXY(p.x, p.t); } var trainingData = new List>(); var trainingIndices = RandomEnumerable.SampleRandomWithoutRepetition(Enumerable.Range(0, y.Count), random, 10); var trainingY = trainingIndices.Select(i => y[i]).ToList(); var trainingX = trainingIndices.Select(i => data[0][i]).ToList(); trainingData.Add(trainingY); trainingData.Add(trainingX); //chart1.Series[2].Points.Clear(); //foreach (var p in trainingY.Zip(trainingX, (t, x) => new { t, x })) { // chart1.Series[2].Points.AddXY(p.x, p.t); //} var allData = new List>(); allData.Add(y); allData.Add(data[0]); var variableNames = new string[] { "y", "x" }; var fullDataSet = new Dataset(variableNames, allData); var trainingDataSet = new Dataset(variableNames, trainingData); var trainingRows = Enumerable.Range(0, trainingIndices.Count()); var fullRows = Enumerable.Range(0, data[0].Count); var correctModel = new GaussianProcessModel(fullDataSet, variableNames.First(), variableNames.Skip(1), fullRows, hyp, new MeanZero(), (ICovarianceFunction)covFunction.Clone()); var yPred = correctModel.GetEstimatedValues(fullDataSet, fullRows); chart1.Series[1].Points.Clear(); foreach (var p in yPred.Zip(data[0], (t, x) => new { t, x })) { chart1.Series[1].Points.AddXY(p.x, p.t); } } private double[] GetSliderValues() { var hyp = new List(); foreach (var slider in flowLayoutPanel1.Controls.OfType()) { Console.Write(slider.Value / 10.0 + " "); hyp.Add(slider.Value / 10.0); } Console.WriteLine(); return hyp.ToArray(); } private void dataButton_Click(object sender, EventArgs e) { var dataForm = new Form(); var dataTextField = new TextBox(); dataTextField.Multiline = true; dataTextField.Text = DataToText(); dataTextField.Dock = DockStyle.Fill; dataForm.Controls.Add(dataTextField); dataForm.ShowDialog(); } private string DataToText() { var str = new StringBuilder(); foreach (var p in chart1.Series[1].Points) { str.AppendLine(p.XValue + "\t" + p.YValues.First()); } return str.ToString(); } } }