1 | #include "traffic_agent.cpp" |
---|
2 | const string testMessage = "test_message"; |
---|
3 | |
---|
4 | class TrafficAgentCycleTime : public BaseTrafficAgent { |
---|
5 | private: |
---|
6 | static double measurement_cycle_time; // s |
---|
7 | static double saturated_stream; // car/s |
---|
8 | static double Tc; |
---|
9 | int cycle_counter; |
---|
10 | Array <int> lane_sum; |
---|
11 | |
---|
12 | void sum_lanes() { |
---|
13 | for ( int i = 0; i < lane_sum.length(); i ++ ) { |
---|
14 | if ( inputs(2*i) > 0 ) { |
---|
15 | lane_sum(i) = lane_sum(i) + inputs(2*i); |
---|
16 | } |
---|
17 | } |
---|
18 | } |
---|
19 | |
---|
20 | void echo ( string message ) { |
---|
21 | cout << name << " hlasi: " << message << endl; |
---|
22 | } |
---|
23 | |
---|
24 | void send2neighbour( Setting &set, int i, string messageName, double messageValue ) { |
---|
25 | if ( i < neighbours.length() ) { |
---|
26 | Setting &msg =set.add(Setting::TypeGroup); |
---|
27 | UI::save( neighbours(i), msg, "to" ); |
---|
28 | UI::save (name,msg,"from"); |
---|
29 | UI::save( messageName, msg, "what" ); |
---|
30 | UI::save( messageValue, msg, "value" ); |
---|
31 | } |
---|
32 | else { |
---|
33 | //throw new Exception("soused "+((string)i)+" neexistuje"); |
---|
34 | //cout << endl << endl << "soused " << i << mimo |
---|
35 | std::stringstream out; |
---|
36 | out << "soused " << i << " neexistuje"; |
---|
37 | //throw out.str(); |
---|
38 | cout << out.str(); |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | void printVector ( RV rv_vector, vec vector, string description ) { |
---|
43 | cout << endl << description << " " << name << endl; |
---|
44 | int k = 0; |
---|
45 | for ( int i = 0; i < rv_vector.length(); i ++ ) { |
---|
46 | cout << rv_vector.name(i) << " : "; |
---|
47 | for ( int j = 0; j < rv_vector.size(i); j ++ ) { |
---|
48 | cout << vector(k) << " "; |
---|
49 | k ++; |
---|
50 | } |
---|
51 | cout << endl; |
---|
52 | } |
---|
53 | cout << endl; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | |
---|
58 | public: |
---|
59 | void from_setting( Setting &set ) { |
---|
60 | BaseTrafficAgent::from_setting(set); |
---|
61 | TrafficAgentCycleTime::measurement_cycle_time = 90; //s |
---|
62 | TrafficAgentCycleTime::saturated_stream = 0.5; |
---|
63 | TrafficAgentCycleTime::Tc = 80; |
---|
64 | } |
---|
65 | |
---|
66 | void broadcast(Setting &set){ |
---|
67 | //BaseTrafficAgent::broadcast(set); |
---|
68 | try { |
---|
69 | for ( int i = 0; i < neighbours.length(); i ++ ) { |
---|
70 | send2neighbour( set, i, testMessage, 2.3 ); |
---|
71 | } |
---|
72 | } catch ( string s ) { |
---|
73 | cout << s << endl; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | void receive(const Setting& msg){ |
---|
78 | string what; |
---|
79 | string from; |
---|
80 | string to; |
---|
81 | double val; |
---|
82 | try { |
---|
83 | UI::get(what, msg, "what", UI::compulsory); |
---|
84 | UI::get(from, msg, "from", UI::compulsory); |
---|
85 | UI::get(to, msg, "to", UI::compulsory); |
---|
86 | UI::get(val, msg, "value"); |
---|
87 | if ( what == testMessage ) { |
---|
88 | //cout << endl << name << ": OD: " << from << " CO: " << what << " KOLIK: " << val <<endl; |
---|
89 | } |
---|
90 | if ( what == "new_stable_state" ) { |
---|
91 | //echo("new_stable_state"); |
---|
92 | BaseTrafficAgent::receive(msg); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | } catch ( UISettingException e ) { |
---|
97 | echo(e.what()); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | void validate (){ |
---|
102 | BaseTrafficAgent::validate(); |
---|
103 | lane_sum.set_size( rv_inputs.length() ); |
---|
104 | for ( int i = 0; i < lane_sum.length(); i ++ ) { |
---|
105 | lane_sum(i) = 0; |
---|
106 | cout << endl << endl << "lane_sum initialization " << name << " " << lane_sum(i) <<endl << endl ; |
---|
107 | } |
---|
108 | echo("validate"); |
---|
109 | } |
---|
110 | |
---|
111 | void adapt (const vec &glob_dt) { |
---|
112 | sum_lanes(); |
---|
113 | //printVector( rv_queues, queues, "queues" ); |
---|
114 | printVector( rv_inputs, inputs, "inputs" ); |
---|
115 | for ( int i = 0; i < lane_sum.length(); i ++ ) { |
---|
116 | cout << lane_sum(i) << endl; |
---|
117 | } |
---|
118 | cout << endl << endl; |
---|
119 | //printVector( green_names, green_times, "inputs" ); |
---|
120 | //cout << endl << name << "green times: " << endl; |
---|
121 | for ( int i = 0; i < green_times.length(); i ++ ) { |
---|
122 | //cout << green_names(i) << " " << green_starts(i) << " " << green_times(i) << endl; |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | /* |
---|
127 | for ( int i = 0; i < outputs.length(); i ++ ) { |
---|
128 | cout << name << " outputs " << outputs(i) << endl; |
---|
129 | } |
---|
130 | |
---|
131 | for ( int i = 0; i < rv_outputs.length(); i ++ ) { |
---|
132 | cout << name << " rv_outputs " << rv_outputs.name(i) << endl; |
---|
133 | } |
---|
134 | |
---|
135 | */ |
---|
136 | |
---|
137 | BaseTrafficAgent::adapt(glob_dt); |
---|
138 | //echo("adapted"); |
---|
139 | } |
---|
140 | |
---|
141 | void act (vec &glob_ut){ |
---|
142 | |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | }; |
---|
147 | UIREGISTER(TrafficAgentCycleTime); |
---|