go: add RandomSearch,SHC algos
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
0f8f3bbef3
commit
ab14e97f16
|
@ -0,0 +1,28 @@
|
|||
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package algo
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// DoRandomSearch executes a search using the 'Random search' method.
|
||||
func DoRandomSearch(wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
|
||||
if err := printRandomSearch("starting..."); err != nil {
|
||||
log.Printf("randomSearch: error printing RandomSearch to stderr: %q", err)
|
||||
}
|
||||
}
|
||||
|
||||
// DoStochasticHillClimbing performs a search using the 'Stochastic Hill
|
||||
// Climbing' method.
|
||||
func DoStochasticHillClimbing(wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
|
||||
if err := printSHC("starting..."); err != nil {
|
||||
log.Printf("shc: error printing SHC to stderr: %q", err)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package algo
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
func TestDoRandomSearchExec(t *testing.T) {
|
||||
wg.Add(1)
|
||||
|
||||
go DoRandomSearch(&wg)
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestDoSHCExec(t *testing.T) {
|
||||
wg.Add(1)
|
||||
|
||||
go DoStochasticHillClimbing(&wg)
|
||||
|
||||
wg.Wait()
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package algo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func getRandomSearchLogPrefix() string {
|
||||
return " *** random search:"
|
||||
}
|
||||
|
||||
func fmtRandomSearchOut(input string) string {
|
||||
return getRandomSearchLogPrefix() + " " + input
|
||||
}
|
||||
|
||||
func printRandomSearch(input string) error {
|
||||
_, err := fmt.Fprintln(os.Stderr, fmtRandomSearchOut(input))
|
||||
|
||||
return err
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package algo
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestFmtRandomSearchOut(t *testing.T) {
|
||||
want := " *** random search: this is so random..."
|
||||
got := fmtRandomSearchOut("this is so random...")
|
||||
|
||||
if want != got {
|
||||
t.Errorf("strings do not equal, want: %q, got: %q", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintRandomSearch(t *testing.T) {
|
||||
if err := printRandomSearch("whatever"); err != nil {
|
||||
t.Errorf("error while printing to stderr: %q", err)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package algo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func getSHCLogPrefix() string {
|
||||
return " *** stochastic hill climbing:"
|
||||
}
|
||||
|
||||
func fmtSHCOut(input string) string {
|
||||
return getSHCLogPrefix() + " " + input
|
||||
}
|
||||
|
||||
func printSHC(input string) error {
|
||||
_, err := fmt.Fprintln(os.Stderr, fmtSHCOut(input))
|
||||
|
||||
return err
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2022 wanderer <a_mirre at utb dot cz>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package algo
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestFmtSHCOut(t *testing.T) {
|
||||
want := " *** stochastic hill climbing: oh what a mountain that is!"
|
||||
got := fmtSHCOut("oh what a mountain that is!")
|
||||
|
||||
if want != got {
|
||||
t.Errorf("strings do not equal, want: %q, got: %q", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintSHC(t *testing.T) {
|
||||
if err := printSHC("whatever"); err != nil {
|
||||
t.Errorf("error while printing to stderr: %q", err)
|
||||
}
|
||||
}
|
20
main.go
20
main.go
|
@ -5,11 +5,27 @@
|
|||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
"git.dotya.ml/wanderer/math-optim/algo"
|
||||
)
|
||||
|
||||
var version = "development"
|
||||
|
||||
func main() {
|
||||
log.Println("starting math-optim version ", version)
|
||||
log.Println("hey")
|
||||
log.Println("starting math-optim version", "'"+version+"'")
|
||||
|
||||
// atm we're only doing Random search and SHC
|
||||
algoCount := 2
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
wg.Add(algoCount)
|
||||
|
||||
go algo.DoRandomSearch(&wg)
|
||||
go algo.DoStochasticHillClimbing(&wg)
|
||||
|
||||
wg.Wait()
|
||||
|
||||
log.Println("looks like we're done")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue