-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.cpp
2162 lines (1693 loc) · 58 KB
/
node.cpp
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
#include "node.h"
void node::set_names(){
associates.set_name("node_associates");
boundaryPoints.set_name("node_boundaryPoints");
basisVectors.set_name("node_basisVectors");
basisModel.set_name("node_basisModel");
range_max.set_name("node_range_max");
range_min.set_name("node_range_min");
candidates.set_name("node_candidates");
geographicCenter.set_name("node_geographicCenter");
centerCandidates.set_name("node_centerCandidates");
oldCenters.set_name("node_oldCenters");
compass_centers.set_name("node_compass_centers");
}
node::node(){
set_names();
time_ricochet=0.0;
time_coulomb=0.0;
time_search=0.0;
time_bases=0.0;
last_nAssociates=0;
last_nBasisAssociates=0;
center_dex=-1;
min_dex=-1;
last_expanded=0;
activity=1;
ct_search=0;
ct_ricochet=0;
ct_coulomb=0;
ct_bases=0;
calls_to_bases=0;
farthest_associate=0.0;
time_penalty=0.5;
gg=NULL;
dice=NULL;
}
node::~node(){}
void node::copy(const node &in){
gg=in.gg;
dice=in.dice;
associates.reset();
boundaryPoints.reset();
basisVectors.reset();
basisModel.reset();
range_min.reset();
range_max.reset();
candidates.reset();
center_dex=in.center_dex;
min_dex=in.min_dex;
last_nAssociates=in.last_nAssociates;
last_nBasisAssociates=in.last_nBasisAssociates;
time_ricochet=in.time_ricochet;
time_coulomb=in.time_coulomb;
time_search=in.time_search;
time_bases=in.time_bases;
time_penalty=in.time_penalty;
ct_search=in.ct_search;
ct_ricochet=in.ct_ricochet;
ct_coulomb=in.ct_coulomb;
ct_bases=in.ct_bases;
calls_to_bases=in.calls_to_bases;
last_expanded=in.last_expanded;
activity=in.activity;
int i,j;
farthest_associate=in.farthest_associate;
centerCandidates.reset();
for(i=0;i<in.centerCandidates.get_dim();i++){
centerCandidates.set(i,in.centerCandidates.get_data(i));
}
oldCenters.reset();
for(i=0;i<in.oldCenters.get_dim();i++){
oldCenters.set(i,in.oldCenters.get_data(i));
}
compass_centers.reset();
for(i=0;i<in.compass_centers.get_dim();i++){
compass_centers.set(i,in.compass_centers.get_data(i));
}
for(i=0;i<in.geographicCenter.get_dim();i++){
geographicCenter.set(i,in.geographicCenter.get_data(i));
}
for(i=0;i<in.range_max.get_dim();i++){
range_max.set(i,in.range_max.get_data(i));
}
for(i=0;i<in.range_min.get_dim();i++){
range_min.set(i,in.range_min.get_data(i));
}
for(i=0;i<in.associates.get_dim();i++){
associates.add(in.associates.get_data(i));
}
for(i=0;i<in.boundaryPoints.get_dim();i++){
boundaryPoints.add(in.boundaryPoints.get_data(i));
}
for(i=0;i<in.candidates.get_dim();i++){
candidates.add(in.candidates.get_data(i));
}
for(i=0;i<in.basisModel.get_dim();i++){
basisModel.set(i,in.basisModel.get_data(i));
}
if(gg!=NULL){
if(gg->is_gp_null()==0){
basisVectors.set_cols(gg->get_dim());
for(i=0;i<gg->get_dim();i++){
for(j=0;j<gg->get_dim();j++){
basisVectors.set(i,j,in.basisVectors.get_data(i,j));
}
}
}
}
}
node::node(const node &in){
set_names();
copy(in);
}
node& node::operator=(const node &in){
if(this==&in) return *this;
copy(in);
return *this;
}
void node::flush_candidates(array_1d<int> &out){
int i;
for(i=0;i<candidates.get_dim();i++){
out.add(candidates.get_data(i));
}
candidates.reset();
}
double node::get_farthest_associate(){
return farthest_associate;
}
int node::get_n_associates(){
return associates.get_dim();
}
void node::set_gpWrapper(gpWrapper *ggin){
int i,j;
basisVectors.reset();
gg=ggin;
basisVectors.set_dim(gg->get_dim(),gg->get_dim());
for(i=0;i<gg->get_dim();i++){
basisModel.set(i,1.0);
for(j=0;j<gg->get_dim();j++){
if(i==j)basisVectors.set(i,j,1.0);
else basisVectors.set(i,j,0.0);
}
}
}
void node::set_dice(Ran *ddin){
dice=ddin;
}
void node::set_center_dex(int ii){
center_dex=ii;
int i;
if(gg!=NULL){
if(range_max.get_dim()!=gg->get_dim() ||
range_min.get_dim()!=gg->get_dim()){
for(i=0;i<gg->get_dim();i++){
range_max.set(i,gg->get_pt(ii,i));
range_min.set(i,gg->get_pt(ii,i));
geographicCenter.set(i,gg->get_pt(ii,i));
}
}
}
if(min_dex<0 || gg->get_fn(ii)<gg->get_fn(min_dex)){
min_dex=ii;
}
}
void node::evaluate(array_1d<double> &pt, double *chiout, int *dexout){
evaluate(pt,chiout,dexout,1);
}
void node::evaluateNoAssociate(array_1d<double> &pt, double *chiout, int *dexout){
evaluate(pt,chiout,dexout,0);
}
void node::evaluate(array_1d<double> &pt, double *chiout, int *dexout, int doAssociate){
if(gg==NULL){
printf("WARNING cannot call node::evaluate; gg is NULL\n");
}
gg->evaluate(pt,chiout,dexout);
double dd;
if(chiout[0]<gg->get_target() && doAssociate==1 && dexout[0]>=0){
associates.add(dexout[0]);
dd=gg->distance(dexout[0],center_dex);
if(dd>farthest_associate){
farthest_associate=dd;
}
}
if(dexout[0]>=0){
if(min_dex<0 || gg->get_fn(dexout[0])<gg->get_fn(min_dex)){
min_dex=dexout[0];
}
}
int i,rangeChanged=0;
double ddCenters,ddTest;
if(chiout[0]<gg->get_target()){
for(i=0;i<gg->get_dim();i++){
if(i>=range_max.get_dim() || pt.get_data(i)>range_max.get_data(i)){
range_max.set(i,pt.get_data(i));
rangeChanged=1;
}
if(i>=range_min.get_dim() || pt.get_data(i)<range_min.get_data(i)){
range_min.set(i,pt.get_data(i));
rangeChanged=1;
}
}
if(rangeChanged==1){
for(i=0;i<gg->get_dim();i++){
geographicCenter.set(i,0.5*(range_max.get_data(i)+range_min.get_data(i)));
}
}
if(dexout[0]>=0 && chiout[0]<0.9*gg->get_chimin()+0.1*gg->get_target()){
ddCenters=gg->distance(center_dex,geographicCenter);
ddTest=gg->distance(dexout[0],geographicCenter);
if(ddTest<ddCenters){
centerCandidates.add(dexout[0]);
last_expanded=ct_search;
}
}
}
}
void node::project_to_unit_sphere(array_1d<double> &in, array_1d<double> &out){
array_1d<double> dir;
out.reset();
double norm=0.0;
int i;
for(i=0;i<gg->get_dim();i++){
dir.set(i,in.get_data(i)-gg->get_pt(center_dex,i));
norm+=power(dir.get_data(i)/(gg->get_max(i)-gg->get_min(i)),2);
}
norm=sqrt(norm);
for(i=0;i<gg->get_dim();i++){
out.set(i,gg->get_pt(center_dex,i)+dir.get_data(i)/norm);
}
}
void node::add_as_boundary(int dex){
array_1d<double> vv,vv_norm;
int i;
for(i=0;i<gg->get_dim();i++)vv.set(i,gg->get_pt(dex,i));
project_to_unit_sphere(vv,vv_norm);
gg->add_to_unitSpheres(vv_norm);
boundaryPoints.add(dex);
}
int node::bisection(int low, int high){
return bisection(low,high,0);
}
int node::bisectionAssociate(int low, int high){
return bisection(low,high,1);
}
int node::bisection(int lowDex, int highDex, int asAssociates){
if(lowDex<0 || lowDex>=gg->get_pts()){
return -1;
}
if(highDex<0 || highDex>=gg->get_pts()){
return -1;
}
array_1d<double> lowball,highball;
double flow,fhigh;
lowball.set_name("node_bisection(int)_lowball");
highball.set_name("node_bisection(int)_highball");
int i;
for(i=0;i<gg->get_dim();i++){
lowball.set(i,gg->get_pt(lowDex,i));
highball.set(i,gg->get_pt(highDex,i));
}
flow=gg->get_fn(lowDex);
fhigh=gg->get_fn(highDex);
return bisection(lowball,flow,highball,fhigh,asAssociates);
}
int node::bisection(array_1d<double> &ll, double l, array_1d<double> &hh, double h){
return bisection(ll,l,hh,h,0);
}
int node::bisectionAssociate(array_1d<double> &ll, double l,
array_1d<double> &hh, double h){
return bisection(ll,l,hh,h,1);
}
int node::bisection(array_1d<double> &lowball_in, double flow_in,
array_1d<double> &highball_in, double fhigh_in, int asAssociates){
/*
lowDex and highDex are the indices of the initial highball and lowball poitns
will return the best point it found
*/
int i;
array_1d<double> lowball,highball;
double flow,fhigh;
lowball.set_name("node_bisection_lowball");
highball.set_name("node_bisection_highball");
fhigh=fhigh_in;
flow=flow_in;
for(i=0;i<gg->get_dim();i++){
lowball.set(i,lowball_in.get_data(i));
highball.set(i,highball_in.get_data(i));
}
if(flow>gg->get_target()){
printf("WARNING in node bisection target %e but flow %e\n",
gg->get_target(),flow);
exit(1);
}
if(fhigh<gg->get_target()){
printf("WARNING in node bisection target %e but fhigh %e\n",
gg->get_target(),fhigh);
exit(1);
}
if(gg->get_iWhere()==iCoulomb){
gg->set_iWhere(iNodeBisect);
}
int iout;
double bisection_tolerance=0.01*(gg->get_target()-gg->get_chimin());
array_1d<double> trial;
double ftrial;
int itrial;
trial.set_name("node_bisection_trial");
double dd=gg->distance(lowball,highball);
int ct=0;
iout=-1;
double target=gg->get_target();
while(ct<100 && dd>1.0e-6 && target-flow>bisection_tolerance){
for(i=0;i<gg->get_dim();i++){
trial.set(i,0.5*(lowball.get_data(i)+highball.get_data(i)));
}
if(asAssociates==0){
evaluateNoAssociate(trial,&ftrial,&itrial);
}
else{
evaluate(trial,&ftrial,&itrial);
}
if(ftrial<gg->get_target()){
//printf("changing lowball\n");
if(itrial>=0)iout=itrial;
flow=ftrial;
for(i=0;i<gg->get_dim();i++)lowball.set(i,trial.get_data(i));
}
else{
fhigh=ftrial;
for(i=0;i<gg->get_dim();i++)highball.set(i,trial.get_data(i));
}
ct++;
dd*=0.5;
}
if(iout>=0)add_as_boundary(iout);
return iout;
}
int node::coulomb_search(){
/*
will return the index of the best point it found
*/
gg->set_iWhere(iCoulomb);
int iout=-1;
double before=double(time(NULL));
int ibefore=gg->get_called();
double eps=1.0e-6,tol=0.01*(gg->get_target()-gg->get_chimin());
gg->reset_cache(); //so that results of previous GP interpolation are not carried over
array_1d<double> av_dir,dir;
av_dir.set_name("node_coulomb_search_av_dir");
dir.set_name("node_coulomb_searc_dir");
int i,j;
for(i=0;i<gg->get_dim();i++)av_dir.set(i,0.0);
/*calculate the negative of the average direction from the node's center to its associates*/
for(i=0;i<associates.get_dim();i++){
for(j=0;j<gg->get_dim();j++){
dir.set(j,gg->get_pt(associates.get_data(i),j)-gg->get_pt(center_dex,j));
}
dir.normalize();
for(j=0;j<gg->get_dim();j++){
av_dir.subtract_val(j,dir.get_data(j));
}
}
double nn=av_dir.get_square_norm();
if(associates.get_dim()==0 || nn<eps){
for(j=0;j<gg->get_dim();j++){
av_dir.set(j,dice->doub());
}
}
av_dir.normalize();
double mu;
array_1d<double> walker;
walker.set_name("node_coulomb_search_walker");
if(farthest_associate>0.0){
nn=20.0*farthest_associate;
}
else{
nn=20.0;
}
int ct_abort=0,abort_max=30;
/*
First try to start the search at a point that is much farther away than
the farthest associate.
Note that we want to start at a point for which the GP predict chisq<chisq_lim
*/
mu=2.0*chisq_exception;
while(mu>gg->get_target() && ct_abort<abort_max){
gg->reset_cache();
nn*=0.5;
for(j=0;j<gg->get_dim();j++){
walker.set(j,gg->get_pt(center_dex,j)+nn*av_dir.get_data(j));
}
mu=gg->user_predict(walker);
ct_abort++;
}
if(ct_abort==abort_max){
/*
If we have failed to find a point for which the GP predicts chisq<chisq_lim,
try to initialize the search from a random point that is very near to the
center
*/
ct_abort=0;
mu=2.0*chisq_exception;
while(mu>gg->get_target() && ct_abort<1000){
ct_abort++;
for(i=0;i<gg->get_dim();i++)av_dir.set(i,dice->doub());
av_dir.normalize();
for(i=0;i<gg->get_dim();i++){
walker.set(i,gg->get_pt(center_dex,i)+eps*av_dir.get_data(i)*(gg->get_max(i)-gg->get_min(i)));
}
gg->reset_cache();
mu=gg->user_predict(walker);
}
if(ct_abort==1000){
/*
If we still have not been able to find a point for which the GP predict
chisq<chisq_lim, pick a random point, evaluate chisq at that point and
abort the searc
*/
for(i=0;i<gg->get_dim();i++){
walker.set(i,gg->get_pt(center_dex,i)+0.001*av_dir.get_data(i)*(gg->get_max(i)-gg->get_min(i)));
}
evaluateNoAssociate(walker,&nn,&iout);
time_coulomb+=double(time(NULL))-before;
ct_coulomb+=gg->get_called()-ibefore;
return iout;
}
}
array_1d<double> velocity,acceleration,newpt;
velocity.set_name("node_coulomb_velocity");
acceleration.set_name("node_coulomb_acceleration");
newpt.set_name("node_coulomb_newpt");
double speed,aa,delta=0.1;
/*initialize with a random velocity pointing (mostly) away from the node's center*/
for(i=0;i<gg->get_dim();i++){
velocity.set(i,walker.get_data(i)-gg->get_pt(center_dex,i));
velocity.add_val(i,1.0e-5*(dice->doub()-0.5));
}
speed=velocity.normalize();
while(speed<1.0e-10){
/*in case the vector between the walker and the center is too small*/
for(i=0;i<gg->get_dim();i++)velocity.set(i,dice->doub());
speed=velocity.normalize();
}
speed=0.001;
for(i=0;i<gg->get_dim();i++){
velocity.multiply_val(i,speed);
}
int istep=0,step_max=100,walker_in_bounds=1;
double dtv,dta,dt,newmu,dx,distance_to_center;
delta=0.1;
dx=1.0;
while(dx>1.0e-6 && delta>1.0e-6 && walker_in_bounds==1 && (gg->get_target()-mu>tol || istep<step_max)){
istep++;
for(i=0;i<gg->get_dim();i++){
acceleration.set(i,0.0);
}
for(i=0;i<associates.get_dim()+1;i++){
/*loop over points and center, adding repulsive force to acceleration vector*/
if(i<associates.get_dim()){
for(j=0;j<gg->get_dim();j++){
dir.set(j,gg->get_pt(associates.get_data(i),j)-walker.get_data(j));
}
}
else{
for(j=0;j<gg->get_dim();j++){
dir.set(j,gg->get_pt(center_dex,j)-walker.get_data(j));
}
}
nn=dir.normalize();
if(i>=associates.get_dim()){
distance_to_center=nn;
}
for(j=0;j<gg->get_dim();j++){
acceleration.subtract_val(j,dir.get_data(j)/(nn*nn+eps));
}
}//loop over points repelling the walker
aa=sqrt(acceleration.get_square_norm());//this does not actually renormalize the acceleration vector
speed=sqrt(velocity.get_square_norm());//this does not actually renormalize the velocity vector
dtv=delta*distance_to_center/speed;
dta=delta*speed/aa;
/*dtv is the time step if we want the walker's motion to be small;
dta is the time step if we want the walker's acceleration to be small*/
if(isnan(dta) && isnan(dtv)){
printf("WARNING both coulomb steps are nans\n");
exit(1);
}
else if(isnan(dtv) && !(isnan(dta)))dt=dta;
else if(isnan(dta) && !(isnan(dtv)))dt=dtv;
else if(dtv<dta) dt=dtv;
else if(dta<dtv) dt=dta;
else{
dt=eps;
}
for(i=0;i<gg->get_dim();i++){
newpt.set(i,walker.get_data(i)+dt*velocity.get_data(i));
}
dx=gg->distance(walker,newpt);
newmu=gg->user_predict(newpt);
/*
We do not want the coulomb search to carry us outside of the region
where the GP predicts chisq<chisq_lim
*/
if(newmu<gg->get_target()){
mu=newmu;
for(i=0;i<gg->get_dim();i++){
walker.add_val(i,dt*velocity.get_data(i));
velocity.add_val(i,dt*acceleration.get_data(i));
}
for(i=0;i<gg->get_dim() && walker_in_bounds==1;i++){
if(walker.get_data(i)>gg->get_max(i) || walker.get_data(i)<gg->get_min(i)){
walker_in_bounds=0;
}
}
}
else{
delta*=0.5;
}
}//loop over number of coulomb steps
evaluate(walker,&nn,&iout);
time_coulomb+=double(time(NULL))-before;
ct_coulomb+=gg->get_called()-ibefore;
return iout;
}
int node::ricochet_driver(int istart, array_1d<double> &vstart, array_1d<double> &vout){
/*
returns the index of the point found
*/
if(istart<0)return -1;
array_1d<double> gradient,trial;
gradient.set_name("node_ricochet_gradient");
trial.set_name("node_ricochet_trial");
int i,j,k;
double nn,y1,y2,x1,x2;
array_1d<double> g_trial;
double dx;
g_trial.set_name("ricochet_driver_g_trial");
//seed some points around istart so that the gradient is accurate
for(i=0;i<gg->get_dim();i++){
for(j=0;j<gg->get_dim();j++){
trial.set(j,gg->get_pt(istart,j));
}
trial.add_val(i,1.0e-7*(gg->get_max(i)-gg->get_min(i)));
evaluateNoAssociate(trial,&nn,&j);
}
try{
gg->actual_gradient(istart,gradient);
}
catch(int iex){
for(i=0;i<gg->get_dim();i++){
g_trial.set(i,gg->get_pt(istart,i));
}
for(i=0;i<gg->get_dim();i++){
dx=1.0e-5*gg->get_length(i);
g_trial.add_val(i,dx);
x1=g_trial.get_data(i);
evaluateNoAssociate(g_trial,&y1,&j);
g_trial.subtract_val(i,2.0*dx);
x2=g_trial.get_data(i);
evaluateNoAssociate(g_trial,&y2,&j);
g_trial.set(i,gg->get_pt(istart,i));
gradient.set(i,(y2-y1)/(x2-x1));
}
dx=gradient.get_square_norm();
if(dx<1.0e-10 || isnan(dx)){
printf("had to abort ricochet; gradient is no good\n");
return -1;
}
}
double dnorm=sqrt(vstart.get_square_norm());
double vdotg,gnorm=gradient.normalize();
vdotg=0.0;
for(i=0;i<gg->get_dim();i++){
vdotg+=vstart.get_data(i)*gradient.get_data(i);
}
/*reflect the incoming velocity about the gradient of chisquared*/
array_1d<double> velocity;
velocity.set_name("node_ricochet_velocity");
for(i=0;i<gg->get_dim();i++){
velocity.set(i,vstart.get_data(i)-2.0*vdotg*gradient.get_data(i));
}
double speed=velocity.normalize(),ss,chibest=2.0*chisq_exception;
double ftrial=2.0*chisq_exception,flow,fhigh;
array_1d<double> lowball,highball;
int iLow,iHigh;
lowball.set_name("ricochet_driver_lowball");
highball.set_name("ricochet_driver_highball");
/*try to find seeds for bisection along the reflected direction*/
flow=gg->get_fn(istart);
for(i=0;i<gg->get_dim();i++){
lowball.set(i,gg->get_pt(istart,i));
}
double startingSpeed=vstart.normalize();
ss=0.01*startingSpeed;
int ct=0;
while(flow>=gg->get_target() && ct<20){
for(i=0;i<gg->get_dim();i++){
lowball.set(i,gg->get_pt(istart,i)-ss*vstart.get_data(i));
}
evaluateNoAssociate(lowball,&flow,&iLow);
ss+=0.01*startingSpeed;
ct++;
}
if(ct>=20){
printf("could not find iLow\n");
return -1;
}
ct=0;
ss=2.0*speed;
fhigh=-2.0*chisq_exception;
while(fhigh<=gg->get_target() && ct<20){
for(i=0;i<gg->get_dim();i++){
highball.set(i,lowball.get_data(i)+ss*velocity.get_data(i));
}
evaluateNoAssociate(highball,&fhigh,&iHigh);
ss*=2.0;
ct++;
}
if(ct>=20){
printf("could not find iHigh %e\n",fhigh);
return -1;
}
int iout=-1,ii;
//iout=bisection(lowball,flow,highball,fhigh);spock
/*implement independent bisection because need different tolerance*/
for(ii=0;ii<15;ii++){
for(i=0;i<gg->get_dim();i++){
trial.set(i,0.5*(lowball.get_data(i)+highball.get_data(i)));
}
evaluateNoAssociate(trial,&ftrial,&j);
if(ftrial<=gg->get_target()){
for(i=0;i<gg->get_dim();i++)lowball.set(i,trial.get_data(i));
flow=ftrial;
if(j>=0)iout=j;
}
else{
for(i=0;i<gg->get_dim();i++)highball.set(i,trial.get_data(i));
}
}
if(iout>=0)add_as_boundary(iout);
for(i=0;i<gg->get_dim();i++){
vout.set(i,velocity.get_data(i));
}
return iout;
}
void node::ricochet_search(int iStart, array_1d<double> &dir){
int ibefore=gg->get_called();
double before=double(time(NULL));
gg->set_iWhere(iRicochet);
double ftrial;
array_1d<double> trial;
trial.set_name("node_ricochet_search_trial");
int iEnd,ii,itrial,i,j;
double dotproduct;
array_1d<double> vout;
vout.set_name("node_ricochet_search_vout");
array_1d<double> distance_traveled;
array_1d<int> pts_visited;
distance_traveled.set_name("node_ricochet_search_distance_traveled");
/*distance_traveled is a running total of how far the ricochet has come*/
pts_visited.set_name("node_ricochet_search_pts_visited");
/*pts_visited logs the end points of the individual ricochets*/
int iMedian;
double medianDistance,nn;
dotproduct=1.0;
distance_traveled.add(0.0);
pts_visited.add(iStart);
for(ii=0;ii<10*gg->get_dim() && dir.get_square_norm()>1.0e-20 && dotproduct>0.0; ii++){
try{
iEnd=ricochet_driver(iStart,dir,vout);
}
catch (int iex){
printf("ending Ricochet because of exception\n");
ii=10*gg->get_dim()+1;
}
if(iEnd>=0){
i=distance_traveled.get_dim();
distance_traveled.add(distance_traveled.get_data(i-1)+gg->distance(iStart,iEnd));
pts_visited.add(iEnd);
dotproduct=0.0;
for(i=0;i<gg->get_dim();i++){
/*can this ever be negative...?*/
dotproduct+=vout.get_data(i)*(gg->get_pt(iEnd,i)-gg->get_pt(center_dex,i));
dir.set(i,gg->get_pt(iEnd,i)-gg->get_pt(iStart,i));
}
iStart=iEnd;
}
else{
printf("ending Ricochet because iEnd %d\n",iEnd);
ii=10*gg->get_dim()+1;
}
}//loop on ii
printf("ending Ricochet %d %e %e\n",ii,dir.get_square_norm(),dotproduct);
printf("points visited %d -- %e\n",
pts_visited.get_dim(),distance_traveled.get_data(distance_traveled.get_dim()-1));
printf("number of points %d\n\n",gg->get_whereCt(iRicochet));
array_1d<int> neigh;
array_1d<double> ddneigh;
neigh.set_name("ricochet_neigh");
ddneigh.set_name("ricochet_ddneigh");
if(pts_visited.get_dim()>1){
/*
First do a compass search in the middle of the last ricochet path
*/
j=pts_visited.get_dim()-1;
for(i=0;i<gg->get_dim();i++){
trial.set(i,0.5*(gg->get_pt(pts_visited.get_data(j),i)+gg->get_pt(pts_visited.get_data(j-1),i)));
}
evaluateNoAssociate(trial,&ftrial,&itrial);
if(itrial>=0)compass_search(itrial);
/*
Now do a compass search as near to the middle of the full richochet as possible
*/
for(i=0;i<pts_visited.get_dim();i++){
nn=fabs(distance_traveled.get_data(i)-0.5*distance_traveled.get_data(distance_traveled.get_dim()-1));
if(i==0 || nn<medianDistance){
medianDistance=nn;
iMedian=i;
}
}
if(iMedian!=distance_traveled.get_dim()-1){
if(iMedian==0){
for(i=0;i<gg->get_dim();i++){
trial.set(i,0.5*(gg->get_pt(pts_visited.get_data(iMedian),i)+gg->get_pt(pts_visited.get_data(iMedian+1),i)));
}
}
else{
for(i=0;i<gg->get_dim();i++){
trial.set(i,0.5*(gg->get_pt(pts_visited.get_data(iMedian),i)+gg->get_pt(pts_visited.get_data(iMedian-1),i)));
}
}
evaluateNoAssociate(trial,&ftrial,&itrial);
if(itrial>=0){
compass_search(itrial);
}
}
/*now find the points nearest to the center of each leg, and save them
as potential nodes themselves*/
for(i=1;i<pts_visited.get_dim();i++){
for(j=0;j<gg->get_dim();j++){
trial.set(j,0.5*(gg->get_pt(pts_visited.get_data(i),j)+gg->get_pt(pts_visited.get_data(i-1),j)));
}
gg->nn_srch(trial,1,neigh,ddneigh);
if(neigh.get_data(0)>=0){
candidates.add(neigh.get_data(0));
}
}
}
time_ricochet+=double(time(NULL))-before;
ct_ricochet+=gg->get_called()-ibefore;
}
void node::compass_search(int istart){
/*perform a compass search centered on the point designated by istart*/
if(gg==NULL){
printf("WARNING cannot compass search; gg is null\n");
exit(1);
}
if(basisVectors.get_rows()!=gg->get_dim() || basisVectors.get_cols()!=gg->get_dim()){
printf("WARNING in compass search dim %d but bases %d by %d\n",
gg->get_dim(),basisVectors.get_rows(),basisVectors.get_cols());
exit(1);
}
if(gg->get_fn(istart)>=gg->get_target()){
return;
}