-
Notifications
You must be signed in to change notification settings - Fork 24
/
tweak.go
228 lines (219 loc) · 6.39 KB
/
tweak.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) 2024, The Emergent Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build not
package params
import (
"cogentcore.org/core/math32"
)
func tweakValue(msd, fact, exp10 float32, isRmdr bool) float32 {
if isRmdr {
return math32.Truncate(msd+fact*math32.Pow(10, exp10), 3)
}
return math32.Truncate(fact*math32.Pow(10, exp10), 3)
}
// Tweak returns parameters to try below and above the given value.
// log: use the quasi-log scheme: 1, 2, 5, 10 etc. Only if val is one of these vals.
// incr: use increments around current value: e.g., if .5, returns .4 and .6.
// These apply to the 2nd significant digit (remainder after most significant digit)
// if that is present in the original value.
func Tweak(v float32, log, incr bool) []float32 {
ex := math32.Floor(math32.Log10(v))
base := math32.Pow(10, ex)
basem1 := math32.Pow(10, ex-1)
fact := math32.Round(v / base)
msd := tweakValue(0, fact, ex, false)
rmdr := math32.Round((v - msd) / basem1)
var vals []float32
sv := fact
isRmdr := false
if rmdr != 0 {
if rmdr < 0 {
msd = tweakValue(0, fact-1, ex, false)
rmdr = math32.Round((v - msd) / basem1)
}
sv = rmdr
ex--
isRmdr = true
}
switch sv {
case 1:
if log {
vals = append(vals, tweakValue(msd, 5, ex-1, isRmdr), tweakValue(msd, 2, ex, isRmdr))
}
if incr {
vals = append(vals, tweakValue(msd, 9, ex-1, isRmdr))
vals = append(vals, tweakValue(msd, 1.1, ex, isRmdr))
}
case 2:
if log {
vals = append(vals, tweakValue(msd, 1, ex, isRmdr), tweakValue(msd, 5, ex, isRmdr))
}
if incr {
if !log {
vals = append(vals, tweakValue(msd, 1, ex, isRmdr))
}
vals = append(vals, tweakValue(msd, 3, ex, isRmdr))
}
case 5:
if log {
vals = append(vals, tweakValue(msd, 2, ex, isRmdr), tweakValue(msd, 1, ex+1, isRmdr))
}
if incr {
vals = append(vals, tweakValue(msd, 4, ex, isRmdr), tweakValue(msd, 6, ex, isRmdr))
}
case 9:
vals = append(vals, tweakValue(msd, 8, ex, isRmdr), tweakValue(msd, 1, ex+1, isRmdr))
default:
vals = append(vals, tweakValue(msd, sv-1, ex, isRmdr), tweakValue(msd, sv+1, ex, isRmdr))
}
return vals
}
// Tweaks holds parameter tweak values associated with one parameter selector.
// Has all the object values affected for a given parameter within one
// selector, that has a tweak hyperparameter set.
type Tweaks struct {
// the parameter path for this param
Param string
// the param selector that set the specific value upon which tweak is based
Sel *Sel
// the search values for all objects covered by this selector
Search []SearchValues
}
// TweaksFromHypers uses given hyper parameters to generate a list of
// parameter values to search, based on simple Tweak values relative to the current
// param starting value, as specified by the .Hypers params. "Tweak" options:
// * log = logarithmic 1, 2, 5, 10 intervals
// * incr = increment by +/- ".1" (e.g., if .5, then .4, .6)
// * list of comma-delimited set of values in square brackets, e.g.: "[1.5, 1.2, 1.8]"
// The resulting search values are organized by the most specific selector that generated
// the final parameter value, upon which the param tweak was based.
// func TweaksFromHypers(hypers Flex) []*Tweaks {
// var tweaks []*Tweaks
// sels := make(map[*Sel]Flex)
//
// fkeys := maps.Keys(hypers)
// slices.Sort(fkeys)
// for _, fnm := range fkeys {
// flx := hypers[fnm]
// hyps := flx.Object.(Hypers)
// hkeys := maps.Keys(hyps)
// slices.Sort(hkeys)
// for _, ppath := range hkeys {
// hv := hyps[ppath]
// vl := hv["Val"]
// for _, sel := range flx.History {
// pv, has := sel.Params[ppath]
// if !has {
// continue
// }
// if vl != pv {
// continue
// }
// fm, ok := sels[sel]
// if !ok {
// fm = make(Flex)
// }
// sflex, ok := fm[fnm]
// if !ok {
// sflex = &FlexVal{}
// sflex.CopyFrom(flx)
// sflex.Object = make(Hypers)
// fm[fnm] = sflex
// }
// shyps := sflex.Object.(Hypers)
// _, ok = shyps[ppath]
// if !ok {
// shyps[ppath] = hv
// }
// sflex.Object = shyps
// sels[sel] = fm
// }
// }
// }
//
// slnms := make(map[string]*Sel)
// for sel := range sels {
// slnms[sel.Sel] = sel
// }
// slsort := maps.Keys(slnms)
// slices.Sort(slsort)
//
// for _, slnm := range slsort {
// sel := slnms[slnm]
// flx := sels[sel]
// // fmt.Println(reflectx.StringJSON(sel), "\n", reflectx.StringJSON(flx))
// var f0 *FlexVal
// for _, fv := range flx {
// if f0 == nil {
// f0 = fv
// break
// }
// }
// hyps := f0.Object.(Hypers)
// hkeys := maps.Keys(hyps)
// slices.Sort(hkeys)
// for _, ppath := range hkeys {
// twk := &Tweaks{Param: ppath, Sel: sel}
// var svals []SearchValues
//
// fkeys := maps.Keys(flx)
// slices.Sort(fkeys)
// for _, fk := range fkeys {
// fv := flx[fk]
// hyp := fv.Object.(Hypers)
// vals := hyp[ppath]
// tweak, ok := vals["Tweak"]
// tweak = strings.ToLower(strings.TrimSpace(tweak))
// if !ok || tweak == "" || tweak == "false" || tweak == "-" {
// continue
// }
//
// val, ok := vals["Val"]
// if !ok {
// continue
// }
// f64, err := strconv.ParseFloat(val, 32)
// if err != nil {
// fmt.Printf("TweakFromHypers float parse error: only works for float type params. Obj: %s Param: %s val: %s parse error: %v\n", fv.Name, ppath, val, err)
// continue
// }
// start := float32(f64)
//
// sval := SearchValues{Name: fv.Name, Type: fv.Type, Path: ppath, Start: start}
//
// var pars []float32 // param vals to search
// if tweak[0] == '[' {
// err := reflectx.SetRobust(&pars, tweak)
// if err != nil {
// fmt.Println("Error processing tweak value list:", tweak, "error:", err)
// continue
// }
// } else {
// log := false
// incr := false
// if strings.Contains(tweak, "log") {
// log = true
// }
// if strings.Contains(tweak, "incr") {
// incr = true
// }
// if !log && !incr {
// fmt.Printf("Tweak value not recognized: %q\n", tweak)
// continue
// }
// pars = Tweak(start, log, incr)
// }
// if len(pars) > 0 {
// sval.Values = pars
// svals = append(svals, sval)
// }
// }
// if len(svals) > 0 {
// twk.Search = svals
// tweaks = append(tweaks, twk)
// }
// }
// }
// return tweaks
// }