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

Merge branch 'devel'

parents d8db8129 14bb4804
No related branches found
No related tags found
No related merge requests found
......@@ -35,7 +35,7 @@ To build without graphics
## Binaries
Where are they ?
If I have built them for linux and windows, they will be linked to from the course moodle page.
# Documentation
......
I want to remove the adaptive step size code from the version used for teaching. I will keep the code in this version.
It works, but the implementation is ugly and full of arbitrary numbers, like checking every n-hundred steps if we should do something.
\ No newline at end of file
......@@ -45,5 +45,4 @@ and they have the meaning
If final_temp is lower than initial temperature, we will do simulated annealing.
*/
package main
package ackley_doc
......@@ -14,12 +14,6 @@ import (
"example.com/ackley_mc/ackley"
)
const (
accRateIdeal = 0.05 // target of 5% acceptance rate
nRunAccAdj = 200 // every n steps, check acceptance rate
maxxDlta = 2. // max step size
)
var seedLocker sync.Mutex
// getSeed returns the random number seed, but we have to put a lock
......@@ -202,23 +196,6 @@ func saveStep(cprm *cprm, n int, tmprtr float64, x []float32, fTrial float64) {
}
}
// nRunAdj will try to adjust the step size, given our history of accept/reject.
func nRunAdj(mcPrm *McPrm, nstep int, runAcc float64, xDlta float64) float64 {
const step = "step"
if runAcc < accRateIdeal-0.01 { // acceptance rate too low
xDlta *= 0.9
if mcPrm.verbose {
fmt.Println(step, nstep, "decrease", xDlta)
}
} else if (xDlta < maxxDlta) && (runAcc > accRateIdeal+0.01) {
xDlta *= 1.1
if mcPrm.verbose {
fmt.Println(step, nstep, "increase", xDlta)
}
}
return xDlta
}
// doRun does a Monte Carlo run.
// We only need single precision for most things, except for function
// values, but the interface to the plot and interface packages mostly
......@@ -245,7 +222,6 @@ func singleRun(mcPrm *McPrm) (MCresult, error) {
var nAcc int // Counter, number of accepted moves
x := make([]float32, len(mcPrm.XIni)) // current position
xT := make([]float32, len(mcPrm.XIni)) // trial position
runAcc := accRateIdeal // Running acceptance rate, exponentially weighted
for i := range mcPrm.XIni { // Initial coordinates from
x[i] = float32(mcPrm.XIni[i]) // the control structure
......@@ -254,20 +230,11 @@ func singleRun(mcPrm *McPrm) (MCresult, error) {
fOld := ackley.Ackley(x) // Initial function value
fTrial := fOld
tmprtr := float64(mcPrm.IniTmp)
nRunAcc := nRunAccAdj // Every nRunAccAdj, try adjusting the step size.
const runMult = 0.99
xDlta := mcPrm.XDlta // Step size which might be adjusted on the fly
saveStep(&cprm, 0, tmprtr, x, fOld)
for n := 0; n < mcPrm.NStep; n++ {
var accept bool
if mcPrm.AdaptStep { // Are we trying adaptive steps ?
nRunAcc--
if nRunAcc == 0 { // Do we want to try adjusting the step size ?
xDlta = nRunAdj(mcPrm, n, runAcc, xDlta)
nRunAcc = nRunAccAdj // Reset the counter
}
}
newx(x, xT, cprm.rand, xDlta)
fTrial = ackley.Ackley(xT)
if fTrial <= fOld {
......@@ -284,19 +251,11 @@ func singleRun(mcPrm *McPrm) (MCresult, error) {
copy(x, xT)
fOld = fTrial
saveStep(&cprm, n+1, tmprtr, x, fTrial)
if mcPrm.AdaptStep {
runAcc = runMult*runAcc + (1.0 - runMult)
}
} else { // else don't really have to do anything, but update statistics
if mcPrm.AdaptStep {
runAcc = runMult * runAcc // update the running estimate of acceptance
}
}
if cprm.coolme {
tmprtr *= cprm.coolMult
}
}
// On plots, we want the last step, even if nothing changed.
if fTrial != fOld { // last move was not accepted, so we have to add last step
saveStep(&cprm, mcPrm.NStep, tmprtr, x, fOld)
......
......@@ -16,7 +16,6 @@ type McPrm struct {
fOutName string // where we write output to
fPltName string // where we plot to (function values)
xPltName string // where we plot x trajectories to
AdaptStep bool // Adaptive step sizes
verbose bool
}
......
......@@ -30,7 +30,6 @@ var cmdDflt = []struct {
{"foutname", ""},
{"fpltname", ""}, // empty means no plots of function
{"xpltname", ""},
{"adaptstep", ""},
{"verbose", ""},
}
......@@ -90,9 +89,6 @@ func digest(prmMap map[string]string, mcPrm *McPrm) error {
if prmMap["verbose"] != "" {
mcPrm.verbose = true
}
if prmMap["adaptstep"] != "" {
mcPrm.AdaptStep = true
}
return nil
}
......
......@@ -117,12 +117,6 @@ func paramForm(genParams genParams) *widget.Form {
nEqulItem, nEqulReload := intItem(&mcPrm.NEquil, "N equilibration", "number of steps for equilibration")
seedItem, seedReload := intItem(&mcwork.Seed, "seed random numbers", "seed for random number generator")
adaptbind := binding.BindBool(&mcPrm.AdaptStep) // checkbox for adaptive stepsize
adaptEntry := widget.NewCheckWithData("adaptive step size", adaptbind)
adaptItem := widget.NewFormItem("use adaptive step size", adaptEntry)
adaptItem.HintText = "Turn on adaptive moves to try to keep acceptance near 5%"
adaptReload := func() { _ = adaptbind.Reload() }
reloadPTab := func() {
iniTmpReload() // The values linked to in the the various fields can
fnlTmpReload() // change outside of this function, so reloadPTab
......@@ -131,16 +125,15 @@ func paramForm(genParams genParams) *widget.Form {
nStepReload()
nEqulReload()
seedReload()
adaptReload()
}
rdfile := func() { rdwork(genParams, reloadPTab) }
rdfileBtn := widget.NewButton("get file ", rdfile)
rdFileItem := widget.NewFormItem("read from a file", rdfileBtn)
// puke-oh-rama, Can we tidy up the next three lines ?
r := widget.NewForm( // Lays out the items in the form
iniTmpItem, fnlTmpItem, xDltaItem, xIniItem,
nStepItem, nEqulItem, seedItem, adaptItem, rdFileItem)
nStepItem, nEqulItem, seedItem, rdFileItem)
r.SubmitText = "start calculation"
r.OnSubmit = func() { startrun(genParams, r, mcPrm) }
reloadPTab() // does this help ? sometimes the form pops up with entries not ready
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment