-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiggle.sh
265 lines (237 loc) · 5.97 KB
/
wiggle.sh
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/bash
#
# wiggle.sh: Optimizes parameters of a program.
#
# Given a command to run a program with its parameters, that simultaniously returns a number which is to be maximized,
# wiggle.sh parses the integer and float numbers of those parameters and continues to run the command with those
# new variations until a local maximum is achieved.
#
# Running time for x iterations is O(1+3n*x) where n is the number of numeric arguments
#
# author: Gustav Henning 2016
#
DEBUG=0
#set -x
# returns 1 if arg is a number
isNum() {
regExp='^[+-]?([0-9]+\.?|[0-9]*\.[0-9]+)$'
if [[ $@ =~ $regExp ]]; then
echo 1
else
echo 0
fi
}
# returns 1 if arg passed is a float
isFloat() {
regExp='^[+-]?([0-9]*\.[0-9]+)$'
if [[ $1 =~ $regExp ]]; then
echo 1
else
echo 0
fi
}
# returns 1 if arg passed is an integer
isInt() {
if [ "$(isFloat $1)" -eq 1 ]; then
echo 0
else
if [ "$1" -eq "$1" ]; then
echo 1
else
echo 0
fi
fi
}
# returns the amount of numbers after the decimal point in a float.
sigFig() {
GTZ=$(echo "$1 < 0" | bc -l)
NUM=$1
NUMCHR=${#NUM}
if [ $GTZ -eq 0 ]; then
echo $(($NUMCHR - 1))
else
echo $(($NUMCHR - 2))
fi
}
deciFromSig(){
POW=$(echo 10^$1 | bc)
echo "$(echo 1/$POW | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')"
}
varyLower() {
if [[ $(isInt $1) -eq 1 ]]; then
echo $(($1 - 1))
else
SIGFIG=$(sigFig $1)
INCR=$(deciFromSig $SIGFIG)
if [ $DEBUG -eq 1 ]; then
(>&2 echo "DBG: $1 gives sigfig: $SIGFIG gives decimal: $INCR, returns: $(echo "$1 - $INCR" | bc -l)")
fi
TO_RET="$(echo "$1 - $INCR" | bc -l)"
# if TO_RET equals 0 then it should return 0.0
if [ "$(echo "$TO_RET == 0" | bc -l)" -eq 0 ];then
echo $TO_RET
else
echo 0.0
fi
fi
}
#set -x
varyHigher() { # TODO floats
if [[ $(isInt $1) -eq 1 ]]; then
echo $(($1 + 1))
else
SIGFIG=$(sigFig $1)
INCR=$(deciFromSig $SIGFIG)
if [ $DEBUG -eq 1 ]; then
(>&2 echo "DBG: $1 gives sigfig: $SIGFIG gives decimal: $INCR, returns: $(echo "$1 + $INCR" | bc -l)")
fi
TO_RET="$(echo "$1 + $INCR" | bc -l)"
# if TO_RET equals 0 then it should return 0.0
if [ "$(echo "$TO_RET == 0" | bc -l)" -eq 0 ];then
echo $TO_RET
else
echo 0.0
fi
fi
}
# returns 1 if $1 > $2, 0 if $1 == $2 and -1 if $1 < $2
compareResults() {
FLT=$(isFloat $1)
SECFLT=$(isFloat $2)
FLT=$FLT || $SECFLT # if any are flt, do flt expr
if [ $DEBUG -eq 1 ]; then
(>&2 echo "DBG: 1: $1, 2: $2, FLT: $FLT, SECFLT: $SECFLT")
fi
# both floats TODO
if [[ $FLT -eq 1 ]]; then
if [ "$(echo "$1 > $2" | bc -l)" -eq 1 ]; then
echo 1
else
if [ "$(echo "$1 == $2" | bc -l)" -eq 1 ]; then
echo 0
else
echo -1
fi
fi
else
# both integers
if (( $1 > $2 )); then
echo 1
else
if (( $1 < $2 )); then
echo -1
else
echo 0
fi
fi
fi
}
PROGRAM_COMMAND=$1
# run the command
CMD_RET=$($PROGRAM_COMMAND)
# if it doesnt return a number, exit with error
CMD_RETURNS_NUM=$(isNum $CMD_RET)
if [[ $CMD_RETURNS_NUM -eq 0 ]]; then
echo "Program command did not return a number, instead it returned:"
echo $CMD_RET
exit 1
fi
declare -A CMD_SPLIT
declare -A NUM_INDICES
# split the command by spaces
IT=0
for str in $PROGRAM_COMMAND
do
CMD_SPLIT[$IT]=$str;
let IT=IT+1
done
# find the indices of numbers
IT_NUM=0
for ((i=0;i<${#CMD_SPLIT[@]};i++)) do
IS_NUM=$(isNum ${CMD_SPLIT[$i]})
if [[ $IS_NUM -eq 1 ]]; then
NUM_INDICES[$IT_NUM]=$i
let IT_NUM=IT_NUM+1
fi
done
ROWS=${#NUM_INDICES[@]}
COLS=3
declare -A VARIATIONS
declare -A NEXT_CMD_SPLIT
# Use NEXT_CMD_SPLIT to remember which has the high score, populate by associative array
for key in "${!CMD_SPLIT[@]}"
do
NEXT_CMD_SPLIT["$key"]="${CMD_SPLIT["$key"]}"
done
STAGNATION=0
CURRENT_HIGH=$CMD_RET
CURRENT_CMD=$PROGRAM_COMMAND
while [[ $STAGNATION -ne 1 ]]; do
STAGNATION=1
if [ $DEBUG -eq 1 ]; then
(>&2 echo "DBG: new epoch with $CURRENT_CMD")
fi
# tweak the numbers and fill the VARIATIONS array
# TODO add option for 5 columns or more
for ((i=0;i<ROWS;i++)) do
for ((j=0;j<COLS;j++)) do
IND=${NUM_INDICES[$i]}
NUM=${NEXT_CMD_SPLIT[$IND]}
case $j in
0)
VARIATIONS[$i,$j]=$(varyLower $NUM)
;;
1)
VARIATIONS[$i,$j]=$NUM
;;
2)
VARIATIONS[$i,$j]=$(varyHigher $NUM)
;;
esac
done
done
CURRENT_STILL_HIGH=0
# keep the best result and continue unless stagnation
# iterate through the possibilities
for ((i=0;i<ROWS;i++)) do
for ((j=0;j<COLS;j++)) do
# build cmd from VARIATIONS
for ((k=0;k<ROWS;k++)) do
CMD_SPLIT[${NUM_INDICES[$k]}]=${VARIATIONS[$k,$j]}
done
CMD_TO_RUN=${CMD_SPLIT[0]}
for ((k=1;k<${#CMD_SPLIT[@]};k++)) do
CMD_TO_RUN="$CMD_TO_RUN ${CMD_SPLIT[$k]}"
done
# execute new cmd
NEW_RET=$($CMD_TO_RUN)
CMP=$(compareResults $CURRENT_HIGH $NEW_RET)
if [ $DEBUG -eq 1 ]; then
(>&2 echo "DBG: $CMD_TO_RUN: $NEW_RET, compared to $CURRENT_HIGH: $CMP")
fi
# if new top score
if [[ $CMP -le 0 ]]; then
CURRENT_HIGH=$NEW_RET
# dont replace CMD if same score
if [[ $CMP -lt 0 ]]; then
CURRENT_CMD=$CMD_TO_RUN
# Use NEXT_CMD_SPLIT to remember which has the high score, populate by associative array
for key in "${!CMD_SPLIT[@]}"
do
NEXT_CMD_SPLIT["$key"]="${CMD_SPLIT["$key"]}"
done
if [ $DEBUG -eq 1 ]; then
(>&2 echo "DBG: New high with $CMD_TO_RUN: $NEW_RET, compared to $CURRENT_HIGH: $CMP")
fi
fi
fi
# stagnate only if all iterations gave no new top score
CURRENT_STILL_HIGH=$CMP || $CURRENT_STILL_HIGH
if [[ $CURRENT_STILL_HIGH -lt 0 ]]; then
STAGNATION=0
fi
done
done
done
echo "Score $CURRENT_HIGH achieved with program arguments $CURRENT_CMD"
exit 0;