public void MakePlot(double[] xBound, double[] yBound) { string xr = "xr[" + xBound[0] + ":" + xBound[1] + "]"; string yr = "yr[" + yBound[0] + ":" + yBound[1] + "]"; GnuPlot.Set("cntrparam levels 30", "isosamples 40", xr, yr, "decimalsign locale"); GnuPlot.SPlot(filename, "with pm3d"); }
// Plot J cost function private static void PlotJ(Matrix <double> X, Matrix <double> y, Matrix <double> theta) { // Grid over which we will calculate J double[] theta0_vals = MathNet.Numerics.Generate.LinearSpaced(100, -10, 10); double[] theta1_vals = MathNet.Numerics.Generate.LinearSpaced(100, -1, 4); // initialize J_vals to a matrix of 0's int size = theta0_vals.Length * theta1_vals.Length; double[] sx = new double[size]; double[] sy = new double[size]; double[] sz = new double[size]; // Fill out J_vals int idx = 0; for (int i = 0; i < theta0_vals.Length; i++) { for (int k = 0; k < theta1_vals.Length; k++) { Matrix <double> t = Matrix <double> .Build.Dense(2, 1); t[0, 0] = theta0_vals[i]; t[1, 0] = theta1_vals[k]; sx[idx] = theta0_vals[i]; sy[idx] = theta1_vals[k]; sz[idx] = ComputeCost(X, y, t); idx++; } } GnuPlot.HoldOn(); GnuPlot.Set("terminal wxt 1"); GnuPlot.Set("title \"Cost function J\""); GnuPlot.Set("key bottom right"); GnuPlot.Set("xlabel \"{/Symbol q}_0\""); GnuPlot.Set("ylabel \"{/Symbol q}_1\""); // surface plot GnuPlot.SPlot(sx, sy, sz, "palette title \"J({/Symbol q}_0,{/Symbol q}_1)\""); // Contour plot GnuPlot.Set("terminal wxt 2"); GnuPlot.Set("cntrparam levels auto 10", "logscale z", "xr[-10:10]", "yr[-1:4]"); GnuPlot.Unset("key", "label"); GnuPlot.Contour(sx, sy, sz); GnuPlot.Plot(new double[] { theta[0, 0] }, new double[] { theta[1, 0] }, "pt 2 ps 1 lc rgb \"red\""); }
private static void DisplayData(Vector <double>[] X) { int w = 20; int h = 20; int row = 0; int col = 0; int offsetRow = 0; int offsetCol = 0; int dim = (int)Math.Sqrt(X.Length); double[,] d = new double[h * dim, w *dim]; for (int i = 0; i < X.Length; i++) { for (int k = 0; k < (w * h); k++) { d[row + offsetRow, col + offsetCol] = X[i][k]; offsetCol++; if (offsetCol % w == 0) { offsetRow++; offsetCol = 0; } } col += w; if (col >= (w * dim)) { row += h; col = 0; } offsetRow = 0; offsetCol = 0; } //d = RotateRight(d); //GnuPlot.Unset("key"); GnuPlot.Unset("colorbox"); //GnuPlot.Unset("tics"); GnuPlot.Set("grid"); GnuPlot.Set("palette grey"); //GnuPlot.Set("xrange [0:200]"); //GnuPlot.Set("yrange [0:200]"); GnuPlot.Set("pm3d map"); GnuPlot.SPlot(d, "with image"); }
///* Function to display the current population for the subsequent generation */ public void PlotPopulation(Population pop, ProblemDefinition problem, int genNo = 0, List <Individual> best = null) { if (!Use3D) //2D { var xr = new double[problem.PopulationSize]; var yr = new double[problem.PopulationSize]; for (int x = 0; x < problem.PopulationSize; x++) { xr[x] = pop.IndList[x].Obj[GnuplotObjective1]; yr[x] = pop.IndList[x].Obj[GnuplotObjective2]; } if (best != null) { GnuPlot.HoldOn(); } GnuPlot.Plot(xr, yr, $"title 'Generation #{genNo} of {problem.MaxGeneration}' with points pointtype 8 lc rgb 'blue'"); if (best != null) { var x = new double[best.Count]; var y = new double[best.Count]; for (int i = 0; i < best.Count; i++) { x[i] = best[i].Obj[GnuplotObjective1]; y[i] = best[i].Obj[GnuplotObjective2]; } GnuPlot.Plot(x, y, $"title 'best ({best.First().TotalResult})' with points pointtype 6 lc rgb 'red'"); } GnuPlot.Set($"xlabel \"obj[{GnuplotObjective1 }]\""); GnuPlot.Set($"ylabel \"obj[{GnuplotObjective2 }]\""); } else //3D { var rank1Count = pop.IndList.Count(x => x.Rank == 1); var rankOtherCount = pop.IndList.Count(x => x.Rank != 1); if (rank1Count > 0) { var xr = new double[rank1Count]; var yr = new double[rank1Count]; var zr = new double[rank1Count]; int index = 0; foreach (var ind in pop.IndList.Where(x => x.Rank == 1)) { xr[index] = ind.Obj[GnuplotObjective1]; yr[index] = ind.Obj[GnuplotObjective2]; zr[index] = ind.Obj[GnuplotObjective3]; index++; } if (best != null || rankOtherCount > 0) { GnuPlot.HoldOn(); } GnuPlot.SPlot(xr, yr, zr, $"title 'Rank 1 individuals' with points pointtype 8 lc rgb 'red'"); } if (rankOtherCount > 0) { var xr = new double[rankOtherCount]; var yr = new double[rankOtherCount]; var zr = new double[rankOtherCount]; int index = 0; foreach (var ind in pop.IndList.Where(x => x.Rank != 1)) { xr[index] = ind.Obj[GnuplotObjective1]; yr[index] = ind.Obj[GnuplotObjective2]; zr[index] = ind.Obj[GnuplotObjective3]; index++; } GnuPlot.SPlot(xr, yr, zr, $"title 'Rank 2-{pop.IndList.Max(x=>x.Rank)} individuals' with points pointtype 8 lc rgb 'blue'"); } if (best != null) { var x = new double[best.Count]; var y = new double[best.Count]; var z = new double[best.Count]; for (int i = 0; i < best.Count; i++) { x[i] = best[i].Obj[GnuplotObjective1]; y[i] = best[i].Obj[GnuplotObjective2]; z[i] = best[i].Obj[GnuplotObjective3]; } GnuPlot.SPlot(x, y, z, $"title 'best ({best.First().TotalResult})' with points pointtype 6 lc rgb 'green'"); } GnuPlot.Set($"title 'Generation #{genNo} of {problem.MaxGeneration}'"); GnuPlot.Set($"xlabel \"obj[{GnuplotObjective1}]\""); GnuPlot.Set($"ylabel \"obj[{GnuplotObjective2}]\""); GnuPlot.Set($"zlabel \"obj[{GnuplotObjective3}]\""); } }
/*When the import button is clicked*/ private void import_Click(object sender, RoutedEventArgs e) { // Declare variable to hold scaled data set for faster plotting MappedData[,] scaledData; //Show the user that we are importing TextLabel.Content = "Importing..."; //Open the file dialog OpenFileDialog fopen = new OpenFileDialog(); fopen.RestoreDirectory = true; fopen.Filter = "dt1 files (*.dt1)|*.dt1"; //When the file is selected if (fopen.ShowDialog() == System.Windows.Forms.DialogResult.OK) { try { //Read the file FileReader reader = new FileReader(fopen.FileName); reader.read(out data); //Map data to the matrix DataMapper mapper = new DataMapper(data); // Scale the data as desired, by default half the points taken DataScaler scaler = new DataScaler(mapper.map()); scaledData = scaler.scale(DEFAULT_SCALE_FACTOR); // Scale about half resolution (or about half the points in the file) } catch (Exception error) { //Show the user any error that has occured System.Windows.MessageBox.Show("File is invalid. Aborting Import."); TextLabel.Content = "Please select Import or Export"; return; } } else { TextLabel.Content = "Please select Import or Export"; return; } // Retrieve size of scaled data int longiNum = scaledData.GetLength(0); int latiNum = scaledData.GetLength(1); int size = longiNum * latiNum; double[] x = new double[size]; // 'x' is distance eastward double[] y = new double[size]; // 'y' is distance northward double[] z = new double[size]; // 'z' is elevation int counter = 0; // Counter to place elevation in proper order cData = new CartesianData(scaledData); CartesianPoint[,] pts = cData.getCartesianData(); // Iterate from left to right on grid for (int i = 0; i < longiNum; ++i) { // Iterate from bottom to top on grid for (int j = 0; j < latiNum; ++j) { //z[counter] = scaledData[i, j].Elevation; x[counter] = pts[i, j].X; y[counter] = pts[i, j].Y; z[counter] = pts[i, j].Z; counter++; } } //Setup grid size and sampling GnuPlot.Set("dgrid3d 45, 45, 2"); GnuPlot.Set("isosamples 30"); //set the range for the x,y,z axis and plot (using pm3d to map height to color) /* * GnuPlot.Set("xrange[" + x.Min().ToString() + ":" + x.Max().ToString() + "]", * "yrange[" + y.Min().ToString() + ":" + y.Max().ToString() + "]", * "zrange[" + z.Min().ToString() + ":" + z.Max().ToString() + "]"); */ //GnuPlot.SPlot(x, y, z); // Don't think this does what I think it does. :/ GnuPlot.SPlot(scaledData.GetLength(1), z); // Enable exporting and update status export.IsEnabled = true; TextLabel.Content = "File Loaded"; }