Skip to content
Snippets Groups Projects
Commit 1416547d authored by andrew-torda's avatar andrew-torda
Browse files

This will be the driver for playing with Monte Carlo and the Ackley function....

This will be the driver for playing with Monte Carlo and the Ackley function. This version expects a set of command line arguments to control a run. This was a bad idea. I am going to replace it with a parameter file.
parent 39773c1e
Branches
No related tags found
No related merge requests found
main.go 0 → 100644
// Aug 2021
// Ackley_mc is for playing with Monte Carlo or simulated annealing on the
// ackley function in N dimensions.
//
// ackley_mc [-n nthread] [-v] ini fnl seed n_step x_ini
// so,
// go run . 200 100 1637 300 1,2,3
// will start at 200, cool down to 100 over 300 steps and use 1637 as the
// randome number seed. It will be a 3 dimensional function starting from the
// coordinates 1,2,3
package main
import (
"os"
"fmt"
"flag"
"strconv"
"strings"
"ackley_mc/ackley"
)
const ndimDflt = 2
const (
exitSuccess = iota
exitFailure
)
type mcPrm struct {
iniTmp, fnlTmp float32
xIni []float32
nStep uint32
nRun uint32
verbose bool
}
var seed int64
// usage
func usage() {
u := `
[options] seed initial_temp final_temp num_steps initial_x
and the initial_x is a comma separated list of values like "7,7,7"`
fmt.Fprintln(flag.CommandLine.Output(), "usage of %s", os.Args[0], u)
flag.PrintDefaults()
}
// cmdln sorts out the command line
func cmdLn() (mcPrm, error) {
var mcPrm mcPrm = mcPrm {nRun: 1, verbose: true}
var utmp uint
flag.UintVar(&utmp, "r", 1, "number of runs")
flag.BoolVar(&mcPrm.verbose, "v", true, "verbose")
flag.Usage = usage
flag.Parse()
mcPrm.nRun = uint32 (utmp)
var err error
tooFewArgs := fmt.Errorf ("Too few command line arguments")
getf := func (s string) float32 {
if err != nil {
return 0.0 }
if s == "" {
err = tooFewArgs
}
r := 0.
r, err = strconv.ParseFloat(s, 32)
return float32(r)
}
getu := func (s string) uint32 {
if err != nil {
return 0}
if s == "" {
err = tooFewArgs
return 0
}
var u uint64
u, err = strconv.ParseUint(s, 10, 32)
return uint32(u)
}
getx := func (s string) []float32 {
if err != nil {
return nil
}
if s == "" {
err = tooFewArgs
return nil
}
y := strings.Split(s, ",")
if len(y) < 1 {
err = fmt.Errorf("broke trying to get initial x values")
return nil
}
x := make ([]float32, len(y))
for i, s := range y {
r := 0.
if r, err = strconv.ParseFloat(s, 32); err != nil {
return nil
}
x[i] = float32(r)
}
return x
}
if flag.NArg() != 4 && false{
flag.Usage()
return mcPrm, fmt.Errorf("Wrong number of command line args")
}
mcPrm.iniTmp = getf(flag.Arg(0))
mcPrm.fnlTmp = getf(flag.Arg(1))
seed = int64 (getu(flag.Arg(2)))
mcPrm.nStep = getu(flag.Arg(3))
mcPrm.xIni = getx (flag.Arg(4))
if err != nil {
return mcPrm, err
}
if mcPrm.verbose {
fmt.Println ("initial temp:", mcPrm.iniTmp,
"\nfinal temp:", mcPrm.fnlTmp,
"\nseed:", seed,
"\nn steps:", mcPrm.nStep,
"\nn runs:", mcPrm.nRun,
"\ninitial x:", mcPrm.xIni)
}
return mcPrm, nil
}
// myMain
func myMain () error {
var mcPrm mcPrm
var err error
if mcPrm, err = cmdLn(); err != nil {
return err
}
x := [1]float32{0.001}
fmt.Println("ackley ", ackley.Ackley(x[:]))
fmt.Println("mcPrm", mcPrm)
doRun (mcPrm)
doRun (mcPrm)
return nil
}
func main() {
if err := myMain (); err != nil {
fmt.Println (os.Stderr, err)
os.Exit (exitFailure)
}
os.Exit (exitSuccess)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment