This repository has been archived by the owner on Sep 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet1.c
1700 lines (1328 loc) · 47.1 KB
/
net1.c
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
// net1.c - precompute 1-stop connections
/*
This file is part of Tripover, a broad-search journey planner.
Copyright (C) 2014-2017 Joris van der Geer.
*/
/*
Build connectivity matrix between any 2 full ports
for each dep
for each via = direct arr of dep
for each direct arr of via
store via+typical endtoend time
Initialize the network once at startup :
create a pre-computed 1-stop connectivity network used in search
derived matrix for each of n intermediate hops
each matrix contains a list of possible trips from port A to port B
the list is limited to a top-n on average trip duration, and trimmed on heuristics such as distance, cost, timing
base matrix for direct (non-stop) hops is prepared in net.c
*/
#include <string.h>
#include <pthread.h>
#include "base.h"
#include "cfg.h"
#include "mem.h"
#include "math.h"
static ub4 msgfile;
#include "msg.h"
#include "iadr.h"
#include "util.h"
#include "time.h"
#include "net.h"
#include "net1.h"
#include "netev.h"
#undef hdrstop
static int vrbena;
void ininet1(void)
{
msgfile = setmsgfile(__FILE__);
iniassert();
vrbena = (getmsglvl() >= Vrb);
}
// max number of alternatives per dep,arr pair to consider
#define Durcnt 256
static const ub4 no_airsep = 1; // pending change to un/reserved
// heuristic limit for over-route net1 versus direct
static ub4 geodistlim(ub4 dir)
{
ub4 lim = 0;
if (dir <= 100) return dir * 10;
if (dir > 100) { lim += 100 * 10; dir -= 100; }
if (dir > 1000) { lim += 1000 * 5; dir -= 1000; }
if (dir > 10000) { lim += 10000 * 5; dir -= 10000; }
if (dir > 100000) { lim += 100000 * 4; dir -= 100000; }
if (dir > 500000) { lim += 500000 * 3; dir -= 500000; }
lim += dir * 2;
return lim;
}
// geographical direct line distance from grid or direct
static ub4 xgeodist(struct port *pdep,struct port *parr,ub4 seqdep,ub8 gseqcnt,ub2 *seqdist)
{
ub4 dir;
if (seqdist && (pdep->gridlat + 1 < parr->gridlat || pdep->gridlon + 1 < parr->gridlon
|| parr->gridlat + 1 < pdep->gridlat || parr->gridlon + 1 < pdep->gridlon) ) {
dir = (ub4)seqdist[(ub8)seqdep * gseqcnt + parr->gridseq] << Gridshift;
} else dir = fgeodist(pdep,parr,1);
return dir;
}
static ub4 getvarlim(struct port *pdep,struct port *parr)
{
ub4 nda = max(pdep->nda,parr->nda);
if (nda < globs.net1con[0]) return 0;
else if (nda < globs.net1con[1]) return globs.net1limit[0];
else if (nda < globs.net1con[2]) return globs.net1limit[1];
else return globs.net1limit[2];
}
#if 0
// check whether hop2 is to be filtered, given hop1 and net0
static int net1filter(lnet *net,ub4 n0,ub4 *lst0,ub4 hop1,ub4 hop2)
{
struct chop *hp0,*hp1,*hp2,*hops = net->chops;
ub4 chopcnt = net->chopcnt;
ub4 nev0;
ub4 hop0,v0;
ub1 *covhrp0,*covhrp1,*covhr = net->covhr;
ub8 rnghr = net->rnghr;
ub8 mapofs0,mapofs1;
ub4 cov0,cov1,h;
if (hop1 >= chopcnt && hop2 >= chopcnt) return warn(0,"unexpected walk link for %u-%u",hop1,hop2);
hp1 = hops + hop1;
hp2 = hops + hop2;
ub4 lodur1 = hp1->lodur;
ub4 lodur2 = hp2->lodur;
ub4 lodur12 = lodur1 + lodur2;
if (hop2 >= chopcnt) {
for (v0 = 0; v0 < n0; v0++) {
hop0 = lst0[v0];
if (hop0 >= chopcnt) continue;
hp0 = hops + hop0;
nev0 = hp0->evcnt;
if (nev0 == 0) continue;
mapofs1 = hp1->mapofshr;
mapofs0 = hp0->mapofshr;
covhrp1 = covhr + mapofs1;
covhrp0 = covhr + mapofs0;
cov1 = cov0 = 0;
for (h = 0; h < rnghr; h++) {
if (covhrp1[h] == covhrp0[h]) continue;
else if (covhrp1[h] > covhrp0[h]) cov1++;
else cov0++;
}
if (hp1->rid == hp0->rid && cov1 == 0) break;
else if (cov1 == 0 && lodur12 > hp0->hidur && lodur12 - hp0->hidur >= 60 / max(hp0->avgperhr,1)) break;
}
if (v0 != n0) return 1;
else return 0;
}
if (hop1 < chopcnt) {
for (v0 = 0; v0 < n0; v0++) {
hop0 = lst0[v0];
if (hop0 >= chopcnt) continue;
hp0 = hops + hop0;
nev0 = hp0->evcnt;
if (nev0 == 0) continue;
if (hp0->cnthr < min(hp1->cnthr,hp2->cnthr)) continue;
if (hp0->lodur > lodur12) continue;
mapofs1 = hp1->mapofshr;
mapofs0 = hp0->mapofshr;
covhrp1 = covhr + mapofs1;
covhrp0 = covhr + mapofs0;
cov1 = cov0 = 0;
for (h = 0; h < rnghr; h++) {
if (covhrp1[h] == covhrp0[h]) continue;
else if (covhrp1[h] > covhrp0[h]) cov1++;
else cov0++;
}
if (hp1->rid == hp0->rid && cov1 == 0 && hp0->lodur <= lodur12) break;
else if (cov1 == 0 && lodur12 > hp0->hidur && lodur12 - hp0->hidur >= 60 / max(hp0->avgperhr,1)) break;
}
if (v0 != n0) return 1;
else return 0;
}
return 0;
}
#endif
/*
mid1:12
v1:10
v2:10
dur:16
*/
#define Lst1midbit 12
#define Lst1midcnt (1 << Lst1midbit)
#define Lst1midmask (Lst1midcnt - 1)
#define Lst1vbit 10
#define Lst1vcnt (1 << Lst1vbit)
#define Lst1vmask (Lst1vcnt - 1)
#define Lst1v1shift Lst1midbit
#define Lst1v2shift (Lst1v1shift + Lst1vbit)
#define Lst1durshift (Lst1v2shift + Lst1vbit)
#if Lst1durshift > (64 - 16)
#error "Lst1durshift too large"
#endif
#define Lst1durcnt 65536
// max number of via stops to consider for net1
#define Viacnt1 Lst1midcnt
struct net1_args {
// in
ub4 tid,tidcnt;
struct network *net;
ub4 depfrom,depto;
ub4 varlimit,var12limit,cntonly;
ub4 *depmid1s;
ub4 *depmid1ds;
ub4 *depmid1cnts;
ub4 *dmid1cnts;
ub4 *portdst;
// workspace
ub4 *aid2s;
// out
int rv;
ub8 lstlen;
};
static int net1_pas1(struct net1_args *argp)
{
ub4 tid = argp->tid;
ub4 tidcnt = argp->tidcnt;
ub4 msgtid;
ub4 *depmid1s = argp->depmid1s;
ub4 *dmid1ds,*depmid1ds = argp->depmid1ds;
ub4 *depmid1cnts = argp->depmid1cnts;
ub4 *dmid1cnts = argp->dmid1cnts;
ub4 *dmid1s;
ub4 *dmid1acnt;
ub4 dmid1cnt;
ub4 varlimlh;
ub4 varlimit = argp->varlimit;
ub4 var12limit = argp->var12limit;
ub4 cntonly = argp->cntonly;
struct network *net = argp->net;
ub4 portcnt = net->portcnt;
struct port *pdep,*pmid,*parr,*ports = net->ports;
iadrbase *cnts0,*precnts;
ub4 dep,mid,arr;
ub4 dmid1;
ub4 cnt,n1,n2,n12;
ub8 lstlen;
ub4 cntlim,geocnt;
ub4 dirdist,dirdistdm,dirdistma,gdistlim;
ub4 seqdep,seqarr,seqmid;
ub4 gseqcnt = net->seqdlen;
ub2 *seqdist = net->seqdist;
ub1 lotx,*seqlotx = net->seqlotx;
int havelotx = net->havelotx;
ub8 stat_var12lim = 0,stat_distlim1 = 0,stat_distlim2 = 0,stat_lotx = 0;
struct eta eta;
lstlen = 0;
ub4 depfrom = argp->depfrom;
ub4 depto = argp->depto;
ports = net->ports;
precnts = &net->conipos;
cnts0 = net->concnt;
iadrbase *lstitems = &net->lst1items;
if (tidcnt > 1) {
msgtid = (tid << Tidshift) | Tidbit;
info(msgtid,"start in thread %u",tid);
} else msgtid = 0;
info(msgtid,"ports %u-%u of %u",depfrom,depto,portcnt);
// for each departure port
for (dep = depfrom; dep < depto; dep++) {
if (tprogress(tid,tidcnt,&eta,"1-stop pass 1 port %u of %u \ah%lu conns",dep - depfrom,depto - depfrom,lstlen)) return 2;
pdep = ports + dep;
if (pdep->valid == 0) continue;
dmid1s = depmid1s + dep * Viacnt1;
dmid1ds = depmid1ds + dep * Viacnt1;
dmid1acnt = depmid1cnts + dep * Viacnt1;
dmid1cnt = dmid1cnts[dep];
if (dmid1cnt == 0) continue;
seqdep = pdep->gridseq;
// for each arrival port
for (arr = 0; arr < portcnt; arr++) {
if (arr == dep) continue;
parr = ports + arr;
if (parr->valid == 0) continue;
seqarr = parr->gridseq;
if (havelotx && seqdep != seqarr) {
lotx = seqlotx[(ub8)seqdep * gseqcnt + seqarr];
if (lotx > 1) { stat_lotx++; continue; }
}
if (cntonly < 256) {
n1 = rdiadr2(cnts0,dep,arr);
if (n1 > cntonly) continue;
}
varlimlh = getvarlim(pdep,parr);
error_gt(varlimlh,varlimit,0);
if (varlimlh == 0) continue;
dirdist = xgeodist(pdep,parr,seqdep,gseqcnt,seqdist);
gdistlim = geodistlim(dirdist);
cnt = cntlim = geocnt = 0;
error_ovf(lstlen + varlimit * portcnt,ub4);
// for each via
for (dmid1 = 0; cnt < varlimlh && dmid1 < dmid1cnt; dmid1++) {
mid = dmid1s[dmid1];
if (mid == arr) continue;
dirdistdm = dmid1ds[dmid1];
if (dirdistdm > gdistlim) { geocnt = 1; stat_distlim1++; continue; }
n2 = rdiadr2(cnts0,mid,arr);
if (n2 == 0) continue;
pmid = ports + mid;
seqmid = pmid->gridseq;
dirdistma = xgeodist(pmid,parr,seqmid,gseqcnt,seqdist);
if (dirdistdm + dirdistma > gdistlim) {
geocnt = 1;
stat_distlim2++;
continue;
}
n2 = min(n2,Lst1vcnt);
n1 = dmid1acnt[dmid1];
error_z(n1,dmid1);
n12 = n1 * n2;
if (n12 > var12limit) { n12 = var12limit; stat_var12lim++; }
cnt += n12;
} // each mid stopover port
// if (geocnt) cnt++; // add one tentative item
cntlim = min(cnt,varlimlh);
if (cntlim) {
iadrinc(precnts,dep,arr,tid);
iadrincn(lstitems,dep,arr * varlimit,cntlim,tid);
lstlen += cntlim;
}
} // each arr
} // each dep
argp->lstlen = lstlen;
info(msgtid,"limits dist \ah%lu \ah%lu var \ah%lu lotx \ah%lu",stat_distlim1,stat_distlim2,stat_var12lim,stat_lotx);
return info(msgtid,"tid %u pass 1 \ah%lu tentative triplets",tid,lstlen);
} // net1 pass 1
static void *net1_pass1(void *vp)
{
struct net1_args *argp = (struct net1_args *)vp;
ub4 tid = argp->tid;
globs.tids[tid] = 1;
int rv = net1_pas1(argp);
argp->rv = rv;
globs.tids[tid] = 0;
return vp;
}
static int net1_pas2(struct net1_args *argp)
{
ub4 tid = argp->tid;
ub4 tidcnt = argp->tidcnt;
ub4 msgtid;
ub4 nstop = 1;
ub4 *dmid1s,*depmid1s = argp->depmid1s;
ub4 *dmid1ds,*depmid1ds = argp->depmid1ds;
ub4 *depmid1cnts = argp->depmid1cnts;
ub4 *dmid1cnts = argp->dmid1cnts;
ub4 *dmid1acnt;
ub4 dmid1cnt;
ub4 varlimlh;
ub4 varlimit = argp->varlimit;
ub4 var12limit = argp->var12limit;
ub4 cntonly = argp->cntonly;
ub4 *portdst = argp->portdst;
struct network *net = argp->net;
ub4 portcnt = net->portcnt;
ub4 thopcnt = net->thopcnt;
ub4 whopcnt = net->whopcnt;
ub4 aidcnt = net->agencycnt;
struct port *pdep,*pmid,*parr,*ports = net->ports;
struct chop *hp1,*hp2,*chops = net->chops;
block *lstblk0;
ub4 *portsbyhop;
iadrbase *cnts0,*precnts,*cnts;
iadrbase *conofs0;
ub8 ofs0,ofs1,ofs2;
ub4 *conlst1,*lst0,*lst1,*lst2;
ub4 *hopdist = net->hopdist;
ub1 leg12mode,*hopmode = net->hopmodes;
ub4 air,aircnt;
ub4 lodur1,lodur2;
ub4 dtlo,dthi,typdt,dtcnt;
ub4 rid1;
ub4 dep,mid,arr;
ub4 dmid1;
ub4 n,n0,n1,n2,n12,altcnt,v1,v2,leg1,leg2;
ub8 lstlen;
ub4 dirdist,dirdistdm,dirdistma;
ub4 dist1,dist2,gdistlim;
ub4 sumwalkdist1,sumwalkdist2;
ub4 cntlimdur,gen,outcnt;
ub4 walklimit = net->net1walklimit;
ub4 midur,durndx,durcnt,durlim,durcntgnd,durlimgnd;
ub4 midurs[Durcnt],midursgnd[Durcnt];
ub4 sortdurs[Durcnt];
ub4 tmpmodes[Durcnt];
ub8 tmpitem,tmpitems[Durcnt],tmpitemsgnd[Durcnt];
ub4 sumwalklimit = net->sumwalklimit;
ub8 stat_cntlim = 0,stat_partlimdur = 0;
ub8 stat_var12limit = 0,stat_flt = 0,stat_lotx = 0,stat_varlim = 0;
ub8 stat_noestdur = 0,stat_estdur = 0,stat_timlim = 0;
ub4 hicnt = 0;
ub8 nogen = 0,sumoutcnt = 0;
ub4 hialt = 0,altdep = 0,altarr = 0;
struct eta eta;
lstlen = 0;
ub4 depfrom = argp->depfrom;
ub4 depto = argp->depto;
ub8 tport2 = depto - depfrom;
tport2 *= tport2;
ub4 *aid2s = argp->aid2s;
nclear(aid2s,aidcnt * aidcnt);
ub4 da_timlim = globs.net1timlim;
ports = net->ports;
portsbyhop = net->portsbyhop;
precnts = &net->conipos;
conofs0 = net->conofs;
cnts0 = net->concnt;
cnts = net->concnt + nstop;
lstblk0 = net->conlst;
lst0 = conlst1 = blkdata(lstblk0,0,ub4);
iadrbase *lstitems = &net->lst1items;
ub4 hindx,hidur;
ub4 seqdep,seqmid,seqarr;
ub4 gseqcnt = net->seqdlen;
ub2 *seqdist = net->seqdist;
ub1 lotx,*seqlotx = net->seqlotx;
int havelotx = net->havelotx;
if (tidcnt > 1) {
msgtid = (tid << Tidshift) | Tidbit;
info(msgtid,"start in thread %u",tid);
} else msgtid = 0;
for (dep = depfrom; dep < depto; dep++) {
pdep = ports + dep;
if (tprogress(tid,tidcnt,&eta,"p2 port %4u/%u \ah%lu con %*s",
dep - depfrom,depto - depfrom,lstlen,*pdep->prefix ? 17 : 0,pdep->prefix)) return 2;
if (pdep->valid == 0) continue;
dmid1s = depmid1s + dep * Viacnt1;
dmid1ds = depmid1ds + dep * Viacnt1;
dmid1acnt = depmid1cnts + dep * Viacnt1;
dmid1cnt = dmid1cnts[dep];
if (dmid1cnt == 0) continue;
error_ovf(lstlen + 2 * portcnt,ub4);
if (hi32 - varlimit <= lstlen) {
warn(msgtid,"limiting net1 to \ah%lu at dep %u of %u",lstlen,dep,portcnt);
break;
}
seqdep = pdep->gridseq;
outcnt = 0;
// for each arrival port
for (arr = 0; arr < portcnt; arr++) {
if (arr == dep) continue;
parr = ports + arr;
if (parr->valid == 0) continue;
seqarr = parr->gridseq;
if (havelotx && seqdep != seqarr) {
lotx = seqlotx[(ub8)seqdep * gseqcnt + seqarr];
if (lotx > 1) { stat_lotx++; continue; }
}
n0 = rdiadr2(cnts0,dep,arr);
if (cntonly < 256 && n0 > cntonly) continue;
if (n0) {
ofs0 = rdiadr8(conofs0,dep,arr);
lst0 = conlst1 + ofs0;
}
if (hi32 - varlimit <= lstlen) {
warn(msgtid,"limiting net1 to \ah%lu at dep %u of %u arr %u",lstlen,dep,portcnt,arr);
break;
}
setthtimer(tid,da_timlim);
varlimlh = getvarlim(pdep,parr);
if (varlimlh == 0) { stat_varlim++; continue; }
dirdist = xgeodist(pdep,parr,seqdep,gseqcnt,seqdist);
gdistlim = geodistlim(dirdist);
gen = 0;
// todo: start with limits derived from previous nstop
// e.g. lodists[da] * 2
// initial heuristic distance-based limit
if (dirdist < 10) durlim = 60;
else if (dirdist < 1 * 100) durlim = 2 * 60;
else if (dirdist < 10 * 100) durlim = 12 * 60;
else if (dirdist < 100 * 100) durlim = 1 * 1440;
else if (dirdist < 1000 * 100) durlim = 4 * 1440;
else if (dirdist < 10000 * 100) durlim = 8 * 1440;
else durlim = 14 * 1440;
durlimgnd = durlim;
cntlimdur = min(varlimlh,Durcnt-1);
nsethi(midurs,cntlimdur);
nsethi(midursgnd,cntlimdur);
// create time top-n list, derive threshold
altcnt = 0;
durcnt = 0;
durcntgnd = 0;
// for each via
for (dmid1 = 0; dmid1 < dmid1cnt; dmid1++) {
dirdistdm = dmid1ds[dmid1];
if (dirdistdm > gdistlim) continue;
mid = dmid1s[dmid1];
if (mid == arr) continue;
n1 = dmid1acnt[dmid1];
error_z(n1,dmid1);
if (isexpired(tid)) { stat_timlim++; break; }
n2 = rdiadr2(cnts0,mid,arr);
if (n2 == 0) continue;
n2 = min(n2,Lst1vcnt);
pmid = ports + mid;
seqmid = pmid->gridseq;
dirdistma = xgeodist(pmid,parr,seqmid,gseqcnt,seqdist);
if (dirdistdm + dirdistma > gdistlim) continue;
n12 = 0;
ofs1 = rdiadr8(conofs0,dep,mid);
ofs2 = rdiadr8(conofs0,mid,arr);
error_eq(ofs1,hi32);
error_eq(ofs2,hi32);
lst1 = conlst1 + ofs1;
lst2 = conlst1 + ofs2;
bound(lstblk0,ofs1,ub4);
bound(lstblk0,ofs2,ub4);
// each dep-via-arr alternative
for (v1 = 0; v1 < n1 && n12 < var12limit; v1++) {
leg1 = lst1[v1];
if (leg1 == hi32) {
error(msgtid,"invalid trip %u-%u-%u",dep,mid,arr);
break;
}
error_ge(leg1,whopcnt);
hp1 = chops + leg1;
if (hp1->dep != dep) {
warn(msgtid,"hop %u %u-%u vs %u-%u",leg1,hp1->dep,hp1->arr,dep,mid);
continue;
}
dist1 = hopdist[leg1];
if (dist1 > gdistlim) continue;
if (leg1 >= thopcnt) {
if (dist1 > walklimit) continue;
sumwalkdist1 = dist1;
} sumwalkdist1 = 0;
air = (hopmode[leg1] & Airbit) | no_airsep;
lodur1 = hp1->lodur;
if (lodur1 > (air ? durlim : durlimgnd)) continue;
rid1 = hp1->rid;
for (v2 = 0; v2 < n2 && n12 < var12limit; v2++) {
n12++;
leg2 = lst2[v2];
error_ge(leg2,whopcnt);
dist2 = dist1 + hopdist[leg2];
if (dist2 > gdistlim) continue;
hp2 = chops + leg2;
if (rid1 == hp2->rid && hp2->tripstart == 0) continue;
sumwalkdist2 = sumwalkdist1;
if (leg2 >= thopcnt) {
if (dist2 > walklimit) continue;
if (leg1 >= thopcnt) continue; // if dep-arr within walkdist, do not add dep-mid-arr
sumwalkdist2 += hopdist[leg2];
}
if (sumwalkdist2 > sumwalklimit) continue;
// if (n0 && net1filter(net,n0,lst0,leg1,leg2)) { stat_flt++; continue; }
leg12mode = hopmode[leg1] | hopmode[leg2];
air = (leg12mode & Airbit) | no_airsep;
lodur2 = chops[leg2].lodur;
if (lodur1 + lodur2 > (air ? durlim : durlimgnd)) continue;
// maintain top-n list
dtcnt = estdur_2(net,leg1,leg2,&dtlo,&dthi,&typdt);
midur = typdt;
if (midur == 0) {
warn(msgtid,"port %u-%u var %u dur 0 dist %u %s to %s",dep,arr,v1,fgeodist(pdep,parr,1),pdep->iname,parr->iname);
continue;
}
if (dtcnt == 0) {
stat_noestdur++;
aid2s[hp1->aid * aidcnt + hp2->aid]++;
if (stat_noestdur < 10) {
if (net->agencycnt > 1) warn(0,"no estdur for %u-%u aid %u-%u %s - %s - %s",leg1,leg2,hp1->aid,hp2->aid,pdep->iname,pmid->iname,parr->iname);
else warn(0,"no estdur for %u-%u %s - %s - %s",leg1,leg2,pdep->iname,pmid->iname,parr->iname);
estdur_2(net,leg1,leg2,&dtlo,&dthi,&typdt);
}
continue;
} else if (midur > Lst1durcnt) {
warn(msgtid,"durlim %u exceeds %u",midur,Lst1durcnt);
continue;
} else if (midur < lodur1 + lodur2) {
estdur_2(net,leg1,leg2,&dtlo,&dthi,&typdt);
midur = typdt;
error(msgtid|Exit,"hop %u-%u dur %u below min %u+%u",leg1,leg2,midur,lodur1,lodur2);
continue;
}
altcnt++;
stat_estdur++;
if (no_airsep && midur > durlim) continue;
else if (midur > (air ? durlim : durlimgnd)) continue;
error_gt(durcnt,varlimit,arr);
error_gt(durcntgnd,varlimit,arr);
tmpitem = (ub8)dmid1 | ((ub8)v1 << Lst1v1shift) | ((ub8)v2 << Lst1v2shift) | ((ub8)midur << Lst1durshift);
todo: top-n for lo,mid,hi ?
// store in both mixed mode and ground-only. select part of each if needed lateron
if (durcnt == 0) { // first item
midurs[0] = midur;
tmpitems[0] = tmpitem;
tmpmodes[0] = leg12mode;
warncc(midur != hi32 && midur > 65534,Exit|msgtid,"durlim %u at 0 of %u exceeds 64k",midur,cntlimdur);
durcnt = 1;
} else if (durcnt < cntlimdur) { // next items
warncc(midur != hi32 && midur > 65534,Exit|msgtid,"durlim %u at %u of %u exceeds 64k",midur,durcnt,cntlimdur);
tmpitems[durcnt] = tmpitem;
tmpmodes[durcnt] = leg12mode;
midurs[durcnt++] = midur;
} else { // full: eventually replace slower entry
hidur = hindx = 0;
for (durndx = 0; durndx < cntlimdur; durndx++) {
if (midurs[durndx] > hidur) { hidur = midurs[durndx]; hindx = durndx; }
warncc(midurs[durndx] != hi32 && midurs[durndx] > 65534,Exit|msgtid,"durlim %u at %u of %u exceeds 64k",midurs[durndx],durndx,cntlimdur);
}
durlim = hidur;
warncc(durlim != hi32 && durlim > 65534,Exit|msgtid,"durlim %u at %u exceeds 64k",durlim,cntlimdur);
error_eq(durlim,hi32);
if (midur < hidur) {
midurs[hindx] = midur;
tmpitems[hindx] = tmpitem;
tmpmodes[hindx] = leg12mode;
}
}
if (no_airsep || air) continue;
// ground-only
// todo: change into reserved versus unreserved to accomodate fare availability
if (durcntgnd == 0) {
midursgnd[0] = midur;
tmpitemsgnd[0] = tmpitem;
durcntgnd = 1;
} else if (durcntgnd < cntlimdur) {
tmpitemsgnd[durcntgnd] = tmpitem;
midursgnd[durcntgnd++] = midur;
} else {
hidur = hindx = 0;
for (durndx = 0; durndx < cntlimdur; durndx++) {
if (midursgnd[durndx] > hidur) { hidur = midursgnd[durndx]; hindx = durndx; }
}
durlimgnd = hidur;
error_eq(durlimgnd,hi32);
if (midur < hidur) {
midursgnd[hindx] = midur;
tmpitemsgnd[hindx] = tmpitem;
}
}
} // each v2
} // each v1
if (n12 == var12limit) stat_var12limit++;
} // each mid
if (altcnt > hialt) { hialt = altcnt; altdep = dep; altarr = arr; }
error_gt(durcnt,varlimlh,dep);
if (durcnt == cntlimdur) stat_partlimdur++;
warncc(durlim != hi32 && durlim > 65534,Exit|msgtid,"durlim %u exceeds 64k",durlim);
if (durcnt == 0) { nogen++; continue; }
// emit mixed tmpitems only, or best half of tmpitems and -gnd if needed
iadrinc(cnts,dep,arr,tid);
outcnt++;
aircnt = 0;
for (n = 0; n < durcnt; n++) {
if (tmpmodes[n] & Airbit) aircnt++;
}
if (no_airsep || aircnt * 2 <= cntlimdur) { // mixed only
error_gt(durcnt,varlimlh,dep);
error_gt(durcnt,cntlimdur,dep);
for (n = 0; n < durcnt; n++) {
tmpitem = tmpitems[n];
if (wriadr8(lstitems,dep,arr * varlimit + n,tmpitem)) {
info(0,"dep %u arr %u n %u varlim %u dist %u",dep,arr,n,varlimit,dirdist);
break;
}
}
if (n < durcnt) continue;
if (wriadr2(precnts,dep,arr,(ub2)n)) {
info(0,"dep %u arr %u n %u varlim %u dist %u",dep,arr,n,varlimit,dirdist);
continue;
}
lstlen += n;
continue;
}
// best half of each
for (n = 0; n < durcnt; n++) sortdurs[n] = (midurs[n] << 16) | n;
sort4(sortdurs,durcnt,FLN,"net1 midurs");
n = gen = 0;
while (n < durcnt && gen < cntlimdur / 2) {
hindx = sortdurs[gen] & hi16;
error_ge(hindx,durcnt);
tmpitem = tmpitems[hindx];
if (wriadr8(lstitems,dep,arr * varlimit + gen,tmpitem)) { n = 0; break; }
gen++;
}
if (n == 0) continue;
lstlen += gen;
if (durcntgnd == 0) {
if (wriadr2(precnts,dep,arr,(ub2)gen)) info(0,"dep %u arr %u n %u varlim %u dist %u",dep,arr,n,varlimit,dirdist);
continue;
}
for (n = 0; n < durcntgnd; n++) sortdurs[n] = (midursgnd[n] << 16) | n;
sort4(sortdurs,durcntgnd,FLN,"net1 midurs");
n = 0;
while (n < durcntgnd && gen < cntlimdur) {
hindx = sortdurs[n] & hi16;
error_ge(hindx,durcntgnd);
tmpitem = tmpitemsgnd[hindx];
if (midursgnd[hindx] == hi32) { warn(0,"dep %u arr %u no midur at %u",dep,arr,n); n = 0; break; }
if (wriadr8(lstitems,dep,arr * varlimit + gen,tmpitem)) { n = 0; break; }
gen++;
}
if (n == 0) continue;
if (wriadr2(precnts,dep,arr,(ub2)gen)) {
info(0,"dep %u arr %u n %u varlim %u dist %u",dep,arr,n,varlimit,dirdist);
continue;
}
lstlen += gen;
} // each arrival port
portdst[dep] = outcnt;
sumoutcnt += outcnt;
} // each departure port
setthtimer(tid,0);
ub4 aid2,hiaidcnt = 0,hiaid2 = 0;
for (aid2 = 0; aid2 < aidcnt * aidcnt; aid2++) {
if (aid2s[aid2] > hiaidcnt) { hiaidcnt = aid2s[aid2]; hiaid2 = aid2; }
}
ub8 allestdur = stat_estdur + stat_noestdur;
info(msgtid|CC0,"no estdur \ah%lu of \ah%lu = \ap%lu%lu \ah%u at hi aid %u-%u",
stat_noestdur,allestdur,stat_noestdur,allestdur,hiaidcnt,hiaid2 / aidcnt,hiaid2 % aidcnt);
estdur2_stats(tid);
info(msgtid,"limits hicnt \ah%u cntlim \ah%lu partlim dur \ah%lu time \ah%lu var12 \ah%lu",hicnt,stat_cntlim,stat_partlimdur,stat_timlim,stat_var12limit);
info(msgtid," var \ah%lu lotx \ah%lu",stat_varlim,stat_lotx);
info(msgtid,"%u-stop p2: \ah%lu triplets in \ah%lu pairs, \ap%lu%lu",nstop,lstlen,sumoutcnt,sumoutcnt,tport2);
info(msgtid," \ah%lu filtered \ah%lu nogen",stat_flt,nogen);
pdep = ports + altdep;
parr = ports + altarr;
msgopt("pass1"); info(msgtid,"high alt %u for %u-%u %s %s",hialt,altdep,altarr,pdep->iname,parr->iname);
argp->lstlen = lstlen;
return 0;
}
static void *net1_pass2(void *vp)
{
struct net1_args *argp = (struct net1_args *)vp;
ub4 tid = argp->tid;
globs.tids[tid] = 1;
int rv = net1_pas2(argp);
argp->rv = rv;
globs.tids[tid] = 0;
rmthtimer(tid);
return vp;
}
// create 1-stop connectivity matrix and derived info
// uses 1 via from 0-stop net
int mknet1(struct network *net,ub4 varlimit,ub4 var12limit,ub4 cntonly,ub4 netstop)
{
ub4 nstop = 1;
ub4 portcnt = net->portcnt;
ub4 hopcnt = net->hopcnt;
ub4 thopcnt = net->thopcnt;
ub4 whopcnt = net->whopcnt;
struct port *ports,*pmid,*pdep,*parr;
block *lstblk,*lstblk0;
ub4 *portsbyhop;
iadrbase *cnts,*cnts0,*precnts;
iadrbase *conofs,*conofs0;
ub4 *portdst;
ub8 ofs,ofs1,ofs2,endofs,lstlen,tlstlen,newlstlen;
ub4 *lst,*newlst,*conlst1,*lst1,*lst2,*lstv1;
ub4 *hopdist = net->hopdist;
ub4 dep,mid,arr,dep2;
ub4 dmid1;
ub4 iv;
ub4 cnt,n,n1,n2,v1,v2,leg,leg1,leg2,nleg;
ub4 dist1,dist2,sumwalkdist2,walkdist1,walkdist2,maxwalk;
ub4 gen;
ub4 midur,durlim;
ub8 lstitem;
ub4 stat_nocon = 0,stat_partcnt = 0,stat_cntlim = 0,stat_partlimdur = 0;
struct eta eta;
if (varlimit == 0) return warn0(0,"skip 1-stop net on zero limit");
error_zz(portcnt,hopcnt);
info(0,"init %u-stop connections for %u port \ah%u hop network",nstop,portcnt,whopcnt);
info(0,"limits: var %u var12 %u",varlimit,var12limit);
ports = net->ports;
portsbyhop = net->portsbyhop;
precnts = &net->conipos;
clear(precnts);
cnts = net->concnt + nstop;
conofs = net->conofs + nstop;
ub4 sparsethres = net->sparsethres;
iadrbase *lstitems = &net->lst1items;
iadrbase *durlims = net->durlims + nstop;
error_lt(varlimit,globs.net1limit[0]);
error_lt(varlimit,globs.net1limit[1]);
error_ne(varlimit,globs.net1limit[2]);
if (mkiadr0(lstitems,portcnt,portcnt * varlimit,ub8,sparsethres,32,"net1 lstitems")) return 1;
if (setiadropts(lstitems,Iadr_append | Iadr_softlim)) return 1;
// main net1 structure
if (mkiadr0(precnts,portcnt,portcnt,ub2,sparsethres,10,"net1 precnts")) return 1;
if (setiadropts(precnts,Iadr_softlim)) return 1;
if (mkiadr0(cnts,portcnt,portcnt,ub2,sparsethres,10,"net1 cnts")) return 1;
if (mkiadr0(conofs,portcnt,portcnt,ub8,sparsethres,10,"net1 ofs")) return 1;
if (mkiadr0(durlims,portcnt,portcnt,ub2,sparsethres,10,"net1 durlims")) return 1;
setiadropts(durlims,Iadr_defhi);
portdst = talloc(portcnt, ub4,0,"net portdst",portcnt); // outbound conn stats
error_zp(hopdist,0);
nleg = nstop + 1;
/* Essentially we do for each (departure,arrival) pair:
Search for a 'via' port such that trip (dep,via) and (via2,arr) exist
Trim list of alternatives based on typical trip duration
Store result by value. This is memory-intensive but keeps code simple
In short:
foreach dep
foreach arr
foreach mid with [dep,mid]
...
*/