Transpiled version (11147L) is out of date.
1 | // Sc = type of scores (e.g. Double) |
2 | // Sc can also be the actual instance created (e.g. a strategy) |
3 | // If Sc is not Comparable, you can supply an additional |
4 | // comparisonFunction. |
5 | sclass TunableParameters<Sc> { |
6 | //replace Scrd with GenericallyScored<MapSO, Sc>. |
7 | |
8 | class Scrd extends Var<MapSO> { |
9 | gettable Sc score; |
10 | |
11 | *() {} |
12 | *(MapSO a, Sc score) { super(a); this.score = score; } |
13 | |
14 | toString { |
15 | ret actualScoreFunction.get(score) + ": " + str(get()); |
16 | } |
17 | |
18 | bool isBest() { ret this == best; } |
19 | bool tiesBest() { ret compareScrd(this, best) == 0; } |
20 | |
21 | O actualScore() { ret actualScoreFunction.get(score); } |
22 | } |
23 | |
24 | settable IF1<MapSO, Sc> scoreFunction; |
25 | settable IF2_OOInt<O> comparisonFunction; |
26 | settable IF1<Sc, O> actualScoreFunction = l1 id; |
27 | |
28 | gettable Scrd best; |
29 | new LinkedHashMap<S, Parameter> parameters; |
30 | new L<Parameter> parameterList; |
31 | gettable volatile long instancesTested; |
32 | |
33 | event testingInstance(MapSO instance); |
34 | event fireInstanceScored(Scrd scored); |
35 | event newBest(Scrd instance); |
36 | |
37 | abstract class Parameter<A> { |
38 | settable S name; |
39 | settable A defaultValue; |
40 | |
41 | abstract A randomValue(); |
42 | |
43 | A get(MapSO instance) { |
44 | ret (A) instance.get(name); |
45 | } |
46 | |
47 | TunableParameters<Sc> tuner() { ret TunableParameters.this; } |
48 | } |
49 | |
50 | class IntRangeParameter extends Parameter<Int> { |
51 | settable IntRange range; |
52 | settable int step = 1; |
53 | |
54 | Int randomValue() { ret random(range); } |
55 | |
56 | selfType fixedValue(int value) { |
57 | defaultValue(value); |
58 | range(intRange(value, value)); |
59 | this; |
60 | } |
61 | } |
62 | |
63 | class DoubleRangeParameter extends Parameter<Double> { |
64 | settable DoubleRange range; |
65 | settable double step = .1; |
66 | |
67 | Double randomValue() { ret random(range); } |
68 | |
69 | selfType fixedValue(double value) { |
70 | defaultValue(value); |
71 | range(doubleRange(value, value)); |
72 | this; |
73 | } |
74 | } |
75 | |
76 | <A extends Parameter> A add(A p) { |
77 | parameters.put(p.name(), p); |
78 | parameterList.add(p); |
79 | ret p; |
80 | } |
81 | |
82 | IntRangeParameter intParam(S name, int defaultValue, int min, int max) { |
83 | new IntRangeParameter p; |
84 | p.range(intRange(min, max)).+defaultValue.+name; |
85 | ret add(p); |
86 | } |
87 | |
88 | DoubleRangeParameter doubleParam(S name, double defaultValue, double min, double max, double step default .1) { |
89 | new DoubleRangeParameter p; |
90 | p.range(doubleRange(min, max)).+step.+defaultValue.+name; |
91 | ret add(p); |
92 | } |
93 | |
94 | MapSO randomInstance() { |
95 | MapSO map = new LinkedHashMap; |
96 | for (param : parameterList) |
97 | map.put(param.name(), param.randomValue()); |
98 | ret map; |
99 | } |
100 | |
101 | MapSO defaultInstance() { |
102 | MapSO map = new LinkedHashMap; |
103 | for (param : parameterList) |
104 | map.put(param.name(), param.defaultValue()); |
105 | ret map; |
106 | } |
107 | |
108 | Scrd tryARandomCombination() { |
109 | ret tryInstance(randomInstance()); |
110 | } |
111 | |
112 | Scrd tryDefaultValues() { |
113 | ret tryInstance(defaultInstance()); |
114 | } |
115 | |
116 | // replaces the parameter with a random value |
117 | L<Scrd> varyEachParameterOnce(MapSO instance) { |
118 | if (instance == null) null; |
119 | new L<Scrd> out; |
120 | for (param : parameterList) { |
121 | O value = param.randomValue(); |
122 | if (eq(param.get(instance), value)) |
123 | continue; |
124 | MapSO instance2 = cloneMap(instance); |
125 | instance2.put(param.name(), value); |
126 | out.add(tryInstance(instance2)); |
127 | } |
128 | ret out; |
129 | } |
130 | |
131 | Sc score(MapSO instance) { |
132 | ++instancesTested; |
133 | testingInstance(instance); |
134 | ret scoreFunction.get(instance); |
135 | } |
136 | |
137 | Scrd scored(MapSO instance) { |
138 | Scrd scored = new(instance, score(instance)); |
139 | fireInstanceScored(scored); |
140 | ret scored; |
141 | } |
142 | |
143 | Scrd tryInstance(MapSO instance) { |
144 | if (instance == null) null; |
145 | Scrd scored = scored(instance); |
146 | tryInstance(scored); |
147 | ret scored; |
148 | } |
149 | |
150 | void tryInstance(Scrd scored) { |
151 | if (scored == null) ret; |
152 | if (best == null || compareScrd(scored, best) > 0) { |
153 | best = scored; |
154 | newBest(scored); |
155 | } |
156 | } |
157 | |
158 | int compareScrd(Scrd a, Scrd b) { |
159 | Sc scoreA = a.score(); |
160 | Sc scoreB = b.score(); |
161 | ret comparisonFunction == null ? cmp(scoreA, scoreB) : comparisonFunction.get(actualScoreFunction.get(scoreA), actualScoreFunction.get(scoreB)); |
162 | } |
163 | |
164 | toString { |
165 | ret "Best: " + best + " (parameters: " + keys(parameters) + ")"; |
166 | } |
167 | |
168 | MapSO bestOrDefaultInstance() { |
169 | if (best == null) |
170 | tryDefaultValues(); |
171 | ret best!; |
172 | } |
173 | } |
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1036267 |
Snippet name: | TunableParameters - a set of auto-tunable parameters |
Eternal ID of this version: | #1036267/40 |
Text MD5: | b1a0fd497a451041a6a69012e41ec703 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2023-03-22 20:18:47 |
Source code size: | 4576 bytes / 173 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 242 / 519 |
Version history: | 39 change(s) |
Referenced in: | [show references] |