Skip to content

Commit

Permalink
add DeltaFnTol option
Browse files Browse the repository at this point in the history
  • Loading branch information
Aizen committed Apr 22, 2024
1 parent f748db8 commit 3219d25
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type opt_config struct {
LR_sigma float64
Momentum float64
SigmaTol float64
DeltaFnTol float64
Verbose bool
Seed uint64
}
Expand All @@ -63,6 +64,7 @@ func Config() opt_config {
cfg.LR_sigma = 0.15
cfg.Momentum = 0.75
cfg.SigmaTol = 1e-14
cfg.DeltaFnTol = 1e-14
cfg.Verbose = false
cfg.Seed = 798371291237
return cfg
Expand Down Expand Up @@ -149,7 +151,10 @@ func Opt(fn func([]float64) float64, mu []float64, sigma []float64, cfg opt_conf
mu[j] += v[j]
sigma[j] *= math.Exp(cfg.LR_sigma * g_log_sigma[j])
}
if slices.Max(sigma) < cfg.SigmaTol {
if slices.Max(sigma) <= cfg.SigmaTol || math.Abs(pop[0].C-pop[pop_n-1].C) <= cfg.DeltaFnTol {
if cfg.Verbose {
log.Println("END OPT: Convergence reached.")
}
break
}
if cfg.Verbose {
Expand Down
7 changes: 6 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,16 @@ func TestVerbose(t *testing.T) {
log.SetOutput(buf)
cfg.Verbose = true
cfg.Generations = 10
//Run it for few generations with a cost function to test for iteration verbosity
Opt(func(f []float64) float64 {
return Probability(f[0])
}, []float64{0.0, 0.0}, []float64{1.0, 1.0}, cfg)
//run it with a constant cost to hit convergence verbosity
Opt(func(f []float64) float64 {
return 0.0
}, []float64{0.0, 0.0}, []float64{1.0, 1.0}, cfg)
str := buf.String()
if strings.Count(str, "GoES:") != cfg.Generations {
if strings.Count(str, "GoES:") != cfg.Generations || strings.Count(str, "END OPT:") != 1 {
t.Error(str)
}
}
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ The **`Config()` function:** returns a struct that allows fine-tuning the optimi
* `LR_mu`: Learning rate for mean vector update (default: 0.6).
* `LR_sigma`: Learning rate for standard deviation vector update (default: 0.15).
* `Momentum`: Momentum coefficient for velocity update (default: 0.93).
* `SigmaTol`: Tolerance threshold for stopping the optimization (default: 1e-12).
* `SigmaTol`: Tolerance threshold on parameter variance for stopping the optimization (default: 1e-14).
* `DeltaFnTol`: Tolerance threshold on cost function variance for stopping the optimization (default: 1e-14).
* `Seed`: A uint64 value to seed the optimiser rng.
* `Verbose`: Flag to enable detailed logging of optimization progress during each generation (default: false).

Expand Down

0 comments on commit 3219d25

Please sign in to comment.