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

Close/Flush() were in the wrong order. Changed acceptance rule, so we accept...

Close/Flush() were in the wrong order. Changed acceptance rule, so we accept if the trial's function value is the same as the old one. Removed a temporary variable - still trying to make main loop shorter.
parent ecb36263
No related branches found
No related tags found
No related merge requests found
In dorun.go
* make the csv files optional
Note.. Benchmarking suggests that most of the time is spent in Ackley function, and, to be exact, in the cosine and exp() function.
\ No newline at end of file
......@@ -177,15 +177,14 @@ func doRun(mcPrm *mcPrm) error {
if err := setupRun(mcPrm, &cprm); err != nil {
return err
}
if cprm.fOut != nil {
defer cprm.fOut.Flush()
if cprm.fOut != nil { // The text file documenting the run
defer cprm.fOutRaw.Close()
defer cprm.fOut.Flush()
}
var nAcc int // Counter of number of accepted moves
var nAcc int // Counter, number of accepted moves
x := make([]float32, len(mcPrm.xIni)) // current position
xT := make([]float32, len(mcPrm.xIni)) // trial position
// nout := uint32(1) // counter for printing output
runAcc := accRateIdeal // Running acceptance rate, exponentially weighted
copy(x, mcPrm.xIni)
fOld := ackley.Ackley(x) // Initial function value
......@@ -194,13 +193,10 @@ func doRun(mcPrm *mcPrm) error {
nRunAcc := nRunAccAdj // Every nRunAccAdj, try adjusting the step size.
const runMult = 0.99
xDlta := mcPrm.xDlta // Adaptable step size
if mcPrm.fOutName == "test9d.csv" {
breaker()
}
saveStep(&cprm, 0, tmprtr, x, fOld)
for n := uint32(0); n < mcPrm.nStep; n++ {
var acc bool
var accept bool
nRunAcc--
if nRunAcc == 0 { // Do we want to try adjusting the step size ?
xDlta = nRunAdj(mcPrm, n, runAcc, xDlta)
......@@ -209,17 +205,16 @@ func doRun(mcPrm *mcPrm) error {
newx(x, xT, cprm.rand, xDlta)
fTrial = ackley.Ackley(xT)
if fOld < fTrial {
r := cprm.rand.Float64() // Make the decision as to whether to
delta := fTrial - fOld // accept or reject the trial
t := math.Exp(-delta / tmprtr)
if r < t {
acc = true
}
if fTrial <= fOld {
accept = true
} else {
acc = true
delta := fTrial - fOld // Make the decision as to whether to
t := math.Exp(-delta / tmprtr) // accept or reject the trial
if cprm.rand.Float64() < t {
accept = true
}
}
if acc == true {
if accept == true {
nAcc++
runAcc = runMult*runAcc + (1.0 - runMult)
copy(x, xT)
......@@ -227,26 +222,23 @@ func doRun(mcPrm *mcPrm) error {
saveStep(&cprm, n+1, tmprtr, x, fTrial)
} else { // update the running estimate of acceptance
runAcc = runMult * runAcc
// saveStep(&cprm, n+1, tmprtr, x, fTrial) // debugging delete me
}
if cprm.coolme {
tmprtr *= cprm.coolMult
}
}
if mcPrm.fOutName == "test9d.csv" {
breaker()
}
// 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, mcPrm.fPltName, cprm.fplotme)
if err != nil {
defer fmt.Println("n accepted:", nAcc, "of", mcPrm.nStep+1)
if err := plotfWrt(&cprm, mcPrm.fPltName); err != nil {
return err
}
err = plotxWrt(&cprm, mcPrm.xPltName, len(mcPrm.xIni))
fmt.Println("n accepted:", nAcc, "of", mcPrm.nStep+1)
if err := plotxWrt(&cprm, mcPrm.xPltName, len(mcPrm.xIni)); err != nil {
return err
}
return nil
}
......@@ -59,13 +59,14 @@ func (rng *range2) GetTicks(r chart.Renderer, cs chart.Style, vf chart.ValueForm
}
// plotfWrt writes out a plot of function values to the given
// io.writer. We do not close the file. The caller opened it,
// so he should close it.
func plotfWrt(xdata, ydata []float64, tmprtrData []float64,
fname string, plotme bool) error {
if !plotme {
// filename
func plotfWrt(cprm *cprm, fname string) error {
if !cprm.fplotme {
return nil
}
xdata := cprm.plotnstp // We just unpack for readability
ydata := cprm.plotf
tmprtrData := cprm.plotTmprtr
xaxis := chart.XAxis{
Name: "step",
......@@ -127,7 +128,6 @@ func plotfWrt(xdata, ydata []float64, tmprtrData []float64,
return nil
}
func breaker() {}
// plotxWrt
// For each dimension, allocate space. Copy elements over from the long array.
......
......@@ -12,7 +12,6 @@ package main
{"final_temp", "1"},
{"n_step", "1000"},
{"n_run", "1"},
{"n_output", "500"},
{"x_ini", "3,4,5"},
{"x_delta", "0.5"},
{"seed", "1637"},
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment