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

Working well and the test9d with plots look very pretty.

parent f9136d0f
No related branches found
No related tags found
No related merge requests found
...@@ -108,17 +108,28 @@ func TestCsv(t *testing.T) { ...@@ -108,17 +108,28 @@ func TestCsv(t *testing.T) {
} }
} }
var s1dplot = ` var s9dplot = `
ini_temp 0.0925 ini_temp 0.0925
final_temp 0.09 final_temp 0.09
x_ini 15,10,11,12,14,18,-12,-8,-9 x_ini 15,10,11,12,14,18,-12,-8,-9
n_step 1000 n_step 50000
x_delta 0.2
fOutName test9d fOutName test9d
fPltName testplot.svg fPltName testplot.svg
xPltName testrajplt` xPltName testrajplt`
var s9bdplot = `
ini_temp 0.0925
final_temp 0.09
x_ini 15,10,11,12,14,0,-12,-8,-9
n_step 50000
x_delta 0.2
fOutName test9db
fPltName test9dbf
xPltName test9dbtraj`
var plotTest = []string{ var plotTest = []string{
s1dplot, s9dplot, s9bdplot,
} }
func TestPlot(t *testing.T) { func TestPlot(t *testing.T) {
......
...@@ -146,18 +146,18 @@ func saveStep(cprm *cprm, n uint32, tmprtr float64, x []float32, fTrial float64) ...@@ -146,18 +146,18 @@ func saveStep(cprm *cprm, n uint32, tmprtr float64, x []float32, fTrial float64)
} }
// nRunAdj will try to adjust the step size, given our history of accept/reject. // nRunAdj will try to adjust the step size, given our history of accept/reject.
func nRunAdj(mcPrm *mcPrm, runAcc float64, xDlta float32) float32 { func nRunAdj(mcPrm *mcPrm, nstep uint32, runAcc float64, xDlta float32) float32 {
const step = "step" const step = "step"
if runAcc < accRateIdeal-0.01 { // acceptance rate too low if runAcc < accRateIdeal-0.01 { // acceptance rate too low
xDlta *= 0.9 xDlta *= 0.9
if mcPrm.verbose { if mcPrm.verbose {
fmt.Println(step, xDlta, "decrease") fmt.Println(step, nstep, "decrease", xDlta)
} }
} else if (xDlta < maxxDlta) && (runAcc > accRateIdeal+0.01) { } else if (xDlta < maxxDlta) && (runAcc > accRateIdeal+0.01) {
xDlta *= 1.1 xDlta *= 1.1
if mcPrm.verbose { if mcPrm.verbose {
fmt.Println(step, xDlta, "increase") fmt.Println(step, nstep, "increase", xDlta)
} }
} }
return xDlta return xDlta
...@@ -188,26 +188,25 @@ func doRun(mcPrm *mcPrm) error { ...@@ -188,26 +188,25 @@ func doRun(mcPrm *mcPrm) error {
runAcc := accRateIdeal // Running acceptance rate, exponentially weighted runAcc := accRateIdeal // Running acceptance rate, exponentially weighted
copy(x, mcPrm.xIni) copy(x, mcPrm.xIni)
fOld := ackley.Ackley(x) // Initial function value fOld := ackley.Ackley(x) // Initial function value
fTrial := fOld
tmprtr := float64(mcPrm.iniTmp) tmprtr := float64(mcPrm.iniTmp)
nRunAcc := nRunAccAdj // Every nRunAccAdj, try adjusting the step size. nRunAcc := nRunAccAdj // Every nRunAccAdj, try adjusting the step size.
const runMult = 0.99 const runMult = 0.99
xDlta := mcPrm.xDlta // Adaptable step size xDlta := mcPrm.xDlta // Adaptable step size
if mcPrm.fOutName == "test9d.csv"{
breaker()
}
saveStep(&cprm, 0, tmprtr, x, fOld) saveStep(&cprm, 0, tmprtr, x, fOld)
for n := uint32(0); n < mcPrm.nStep; n++ { for n := uint32(0); n < mcPrm.nStep; n++ {
var acc bool var acc bool
// nout--
// if nout == 0 { // Do we want to print out results on this step ?
// printfVal(cprm.fOut, x, n, tmprtr, fOld)
// nout = cprm.nEvery
// }
nRunAcc-- nRunAcc--
if nRunAcc == 0 { // Do we want to try adjusting the step size ? if nRunAcc == 0 { // Do we want to try adjusting the step size ?
xDlta = nRunAdj(mcPrm, runAcc, xDlta) xDlta = nRunAdj(mcPrm, n, runAcc, xDlta)
nRunAcc = nRunAccAdj // Reset the counter nRunAcc = nRunAccAdj // Reset the counter
} }
newx(x, xT, cprm.rand, xDlta) newx(x, xT, cprm.rand, xDlta)
fTrial := ackley.Ackley(xT) fTrial = ackley.Ackley(xT)
if fOld < fTrial { if fOld < fTrial {
r := cprm.rand.Float64() // Make the decision as to whether to r := cprm.rand.Float64() // Make the decision as to whether to
...@@ -232,7 +231,14 @@ func doRun(mcPrm *mcPrm) error { ...@@ -232,7 +231,14 @@ func doRun(mcPrm *mcPrm) error {
tmprtr *= cprm.coolMult tmprtr *= cprm.coolMult
} }
} }
if mcPrm.fOutName == "test9d.csv"{
breaker()
}
// On plots, we want the last step, even if nothing changed. Print me. Fix. // On plots, we want the last step, even if nothing changed. Print me. Fix.
if fTrial != fOld { // last move was not accepted, so we have to add last step
saveStep (&cprm, mcPrm.nStep, tmprtr, x, fOld)
}
err := plotfWrt(cprm.plotnstp, cprm.plotf, cprm.plotTmprtr, cprm.fPlt, cprm.fplotme) err := plotfWrt(cprm.plotnstp, cprm.plotf, cprm.plotTmprtr, cprm.fPlt, cprm.fplotme)
if err != nil { if err != nil {
return err return err
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment