diff --git a/mc_work/dorun.go b/mc_work/dorun.go index 0ad6d92b7f9e77b0ee86b139e4e9c7ef8912b595..0d2e1c7249b8a3fc5ed304f172c88f429cd80675 100644 --- a/mc_work/dorun.go +++ b/mc_work/dorun.go @@ -135,7 +135,8 @@ func setupRun(mcPrm *McPrm, cprm *cprm) error { return nil } -// newx will move just one coordinate at a time +// newx will move just one dimension at a time. Moving all dimensions at once +// required teeny moves. func newx(xold []float32, xT []float32, rand *rand.Rand, xDlta float64) { var iDim int if (len(xold)) > 1 { @@ -147,7 +148,7 @@ func newx(xold []float32, xT []float32, rand *rand.Rand, xDlta float64) { } // printfVal is the loop to print out the function value and coordinates -// probably for later plotting. +// for later plotting. func printfVal(fOut io.Writer, x []float32, n int, tmprtr float64, fOld float64) { fmt.Fprintf(fOut, "%d,%.4g,%.5g", n, tmprtr, fOld) for _, xx := range x { @@ -156,9 +157,27 @@ func printfVal(fOut io.Writer, x []float32, n int, tmprtr float64, fOld float64) fmt.Fprintln(fOut) } +// saveBest checks if we have found a best value for the function. If so, +// update the location where we store it. +var bestfval float64 +var bestX []float32 +var bestN int + +func saveBest(x []float32, fTrial float64, n int) { + if bestX == nil { + bestX = make([]float32, len(x)) + } + if fTrial < bestfval || n == 0 { + bestfval = fTrial + bestN = n + copy(bestX, x) + } +} + // saveStep is called when we accept a change. It writes or saves for // later plotting. func saveStep(cprm *cprm, n int, tmprtr float64, x []float32, fTrial float64) { + saveBest(x, fTrial, n) cprm.plotnstp = append(cprm.plotnstp, float64(n)) // num steps if cprm.fplotWrt != nil { // Function values // function values cprm.plotf = append(cprm.plotf, fTrial) @@ -272,7 +291,11 @@ func DoRun(mcPrm *McPrm) (MCresult, error) { saveStep(&cprm, mcPrm.NStep, tmprtr, x, fOld) } defer fmt.Println("n accepted:", nAcc, "of", mcPrm.NStep) - + if bestX != nil { + defer fmt.Printf("Best function value: %.2f at %.2v\n", bestfval, bestX) + } else { + defer fmt.Println ("bestX was never initialised") + } if err := plotfWrt(&cprm); err != nil { return broken, err } @@ -288,5 +311,9 @@ func DoRun(mcPrm *McPrm) (MCresult, error) { xdata = xBuf.Bytes() } - return MCresult{Fdata: fdata, Xdata: xdata, NStep: mcPrm.NStep, NAcc: nAcc}, nil + return MCresult{ + Fdata: fdata, Xdata: xdata, + NStep: mcPrm.NStep, NAcc: nAcc, + Bestfval: bestfval, BestX: bestX, + }, nil } diff --git a/mc_work/mc_work.go b/mc_work/mc_work.go index 294210546810bcd9ababe1ac02a59841d0e28c6f..a63d31c32b59e37caac3d5a5422784297fac5dd4 100644 --- a/mc_work/mc_work.go +++ b/mc_work/mc_work.go @@ -26,6 +26,8 @@ var Seed int // For returning results type MCresult struct { - Fdata, Xdata []byte // png images of function and X data - NStep, NAcc int // number of steps and accepted steps + Fdata, Xdata []byte // png images of function and X data + NStep, NAcc int // number of steps and accepted steps + Bestfval float64 // best function value found + BestX []float32 // location of this best value found } diff --git a/ui/output_tab.go b/ui/output_tab.go index a0eaf74fdbb675e56dec6386ef158fad946fadc3..fc69ec2600a1369b1bd45ef828ccd243ff7906df 100644 --- a/ui/output_tab.go +++ b/ui/output_tab.go @@ -28,9 +28,10 @@ import ( // runStatTxt should summarise some of the statistics func runStatTxt(rslt *mcwork.MCresult) fyne.Widget { - txt := fmt.Sprintf("Num steps %d\nNum accepted %d\nacceptance rate %.1f %%", + t1 := fmt.Sprintf("Num steps %d\nNum accepted %d\nacceptance rate %.1f %%\n", rslt.NStep, rslt.NAcc, (float32(rslt.NAcc)/float32(rslt.NStep))*100.) - r := widget.NewLabel(txt) + t2 := fmt.Sprintf ("best function value: %.2f\nat %.1g", rslt.Bestfval, rslt.BestX) + r := widget.NewLabel(t1+t2) return r } diff --git a/ui/ui_run.go b/ui/ui_run.go index 1360bd2d445779d786e5e3f58b8fcda38f9f5a15..8a9a122a3eca00f5149e7d4dd6e78d093aa1f8f6 100644 --- a/ui/ui_run.go +++ b/ui/ui_run.go @@ -33,7 +33,6 @@ const ( type workstatus struct { mcwork.MCresult - // fdata, xdata []byte // the data in a function or X value plot err error status status // Tells us what we should do now }