go(plot): use chans, output latex from pic.tmpl
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
3401a2e58e
commit
0169196a93
31
algo/algo.go
31
algo/algo.go
@ -7,6 +7,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"git.dotya.ml/wanderer/math-optim/bench"
|
"git.dotya.ml/wanderer/math-optim/bench"
|
||||||
|
"git.dotya.ml/wanderer/math-optim/report"
|
||||||
"git.dotya.ml/wanderer/math-optim/stats"
|
"git.dotya.ml/wanderer/math-optim/stats"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,8 +15,6 @@ import (
|
|||||||
// methods over it.
|
// methods over it.
|
||||||
type Values []float64
|
type Values []float64
|
||||||
|
|
||||||
var plotWg sync.WaitGroup
|
|
||||||
|
|
||||||
// DoRandomSearch executes a search using the 'Random search' method.
|
// DoRandomSearch executes a search using the 'Random search' method.
|
||||||
func DoRandomSearch(wg *sync.WaitGroup) {
|
func DoRandomSearch(wg *sync.WaitGroup) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
@ -41,15 +40,19 @@ func DoRandomSearch(wg *sync.WaitGroup) {
|
|||||||
algoStats[i] = s
|
algoStats[i] = s
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range algoStats {
|
pCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
|
||||||
plotWg.Add(1)
|
|
||||||
|
|
||||||
go plotAllDims(algoStats[i], "plot", ".svg", &plotWg)
|
for i := range algoStats {
|
||||||
|
go plotAllDims(algoStats[i], "plot", ".svg", pCh)
|
||||||
|
}
|
||||||
|
|
||||||
|
for range algoStats {
|
||||||
|
pL := <-pCh
|
||||||
|
|
||||||
|
report.SavePicsToFile(pL, "Random Search")
|
||||||
}
|
}
|
||||||
|
|
||||||
stats.SaveTable("Random Search", algoStats)
|
stats.SaveTable("Random Search", algoStats)
|
||||||
|
|
||||||
plotWg.Wait()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DoStochasticHillClimbing performs a search using the 'Stochastic Hill
|
// DoStochasticHillClimbing performs a search using the 'Stochastic Hill
|
||||||
@ -77,15 +80,19 @@ func DoStochasticHillClimbing(wg *sync.WaitGroup) {
|
|||||||
algoStats[i] = s
|
algoStats[i] = s
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, algoStat := range algoStats {
|
pCh := make(chan report.PicList, funcCount*len(bench.Dimensions))
|
||||||
plotWg.Add(1)
|
|
||||||
|
|
||||||
go plotAllDims(algoStat, "plot", ".svg", &plotWg)
|
for _, algoStat := range algoStats {
|
||||||
|
go plotAllDims(algoStat, "plot", ".svg", pCh)
|
||||||
|
}
|
||||||
|
|
||||||
|
for range algoStats {
|
||||||
|
pL := <-pCh
|
||||||
|
|
||||||
|
report.SavePicsToFile(pL, "Stochastic Hill Climbing")
|
||||||
}
|
}
|
||||||
|
|
||||||
stats.SaveTable("Stochastic Hill CLimbing", algoStats)
|
stats.SaveTable("Stochastic Hill CLimbing", algoStats)
|
||||||
|
|
||||||
plotWg.Wait()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newValues() *Values {
|
func newValues() *Values {
|
||||||
|
37
algo/plot.go
37
algo/plot.go
@ -6,7 +6,6 @@ package algo
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.dotya.ml/wanderer/math-optim/report"
|
"git.dotya.ml/wanderer/math-optim/report"
|
||||||
@ -21,19 +20,25 @@ import (
|
|||||||
|
|
||||||
const preferredFontStyle = "Mono"
|
const preferredFontStyle = "Mono"
|
||||||
|
|
||||||
// violating the limit of 30, TODO(me): split this up.
|
// edit: now not violating gocognit limit of 30, TODO(me): still split this up.
|
||||||
// nolint: gocognit
|
func plotAllDims(algoStats []stats.Stats, fPrefix, fExt string, ch chan report.PicList) {
|
||||||
func plotAllDims(algoStats []stats.Stats, fPrefix, fExt string, wg *sync.WaitGroup) {
|
|
||||||
defer wg.Done() // does this work with panic in the mix? TODO(me): find out.
|
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
picsDir := report.GetPicsDir()
|
picsDir := report.GetPicsDir()
|
||||||
|
|
||||||
|
// create picsDir (if not exists), fail on error, no point in going on
|
||||||
|
// parsing algoStats and computing images if we cannot save them.
|
||||||
if err := util.CreatePath(picsDir); err != nil {
|
if err := util.CreatePath(picsDir); err != nil {
|
||||||
log.Println(err)
|
log.Fatalln("went to create picsDir, there was an issue: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pL := report.NewPicList()
|
||||||
|
pics := make([]report.Pic, 0)
|
||||||
|
|
||||||
|
// since the algoStats only contains results of a single algo, it's safe to
|
||||||
|
// set the value like this.
|
||||||
|
pL.Algo = algoStats[0].Algo
|
||||||
|
|
||||||
pWidth := 13 * vg.Centimeter
|
pWidth := 13 * vg.Centimeter
|
||||||
pHeight := 13 * vg.Centimeter
|
pHeight := 13 * vg.Centimeter
|
||||||
|
|
||||||
@ -42,10 +47,18 @@ func plotAllDims(algoStats []stats.Stats, fPrefix, fExt string, wg *sync.WaitGro
|
|||||||
|
|
||||||
for _, s := range algoStats {
|
for _, s := range algoStats {
|
||||||
p := plot.New()
|
p := plot.New()
|
||||||
|
pic := report.NewPic()
|
||||||
|
|
||||||
p.Title.Text = s.Algo + ", D=" + fmt.Sprint(s.Dimens) +
|
p.Title.Text = s.Algo + ", D=" + fmt.Sprint(s.Dimens) +
|
||||||
", func=" + s.BenchFuncStats[0].BenchName +
|
", func=" + s.BenchFuncStats[0].BenchName +
|
||||||
", G=" + fmt.Sprint(s.Generations) +
|
", G=" + fmt.Sprint(s.Generations) +
|
||||||
", I=" + fmt.Sprint(s.Iterations)
|
", I=" + fmt.Sprint(s.Iterations)
|
||||||
|
|
||||||
|
pic.Caption = p.Title.Text
|
||||||
|
// since a single stat slice of algoStats only contains results of a
|
||||||
|
// single bench func, it's safe to set the value like this.
|
||||||
|
pL.Bench = s.BenchFuncStats[0].BenchName
|
||||||
|
|
||||||
p.X.Label.Text = "Generations"
|
p.X.Label.Text = "Generations"
|
||||||
p.X.Label.TextStyle.Font.Variant = preferredFontStyle
|
p.X.Label.TextStyle.Font.Variant = preferredFontStyle
|
||||||
p.X.Label.TextStyle.Font.Weight = 1 // Medium
|
p.X.Label.TextStyle.Font.Weight = 1 // Medium
|
||||||
@ -105,6 +118,9 @@ func plotAllDims(algoStats []stats.Stats, fPrefix, fExt string, wg *sync.WaitGro
|
|||||||
fmt.Sprint(s.Generations) + "G-" +
|
fmt.Sprint(s.Generations) + "G-" +
|
||||||
fmt.Sprint(len(dim.Solution)) + "I"
|
fmt.Sprint(len(dim.Solution)) + "I"
|
||||||
|
|
||||||
|
// set pic file path (later used in tmpl generation)
|
||||||
|
pic.FilePath = filename
|
||||||
|
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
info := "saving img to file: " + filename + fExt +
|
info := "saving img to file: " + filename + fExt +
|
||||||
" [generated in " + fmt.Sprint(elapsed) + "]"
|
" [generated in " + fmt.Sprint(elapsed) + "]"
|
||||||
@ -123,6 +139,13 @@ func plotAllDims(algoStats []stats.Stats, fPrefix, fExt string, wg *sync.WaitGro
|
|||||||
); err != nil {
|
); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// save pic.
|
||||||
|
pics = append(pics, *pic)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pL.Pics = pics
|
||||||
|
|
||||||
|
ch <- *pL
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,16 @@ func SavePicsToFile(p PicList, algoName string) {
|
|||||||
safeName := util.SanitiseFName(p.Algo + "-" + p.Bench)
|
safeName := util.SanitiseFName(p.Algo + "-" + p.Bench)
|
||||||
texPicsFile := GetTexDir() + "pics-" + safeName + ".tex"
|
texPicsFile := GetTexDir() + "pics-" + safeName + ".tex"
|
||||||
tmplPicsFile := "report/pics.tmpl"
|
tmplPicsFile := "report/pics.tmpl"
|
||||||
|
|
||||||
|
if _, err := os.Stat(tmplPicsFile); err != nil {
|
||||||
|
// TODO(me): fix this, same as in table.go.
|
||||||
|
// this block is relevant for the unit test path, somehow the file is
|
||||||
|
// not found as defined above.
|
||||||
|
log.Println(err, `, weird test behaviour , prepending "../"`)
|
||||||
|
|
||||||
|
tmplPicsFile = "../" + tmplPicsFile
|
||||||
|
}
|
||||||
|
|
||||||
tmplPics := template.Must(template.ParseFiles(tmplPicsFile))
|
tmplPics := template.Must(template.ParseFiles(tmplPicsFile))
|
||||||
|
|
||||||
// make sure the output dir exists, else die early.
|
// make sure the output dir exists, else die early.
|
||||||
|
Loading…
Reference in New Issue
Block a user