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 | #include "traffic_agent_offset.h" |
---|
18 | |
---|
19 | using namespace bdm; |
---|
20 | |
---|
21 | int main ( int argc, char* argv[] ) { |
---|
22 | const char *fname; |
---|
23 | int max_cycles=5; //max. number of sending/recieving during negotiation, changed to number from config file, if present (negotiation_cycles) |
---|
24 | |
---|
25 | if ( argc>1 ) {fname = argv[1]; } |
---|
26 | else { cout << "Missing configuration file.\n Usage: \n $> estimator config_file.cfg"<<endl; abort(); } |
---|
27 | UIFile Cfg ( fname ); |
---|
28 | |
---|
29 | // SYSTEM TO CONTROL |
---|
30 | shared_ptr<AimsunDS> Ds=UI::build<AimsunDS>(Cfg.lookup("system"),UI::compulsory); |
---|
31 | |
---|
32 | // AGENTS |
---|
33 | Array<shared_ptr<Participant> > Ags; |
---|
34 | UI::get ( Ags,Cfg, "agents" ); |
---|
35 | |
---|
36 | // LOGGER |
---|
37 | shared_ptr<logger> L = UI::build <logger>( Cfg, "logger" ); |
---|
38 | if (!L) { |
---|
39 | L=new stdlog(); // DEFAULT LOGGER <== poor choice, use better ones |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | Config MsgStore; |
---|
45 | MsgStore.setAutoConvert(true); |
---|
46 | Setting& Queue = MsgStore.getRoot().add("queue", Setting::TypeList); |
---|
47 | |
---|
48 | // REGISTER ACTIVE OBJECTS IN LOGGER |
---|
49 | Ds->log_register ( *L, "DS" ); |
---|
50 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
51 | Ags ( i )->log_register ( *L,Ags(i)->_name() ); // estimate |
---|
52 | Ags ( i )->ds_register(*Ds); // allows agents to update their datalinks |
---|
53 | } |
---|
54 | L->init(); |
---|
55 | |
---|
56 | vec glob_dt(Ds->_drv()._dsize() ); |
---|
57 | vec glob_ut(Ds->_urv()._dsize() ); |
---|
58 | |
---|
59 | // INITIALISATION OF UT |
---|
60 | glob_ut[ 0] = 80; // cycle time |
---|
61 | glob_ut[ 1] = 60; // offset 495 |
---|
62 | glob_ut[ 2] = 30; // ut[2]+ut[3]+ut[4] has to sum up to ut[0] |
---|
63 | glob_ut[ 3] = 30; |
---|
64 | glob_ut[ 4] = 20; |
---|
65 | glob_ut[ 8] = 40; // offset 601 |
---|
66 | glob_ut[ 9] = 30; // ut[9]+ut[10]+ut[11] has to sum up to ut[0] |
---|
67 | glob_ut[10] = 30; |
---|
68 | glob_ut[11] = 20; |
---|
69 | |
---|
70 | //LENGTH OF NEGOTIATION |
---|
71 | UI::get(max_cycles,Cfg,"negotiation_cycles"); |
---|
72 | |
---|
73 | for ( int tK=0; tK < Ds->max_length(); tK++ ) { |
---|
74 | |
---|
75 | Ds->log_write ( ); // write stuff to |
---|
76 | Ds->getdata(glob_dt); |
---|
77 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
78 | Ags(i) -> adapt(glob_dt); |
---|
79 | } |
---|
80 | |
---|
81 | // NEGOTIATION CYCLE |
---|
82 | // ends when Queue is empty or after defined number of cycles |
---|
83 | int cycle=0; |
---|
84 | do { |
---|
85 | //DBG |
---|
86 | MsgStore.writeFile("msg"); |
---|
87 | // parse message queue |
---|
88 | for ( int m=Queue.getLength()-1; m>=0; m-- ) { // go backwards - last mesages are discarded |
---|
89 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
90 | Setting& msg=Queue[m]; |
---|
91 | string m_to=msg["to"]; |
---|
92 | if (m_to==Ags(i)->_name()) { |
---|
93 | Ags(i)->receive(msg); |
---|
94 | Queue.remove(m); |
---|
95 | break; |
---|
96 | // message delivered; |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | if (Queue.getLength()>0){bdm_error("undelivered messages - probably unknown neighbours");} |
---|
101 | |
---|
102 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
103 | Ags(i) -> broadcast(Queue); |
---|
104 | } |
---|
105 | |
---|
106 | cycle++; |
---|
107 | } while ((Queue.getLength()>0) && (cycle<max_cycles)); |
---|
108 | |
---|
109 | if (cycle==max_cycles) { |
---|
110 | bdm_warning("Reached maximum number of cycles"); |
---|
111 | } |
---|
112 | |
---|
113 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
114 | Ags(i) -> act(glob_ut); |
---|
115 | } |
---|
116 | |
---|
117 | L->step(); |
---|
118 | Ds->write(glob_ut); |
---|
119 | Ds->step(); // simulator step |
---|
120 | |
---|
121 | for ( int i=0; i<Ags.length(); i++ ) { |
---|
122 | Ags(i) -> log_write(); |
---|
123 | Ags(i) -> step(); |
---|
124 | } |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | L->finalize(); |
---|
129 | |
---|
130 | return 0; |
---|
131 | } |
---|