1 | |
---|
2 | /*! |
---|
3 | \file |
---|
4 | \brief Distributed Traffic light Control Scenario |
---|
5 | \author Vasek Smidl |
---|
6 | |
---|
7 | */ |
---|
8 | |
---|
9 | #include "base/user_info.h" |
---|
10 | #include "base/loggers.h" |
---|
11 | #ifdef _WIN32 |
---|
12 | #include "aimsun_bdm/aimsun_ds.h" |
---|
13 | #else |
---|
14 | #include "aimsun_bdm/aimsun_fake.h" |
---|
15 | #endif |
---|
16 | #include "traffic_agent.h" |
---|
17 | |
---|
18 | using namespace bdm; |
---|
19 | |
---|
20 | int main ( int argc, char* argv[] ) { |
---|
21 | const char *fname; |
---|
22 | if ( argc>1 ) {fname = argv[1]; } |
---|
23 | else { cout << "Missing configuration file.\n Usage: \n $> estimator config_file.cfg"<<endl; abort(); } |
---|
24 | UIFile Cfg ( fname ); |
---|
25 | |
---|
26 | // SYSTEM TO CONTROL |
---|
27 | AimsunDS Ds ( Cfg ); // Mostly hard-coded so far |
---|
28 | |
---|
29 | // AGENTS |
---|
30 | Array<shared_ptr<Participant> > Ags; |
---|
31 | UI::get ( Ags,Cfg, "agents" ); |
---|
32 | |
---|
33 | // LOGGER |
---|
34 | shared_ptr<logger> L = UI::build <logger>( Cfg, "logger" ); |
---|
35 | if (!L) {L=new stdlog();} // DEFAULT LOGGER <== poor choice, use better ones |
---|
36 | |
---|
37 | Config MsgStore; |
---|
38 | MsgStore.setAutoConvert(true); |
---|
39 | Setting& Queue = MsgStore.getRoot().add("queue", Setting::TypeList); |
---|
40 | |
---|
41 | // REGISTER ACTIVE OBJECTS IN LOGGER |
---|
42 | Ds.log_register ( *L, "DS" ); |
---|
43 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
44 | Ags ( i )->log_register ( *L,Ags(i)->_name() ); // estimate |
---|
45 | Ags ( i )->ds_register(Ds); // allows agents to update their datalinks |
---|
46 | } |
---|
47 | L->init(); |
---|
48 | |
---|
49 | vec glob_dt(Ds._drv()._dsize() ); |
---|
50 | vec glob_ut(Ds._urv()._dsize() ); |
---|
51 | |
---|
52 | // INITIALISATION OF UT |
---|
53 | glob_ut[ 0] = 80; // cycle time |
---|
54 | glob_ut[ 1] = 60; // offset 495 |
---|
55 | glob_ut[ 2] = 30; // ut[2]+ut[3]+ut[4] has to sum up to ut[0] |
---|
56 | glob_ut[ 3] = 30; |
---|
57 | glob_ut[ 4] = 20; |
---|
58 | glob_ut[ 8] = 40; // offset 601 |
---|
59 | glob_ut[ 9] = 30; // ut[9]+ut[10]+ut[11] has to sum up to ut[0] |
---|
60 | glob_ut[10] = 30; |
---|
61 | glob_ut[11] = 20; |
---|
62 | |
---|
63 | for ( int tK=0; tK < Ds.max_length(); tK++ ) { |
---|
64 | Ds.log_write ( ); // write stuff to |
---|
65 | Ds.getdata(glob_dt); |
---|
66 | |
---|
67 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
68 | Ags(i) -> adapt(glob_dt); |
---|
69 | } |
---|
70 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
71 | Ags(i) -> broadcast(Queue); |
---|
72 | } |
---|
73 | //DBG |
---|
74 | MsgStore.writeFile("xxx"); |
---|
75 | // parse message queue |
---|
76 | for ( int m=Queue.getLength()-1; m>=0; m-- ) { // go backwards - last mesages are discarded |
---|
77 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
78 | Setting& msg=Queue[m]; |
---|
79 | string m_to=msg["to"]; |
---|
80 | if (m_to==Ags(i)->_name()) { |
---|
81 | Ags(i)->receive(msg); |
---|
82 | Queue.remove(m); |
---|
83 | break; |
---|
84 | // message delivered; |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | if (Queue.getLength()>0){bdm_error("undelivered messages - probably unknown neighbours");} |
---|
89 | |
---|
90 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
91 | Ags(i) -> act(glob_ut); |
---|
92 | } |
---|
93 | |
---|
94 | L->step(); |
---|
95 | Ds.write(glob_ut); |
---|
96 | Ds.step(); // simulator step |
---|
97 | |
---|
98 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
99 | Ags(i) -> log_write(); |
---|
100 | Ags(i) -> step(); |
---|
101 | } |
---|
102 | |
---|
103 | } |
---|
104 | |
---|
105 | L->finalize(); |
---|
106 | |
---|
107 | return 0; |
---|
108 | } |
---|