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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
|
/*****************************************************/
/* Smoothness factor tuning of RSPL in N Dimensions. */
/*****************************************************/
/* Author: Graeme Gill
* Date: 28/11/2005
* Derived from cmatch.c
* Copyright 1995 - 2005 Graeme W. Gill
*
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
* see the License.txt file for licencing details.
*
* Test set for tuning smoothness factor for optimal interpolation
* with respect to dimension, number of sample points, and uncertainty
* of the sample points.
*/
#undef DEBUG
#undef DETAILED
#include <stdio.h>
#include <fcntl.h>
#include <math.h>
#include "rspl.h"
#include "numlib.h"
#include "xicc.h" /* For mpp support */
#include "rspl_imp.h"
#include "counters.h" /* Counter macros */
#include "plot.h"
#include "ui.h"
#define MXCHPARAMS 10 /* Input channel curve parameters */
#define PLOTRES 256
/* Function being modeled by rspl */
/* Similar to MPP model */
typedef struct {
int di; /* Number of dimensions */
double ip[MXDI][MXCHPARAMS]; /* Input channel parameters */
double shape[MXDI][1 << MXDI]; /* Channel interaction shape parameters */
double op[1 << MXDI]; /* Output channel combination parameters */
} funcp;
/* Setup a random function in the given dimensions */
static void setup_func(funcp *p, int di) {
double mn,mx;
double ax[MXDI][2];
int i, j;
p->di = di;
/* Setup random input curve parameters */
/* (This is the one that effects smoothness of function the most) */
for (j = 0; j < di; j++) {
for (mx = 4.0, i = 0; i < MXCHPARAMS; i++, mx *= 0.6) {
p->ip[j][i] = d_rand(-mx, mx);
}
}
/* Setup random shape parameters */
for (j = 0; j < di; j++) {
for (i = 0; i < (1 << di); i++) { /* Initially random */
p->shape[j][i] = d_rand(-0.1, 0.1);
}
}
/* Setup the random output value parameters */
/* First some axis dominant values */
for (i = 0; i < di; i++) {
ax[i][0] = d_rand(0.0, 1.0);
ax[i][1] = ax[i][0] + d_rand(-1.0, 1.0);
}
/* Sum them orthogonally and add indepent terms */
mn = 5.0;
mx = -5.0;
for (i = 0; i < (1 << di); i++) { /* Initially random */
p->op[i] = 0.0;
for (j = 0; j < di; j++) {
if ((1 << j) & i)
p->op[i] += ax[j][1];
else
p->op[i] += ax[j][0];
}
p->op[i] += d_rand(-0.3, 0.3);
if (p->op[i] < mn)
mn = p->op[i];
if (p->op[i] > mx)
mx = p->op[i];
}
for (i = 0; i < (1 << di); i++) { /* Then scale to between 0.0 and 1.0 */
p->op[i] = (p->op[i] - mn)/(mx - mn);
}
}
/* Lookup the function value */
static double lookup_func(funcp *p, double *v) {
int m, k;
int di = p->di;
double tcnv[MPP_MXINKS]; /* Transfer curve corrected device values */
double tcnv1[MPP_MXINKS]; /* 1.0 - Transfer curve corrected device values */
double ww[MPP_MXINKS]; /* Interpolated tweak params for each channel */
double ov; /* Output value */
/* Input curve lookup */
for (m = 0; m < di; m++) {
tcnv[m] = icxTransFunc(p->ip[m],MXCHPARAMS,v[m]);
tcnv1[m] = 1.0 - tcnv[m];
}
for (m = 0; m < di; m++)
ww[m] = 0.0;
/* Interpolate the shape values */
for (k = 0; k < (1 << di); k++) { /* For each interp vertex */
double vv;
for (vv = 1.0, m = 0; m < di; m++) { /* Compute weighting */
if (k & (1 << m))
vv *= tcnv[m];
else
vv *= tcnv1[m];
}
for (m = 0; m < di; m++) {
ww[m] += p->shape[m][k & ~(1<<m)] * vv; /* Apply weighting to shape vertex value */
}
}
/* Apply the shape values to adjust the primaries */
for (m = 0; m < di; m++) {
double gg = ww[m]; /* Curve adjustment */
double vv = tcnv[m]; /* Input value to be tweaked */
if (gg >= 0.0) {
vv = vv/(gg - gg * vv + 1.0);
} else {
vv = (vv - gg * vv)/(1.0 - gg * vv);
}
tcnv[m] = vv;
tcnv1[m] = 1.0 - vv;
}
/* Compute the primary combination values */
for (ov = 0.0, k = 0; k < (1 << di); k++) {
double vv = p->op[k];
for (m = 0; m < di; m++) {
if (k & (1 << m))
vv *= tcnv[m];
else
vv *= tcnv1[m];
}
ov += vv;
}
return ov;
}
/* Do one set of tests and return the results */
static void do_test(
double *trmse, /* RETURN total RMS error */
double *tmaxe, /* RETURN total maximum error */
double *tavge, /* RETURN total average error */
int verb, /* Verbosity */
int plot, /* Plot graphs */
int di, /* Dimensions */
int its, /* Number of function tests */
int res, /* RSPL grid resolution */
int ntps, /* Number of sample points */
double noise, /* Sample point noise volume */
int unif, /* NZ if uniform rather than standard deistribution noise */
double smooth, /* Smoothness to test */
int autosm, /* Use auto smoothing */
int seed /* Random seed value offset */
);
/* Compute smoothness of function */
static double do_stest(
int verb, /* Verbosity */
int di, /* Dimensions */
int its, /* Number of function tests */
int res /* RSPL grid resolution */
);
/* ---------------------------------------------------------------------- */
/* Locate minimum of smoothness series result */
#define MXMSS 50 /* Maximum smoothness series */
/* Return the optimal smoothness value, based on the */
/* minimum RMS value. */
static double best(int n, double *rmse, double *smv) {
int i, bi;
rspl *curve;
co *tps = NULL;
int ns = 2000; /* Number of samples */
datai low,high;
int gres[1];
datai dlow,dhigh;
double avgdev[1];
double brmse; /* best solution value */
double blsmv = 0.0; /* best solution location */
double rv; /* Return value */
/* Create interpolated curve */
if ((curve = new_rspl(RSPL_NOFLAGS,1, 1)) == NULL)
error ("New rspl failed");
/* Create the list of sampling points */
if ((tps = (co *)malloc(n * sizeof(co))) == NULL)
error ("malloc failed");
for (i = 0; i < n; i++) {
tps[i].p[0] = log10(smv[i]);
tps[i].v[0] = rmse[i];
}
gres[0] = 100;
low[0] = log10(smv[0]);
high[0] = log10(smv[n-1]);
dlow[0] = 0.0;
dhigh[0] = 1.0;
avgdev[0] = 0.0;
curve->fit_rspl(curve,
0, /* Non-mon and clip flags */
tps, /* Test points */
n, /* Number of test points */
NULL, NULL, gres, /* Low, high, resolution of grid */
NULL, NULL, /* Default data scale */
-0.0007, /* Underlying smoothing */
avgdev, /* Average deviation */
NULL); /* iwidth */
#ifdef NEVER
/* Check the fit */
for (i = 0; i < n; i++) {
co tp;
tp.p[0] = log10(smv[i]);
curve->interp(curve, &tp);
printf("Point %d at %f, should be %f is %f\n",i,log10(smv[i]),rmse[i],tp.v[0]);
}
#endif
/* Choose a solution */
brmse = 1e38;
/* Find lowest rms error point */
for (i = ns-1; i >= 0; i--) {
co tp;
double vi;
vi = i/(ns-1.0);
tp.p[0] = log10(smv[0]) + (log10(smv[n-1]) - log10(smv[0])) * vi;
curve->interp(curve, &tp);
if (tp.v[0] < brmse) {
blsmv = tp.p[0];
brmse = tp.v[0];
bi = i;
}
}
/* Then increase smoothness until fit error is 1% higher */
for (i = bi+1; i < ns; i++) {
co tp;
double vi;
vi = i/(ns-1.0);
tp.p[0] = log10(smv[0]) + (log10(smv[n-1]) - log10(smv[0])) * vi;
curve->interp(curve, &tp);
if (tp.v[0] >= (1.01 * brmse)) {
blsmv = tp.p[0];
brmse = tp.v[0];
break;
}
}
rv = pow(10.0, blsmv);
#ifdef NEVER
#define TPRES 100
/* Plot the result */
{
double xx[TPRES], yy[TPRES];
for (i = 0; i < TPRES; i++) {
co tp;
double vi = i/(TPRES-1.0);
tp.p[0] = log10(smv[0]) + (log10(smv[n-1]) - log10(smv[0])) * vi;
curve->interp(curve, &tp);
xx[i] = tp.p[0];
yy[i] = tp.v[0];
}
printf("Best at %f\n",blsmv);
do_plot(xx,yy,NULL,NULL,TPRES);
}
#endif
return rv;
}
/* ---------------------------------------------------------------------- */
/* Test series */
/* Explore ideal smoothness change with test point number and noise volume */
/* If tdi != 0, just do the given dimension */
/* If tntps != 0, just do the given number of points */
/* If tnlev != 0, just do the given noise level */
static void do_series_1(int unif, int tdi, int tntps, int tnlev, int autosm, int seed) {
int verb = 0;
int plot = 0;
int sdi = 1, edi = 4, di;
int its;
int res = 0;
int ntps = 0;
double noise = 0.0;
double smooth = 0.0;
double trmse, tavge, tmaxe;
int m, i, j, k;
/* Number of trials to do for each dimension */
int trials[4] = {
12,
10,
8,
5
};
/* Resolution of grid for each dimension */
int reses[4][4] = {
{ 257, 129, 65, 33 },
{ 128, 65, 33, 17 },
{ 65, 33, 17, 9 },
{ 33, 17, 9, 5 }
};
#ifndef NEVER
/* Set of sample points to explore */
int nset[4][20] = {
{
5, 10, 20, 50, 100, 200, 400, 800 /* di = 1 */
},
{
10, 25, 50, 100, 200, 400, 1000, 2500, 10000, 40000 /* di = 2 */
},
{
10, 25, 75, 125, 250, 500, 1000, 2000, 4000, 8000, 16000, 30000, 100000 /* di = 3 */
},
{
100, 200, 450, 625, 900, 1200, 1800, 3600, 10000, 200000, 500000 /* di = 4 */
}
};
#else
/* Set of sample points to explore */
int nset[4][20] = {
{
20, 50, 100, 200 /* di = 1 */
},
{
100, 400, 2500, 10000 /* di = 2 */
},
{
500, 1000, 2000, 8000 /* di = 3 */
},
{
50, 900, 1800, 3600, 10000 /* di = 4 */
}
};
#endif
/* Set of smoothnesses to explore */
double smset[4][20] = {
{
-0.00000001,
-0.00000010,
-0.00000100,
-0.00001000,
-0.00010000,
-0.00100000,
-0.01000000,
-0.10000000,
-1.00000000,
0.0
},
{
-0.0000001,
-0.0000010,
-0.0000100,
-0.0001000,
-0.0010000,
-0.0100000,
-0.1000000,
-1.0000000,
0.0
},
{
-0.0000010,
-0.0000100,
-0.0001000,
-0.0010000,
-0.0100000,
-0.1000000,
-1.0000000,
0.0
},
{
-0.0000100,
-0.0001000,
-0.0010000,
-0.0100000,
-0.1000000,
-1.0000000,
-10.000000,
0.0
}
};
#ifndef NEVER
/* Set of noise levels to explore (average deviation * 4) */
double noiseset[4][20] = {
{
0.0, /* Perfect data */
0.005, /* 0.5 % */
0.01, /* 1.0 % */
0.02, /* 2.0 % */
0.05, /* 5.0 % */
0.10, /* 10.0 % */
// 0.20, /* 20.0 % */
-1.0
},
{
0.0, /* Perfect data */
0.005, /* 0.5 % */
0.01, /* 1.0 % */
0.02, /* 2.0 % */
0.05, /* 5.0 % */
0.10, /* 10.0 % */
// 0.20, /* 20.0 % */
-1.0
},
{
0.0, /* Perfect data */
0.005, /* 0.5 % */
0.01, /* 1.0 % */
0.02, /* 2.0 % */
0.05, /* 5.0 % */
0.10, /* 10.0 % */
// 0.20, /* 20.0 % */
-1.0
},
{
0.0, /* Perfect data */
0.005, /* 0.5 % */
0.01, /* 1.0 % */
0.02, /* 2.0 % */
0.05, /* 5.0 % */
0.10, /* 10.0 % */
// 0.20, /* 20.0 % */
-1.0
},
};
#else
/* Set of noise levels to explore (average deviation * 4) */
double noiseset[4][20] = {
{
-1.0
},
{
-1.0
},
{
-1.0
},
{
0.0, /* Perfect data */
0.005, /* 0.2 % */
0.01, /* 1.0 % */
0.02, /* 2.0 % */
0.05, /* 5.0 % */
0.10, /* 10.0 % */
// 0.20, /* 20.0 % */
-1.0
},
};
#endif
printf("Testing effect of underlying smoothness factors\n");
/* For dimensions */
if (tdi != 0)
sdi = edi = tdi;
for (di = sdi; di <= edi; di++) { // dimensions
its = trials[di-1];
for (m = 1; m < 2; m++) { // Just 2nd-highest resolution
res = reses[di-1][m];
printf("Tests %d\n",its);
printf("Dimensions %d\n",di);
printf("RSPL resolution %d\n",res);
/* For number of sample points */
for (i = 0; i < 20; i++) { // All test points
int rpts;
ntps = nset[di-1][i];
if (ntps == 0)
break;
if (tntps != 0 && ntps != tntps) /* Skip any not requested */
continue;
/* Make sure at least 100 points are tested */
rpts = 1 + 100/ntps;
if (rpts > 5)
rpts = 5;
printf("\nNo. Sample points %d, norm %8.2f, total its %d\n",ntps, pow((double)ntps, 1.0/di),its * rpts);
/* For noise levels */
for (j = tnlev; j < 20; j++) { // All noise levels
double smv[20];
double rmse[20];
double bfit;
if (tnlev != 0 && j != tnlev)
break;
noise = noiseset[di-1][j];
if (noise < 0.0)
break;
printf("\nNoise volume %f%%, average deviation %f%% Log ADev %f\n",noise * 100.0, noise * 25.0, noise > 0.0 ? log10(0.25 * noise) : -9.0);
/* For smooth factors */
for (k = 0; k < 20; k++) { // All smoothing levels
smooth = smset[di-1][k];
if (smooth == 0.0)
break;
printf("Underlying smooth %9.7f, ",-smooth); fflush(stdout);
do_test(&trmse, &tmaxe, &tavge, verb, plot, di, its * rpts, res, ntps, noise, unif, smooth, autosm, seed);
smv[k] = -smooth;
rmse[k] = trmse;
printf("maxerr %f%%, avgerr %f%%, rmserr %f%%\n",
tmaxe * 100.0, tavge * 100.0, trmse * 100.0);
}
bfit = best(k, rmse, smv);
printf("Best smoothness = %9.7f, log10 = %4.2f\n",bfit,log10(bfit));
}
}
}
printf("\n");
}
}
/* Explore performance of "optimised" smoothness over test point number and noise volume */
static void do_series_2(int unif, int autosm, int seed) {
int verb = 0;
int plot = 0;
int di = 0;
int its;
int res = 0;
int ntps = 0;
double noise = 0.0;
double smooth = 0.0;
double trmse, tavge, tmaxe;
int i, j, k;
/* Number of trials to do for each dimension */
int trials[4] = {
16,
12,
8,
5
};
/* Resolution of grid for each dimension */
int reses[4] = {
129,
65,
33,
17
};
#ifdef NEVER
/* Set of sample points to explore */
int nset[4][20] = {
{
5, 10, 20, 50, 0
},
{
25, 100, 400, 2500, 0
},
{
125, 1000, 8000, 125000, 0
},
{
625, 10000, 160000, 1000000, 0
}
};
#else
/* Set of sample points to explore */
int nset[4][20] = {
{
5, 10, 20, 50, 0
},
{
25, 100, 400, 2500, 0
},
{
250, 500, 1000, 2000, 0
},
{
450, 900, 1800, 3600, 0
}
};
#endif /* NEVER */
/* Set of smoothnesses to explore */
double smset[5] = {
00.01,
00.10,
01.00,
10.00,
100.0
};
/* Set of noise levels to explore (average deviation * 4) */
double noiseset[6] = {
0.0, /* Perfect data */
0.01, /* 1.0 % */
0.02, /* 2.0 % */
0.05, /* 5.0 % */
0.10, /* 10.0 % */
0.20, /* 20.0 % */
};
printf("Verifying optimised smoothness factors\n");
/* For dimensions */
for (di = 1; di <= 4; di++) {
its = trials[di-1];
res = reses[di-1];
printf("Tests %d\n",its);
printf("Dimensions %d\n",di);
printf("RSPL resolution %d\n",res);
/* For number of sample points */
for (i = 0; i < 20; i++) {
ntps = nset[di-1][i];
if (ntps == 0)
break;
printf("\nNo. Sample points %d, norm %8.2f\n",ntps, pow((double)ntps, 1.0/di));
/* For noise levels */
for (j = 0; j < 6; j++) {
double rmse[20];
double bfit;
noise = noiseset[j];
printf("Noise volume %f%%, average deviation %f%% Log ADev %f\n",noise * 100.0, noise * 25.0, noise > 0.0 ? log10(0.25 * noise) : -9.0);
/* For smooth factors */
for (k = 0; k < 5; k++) {
smooth = smset[k];
printf("Extra smooth %f, ",smooth); fflush(stdout);
do_test(&trmse, &tmaxe, &tavge, verb, plot, di, its, res, ntps, noise, unif, smooth, autosm, seed);
rmse[k] = trmse;
printf("maxerr %f%%, avgerr %f%%, rmserr %f%%\n",
tmaxe * 100.0, tavge * 100.0, trmse * 100.0);
}
bfit = best(5, rmse, smset);
printf("Best smoothness = %9.7f, log10 = %4.2f\n",bfit,log10(bfit));
}
}
printf("\n");
}
}
/* ---------------------------------------------------------------------- */
void usage(void) {
fprintf(stderr,"Test smoothness factor tuning of RSPL in N Dimensions\n");
fprintf(stderr,"Author: Graeme W. Gill\n");
fprintf(stderr,"usage: smtnd [options]\n");
fprintf(stderr," -v Verbose\n");
fprintf(stderr," -p Plot graphs\n");
fprintf(stderr," -z n Do test series ""n"" else single test\n");
fprintf(stderr," 1 = Underlying smoothness\n");
fprintf(stderr," 2 = Verify optimised smoothness\n");
fprintf(stderr," -S Compute smoothness factor instead\n");
fprintf(stderr," -u Use uniformly distributed noise rather than normal\n");
fprintf(stderr," -d n Test ""d"" dimension only, 1-4 (default 1)\n");
fprintf(stderr," -t n Test ""n"" random functions (default 1)\n");
fprintf(stderr," -r res Rspl resolution (defaults 129, 65, 33, 17)\n");
fprintf(stderr," -n no Test ""no"" sample points (default 20, 40, 80, 100)\n");
fprintf(stderr," -a amnt Add total randomness to function value (default 0.0)\n");
fprintf(stderr," -A n Just do the n'th noise level of series\n");
fprintf(stderr," -s smooth RSPL extra smoothness factor to test (default 1.0)\n");
fprintf(stderr," -g smooth RSPL underlying smoothness factor to test\n");
fprintf(stderr," -x Use auto smoothing\n");
fprintf(stderr," -Z seed Random seed value\n");
exit(1);
}
int main(int argc, char *argv[]) {
int fa,nfa; /* argument we're looking at */
int verb = 0;
int plot = 0;
int series = 0;
int unif = 0; /* default normal noise distribution */
int di = 0;
int its = 1;
int res = -1;
int ntps = 0;
double noise = 0.0;
int nlev = 0;
double smooth = 1.0;
double gsmooth = 0.0;
int autosm = 0;
int smfunc = 0;
double trmse, tavge, tmaxe;
int seed = 0;
error_program = "smtnd";
#ifdef NEVER
{
double rmse[10], smv[10], rv;
smv[0] = 0.0000100, rmse[0] = 2.566116;
smv[1] = 0.0001000, rmse[1] = 2.528666;
smv[2] = 0.0010000, rmse[2] = 2.489116;
smv[3] = 0.0100000, rmse[3] = 3.409045;
smv[4] = 0.1000000, rmse[4] = 5.727079;
smv[5] = 1.0000000, rmse[5] = 6.653747;
rv = best(6,rmse, smv);
printf("~1 best = %f\n",rv);
exit(0);
}
#endif
/* Process the arguments */
for(fa = 1;fa < argc;fa++) {
nfa = fa; /* skip to nfa if next argument is used */
if (argv[fa][0] == '-') { /* Look for any flags */
char *na = NULL; /* next argument after flag, null if none */
if (argv[fa][2] != '\000')
na = &argv[fa][2]; /* next is directly after flag */
else {
if ((fa+1) < argc) {
if (argv[fa+1][0] != '-') {
nfa = fa + 1;
na = argv[nfa]; /* next is seperate non-flag argument */
}
}
}
if (argv[fa][1] == '?') {
usage();
} else if (argv[fa][1] == 'v' ) {
verb = 1;
} else if (argv[fa][1] == 'p') {
plot = 1;
} else if (argv[fa][1] == 'u') {
unif = 1;
/* Test series */
} else if (argv[fa][1] == 'z') {
fa = nfa;
if (na == NULL) usage();
series = atoi(na);
if (series <= 0) usage();
/* Compute smoothness factor */
} else if (argv[fa][1] == 'S') {
smfunc = 1;
/* Dimension */
} else if (argv[fa][1] == 'd') {
fa = nfa;
if (na == NULL) usage();
di = atoi(na);
if (di <= 0 || di > 4) usage();
/* Number of tests */
} else if (argv[fa][1] == 't') {
fa = nfa;
if (na == NULL) usage();
its = atoi(na);
if (its <= 0) usage();
/* Resolution */
} else if (argv[fa][1] == 'r') {
fa = nfa;
if (na == NULL) usage();
res = atoi(na);
if (res <= 0) usage();
/* Number of sample points */
} else if (argv[fa][1] == 'n') {
fa = nfa;
if (na == NULL) usage();
ntps = atoi(na);
if (ntps <= 0) usage();
/* Randomness */
} else if (argv[fa][1] == 'a') {
fa = nfa;
if (na == NULL) usage();
noise = atof(na);
if (noise < 0.0) usage();
/* Series Noise Level */
} else if (argv[fa][1] == 'A') {
fa = nfa;
if (na == NULL) usage();
nlev = atoi(na);
if (noise < 0) usage();
/* Extra smooth factor */
} else if (argv[fa][1] == 's') {
fa = nfa;
if (na == NULL) usage();
smooth = atof(na);
if (smooth < 0.0) usage();
/* Underlying smoothnes factor */
} else if (argv[fa][1] == 'g') {
fa = nfa;
if (na == NULL) usage();
gsmooth = atof(na);
if (gsmooth < 0.0) usage();
} else if (argv[fa][1] == 'x') {
autosm = 1;
/* Random seed offset */
} else if (argv[fa][1] == 'Z') {
fa = nfa;
if (na == NULL) usage();
seed = atoi(na);
} else
usage();
} else
break;
}
if (series > 0) {
if (series == 1)
do_series_1(unif, di, ntps, nlev, autosm, seed);
else if (series == 2)
do_series_2(unif, autosm, seed);
else
error("Unknown series %d\n",series);
return 0;
}
if (di == 0)
di = 1;
if (res < 0) {
if (di == 1)
res = 129;
else if (di == 2)
res = 65;
else if (di == 3)
res = 33;
else
res = 17;
}
if (ntps <= 0) {
if (di == 1)
ntps = 20;
else if (di == 2)
ntps = 40;
else if (di == 3)
ntps = 60;
else
ntps = 80;
}
if (smfunc) {
double sm;
if (verb) {
printf("Dimensions %d\n",di);
printf("Tests %d\n",its);
printf("Grid resolution %d\n",res);
}
sm = do_stest(verb, di, its, res);
printf("Results: smoothness factor = %f\n",sm);
} else {
if (verb) {
printf("Dimensions %d\n",di);
printf("Tests %d\n",its);
printf("RSPL resolution %d\n",res);
printf("No. Sample points %d (norm %f)\n",ntps, pow((double)ntps, 1.0/di));
printf("Noise volume %f\n",noise);
if (gsmooth > 0.0)
printf("Underlying smooth %f\n",gsmooth);
else
printf("Extra smooth %f\n",smooth);
}
if (gsmooth > 0.0)
do_test(&trmse, &tmaxe, &tavge, verb, plot, di, its, res, ntps, noise, unif, -gsmooth, autosm, seed);
else
do_test(&trmse, &tmaxe, &tavge, verb, plot, di, its, res, ntps, noise, unif, smooth, autosm, seed);
printf("Results: maxerr %f%%, avgerr %f%%, rmserr %f%%\n",
tmaxe * 100.0, tavge * 100.0, trmse * 100.0);
}
return 0;
}
/* Do one set of tests and return the results */
static void do_test(
double *trmse, /* RETURN total RMS error */
double *tmaxe, /* RETURN total maximum error */
double *tavge, /* RETURN total average error */
int verb, /* Verbosity */
int plot, /* Plot graphs */
int di, /* Dimensions */
int its, /* Number of function tests */
int res, /* RSPL grid resolution */
int ntps, /* Number of sample points */
double noise, /* Sample point noise volume (total = 4 x average deviation) */
int unif, /* NZ if uniform rather than standard deistribution noise */
double smooth, /* Smoothness to test, +ve for extra, -ve for underlying */
int autosm, /* Use auto smoothing */
int seed /* Random seed value offset */
) {
funcp fp; /* Function parameters */
sobol *so; /* Sobol sequence generator */
co *tps = NULL;
rspl *rss; /* Multi-resolution regularized spline structure */
datai low,high;
double avgdev[MXDO];
int gres[MXDI];
int i, j, it;
int flags = RSPL_NOFLAGS;
if (autosm)
flags |= RSPL_AUTOSMOOTH;
*trmse = 0.0;
*tmaxe = 0.0;
*tavge = 0.0;
for (j = 0; j < di; j++) {
low[j] = 0.0;
high[j] = 1.0;
gres[j] = res;
}
if ((so = new_sobol(di)) == NULL)
error("Creating sobol sequence generator failed");
for (it = 0; it < its; it++) {
double rmse, avge, maxe;
double tnoise = 0.0;
/* Make repeatable by setting random seed before a test set. */
rand32(0x12345678 + seed + 0x1000 * it);
/* New function */
setup_func(&fp, di);
/* Create the object */
rss = new_rspl(RSPL_NOFLAGS,di, 1);
/* Create the list of sampling points */
if ((tps = (co *)malloc(ntps * sizeof(co))) == NULL)
error ("malloc failed");
so->reset(so);
if (verb) printf("Generating the sample points\n");
for (i = 0; i < ntps; i++) {
double out, n;
so->next(so, tps[i].p);
out = lookup_func(&fp, tps[i].p);
if (unif)
n = d_rand(-0.5 * noise, 0.5 * noise);
else
n = noise * 0.25 * 1.2533 * norm_rand();
tps[i].v[0] = out + n;
//printf("~1 data %d: %f %f %f -> %f, inc noise %f\n", i, tps[i].p[0], tps[i].p[1], tps[i].p[2], out, tps[i].v[0]);
tnoise += fabs(n);
}
tnoise /= (double) ntps;
if (verb) printf("Measured noise average deviation = %f%%\n",tnoise * 100.0);
/* Fit to scattered data */
if (verb) printf("Fitting the scattered data, smooth = %f, avgdev = %f\n",smooth, avgdev[0]);
avgdev[0] = 0.25 * noise;
rss->fit_rspl(rss,
flags, /* Non-mon and clip flags */
tps, /* Test points */
ntps, /* Number of test points */
low, high, gres, /* Low, high, resolution of grid */
low, high, /* Default data scale */
smooth, /* Smoothing to test */
avgdev, /* Average deviation */
NULL); /* iwidth */
/* Plot out function values */
if (plot) {
int slice;
printf("Black is target, Red is rspl\n");
for (slice = 0; slice < (di+1); slice++) {
co tp; /* Test point */
double x[PLOTRES];
double ya[PLOTRES];
double yb[PLOTRES];
double yc[PLOTRES];
double pp[MXDI], p1[MXDI], p2[MXDI], ss[MXDI];
int n = PLOTRES;
/* setup slices on each axis at 0.5 and diagonal */
if (slice < di) {
for (j = 0; j < di; j++)
p1[j] = p2[j] = 0.5;
p1[slice] = 0.0;
p2[slice] = 1.0;
printf("Slice along axis %d\n",slice);
} else {
for (j = 0; j < di; j++) {
p1[j] = 0.0;
p2[j] = 1.0;
}
printf("Slice along diagonal\n");
}
/* Start point and step increment */
for (j = 0; j < di; j++) {
ss[j] = (p2[j] - p1[j])/n;
pp[j] = p1[j];
}
for (i = 0; i < n; i++) {
double vv = i/(n-1.0);
x[i] = vv;
/* Reference */
ya[i] = lookup_func(&fp, pp);
/* RSPL aproximation */
for (j = 0; j < di; j++)
tp.p[j] = pp[j];
if (rss->interp(rss, &tp))
tp.v[0] = -0.1;
yb[i] = tp.v[0];
/* Crude way of setting the scale: */
yc[i] = 0.0;
if (i == (n-1))
yc[0] = 1.0;
for (j = 0; j < di; j++)
pp[j] += ss[j];
}
/* Plot the result */
do_plot(x,ya,yb,yc,n);
}
}
/* Compute statistics */
rmse = 0.0;
avge = 0.0;
maxe = 0.0;
// so->reset(so);
/* Fit to scattered data */
if (verb) printf("Fitting the scattered data\n");
for (i = 0; i <100000; i++) {
co tp; /* Test point */
double aa, bb, err;
so->next(so, tp.p);
/* Reference */
aa = lookup_func(&fp, tp.p);
/* RSPL aproximation */
rss->interp(rss, &tp);
bb = tp.v[0];
err = fabs(aa - bb);
avge += err;
rmse += err * err;
if (err > maxe)
maxe = err;
}
avge /= (double)i;
rmse /= (double)i;
if (verb)
printf("Dim %d, res %d, noise %f, points %d, maxerr %f%%, rmserr %f%%, avgerr %f%%\n",
di, res, noise, ntps, maxe * 100.0, sqrt(rmse) * 100.0, avge * 100.0);
*trmse += rmse;
*tmaxe += maxe;
*tavge += avge;
rss->del(rss);
free(tps);
}
so->del(so);
*trmse = sqrt(*trmse/(double)its);
*tmaxe /= (double)its;
*tavge /= (double)its;
}
/* Do smoothness scaling check & return results */
static double do_stest(
int verb, /* Verbosity */
int di, /* Dimensions */
int its, /* Number of function tests */
int res /* RSPL grid resolution */
) {
funcp fp; /* Function parameters */
DCOUNT(gc, MXDIDO, di, 1, 1, res-1);
int it;
double atse = 0.0;
/* Make repeatable by setting random seed before a test set. */
rand32(0x12345678);
for (it = 0; it < its; it++) {
double tse;
setup_func(&fp, di); /* New function */
DC_INIT(gc)
tse = 0.0;
for (; !DC_DONE(gc);) {
double g[MXDI];
int e, k;
double y1, y2, y3;
double del;
for (e = 0; e < di; e++)
g[e] = gc[e]/(res-1.0);
y2 = lookup_func(&fp, g);
del = 1.0/(res-1.0);
for (k = 0 ; k < di; k++) {
double err;
g[k] -= del;
y1 = lookup_func(&fp, g);
g[k] += 2.0 * del;
y3 = lookup_func(&fp, g);
g[k] -= del;
err = 0.5 * (y3 + y1) - y2;
tse += err * err;
}
DC_INC(gc);
}
/* Apply adjustments and corrections */
tse *= pow((res-1.0), 4.0); /* Aprox. geometric resolution factor */
tse /= pow((res-2.0),(double)di); /* Average squared non-smoothness */
if (verb)
printf("smf for it %d = %f\n",it,tse);
atse += tse;
}
return atse/(double)its;
}
|