go(report): aggregate all tex files tracking plots
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
610060c88f
commit
01a0ed12c4
10
algo/algo.go
10
algo/algo.go
@ -16,7 +16,7 @@ import (
|
||||
type Values []float64
|
||||
|
||||
// DoRandomSearch executes a search using the 'Random search' method.
|
||||
func DoRandomSearch(wg *sync.WaitGroup) {
|
||||
func DoRandomSearch(wg *sync.WaitGroup, m *sync.Mutex) {
|
||||
defer wg.Done()
|
||||
|
||||
printRandomSearch("starting...")
|
||||
@ -54,14 +54,17 @@ func DoRandomSearch(wg *sync.WaitGroup) {
|
||||
pLs = append(pLs, pL)
|
||||
}
|
||||
|
||||
// protect access to shared data.
|
||||
m.Lock()
|
||||
report.SavePicsToFile(pLs, "Random Search")
|
||||
m.Unlock()
|
||||
|
||||
stats.SaveTable("Random Search", algoStats)
|
||||
}
|
||||
|
||||
// DoStochasticHillClimbing performs a search using the 'Stochastic Hill
|
||||
// Climbing' method.
|
||||
func DoStochasticHillClimbing(wg *sync.WaitGroup) {
|
||||
func DoStochasticHillClimbing(wg *sync.WaitGroup, m *sync.Mutex) {
|
||||
defer wg.Done()
|
||||
|
||||
printSHC("starting...")
|
||||
@ -98,7 +101,10 @@ func DoStochasticHillClimbing(wg *sync.WaitGroup) {
|
||||
pLs = append(pLs, pL)
|
||||
}
|
||||
|
||||
// protect access to shared data.
|
||||
m.Lock()
|
||||
report.SavePicsToFile(pLs, "Stochastic Hill Climbing")
|
||||
m.Unlock()
|
||||
|
||||
stats.SaveTable("Stochastic Hill CLimbing", algoStats)
|
||||
}
|
||||
|
@ -13,10 +13,12 @@ import (
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
var m sync.Mutex
|
||||
|
||||
func TestDoRandomSearchExec(t *testing.T) {
|
||||
wg.Add(1)
|
||||
|
||||
go DoRandomSearch(&wg)
|
||||
go DoRandomSearch(&wg, &m)
|
||||
|
||||
wg.Wait()
|
||||
|
||||
@ -36,7 +38,7 @@ func TestDoRandomSearchExec(t *testing.T) {
|
||||
func TestDoSHCExec(t *testing.T) {
|
||||
wg.Add(1)
|
||||
|
||||
go DoStochasticHillClimbing(&wg)
|
||||
go DoStochasticHillClimbing(&wg, &m)
|
||||
|
||||
wg.Wait()
|
||||
|
||||
|
7
main.go
7
main.go
@ -23,11 +23,14 @@ func main() {
|
||||
|
||||
wg.Add(algoCount)
|
||||
|
||||
go algo.DoRandomSearch(&wg)
|
||||
go algo.DoStochasticHillClimbing(&wg)
|
||||
var m sync.Mutex
|
||||
|
||||
go algo.DoRandomSearch(&wg, &m)
|
||||
go algo.DoStochasticHillClimbing(&wg, &m)
|
||||
|
||||
wg.Wait()
|
||||
|
||||
report.SaveTexAllPics()
|
||||
report.Print()
|
||||
|
||||
log.Println("looks like we're done")
|
||||
|
64
report/allPics.go
Normal file
64
report/allPics.go
Normal file
@ -0,0 +1,64 @@
|
||||
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package report
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"text/template"
|
||||
"time"
|
||||
)
|
||||
|
||||
// picTexFiles holds paths to tex files including pics of a certain algo.
|
||||
type picTexFiles struct {
|
||||
Algo string
|
||||
FilePaths []string
|
||||
}
|
||||
|
||||
// allThePics contains a slice of picTexFiles and consequently paths to all pic
|
||||
// tex files generated during a single run of the program.
|
||||
type allThePics struct {
|
||||
TexFiles []picTexFiles
|
||||
}
|
||||
|
||||
var allPics = &allThePics{}
|
||||
|
||||
// SaveTexAllPics feeds all paths of generated pics to a template that creates
|
||||
// `allpics.tex` file, which then aggregates all tex files tracking plot pics
|
||||
// in one place.
|
||||
func SaveTexAllPics() {
|
||||
a := allPics
|
||||
texAllPicsFile := GetTexDir() + "allpics" + ".tex"
|
||||
tmplAllPicsFile := "report/allpics.tmpl"
|
||||
|
||||
if _, err := os.Stat(tmplAllPicsFile); err != nil {
|
||||
// TODO(me): fix this.
|
||||
// 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 "../"`)
|
||||
|
||||
tmplAllPicsFile = "../" + tmplAllPicsFile
|
||||
}
|
||||
|
||||
tmplAllPics := template.Must(template.ParseFiles(tmplAllPicsFile))
|
||||
|
||||
f, err := os.Create(texAllPicsFile)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
err = tmplAllPics.Execute(f, struct {
|
||||
AllPics allThePics
|
||||
Timestamp time.Time
|
||||
}{
|
||||
AllPics: *a,
|
||||
Timestamp: time.Now(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
16
report/allpics.tmpl
Normal file
16
report/allpics.tmpl
Normal file
@ -0,0 +1,16 @@
|
||||
% Code generated by math-optim; DO NOT EDIT.
|
||||
%
|
||||
% This file was generated by robots at
|
||||
% {{ .Timestamp }}
|
||||
% source: git.dotya.ml/wanderer/math-optim/report/allpics.tmpl
|
||||
|
||||
% This file is a part of the math-optim project.
|
||||
% project homepage: https://git.dotya.ml/wanderer/math-optim/
|
||||
{{ range $i, $v := .AllPics.TexFiles }}
|
||||
\section{Algo: {{ $v.Algo -}} }
|
||||
{{- range $j, $u := $v.FilePaths }}
|
||||
\include{ {{- $u -}} }
|
||||
{{- end }}
|
||||
{{ end -}}
|
||||
|
||||
% vim: ft=gotexttmpl.tex ts=2 sts=2 sw=2 bs=2 expandtab
|
@ -6,6 +6,7 @@ package report
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
@ -38,6 +39,10 @@ func NewPicList() *PicList {
|
||||
// SavePicsToFile saves each pic list for all bench funcs of a specified algo
|
||||
// to a file.
|
||||
func SavePicsToFile(pls []PicList, algoName string) {
|
||||
var paths []string
|
||||
|
||||
ptf := picTexFiles{Algo: algoName}
|
||||
|
||||
for _, p := range pls {
|
||||
safeName := util.SanitiseFName(p.Algo + "-" + p.Bench)
|
||||
texPicsFile := GetTexDir() + "pics-" + safeName + ".tex"
|
||||
@ -54,6 +59,10 @@ func SavePicsToFile(pls []PicList, algoName string) {
|
||||
|
||||
tmplPics := template.Must(template.ParseFiles(tmplPicsFile))
|
||||
|
||||
paths = append(paths, texPicsFile)
|
||||
// keep the slice sorted.
|
||||
sort.Strings(paths)
|
||||
|
||||
// make sure the output dir exists, else die early.
|
||||
if err := util.CreatePath(GetTexDir()); err != nil {
|
||||
log.Fatalln(err)
|
||||
@ -81,4 +90,7 @@ func SavePicsToFile(pls []PicList, algoName string) {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
ptf.FilePaths = paths
|
||||
allPics.TexFiles = append(allPics.TexFiles, ptf)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user