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 | #include "aimsun_bdm/aimsun_ds.h" |
---|
12 | #include "traffic_agent.h" |
---|
13 | |
---|
14 | using namespace bdm; |
---|
15 | |
---|
16 | int main ( int argc, char* argv[] ) { |
---|
17 | const char *fname; |
---|
18 | if ( argc>1 ) {fname = argv[1]; } |
---|
19 | else { cout << "Missing configuration file.\n Usage: \n $> estimator config_file.cfg"<<endl; abort(); } |
---|
20 | UIFile Cfg ( fname ); |
---|
21 | |
---|
22 | // SYSTEM TO CONTROL |
---|
23 | AimsunDS Ds; //hard-coded so far |
---|
24 | |
---|
25 | // AGENTS |
---|
26 | Array<shared_ptr<Participant> > Ags; |
---|
27 | UI::get ( Ags,Cfg, "agents" ); |
---|
28 | |
---|
29 | // LOGGER |
---|
30 | shared_ptr<logger> L = UI::build <logger>( Cfg, "logger" ); |
---|
31 | if (!L) {L=new stdlog();} // DEFAULT LOGGER <== poor choice, use better ones |
---|
32 | |
---|
33 | Config MsgStore; |
---|
34 | MsgStore.setAutoConvert(true); |
---|
35 | Setting& Queue = MsgStore.getRoot().add("queue", Setting::TypeList); |
---|
36 | |
---|
37 | // REGISTER ACTIVE OBJECTS IN LOGGER |
---|
38 | Ds.log_register ( *L, "DS" ); |
---|
39 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
40 | Ags ( i )->log_register ( *L,Ags(i)->_name() ); // estimate |
---|
41 | Ags ( i )->ds_register(Ds); // allows agents to update their datalinks |
---|
42 | } |
---|
43 | L->init(); |
---|
44 | |
---|
45 | vec glob_dt(Ds._drv()._dsize() ); |
---|
46 | vec glob_ut(Ds._urv()._dsize() ); |
---|
47 | for ( int tK=0;tK<Ds.max_length();tK++ ) { |
---|
48 | Ds.log_write ( ); // write stuff to |
---|
49 | Ds.getdata(glob_dt); |
---|
50 | |
---|
51 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
52 | Ags(i) -> adapt(glob_dt); |
---|
53 | } |
---|
54 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
55 | Ags(i) -> broadcast(Queue); |
---|
56 | } |
---|
57 | // parse message queue |
---|
58 | for ( int m=Queue.getLength()-1; m>=0; m-- ) { // go backwards - last mesages are discarded |
---|
59 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
60 | Setting& msg=Queue[m]; |
---|
61 | string m_to=msg["to"]; |
---|
62 | if (m_to==Ags(i)->_name()) { |
---|
63 | Ags(i)->receive(msg); |
---|
64 | Queue.remove(m); |
---|
65 | break; |
---|
66 | // message delivered; |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | if (Queue.getLength()>0){bdm_error("undelivered messages - probably unknown neighbours");} |
---|
71 | |
---|
72 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
73 | Ags(i) -> act(glob_ut); |
---|
74 | } |
---|
75 | |
---|
76 | L->step(); |
---|
77 | Ds.write(glob_ut); |
---|
78 | Ds.step(); // simulator step |
---|
79 | |
---|
80 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
81 | Ags(i) -> step(); |
---|
82 | } |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | L->finalize(); |
---|
87 | |
---|
88 | return 0; |
---|
89 | } |
---|