-
Notifications
You must be signed in to change notification settings - Fork 0
/
raam.h
1491 lines (969 loc) · 34.2 KB
/
raam.h
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 <assert.h>
#include <iterator>
#include <map>
#include <math.h>
#include <limits>
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include "stdio.h"
#include "string"
#include "math.h"
#include <vector>
#include <iomanip>
#include <list>
#include <iostream>
#include <fstream>
#include <algorithm>
#include "csv.h"
#define DEFAULT_REGION -1
#define RHO 1.315e3
using std::cout;
using std::cerr;
using std::endl;
using std::map;
using std::vector;
using std::string;
using std::stringstream;
using std::ofstream;
namespace RAAM {
const float FMAX = std::numeric_limits<float>::max();
const float FINF = std::numeric_limits<double>::infinity();
class Graph;
class Region;
class Agent;
class Resource;
class Edge;
class Tunnel;
class Graph {
public:
Graph(float tau = 15, float tol = 0.01, long long default_region = DEFAULT_REGION);
unsigned int load_regions (string fname, bool load_tau = false);
unsigned int load_resources(string fname, bool load_regions = false);
unsigned int load_agents (string fname, bool load_regions = false);
unsigned int load_edges (string fname, int mult = 1, int add = 0);
unsigned int load_tunnels (string fname);
void new_region (long long _id, float _frac = 0.5, float _tau = 0);
void new_agent (long long _id, int _pop, long long _region = DEFAULT_REGION);
void new_resource(long long _id, int _doc, long long _region = DEFAULT_REGION);
void new_edge (long long _agent, long long _resouce, float cost);
void new_tunnel (long long origin, long long destination, float capacity);
unsigned int change_resource_supply(long long id, int delta);
unsigned int increment_resource_supply(long long _id) { return change_resource_supply(_id, +1); }
unsigned int decrement_resource_supply(long long _id) { return change_resource_supply(_id, -1); }
float transform_cost(float cost);
void set_transform(float scale = 1, float offset = 0, float power = 1, bool log = false, float log_base = 2);
void allocate_min_fixed(bool verbose = false);
void allocate_min_cost(unsigned int max_moves = 10, bool verbose = false);
void set_tol(float t);
void set_tau(float a);
void tune_tau(float delta = 1, float min = 0.1, float max = 120);
bool equalize_use(unsigned int cycles = 1, unsigned int max_moves = 0, float decay = 0,
unsigned int max_tunnel_moves = 0, bool verbose = true);
double get_cost();
float get_error();
void write(string filename);
void write_frac_in_region(string filename);
private:
float _tau, _tol;
bool _transform;
float _transform_scale, _transform_offset, _transform_power;
bool _transform_log;
float _transform_log_base;
map<long long, unsigned int> _addrA;
map<unsigned int, long long> _idenA;
map<long long, unsigned int> _addrR;
map<unsigned int, long long> _idenR;
map<long long, unsigned int> _addrReg;
vector<Region*> _regions;
vector<Agent*> _agents;
vector<Resource*> _resources;
vector<Edge*> _edges;
vector<Tunnel*> _tunnels;
};
class Region {
public:
Region();
Region(long long, float, float);
void add_agent(Agent* a) { _agents.push_back(a); }
long long get_id() { return _id; }
float get_frac() { return _frac; }
float get_tau() { return _tau; }
unsigned int get_demand_in_region();
float get_frac_in_region();
void set_tau(float a);
void tune_tau(float delta = 1, float min = 0, float max = 120);
private:
long long _id;
float _frac;
float _tau;
vector<Agent*> _agents;
};
class Agent {
public:
Agent();
Agent(long long id, int demand, long long region, float tau, float tol);
void add_edge(Edge* e) { _edges.push_back(e); }
void add_tunnel(Tunnel* t) { _tunnels.push_back(t); }
string get_string();
long long get_id() { return _id; }
unsigned int get_demand() { return _demand; }
int get_n_choices() { return _edges.size(); }
float get_tau() { return _tau; }
void set_tau(float a) { _tau = a; }
float get_tol() { return _tol; }
void set_tol(float a) { _tol = a; }
unsigned int get_region() { return _region; }
unsigned int get_allocation() { return _allocated; }
void reset_allocation();
float get_avg_cost();
float get_avg_fixed_cost();
float get_avg_supply_cost();
float get_avg_local_cost();
float get_avg_local_fixed_cost();
float get_avg_local_supply_cost();
int get_total_in_region();
float get_frac_in_region();
float get_local_frac_in_region();
Edge* get_min_cost_edge();
Edge* get_min_fixed_edge();
Edge* get_max_cost_edge();
unsigned int allocate_min_cost(unsigned int max_moves, bool verbose = false);
void allocate_min_fixed(bool verbose = false);
bool equalize_use(unsigned int max_moves = 0);
bool equalize_tunnels(unsigned int max_moves = 0);
void return_demand(unsigned int d);
void remove_demand(unsigned int d);
void add_visitors(unsigned int nv);
void remove_visitors(unsigned int nv);
private:
long long _id;
long long _region;
unsigned int _demand;
unsigned int _absent;
unsigned int _visitors;
unsigned int _allocated;
float _tau, _tol;
bool _disconnected;
vector<Edge*> _edges;
vector<Tunnel*> _tunnels;
};
class Resource {
public:
Resource();
Resource(long long id, unsigned int supply, long long region);
long long get_id() { return _id; }
int get_supply() { return _supply; }
int get_demand() { return _demand; }
int get_n_agents() { return _edges.size(); }
int get_rDS() { return _demand / _supply; }
unsigned int get_region() { return _region; }
void add_edge(Edge* e) { _edges.push_back(e); }
void change_demand(int d_use) { _demand += d_use; }
unsigned int change_supply(int delta);
private:
long long _id;
long long _region;
unsigned int _supply;
unsigned int _demand;
vector<Edge*> _edges;
};
class Edge {
public:
Edge();
Edge(Agent* agent, Resource* resource, float cost);
long long get_agent_id() { return _agent ->get_id(); }
long long get_resource_id() { return _resource->get_id(); }
Agent* agent () { return _agent; }
Resource* resource() { return _resource; }
unsigned int get_use() { return _use; }
void set_use(unsigned int use);
void change_use(int d_use);
bool is_used() { return _use > 0; }
bool is_in_region() { return _in_region; }
float fixed_cost() { return _cost; }
float supply_cost() { return _resource->get_rDS() / RHO; }
float total_cost(float tau);
private:
Agent* _agent;
Resource* _resource;
float _cost;
unsigned int _use;
bool _in_region;
};
class Tunnel {
public:
Tunnel();
Tunnel(Agent* origin, Agent* destination, unsigned int capacity);
long long get_destination_id() { return _destination->get_id(); }
Agent* origin() { return _origin; }
Agent* destination() { return _destination; }
unsigned int get_capacity() { return _capacity; }
unsigned int get_use() { return _use; }
unsigned int get_free_capacity() { return _capacity - _use; }
void set_use(unsigned int use);
void change_use(int d_use);
float get_local_cost_at_destination() { return _destination->get_avg_local_cost(); }
float get_local_fixed_cost_at_destination() { return _destination->get_avg_local_fixed_cost(); }
float get_local_supply_cost_at_destination() { return _destination->get_avg_local_supply_cost(); }
float get_local_frac_in_region_at_destination() { return _destination->get_local_frac_in_region(); }
bool is_in_region() { return _in_region; }
private:
Agent* _origin;
Agent* _destination;
unsigned int _capacity;
unsigned int _use;
bool _in_region;
};
Graph::Graph(float tau, float tol, long long default_region) :
_tau(tau), _tol(tol),
_transform(false), _transform_scale(1), _transform_offset(0), _transform_power(1),
_transform_log(false), _transform_log_base(2) {
new_region(default_region, 0.5);
}
unsigned int Graph::load_regions(string fname, bool load_tau) {
unsigned int nloaded(0);
long long region; float frac_in_region, tau;
cout << "Loading regions from " << fname << " ... ";
if (load_tau) {
io::CSVReader<3> region_csv(fname);
region_csv.read_header(io::ignore_extra_column, "region", "frac_in_region", "tau");
while(region_csv.read_row(region, frac_in_region, tau)){
new_region(region, frac_in_region, tau);
nloaded++;
}
} else {
io::CSVReader<2> region_csv(fname);
region_csv.read_header(io::ignore_extra_column, "region", "frac_in_region");
while(region_csv.read_row(region, frac_in_region)){
new_region(region, frac_in_region);
nloaded++;
}
}
cout << "done." << endl;
return nloaded;
}
unsigned int Graph::load_edges(string fname, int mult, int add) {
unsigned int nloaded(0);
cout << "Loading edges from " << fname << " ... " << std::flush;
io::CSVReader<3> times_csv(fname);
times_csv.read_header(io::ignore_extra_column, "origin", "destination", "cost");
long long origin; long long destination; float cost;
while (times_csv.read_row(origin, destination, cost)) {
new_edge(origin * mult + add, destination, transform_cost(cost));
nloaded++;
if (!(nloaded % 1000000)) {
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
cout << "Loaded " << nloaded / 1000000 << "M edges from " << fname << " ... ";
cout << std::flush;
}
}
cout << "done." << endl;
return nloaded;
}
unsigned int Graph::load_tunnels(string fname) {
unsigned int nloaded(0);
cout << "Loading tunnels from " << fname << " ... ";
io::CSVReader<3> tunnels_csv(fname);
tunnels_csv.read_header(io::ignore_extra_column, "origin", "destination", "capacity");
long long origin; long long destination; float capacity;
while (tunnels_csv.read_row(origin, destination, capacity)) {
new_tunnel(origin, destination, capacity);
if (origin == destination) continue;
nloaded++;
if (!(nloaded % 100000)) {
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
cout << "Loaded " << nloaded / 1000000 << "M tunnels from " << fname << " ... ";
}
}
cout << "done." << endl;
return nloaded;
}
unsigned int Graph::load_agents(string fname, bool load_regions) {
cout << "Loading agents from " << fname << " ... ";
long long geoid, region; int pop;
unsigned int nloaded(0);
if (load_regions) {
io::CSVReader<3> region_csv(fname);
region_csv.read_header(io::ignore_extra_column, "geoid", "pop", "region");
while(region_csv.read_row(geoid, pop, region)){
new_agent(geoid, pop, region);
nloaded++;
}
} else {
io::CSVReader<2> region_csv(fname);
region_csv.read_header(io::ignore_extra_column, "geoid", "pop");
while(region_csv.read_row(geoid, pop)){
new_agent(geoid, pop);
nloaded++;
}
}
cout << "done." << endl;
return nloaded;
}
unsigned int Graph::load_resources(string fname, bool load_regions) {
cout << "Loading resources from " << fname << " ... ";
long long geoid, region; int supply;
unsigned int nloaded(0);
if (load_regions) {
io::CSVReader<3> region_csv(fname);
region_csv.read_header(io::ignore_extra_column, "geoid", "supply", "region");
while(region_csv.read_row(geoid, supply, region)){
new_resource(geoid, supply, region);
nloaded++;
}
} else {
io::CSVReader<2> region_csv(fname);
region_csv.read_header(io::ignore_extra_column, "geoid", "supply");
while(region_csv.read_row(geoid, supply)){
new_resource(geoid, supply);
nloaded++;
}
}
cout << "done." << endl;
return nloaded;
}
void Graph::new_region(long long id, float frac, float tau) {
assert(_addrReg.find(id) == _addrReg.end());
_addrReg[id] = _regions.size();
if (tau <= 0) tau = _tau;
_regions.push_back(new Region(id, frac, tau));
}
void Graph::new_agent(long long id, int demand, long long region) {
assert(_addrA.find(id) == _addrA.end());
if (_addrReg.find(region) == _addrReg.end()) cout << "Region " << region << " not found." << endl;
assert(_addrReg.find(region) != _addrReg.end());
_addrA[id] = _agents.size();
_idenA[_agents.size()] = id;
float tau = _regions[_addrReg.find(region)->second]->get_tau();
_agents.push_back(new Agent(id, demand, _addrReg[region], tau, _tol));
_regions[_addrReg.find(region)->second]->add_agent(_agents.back());
}
void Graph::new_resource(long long id, int supply, long long region) {
assert(_addrR.find(id) == _addrR.end());
if (_addrReg.find(region) == _addrReg.end()) cout << region << endl;
assert(_addrReg.find(region) != _addrReg.end());
_addrR[id] = _resources.size();
_idenR[_resources.size()] = id;
_resources.push_back(new Resource(id, supply, _addrReg[region]));
}
void Graph::new_edge(long long agent_id, long long resource_id, float cost) {
if (_addrA.find(agent_id) == _addrA.end()) cerr << agent_id << endl;
if (_addrA.find(agent_id) == _addrA.end()) cout << agent_id << " " << resource_id << " " << cost << endl;
assert(_addrA.find(agent_id) != _addrA.end());
if (_addrR.find(resource_id) == _addrR.end()) cout << agent_id << " " << resource_id << " " << cost << endl;
assert(_addrR.find(resource_id) != _addrR.end());
if (!_resources[_addrR[resource_id]]->get_supply()) return;
_edges.push_back(new Edge(_agents[_addrA[agent_id]], _resources[_addrR[resource_id]], cost));
_agents [_addrA[agent_id]] ->add_edge(_edges.back());
_resources[_addrR[resource_id]]->add_edge(_edges.back());
}
void Graph::new_tunnel(long long origin, long long destination, float capacity) {
if (!capacity) return;
if (_addrA.find(origin) == _addrA.end()) { cerr << origin << " not yet set for tunnel!" << endl; return; }
if (_addrA.find(destination) == _addrA.end()) { cerr << destination << " not yet set for tunnel!" << endl; return; }
assert(_addrA.find(origin) != _addrA.end());
assert(_addrA.find(destination) != _addrA.end());
_tunnels.push_back(new Tunnel(_agents[_addrA[origin]], _agents[_addrA[destination]], capacity));
_agents[_addrA[origin]]->add_tunnel(_tunnels.back());
}
unsigned int Graph::change_resource_supply(long long resource, int delta) {
if (_addrR.find(resource) == _addrR.end()) {
cout << "Did not find resource " << resource << "; did not alter supply." << endl;
return 0;
}
return _resources[_addrR.find(resource)->second]->change_supply(delta);
}
float Graph::transform_cost(float cost) {
if (!_transform) return cost;
cost = cost * _transform_scale + _transform_offset;
if (_transform_log) return log(cost) / log(_transform_log_base);
return pow(cost, _transform_power);
}
void Graph::set_transform(float scale, float offset, float power, bool log, float log_base) {
if (scale == 1 && offset == 0 && power == 1) return;
if (log && power != 1) {
cout << "Warning: log and power both set. Turning off power." << endl;
power = 1;
}
if (log && offset <= 0) {
cout << "Warning: log set with non-positive offset -- undefined for 0 cost. Setting to offset to 1." << endl;
offset = 1;
}
_transform = true;
_transform_scale = scale;
_transform_offset = offset;
_transform_power = power;
_transform_log = log;
_transform_log_base = log_base;
}
void Graph::allocate_min_cost(unsigned int max_moves, bool verbose) {
unsigned int total_demand(0);
for (auto a : _agents) {
total_demand += a->get_demand();
a->reset_allocation();
}
unsigned int n_alloc(1), total_alloc(0);
while (n_alloc) {
n_alloc = 0;
for (auto a : _agents) {
n_alloc += a->allocate_min_cost(max_moves, verbose || (total_alloc > total_demand));
}
total_alloc += n_alloc;
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
cout << "Allocated " << total_alloc << " / " << total_demand << " (" << std::setprecision(4)
<< 1.0 * total_alloc / total_demand << ") ... " << std::flush;
}
cout << " complete." << endl;
for (auto a : _agents) {
if (a->get_allocation() != a->get_demand()) {
cout << "aid=" << a->get_id() << " a=" << a->get_allocation() << " d=" << a->get_demand() << endl;
}
}
}
void Graph::allocate_min_fixed(bool verbose) {
for (auto a : _agents) {
a->allocate_min_fixed(verbose);
}
}
bool Graph::equalize_use(unsigned int cycles, unsigned int max_moves, float decay,
unsigned int max_tunnel_moves, bool verbose) {
bool converged;
int ni;
for (unsigned int ci = 0; ci < cycles; ci++) {
unsigned int iter_max_moves(max_moves);
unsigned int iter_max_tunnel_moves(max_tunnel_moves);
if (decay > 0) {
iter_max_moves = max_moves * pow(0.5, ci / decay);
if (max_moves && iter_max_moves == 0) iter_max_moves = 1;
iter_max_tunnel_moves = max_moves * pow(0.5, ci / decay);
if (max_tunnel_moves && !iter_max_tunnel_moves) iter_max_tunnel_moves = 1;
}
converged = true;
ni = 0;
for (auto agent : _agents) {
if (!agent->equalize_use(iter_max_moves) ||
(max_tunnel_moves && !agent->equalize_tunnels(iter_max_tunnel_moves))) {
converged = false;
} else ni++;
}
if (verbose) {
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
cout << "cycle " << ci << " :: " << std::setprecision(3) << std::fixed
<< 1.*ni/_agents.size() << " ";
cout << std::flush;
}
if (converged) {
if (verbose) cout << endl;
return true;
}
}
if (verbose) cout << endl;
return false;
}
void Graph::set_tau(float a) {
for (auto r : _regions) r->set_tau(a);
}
void Graph::set_tol(float new_tol) {
_tol = new_tol;
for (auto a : _agents) a->set_tol(_tol);
}
void Graph::tune_tau(float delta, float min, float max) {
for (auto r : _regions) r->tune_tau(delta, min, max);
}
void Graph::write(string filename) {
ofstream ofile;
ofile.open(filename);
for (auto a : _agents) {
ofile << a->get_string();
}
ofile.close();
}
void Graph::write_frac_in_region(string filename) {
ofstream ofile;
ofile.open(filename);
for (auto r : _regions) {
float F = r->get_frac_in_region();
if (!std::isnan(F)) {
ofile << r->get_id() << ","
<< r->get_demand_in_region() << ","
<< r->get_frac() << ","
<< F << endl;
}
}
ofile.close();
}
double Graph::get_cost() {
double total_cost(0);
long long total_demand(0);
for (auto a : _agents) {
unsigned int N = a->get_demand();
if (!N) continue;
total_demand += N;
float cost = a->get_avg_cost();
if (std::isnan(cost)) continue;
if (!(cost < FMAX)) continue;
total_cost += N * a->get_avg_cost();
}
return total_cost / total_demand;
}
float Graph::get_error() {
float error(0);
for (auto r : _regions) {
unsigned int N = r->get_demand_in_region();
if (N) error += N * pow(r->get_frac_in_region() - r->get_frac(), 2);
}
return error;
}
Region::Region(long long id, float frac, float tau) :
_id(id), _frac(frac), _tau(tau) {};
unsigned int Region::get_demand_in_region() {
unsigned int den(0);
for (auto a : _agents) den += a->get_demand();
return den;
}
float Region::get_frac_in_region() {
unsigned int num(0), den(0);
for (auto a : _agents) {
int N = a->get_demand();
num += N * a->get_frac_in_region();
den += N;
}
return 1. * num / den;
}
void Region::set_tau(float new_tau) {
_tau = new_tau;
for (auto a : _agents) a->set_tau(_tau);
}
void Region::tune_tau(float delta, float min, float max) {
float fcalc = get_frac_in_region();
if (fcalc > _frac) {
if (_tau + delta < max) _tau += delta;
else _tau = max;
} else if (fcalc < _frac) {
if (_tau - delta > min) _tau -= delta;
else _tau = min;
}
set_tau(_tau);
}
Agent::Agent(long long id, int demand, long long region, float tau, float tol) :
_id(id), _region(region), _demand(demand), _absent(0), _visitors(0), _allocated(0), _tau(tau), _tol(tol), _disconnected(false) { };
string Agent::get_string() {
float avg_cost = get_avg_cost();
float fixed_cost = get_avg_fixed_cost();
float supply_cost = get_avg_supply_cost();
float cost = supply_cost + fixed_cost;
if (!(cost < FMAX) && fabs(avg_cost - supply_cost - fixed_cost) > 0.02) {
cout << "cost=" << avg_cost << " supply=" << supply_cost << " fixed=" << fixed_cost << endl;
}
assert(!(cost < FMAX) || fabs(avg_cost - supply_cost - fixed_cost) < 0.02);
if (std::isnan(cost)) return "";
stringstream ss;
ss << _id << "," << _demand << ",";
if (cost < FMAX) {
ss << cost << "," << fixed_cost << "," << supply_cost;
} else ss << ",,";
ss << endl;
return ss.str();
}
float Agent::get_avg_supply_cost() {
float total_cost = get_avg_local_supply_cost() * (_demand - _absent);
if (_demand == _absent) total_cost = 0;
unsigned int tunnel_use(0);
for (auto t : _tunnels) {
if (!t->get_use()) continue;
total_cost += t->get_use() * t->get_local_supply_cost_at_destination();
tunnel_use += t->get_use();
}
assert(_absent == tunnel_use);
return total_cost / _demand;
}
float Agent::get_avg_fixed_cost() {
float total_cost = get_avg_local_fixed_cost() * (_demand - _absent);
if (_demand == _absent) total_cost = 0;
unsigned int tunnel_use(0);
for (auto t : _tunnels) {
if (!t->get_use()) continue;
total_cost += t->get_use() * t->get_local_fixed_cost_at_destination();
tunnel_use += t->get_use();
}
assert(_absent == tunnel_use);
return total_cost / _demand;
}
float Agent::get_avg_cost() {
float total_cost = get_avg_local_cost() * (_demand - _absent);
if (_demand == _absent) total_cost = 0;
unsigned int tunnel_use(0);
for (auto t : _tunnels) {
if (!t->get_use()) continue;
total_cost += t->get_use() * t->get_local_cost_at_destination();
tunnel_use += t->get_use();
}
assert(_absent == tunnel_use);
return total_cost / _demand;
}
float Agent::get_avg_local_fixed_cost() {
float cost(0);
if (!_edges.size()) return FINF;
unsigned int local_demand(0);
for (auto e : _edges) {
if (e->is_used()) {
local_demand += e->get_use();
cost += e->get_use() * e->fixed_cost();
}
}
assert(local_demand == _demand + _visitors - _absent);
return cost / _tau / local_demand;
}
float Agent::get_avg_local_supply_cost() {
float cost(0);
if (!_edges.size()) return FINF;
unsigned int local_demand(0);
for (auto e : _edges) {
if (e->is_used()) {
local_demand += e->get_use();
cost += e->get_use() * e->supply_cost();
}
}
assert(local_demand == _demand + _visitors - _absent);
return cost / local_demand;
}
float Agent::get_avg_local_cost() {
float cost(0);
if (!_edges.size()) return FINF;
unsigned int local_demand(0);
for (auto e : _edges) {
if (e->is_used()) {
local_demand += e->get_use();
cost += e->get_use() * e->total_cost(_tau);
}
}
if (local_demand != _demand + _visitors - _absent) cout << local_demand << " == " << _demand << " + " << _visitors << " - " << _absent << " == " << _demand + _visitors - _absent << endl;
assert(local_demand == _demand + _visitors - _absent);
return cost / local_demand;
}
int Agent::get_total_in_region() {
int total_in_region(0);
for (auto e : _edges) {
if (e->is_in_region()) {
total_in_region += e->get_use();
}
}
return total_in_region;
}
float Agent::get_local_frac_in_region() {