Skip to content
Snippets Groups Projects
Commit 607e1fc0 authored by Andrew E. Torda's avatar Andrew E. Torda
Browse files

Added more stats (best position) to graphical display.

parent ac9a0c8c
Branches
No related tags found
No related merge requests found
...@@ -135,7 +135,8 @@ func setupRun(mcPrm *McPrm, cprm *cprm) error { ...@@ -135,7 +135,8 @@ func setupRun(mcPrm *McPrm, cprm *cprm) error {
return nil 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) { func newx(xold []float32, xT []float32, rand *rand.Rand, xDlta float64) {
var iDim int var iDim int
if (len(xold)) > 1 { if (len(xold)) > 1 {
...@@ -147,7 +148,7 @@ func newx(xold []float32, xT []float32, rand *rand.Rand, xDlta float64) { ...@@ -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 // 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) { func printfVal(fOut io.Writer, x []float32, n int, tmprtr float64, fOld float64) {
fmt.Fprintf(fOut, "%d,%.4g,%.5g", n, tmprtr, fOld) fmt.Fprintf(fOut, "%d,%.4g,%.5g", n, tmprtr, fOld)
for _, xx := range x { for _, xx := range x {
...@@ -156,9 +157,27 @@ func printfVal(fOut io.Writer, x []float32, n int, tmprtr float64, fOld float64) ...@@ -156,9 +157,27 @@ func printfVal(fOut io.Writer, x []float32, n int, tmprtr float64, fOld float64)
fmt.Fprintln(fOut) 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 // saveStep is called when we accept a change. It writes or saves for
// later plotting. // later plotting.
func saveStep(cprm *cprm, n int, tmprtr float64, x []float32, fTrial float64) { 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 cprm.plotnstp = append(cprm.plotnstp, float64(n)) // num steps
if cprm.fplotWrt != nil { // Function values // function values if cprm.fplotWrt != nil { // Function values // function values
cprm.plotf = append(cprm.plotf, fTrial) cprm.plotf = append(cprm.plotf, fTrial)
...@@ -272,7 +291,11 @@ func DoRun(mcPrm *McPrm) (MCresult, error) { ...@@ -272,7 +291,11 @@ func DoRun(mcPrm *McPrm) (MCresult, error) {
saveStep(&cprm, mcPrm.NStep, tmprtr, x, fOld) saveStep(&cprm, mcPrm.NStep, tmprtr, x, fOld)
} }
defer fmt.Println("n accepted:", nAcc, "of", mcPrm.NStep) 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 { if err := plotfWrt(&cprm); err != nil {
return broken, err return broken, err
} }
...@@ -288,5 +311,9 @@ func DoRun(mcPrm *McPrm) (MCresult, error) { ...@@ -288,5 +311,9 @@ func DoRun(mcPrm *McPrm) (MCresult, error) {
xdata = xBuf.Bytes() 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
} }
...@@ -28,4 +28,6 @@ var Seed int ...@@ -28,4 +28,6 @@ var Seed int
type MCresult struct { type MCresult struct {
Fdata, Xdata []byte // png images of function and X data Fdata, Xdata []byte // png images of function and X data
NStep, NAcc int // number of steps and accepted steps NStep, NAcc int // number of steps and accepted steps
Bestfval float64 // best function value found
BestX []float32 // location of this best value found
} }
...@@ -28,9 +28,10 @@ import ( ...@@ -28,9 +28,10 @@ import (
// runStatTxt should summarise some of the statistics // runStatTxt should summarise some of the statistics
func runStatTxt(rslt *mcwork.MCresult) fyne.Widget { 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.) 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 return r
} }
......
...@@ -33,7 +33,6 @@ const ( ...@@ -33,7 +33,6 @@ const (
type workstatus struct { type workstatus struct {
mcwork.MCresult mcwork.MCresult
// fdata, xdata []byte // the data in a function or X value plot
err error err error
status status // Tells us what we should do now status status // Tells us what we should do now
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment