// PlotTwoVarsContour plots contour for two variables problem. len(x) == 2 // Input // dirout -- directory to save files // fnkey -- file name key for eps figure // x -- solution. can be <nil> // np -- number of points for contour // extra -- called just before saving figure // axequal -- axis.equal // vmin -- min 0 values // vmax -- max 1 values // f -- function to plot filled contour. can be <nil> // gs -- functions to plot contour @ level 0. can be <nil> func PlotTwoVarsContour(dirout, fnkey string, x []float64, np int, extra func(), axequal bool, vmin, vmax []float64, f TwoVarsFunc_t, gs ...TwoVarsFunc_t) { if fnkey == "" { return } chk.IntAssert(len(vmin), 2) chk.IntAssert(len(vmax), 2) V0, V1 := utl.MeshGrid2D(vmin[0], vmax[0], vmin[1], vmax[1], np, np) var Zf [][]float64 var Zg [][][]float64 if f != nil { Zf = la.MatAlloc(np, np) } if len(gs) > 0 { Zg = utl.Deep3alloc(len(gs), np, np) } xtmp := make([]float64, 2) for i := 0; i < np; i++ { for j := 0; j < np; j++ { xtmp[0], xtmp[1] = V0[i][j], V1[i][j] if f != nil { Zf[i][j] = f(xtmp) } for k, g := range gs { Zg[k][i][j] = g(xtmp) } } } plt.Reset() plt.SetForEps(0.8, 350) if f != nil { cmapidx := 0 plt.Contour(V0, V1, Zf, io.Sf("fsz=7, cmapidx=%d", cmapidx)) } for k, _ := range gs { plt.ContourSimple(V0, V1, Zg[k], false, 8, "zorder=5, levels=[0], colors=['yellow'], linewidths=[2], clip_on=0") } if x != nil { plt.PlotOne(x[0], x[1], "'r*', label='optimum', zorder=10") } if extra != nil { extra() } if dirout == "" { dirout = "." } plt.Cross("clr='grey'") plt.SetXnticks(11) plt.SetYnticks(11) if axequal { plt.Equal() } plt.AxisRange(vmin[0], vmax[0], vmin[1], vmax[1]) args := "leg_out='1', leg_ncol=4, leg_hlen=1.5" plt.Gll("$x_0$", "$x_1$", args) plt.SaveD(dirout, fnkey+".eps") }
func Test_bins02(tst *testing.T) { //verbose() chk.PrintTitle("bins02. find along line (2D)") // bins var bins Bins bins.Init([]float64{-0.2, -0.2}, []float64{0.8, 1.8}, 5) // fill bins structure maxit := 5 // number of entries ID := make([]int, maxit) for k := 0; k < maxit; k++ { x := float64(k) / float64(maxit) ID[k] = k err := bins.Append([]float64{x, 2*x + 0.2}, ID[k]) if err != nil { chk.Panic(err.Error()) } } // add more points to bins for i := 0; i < 5; i++ { err := bins.Append([]float64{float64(i) * 0.1, 1.8}, 100+i) if err != nil { chk.Panic(err.Error()) } } // message for _, bin := range bins.All { if bin != nil { io.Pf("%v\n", bin) } } // find points along diagonal ids := bins.FindAlongSegment([]float64{0.0, 0.2}, []float64{0.8, 1.8}, 1e-8) io.Pforan("ids = %v\n", ids) chk.Ints(tst, "ids", ids, ID) // find additional points ids = bins.FindAlongSegment([]float64{-0.2, 1.8}, []float64{0.8, 1.8}, 1e-8) io.Pfcyan("ids = %v\n", ids) chk.Ints(tst, "ids", ids, []int{100, 101, 102, 103, 104, 4}) // draw if chk.Verbose { plt.SetForPng(1, 500, 150) bins.Draw2d(true, true, true, true, map[int]bool{8: true, 9: true, 10: true}) plt.SetXnticks(15) plt.SetYnticks(15) plt.SaveD("/tmp/gosl/gm", "test_bins02.png") } }
// PlotFltFlt plots flt-flt contour // use iFlt==-1 || jFlt==-1 to plot all combinations func (o *Optimiser) PlotFltFltContour(sols0 []*Solution, iFlt, jFlt, iOva int, pp *PlotParams) { best, _ := GetBestFeasible(o, iOva) plotAll := iFlt < 0 || jFlt < 0 plotCommands := func(i, j int) { o.PlotContour(i, j, iOva, pp) if sols0 != nil { o.PlotAddFltFlt(i, j, sols0, &pp.FmtSols0) } o.PlotAddFltFlt(i, j, o.Solutions, &pp.FmtSols) if best != nil { plt.PlotOne(best.Flt[i], best.Flt[j], pp.FmtBest.GetArgs("")) } if pp.Extra != nil { pp.Extra() } if pp.AxEqual { plt.Equal() } } if plotAll { idx := 1 ncol := o.Nflt - 1 for row := 0; row < o.Nflt; row++ { idx += row for col := row + 1; col < o.Nflt; col++ { plt.Subplot(ncol, ncol, idx) plt.SplotGap(0.0, 0.0) plotCommands(col, row) if col > row+1 { plt.SetXnticks(0) plt.SetYnticks(0) } else { plt.Gll(io.Sf("$x_{%d}$", col), io.Sf("$x_{%d}$", row), "leg=0") } idx++ } } idx = ncol*(ncol-1) + 1 plt.Subplot(ncol, ncol, idx) plt.AxisOff() // TODO: fix formatting of open marker, add star to legend plt.DrawLegend([]plt.Fmt{pp.FmtSols0, pp.FmtSols, pp.FmtBest}, 8, "center", false, "") } else { plotCommands(iFlt, jFlt) if pp.Xlabel == "" { plt.Gll(io.Sf("$x_{%d}$", iFlt), io.Sf("$x_{%d}$", jFlt), pp.LegPrms) } else { plt.Gll(pp.Xlabel, pp.Ylabel, pp.LegPrms) } } plt.SaveD(pp.DirOut, pp.FnKey+pp.FnExt) }
func plot_uniform(A, B float64, xmin, xmax float64) { var dist DistUniform dist.Init(&VarData{Min: A, Max: B}) n := 101 x := utl.LinSpace(xmin, xmax, n) y := make([]float64, n) Y := make([]float64, n) for i := 0; i < n; i++ { y[i] = dist.Pdf(x[i]) Y[i] = dist.Cdf(x[i]) } plt.Subplot(2, 1, 1) plt.Plot(x, y, io.Sf("clip_on=0,zorder=10,label=r'$(%g,%g)$'", A, B)) plt.Gll("$x$", "$f(x)$", "leg_out=1, leg_ncol=4, leg_hlen=1") plt.SetYnticks(11) plt.Subplot(2, 1, 2) plt.Plot(x, Y, io.Sf("clip_on=0,zorder=10,label=r'$(%g,%g)$'", A, B)) plt.Gll("$x$", "$F(x)$", "leg_out=1, leg_ncol=4, leg_hlen=1") }
func plot_frechet(l, c, a float64, xmin, xmax float64) { var dist DistFrechet dist.Init(&VarData{L: l, C: c, A: a}) n := 101 x := utl.LinSpace(xmin, xmax, n) y := make([]float64, n) Y := make([]float64, n) for i := 0; i < n; i++ { y[i] = dist.Pdf(x[i]) Y[i] = dist.Cdf(x[i]) } plt.Subplot(2, 1, 1) plt.Plot(x, y, io.Sf("clip_on=0,zorder=10,label=r'$(%g,%g,%g)$'", l, c, a)) plt.Gll("$x$", "$f(x)$", "leg_out=1, leg_ncol=4, leg_hlen=1") plt.SetYnticks(11) plt.Subplot(2, 1, 2) plt.Plot(x, Y, io.Sf("clip_on=0,zorder=10,label=r'$(%g,%g,%g)$'", l, c, a)) plt.Gll("$x$", "$F(x)$", "leg_out=1, leg_ncol=4, leg_hlen=1") }
func plot_gumbel(μ, σ float64) { var dist DistGumbel dist.Init(&VarData{M: μ, S: σ}) n := 101 x := utl.LinSpace(-5, 20, n) y := make([]float64, n) Y := make([]float64, n) for i := 0; i < n; i++ { y[i] = dist.Pdf(x[i]) Y[i] = dist.Cdf(x[i]) } plt.Subplot(2, 1, 1) plt.Plot(x, y, io.Sf("clip_on=0,zorder=10,label=r'$\\mu=%.4f,\\;\\sigma=%.4f$'", μ, σ)) plt.Gll("$x$", "$f(x)$", "leg_out=1, leg_ncol=2") plt.SetYnticks(11) plt.Subplot(2, 1, 2) plt.Plot(x, Y, io.Sf("clip_on=0,zorder=10,label=r'$\\mu=%.4f,\\;\\sigma=%.4f$'", μ, σ)) plt.Gll("$x$", "$F(x)$", "leg_out=1, leg_ncol=2") }