diff options
author | Fabian Oehlmann <oehlmann@in.tum.de> | 2013-09-17 13:04:44 +0000 |
---|---|---|
committer | Fabian Oehlmann <oehlmann@in.tum.de> | 2013-09-17 13:04:44 +0000 |
commit | 7a02c0cc81e84a455d3281e9635d636c78c41351 (patch) | |
tree | cc3723771f52b8d47a9058ffa30a94caa073f65d | |
parent | e039055bd12606f580ba257df97187cfe82a9f67 (diff) | |
download | gnunet-7a02c0cc81e84a455d3281e9635d636c78c41351.tar.gz gnunet-7a02c0cc81e84a455d3281e9635d636c78c41351.zip |
ats_ril: indent corrected
-rwxr-xr-x | src/ats/gnunet-service-ats-solver_ril.c | 1661 | ||||
-rwxr-xr-x | src/ats/gnunet-service-ats-solver_ril.h | 113 |
2 files changed, 887 insertions, 887 deletions
diff --git a/src/ats/gnunet-service-ats-solver_ril.c b/src/ats/gnunet-service-ats-solver_ril.c index 7659251ff..a47f1ff9a 100755 --- a/src/ats/gnunet-service-ats-solver_ril.c +++ b/src/ats/gnunet-service-ats-solver_ril.c | |||
@@ -1,22 +1,22 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2011 Christian Grothoff (and other contributing authors) | 3 | (C) 2011 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published | 6 | it under the terms of the GNU General Public License as published |
7 | by the Free Software Foundation; either version 3, or (at your | 7 | by the Free Software Foundation; either version 3, or (at your |
8 | option) any later version. | 8 | option) any later version. |
9 | 9 | ||
10 | GNUnet is distributed in the hope that it will be useful, but | 10 | GNUnet is distributed in the hope that it will be useful, but |
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | General Public License for more details. | 13 | General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU General Public License | 15 | You should have received a copy of the GNU General Public License |
16 | along with GNUnet; see the file COPYING. If not, write to the | 16 | along with GNUnet; see the file COPYING. If not, write to the |
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file ats/gnunet-service-ats-solver_ril.c | 22 | * @file ats/gnunet-service-ats-solver_ril.c |
@@ -31,6 +31,7 @@ | |||
31 | #include "gnunet_statistics_service.h" | 31 | #include "gnunet_statistics_service.h" |
32 | 32 | ||
33 | #define RIL_DEFAULT_STEP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000) | 33 | #define RIL_DEFAULT_STEP_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 3000) |
34 | #define RIL_DEFAULT_ALGORITHM RIL_ALGO_Q | ||
34 | #define RIL_DEFAULT_DISCOUNT_FACTOR 0.5 | 35 | #define RIL_DEFAULT_DISCOUNT_FACTOR 0.5 |
35 | #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4 | 36 | #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.4 |
36 | #define RIL_DEFAULT_TRACE_DECAY 0.6 | 37 | #define RIL_DEFAULT_TRACE_DECAY 0.6 |
@@ -44,26 +45,22 @@ | |||
44 | 45 | ||
45 | enum RIL_Action_Type | 46 | enum RIL_Action_Type |
46 | { | 47 | { |
47 | RIL_ACTION_BW_IN_DBL = 0, | 48 | RIL_ACTION_BW_IN_DBL = 0, |
48 | RIL_ACTION_BW_OUT_DBL = 1, | 49 | RIL_ACTION_BW_OUT_DBL = 1, |
49 | RIL_ACTION_BW_IN_HLV = 2, | 50 | RIL_ACTION_BW_IN_HLV = 2, |
50 | RIL_ACTION_BW_OUT_HLV = 3, | 51 | RIL_ACTION_BW_OUT_HLV = 3, |
51 | RIL_ACTION_TYPE_NUM = 4 | 52 | RIL_ACTION_TYPE_NUM = 4 |
52 | }; | 53 | }; |
53 | //TODO! add the rest of the actions | 54 | //TODO! add the rest of the actions |
54 | 55 | ||
55 | enum RIL_Algorithm | 56 | enum RIL_Algorithm |
56 | { | 57 | { |
57 | RIL_ALGO_SARSA, | 58 | RIL_ALGO_SARSA, RIL_ALGO_Q |
58 | RIL_ALGO_Q | ||
59 | }; | 59 | }; |
60 | 60 | ||
61 | enum RIL_E_Modification | 61 | enum RIL_E_Modification |
62 | { | 62 | { |
63 | RIL_E_SET, | 63 | RIL_E_SET, RIL_E_ZERO, RIL_E_ACCUMULATE, RIL_E_REPLACE |
64 | RIL_E_ZERO, | ||
65 | RIL_E_ACCUMULATE, | ||
66 | RIL_E_REPLACE | ||
67 | }; | 64 | }; |
68 | 65 | ||
69 | /** | 66 | /** |
@@ -71,164 +68,164 @@ enum RIL_E_Modification | |||
71 | */ | 68 | */ |
72 | struct RIL_Learning_Parameters | 69 | struct RIL_Learning_Parameters |
73 | { | 70 | { |
74 | /** | 71 | /** |
75 | * The TD-algorithm to use | 72 | * The TD-algorithm to use |
76 | */ | 73 | */ |
77 | enum RIL_Algorithm algorithm; | 74 | enum RIL_Algorithm algorithm; |
78 | 75 | ||
79 | /** | 76 | /** |
80 | * Learning discount factor in the TD-update | 77 | * Learning discount factor in the TD-update |
81 | */ | 78 | */ |
82 | float gamma; | 79 | float gamma; |
83 | 80 | ||
84 | /** | 81 | /** |
85 | * Gradient-descent step-size | 82 | * Gradient-descent step-size |
86 | */ | 83 | */ |
87 | float alpha; | 84 | float alpha; |
88 | 85 | ||
89 | /** | 86 | /** |
90 | * Trace-decay factor for eligibility traces | 87 | * Trace-decay factor for eligibility traces |
91 | */ | 88 | */ |
92 | float lambda; | 89 | float lambda; |
93 | }; | 90 | }; |
94 | 91 | ||
95 | struct RIL_Peer_Agent | 92 | struct RIL_Peer_Agent |
96 | { | 93 | { |
97 | /** | 94 | /** |
98 | * Next agent in solver's linked list | 95 | * Next agent in solver's linked list |
99 | */ | 96 | */ |
100 | struct RIL_Peer_Agent *next; | 97 | struct RIL_Peer_Agent *next; |
101 | 98 | ||
102 | /** | 99 | /** |
103 | * Previous agent in solver's linked list | 100 | * Previous agent in solver's linked list |
104 | */ | 101 | */ |
105 | struct RIL_Peer_Agent *prev; | 102 | struct RIL_Peer_Agent *prev; |
106 | 103 | ||
107 | /** | 104 | /** |
108 | * Environment handle | 105 | * Environment handle |
109 | */ | 106 | */ |
110 | struct GAS_RIL_Handle *envi; | 107 | struct GAS_RIL_Handle *envi; |
111 | 108 | ||
112 | /** | 109 | /** |
113 | * Peer ID | 110 | * Peer ID |
114 | */ | 111 | */ |
115 | struct GNUNET_PeerIdentity peer; | 112 | struct GNUNET_PeerIdentity peer; |
116 | 113 | ||
117 | /** | 114 | /** |
118 | * Whether the agent is active or not | 115 | * Whether the agent is active or not |
119 | */ | 116 | */ |
120 | int active; | 117 | int active; |
121 | 118 | ||
122 | /** | 119 | /** |
123 | * Number of performed time-steps | 120 | * Number of performed time-steps |
124 | */ | 121 | */ |
125 | unsigned long long step_count; | 122 | unsigned long long step_count; |
126 | 123 | ||
127 | /** | 124 | /** |
128 | * Experience matrix W | 125 | * Experience matrix W |
129 | */ | 126 | */ |
130 | double ** W; | 127 | double ** W; |
131 | 128 | ||
132 | /** | 129 | /** |
133 | * Number of rows of W / Number of state-vector features | 130 | * Number of rows of W / Number of state-vector features |
134 | */ | 131 | */ |
135 | int m; | 132 | int m; |
136 | 133 | ||
137 | /** | 134 | /** |
138 | * Number of columns of W / Number of actions | 135 | * Number of columns of W / Number of actions |
139 | */ | 136 | */ |
140 | int n; | 137 | int n; |
141 | 138 | ||
142 | /** | 139 | /** |
143 | * Last perceived state feature vector | 140 | * Last perceived state feature vector |
144 | */ | 141 | */ |
145 | double * s_old; | 142 | double * s_old; |
146 | 143 | ||
147 | /** | 144 | /** |
148 | * Last chosen action | 145 | * Last chosen action |
149 | */ | 146 | */ |
150 | int a_old; | 147 | int a_old; |
151 | 148 | ||
152 | /** | 149 | /** |
153 | * Eligibility trace vector | 150 | * Eligibility trace vector |
154 | */ | 151 | */ |
155 | double * e; | 152 | double * e; |
156 | 153 | ||
157 | /** | 154 | /** |
158 | * Address in use | 155 | * Address in use |
159 | */ | 156 | */ |
160 | struct ATS_Address * address; | 157 | struct ATS_Address * address; |
161 | 158 | ||
162 | /** | 159 | /** |
163 | * Inbound bandwidth assigned by the agent | 160 | * Inbound bandwidth assigned by the agent |
164 | */ | 161 | */ |
165 | unsigned long long bw_in; | 162 | unsigned long long bw_in; |
166 | 163 | ||
167 | /** | 164 | /** |
168 | * Outbound bandwidth assigned by the agent | 165 | * Outbound bandwidth assigned by the agent |
169 | */ | 166 | */ |
170 | unsigned long long bw_out; | 167 | unsigned long long bw_out; |
171 | }; | 168 | }; |
172 | 169 | ||
173 | struct RIL_Network | 170 | struct RIL_Network |
174 | { | 171 | { |
175 | /** | 172 | /** |
176 | * ATS network type | 173 | * ATS network type |
177 | */ | 174 | */ |
178 | enum GNUNET_ATS_Network_Type type; | 175 | enum GNUNET_ATS_Network_Type type; |
179 | 176 | ||
180 | /** | 177 | /** |
181 | * Total available inbound bandwidth | 178 | * Total available inbound bandwidth |
182 | */ | 179 | */ |
183 | unsigned long long bw_in_available; | 180 | unsigned long long bw_in_available; |
184 | 181 | ||
185 | /** | 182 | /** |
186 | * Total assigned outbound bandwidth | 183 | * Total assigned outbound bandwidth |
187 | */ | 184 | */ |
188 | unsigned long long bw_in_assigned; | 185 | unsigned long long bw_in_assigned; |
189 | 186 | ||
190 | /** | 187 | /** |
191 | * Total available outbound bandwidth | 188 | * Total available outbound bandwidth |
192 | */ | 189 | */ |
193 | unsigned long long bw_out_available; | 190 | unsigned long long bw_out_available; |
194 | 191 | ||
195 | /** | 192 | /** |
196 | * Total assigned outbound bandwidth | 193 | * Total assigned outbound bandwidth |
197 | */ | 194 | */ |
198 | unsigned long long bw_out_assigned; | 195 | unsigned long long bw_out_assigned; |
199 | }; | 196 | }; |
200 | 197 | ||
201 | struct RIL_Callbacks | 198 | struct RIL_Callbacks |
202 | { | 199 | { |
203 | /** | 200 | /** |
204 | * Bandwidth changed callback | 201 | * Bandwidth changed callback |
205 | */ | 202 | */ |
206 | GAS_bandwidth_changed_cb bw_changed; | 203 | GAS_bandwidth_changed_cb bw_changed; |
207 | 204 | ||
208 | /** | 205 | /** |
209 | * Bandwidth changed callback cls | 206 | * Bandwidth changed callback cls |
210 | */ | 207 | */ |
211 | void *bw_changed_cls; | 208 | void *bw_changed_cls; |
212 | 209 | ||
213 | /** | 210 | /** |
214 | * ATS function to get preferences | 211 | * ATS function to get preferences |
215 | */ | 212 | */ |
216 | GAS_get_preferences get_preferences; | 213 | GAS_get_preferences get_preferences; |
217 | 214 | ||
218 | /** | 215 | /** |
219 | * Closure for ATS function to get preferences | 216 | * Closure for ATS function to get preferences |
220 | */ | 217 | */ |
221 | void *get_preferences_cls; | 218 | void *get_preferences_cls; |
222 | 219 | ||
223 | /** | 220 | /** |
224 | * ATS function to get properties | 221 | * ATS function to get properties |
225 | */ | 222 | */ |
226 | GAS_get_properties get_properties; | 223 | GAS_get_properties get_properties; |
227 | 224 | ||
228 | /** | 225 | /** |
229 | * Closure for ATS function to get properties | 226 | * Closure for ATS function to get properties |
230 | */ | 227 | */ |
231 | void *get_properties_cls; | 228 | void *get_properties_cls; |
232 | }; | 229 | }; |
233 | 230 | ||
234 | /** | 231 | /** |
@@ -236,66 +233,66 @@ struct RIL_Callbacks | |||
236 | */ | 233 | */ |
237 | struct GAS_RIL_Handle | 234 | struct GAS_RIL_Handle |
238 | { | 235 | { |
239 | /** | 236 | /** |
240 | * Statistics handle | 237 | * Statistics handle |
241 | */ | 238 | */ |
242 | struct GNUNET_STATISTICS_Handle *stats; | 239 | struct GNUNET_STATISTICS_Handle *stats; |
243 | 240 | ||
244 | /** | 241 | /** |
245 | * Hashmap containing all valid addresses | 242 | * Hashmap containing all valid addresses |
246 | */ | 243 | */ |
247 | const struct GNUNET_CONTAINER_MultiHashMap *addresses; | 244 | const struct GNUNET_CONTAINER_MultiHashMap *addresses; |
248 | 245 | ||
249 | /** | 246 | /** |
250 | * Callbacks for the solver | 247 | * Callbacks for the solver |
251 | */ | 248 | */ |
252 | struct RIL_Callbacks *callbacks; | 249 | struct RIL_Callbacks *callbacks; |
253 | 250 | ||
254 | /** | 251 | /** |
255 | * Bulk lock | 252 | * Bulk lock |
256 | */ | 253 | */ |
257 | int bulk_lock; | 254 | int bulk_lock; |
258 | 255 | ||
259 | /** | 256 | /** |
260 | * Number of changes while solver was locked | 257 | * Number of changes while solver was locked |
261 | */ | 258 | */ |
262 | int bulk_requests; | 259 | int bulk_requests; |
263 | 260 | ||
264 | /** | 261 | /** |
265 | * Number of performed time-steps | 262 | * Number of performed time-steps |
266 | */ | 263 | */ |
267 | unsigned long long step_count; | 264 | unsigned long long step_count; |
268 | 265 | ||
269 | /** | 266 | /** |
270 | * Interval time between steps in milliseconds //TODO? put in agent | 267 | * Interval time between steps in milliseconds //TODO? put in agent |
271 | */ | 268 | */ |
272 | struct GNUNET_TIME_Relative step_time; | 269 | struct GNUNET_TIME_Relative step_time; |
273 | 270 | ||
274 | /** | 271 | /** |
275 | * Task identifier of the next time-step to be executed //TODO? put in agent | 272 | * Task identifier of the next time-step to be executed //TODO? put in agent |
276 | */ | 273 | */ |
277 | GNUNET_SCHEDULER_TaskIdentifier next_step; | 274 | GNUNET_SCHEDULER_TaskIdentifier next_step; |
278 | 275 | ||
279 | /** | 276 | /** |
280 | * Learning parameters | 277 | * Learning parameters |
281 | */ | 278 | */ |
282 | struct RIL_Learning_Parameters parameters; | 279 | struct RIL_Learning_Parameters parameters; |
283 | 280 | ||
284 | /** | 281 | /** |
285 | * Array of networks with global assignment state | 282 | * Array of networks with global assignment state |
286 | */ | 283 | */ |
287 | struct RIL_Network * network_entries; | 284 | struct RIL_Network * network_entries; |
288 | 285 | ||
289 | /** | 286 | /** |
290 | * Networks count | 287 | * Networks count |
291 | */ | 288 | */ |
292 | unsigned int networks_count; | 289 | unsigned int networks_count; |
293 | 290 | ||
294 | /** | 291 | /** |
295 | * List of active peer-agents | 292 | * List of active peer-agents |
296 | */ | 293 | */ |
297 | struct RIL_Peer_Agent * agents_head; | 294 | struct RIL_Peer_Agent * agents_head; |
298 | struct RIL_Peer_Agent * agents_tail; | 295 | struct RIL_Peer_Agent * agents_tail; |
299 | }; | 296 | }; |
300 | 297 | ||
301 | /** | 298 | /** |
@@ -311,19 +308,17 @@ struct GAS_RIL_Handle | |||
311 | * @return estimation value | 308 | * @return estimation value |
312 | */ | 309 | */ |
313 | static double | 310 | static double |
314 | agent_estimate_q (struct RIL_Peer_Agent *agent, | 311 | agent_estimate_q (struct RIL_Peer_Agent *agent, double *state, int action) |
315 | double *state, | ||
316 | int action) | ||
317 | { | 312 | { |
318 | int i; | 313 | int i; |
319 | double result = 0; | 314 | double result = 0; |
320 | 315 | ||
321 | for (i = 0; i < agent->m; i++) | 316 | for (i = 0; i < agent->m; i++) |
322 | { | 317 | { |
323 | result += state[i] * (agent->W)[agent->m][action]; | 318 | result += state[i] * (agent->W)[agent->m][action]; |
324 | } | 319 | } |
325 | 320 | ||
326 | return result; | 321 | return result; |
327 | } | 322 | } |
328 | 323 | ||
329 | /** | 324 | /** |
@@ -335,13 +330,14 @@ agent_estimate_q (struct RIL_Peer_Agent *agent, | |||
335 | static int | 330 | static int |
336 | agent_decide_exploration (struct RIL_Peer_Agent *agent) | 331 | agent_decide_exploration (struct RIL_Peer_Agent *agent) |
337 | { | 332 | { |
338 | double r = (double) GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX) / (double) UINT32_MAX; | 333 | double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, |
334 | UINT32_MAX) / (double) UINT32_MAX; | ||
339 | 335 | ||
340 | if (r < RIL_EXPLORE_RATIO) | 336 | if (r < RIL_EXPLORE_RATIO) |
341 | { | 337 | { |
342 | return GNUNET_YES; | 338 | return GNUNET_YES; |
343 | } | 339 | } |
344 | return GNUNET_NO; | 340 | return GNUNET_NO; |
345 | } | 341 | } |
346 | 342 | ||
347 | /** | 343 | /** |
@@ -352,27 +348,26 @@ agent_decide_exploration (struct RIL_Peer_Agent *agent) | |||
352 | * @return the action promising most future reward | 348 | * @return the action promising most future reward |
353 | */ | 349 | */ |
354 | static int | 350 | static int |
355 | agent_get_action_best (struct RIL_Peer_Agent *agent, | 351 | agent_get_action_best (struct RIL_Peer_Agent *agent, double *state) |
356 | double *state) | ||
357 | { | 352 | { |
358 | int i; | 353 | int i; |
359 | int max_i = -1; | 354 | int max_i = -1; |
360 | double cur_q; | 355 | double cur_q; |
361 | double max_q = DBL_MIN; | 356 | double max_q = DBL_MIN; |
362 | 357 | ||
363 | for (i = 0; i < agent->n; i++) | 358 | for (i = 0; i < agent->n; i++) |
364 | { | 359 | { |
365 | cur_q = agent_estimate_q (agent, state, i); | 360 | cur_q = agent_estimate_q (agent, state, i); |
366 | if (cur_q > max_q) | 361 | if (cur_q > max_q) |
367 | { | 362 | { |
368 | max_q = cur_q; | 363 | max_q = cur_q; |
369 | max_i = i; | 364 | max_i = i; |
370 | } | 365 | } |
371 | } | 366 | } |
372 | 367 | ||
373 | GNUNET_assert(-1 != max_i); | 368 | GNUNET_assert(-1 != max_i); |
374 | 369 | ||
375 | return max_i; | 370 | return max_i; |
376 | } | 371 | } |
377 | 372 | ||
378 | /** | 373 | /** |
@@ -382,10 +377,9 @@ agent_get_action_best (struct RIL_Peer_Agent *agent, | |||
382 | * @return any action | 377 | * @return any action |
383 | */ | 378 | */ |
384 | static int | 379 | static int |
385 | agent_get_action_explore (struct RIL_Peer_Agent *agent, | 380 | agent_get_action_explore (struct RIL_Peer_Agent *agent, double *state) |
386 | double *state) | ||
387 | { | 381 | { |
388 | return GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, agent->n); | 382 | return GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, agent->n); |
389 | } | 383 | } |
390 | 384 | ||
391 | /** | 385 | /** |
@@ -397,20 +391,20 @@ agent_get_action_explore (struct RIL_Peer_Agent *agent, | |||
397 | */ | 391 | */ |
398 | static void | 392 | static void |
399 | agent_update_weights (struct RIL_Peer_Agent *agent, | 393 | agent_update_weights (struct RIL_Peer_Agent *agent, |
400 | double reward, | 394 | double reward, |
401 | double *s_next, | 395 | double *s_next, |
402 | int a_prime) | 396 | int a_prime) |
403 | { | 397 | { |
404 | int i; | 398 | int i; |
405 | double delta; | 399 | double delta; |
406 | double *theta = (agent->W)[agent->a_old]; | 400 | double *theta = (agent->W)[agent->a_old]; |
407 | 401 | ||
408 | delta = reward + agent_estimate_q (agent, s_next, a_prime) - | 402 | delta = reward + agent_estimate_q (agent, s_next, a_prime) |
409 | agent_estimate_q (agent, agent->s_old, agent->a_old); | 403 | - agent_estimate_q (agent, agent->s_old, agent->a_old); |
410 | for (i = 0; i < agent->m; i++) | 404 | for (i = 0; i < agent->m; i++) |
411 | { | 405 | { |
412 | theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i]; | 406 | theta[i] += agent->envi->parameters.alpha * delta * (agent->e)[i]; |
413 | } | 407 | } |
414 | } | 408 | } |
415 | 409 | ||
416 | /** | 410 | /** |
@@ -424,31 +418,31 @@ agent_update_weights (struct RIL_Peer_Agent *agent, | |||
424 | */ | 418 | */ |
425 | static void | 419 | static void |
426 | agent_modify_eligibility (struct RIL_Peer_Agent *agent, | 420 | agent_modify_eligibility (struct RIL_Peer_Agent *agent, |
427 | enum RIL_E_Modification mod) | 421 | enum RIL_E_Modification mod) |
428 | { | 422 | { |
429 | int i; | 423 | int i; |
430 | double *e = agent->e; | 424 | double *e = agent->e; |
431 | double gamma = agent->envi->parameters.gamma; | 425 | double gamma = agent->envi->parameters.gamma; |
432 | double lambda = agent->envi->parameters.lambda; | 426 | double lambda = agent->envi->parameters.lambda; |
433 | 427 | ||
434 | for (i = 0; i < agent->m; i++) | 428 | for (i = 0; i < agent->m; i++) |
435 | { | 429 | { |
436 | switch (mod) | 430 | switch (mod) |
437 | { | 431 | { |
438 | case RIL_E_ACCUMULATE: | 432 | case RIL_E_ACCUMULATE: |
439 | e[i] += 1; | 433 | e[i] += 1; |
440 | break; | 434 | break; |
441 | case RIL_E_REPLACE: | 435 | case RIL_E_REPLACE: |
442 | e[i] = 1; | 436 | e[i] = 1; |
443 | break; | 437 | break; |
444 | case RIL_E_SET: | 438 | case RIL_E_SET: |
445 | e[i] = gamma * lambda; | 439 | e[i] = gamma * lambda; |
446 | break; | 440 | break; |
447 | case RIL_E_ZERO: | 441 | case RIL_E_ZERO: |
448 | e[i] = 0; | 442 | e[i] = 0; |
449 | break; | 443 | break; |
450 | } | 444 | } |
451 | } | 445 | } |
452 | } | 446 | } |
453 | 447 | ||
454 | /** | 448 | /** |
@@ -459,20 +453,20 @@ agent_modify_eligibility (struct RIL_Peer_Agent *agent, | |||
459 | static double * | 453 | static double * |
460 | envi_get_state (struct GAS_RIL_Handle *solver) | 454 | envi_get_state (struct GAS_RIL_Handle *solver) |
461 | { | 455 | { |
462 | int i; | 456 | int i; |
463 | struct RIL_Network *net; | 457 | struct RIL_Network *net; |
464 | double *state = GNUNET_malloc (sizeof (double) * solver->networks_count * 4); | 458 | double *state = GNUNET_malloc (sizeof (double) * solver->networks_count * 4); |
465 | 459 | ||
466 | for (i = 0; i < solver->networks_count; i += 4) | 460 | for (i = 0; i < solver->networks_count; i += 4) |
467 | { | 461 | { |
468 | net = (&solver->network_entries)[i]; | 462 | net = (&solver->network_entries)[i]; |
469 | state[i] = (double) net->bw_in_assigned; | 463 | state[i] = (double) net->bw_in_assigned; |
470 | state[i+1] = (double) net->bw_in_available; | 464 | state[i + 1] = (double) net->bw_in_available; |
471 | state[i+2] = (double) net->bw_out_assigned; | 465 | state[i + 2] = (double) net->bw_out_assigned; |
472 | state[i+3] = (double) net->bw_out_available; | 466 | state[i + 3] = (double) net->bw_out_available; |
473 | } | 467 | } |
474 | 468 | ||
475 | return state; | 469 | return state; |
476 | } | 470 | } |
477 | 471 | ||
478 | /** | 472 | /** |
@@ -483,52 +477,57 @@ envi_get_state (struct GAS_RIL_Handle *solver) | |||
483 | static double | 477 | static double |
484 | envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent) | 478 | envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent) |
485 | { | 479 | { |
486 | //TODO! implement reward calculation | 480 | //TODO! implement reward calculation |
487 | 481 | ||
488 | return (double) GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX) / (double) UINT32_MAX; | 482 | return (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, |
483 | UINT32_MAX) / (double) UINT32_MAX; | ||
489 | } | 484 | } |
490 | 485 | ||
491 | static void | 486 | static void |
492 | envi_action_bw_double (struct GAS_RIL_Handle *solver, | 487 | envi_action_bw_double (struct GAS_RIL_Handle *solver, |
493 | struct RIL_Peer_Agent *agent, | 488 | struct RIL_Peer_Agent *agent, |
494 | int direction_in) | 489 | int direction_in) |
495 | { | 490 | { |
496 | if (direction_in) | 491 | if (direction_in) |
497 | { | 492 | { |
498 | agent->bw_in *= 2; | 493 | agent->bw_in *= 2; |
499 | agent->address->assigned_bw_in.value__ = htonl (agent->bw_in); | 494 | agent->address->assigned_bw_in.value__ = htonl (agent->bw_in); |
500 | solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, agent->address); | 495 | solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, |
501 | } | 496 | agent->address); |
502 | else | 497 | } |
503 | { | 498 | else |
504 | agent->bw_out *= 2; | 499 | { |
505 | agent->address->assigned_bw_out.value__ = htonl (agent->bw_out); | 500 | agent->bw_out *= 2; |
506 | solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, agent->address); | 501 | agent->address->assigned_bw_out.value__ = htonl (agent->bw_out); |
507 | } | 502 | solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, |
503 | agent->address); | ||
504 | } | ||
508 | } | 505 | } |
509 | 506 | ||
510 | static void | 507 | static void |
511 | envi_action_bw_halven (struct GAS_RIL_Handle *solver, | 508 | envi_action_bw_halven (struct GAS_RIL_Handle *solver, |
512 | struct RIL_Peer_Agent *agent, | 509 | struct RIL_Peer_Agent *agent, |
513 | int direction_in) | 510 | int direction_in) |
514 | { | 511 | { |
515 | if ((direction_in && 1 == agent->bw_in) || | 512 | if ((direction_in && 1 == agent->bw_in) |
516 | (!direction_in && 1 == agent->bw_out)) | 513 | || (!direction_in && 1 == agent->bw_out)) |
517 | { | 514 | { |
518 | return; | 515 | return; |
519 | } | 516 | } |
520 | if (direction_in) | 517 | if (direction_in) |
521 | { | 518 | { |
522 | agent->bw_in /= 2; | 519 | agent->bw_in /= 2; |
523 | agent->address->assigned_bw_in.value__ = htonl (agent->bw_in); | 520 | agent->address->assigned_bw_in.value__ = htonl (agent->bw_in); |
524 | solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, agent->address); | 521 | solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, |
525 | } | 522 | agent->address); |
526 | else | 523 | } |
527 | { | 524 | else |
528 | agent->bw_out /= 2; | 525 | { |
529 | agent->address->assigned_bw_out.value__ = htonl (agent->bw_out); | 526 | agent->bw_out /= 2; |
530 | solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, agent->address); | 527 | agent->address->assigned_bw_out.value__ = htonl (agent->bw_out); |
531 | } | 528 | solver->callbacks->bw_changed (solver->callbacks->bw_changed_cls, |
529 | agent->address); | ||
530 | } | ||
532 | } | 531 | } |
533 | 532 | ||
534 | /** | 533 | /** |
@@ -538,24 +537,24 @@ envi_action_bw_halven (struct GAS_RIL_Handle *solver, | |||
538 | */ | 537 | */ |
539 | static void | 538 | static void |
540 | envi_do_action (struct GAS_RIL_Handle *solver, | 539 | envi_do_action (struct GAS_RIL_Handle *solver, |
541 | struct RIL_Peer_Agent *agent, | 540 | struct RIL_Peer_Agent *agent, |
542 | int action) | 541 | int action) |
543 | { | 542 | { |
544 | switch (action) | 543 | switch (action) |
545 | { | 544 | { |
546 | case RIL_ACTION_BW_IN_DBL: | 545 | case RIL_ACTION_BW_IN_DBL: |
547 | envi_action_bw_double (solver, agent, GNUNET_YES); | 546 | envi_action_bw_double (solver, agent, GNUNET_YES); |
548 | break; | 547 | break; |
549 | case RIL_ACTION_BW_IN_HLV: | 548 | case RIL_ACTION_BW_IN_HLV: |
550 | envi_action_bw_halven (solver, agent, GNUNET_YES); | 549 | envi_action_bw_halven (solver, agent, GNUNET_YES); |
551 | break; | 550 | break; |
552 | case RIL_ACTION_BW_OUT_DBL: | 551 | case RIL_ACTION_BW_OUT_DBL: |
553 | envi_action_bw_double (solver, agent, GNUNET_NO); | 552 | envi_action_bw_double (solver, agent, GNUNET_NO); |
554 | break; | 553 | break; |
555 | case RIL_ACTION_BW_OUT_HLV: | 554 | case RIL_ACTION_BW_OUT_HLV: |
556 | envi_action_bw_halven (solver, agent, GNUNET_NO); | 555 | envi_action_bw_halven (solver, agent, GNUNET_NO); |
557 | break; | 556 | break; |
558 | } | 557 | } |
559 | } | 558 | } |
560 | 559 | ||
561 | /** | 560 | /** |
@@ -568,57 +567,57 @@ envi_do_action (struct GAS_RIL_Handle *solver, | |||
568 | static void | 567 | static void |
569 | agent_step (struct RIL_Peer_Agent *agent) | 568 | agent_step (struct RIL_Peer_Agent *agent) |
570 | { | 569 | { |
571 | int a_next = -1; | 570 | int a_next = -1; |
572 | double *s_next; | 571 | double *s_next; |
573 | double reward; | 572 | double reward; |
574 | 573 | ||
575 | s_next = envi_get_state(agent->envi); | 574 | s_next = envi_get_state (agent->envi); |
576 | reward = envi_get_reward(agent->envi, agent); | 575 | reward = envi_get_reward (agent->envi, agent); |
577 | 576 | ||
578 | switch (agent->envi->parameters.algorithm) | 577 | switch (agent->envi->parameters.algorithm) |
579 | { | 578 | { |
580 | case RIL_ALGO_SARSA: | 579 | case RIL_ALGO_SARSA: |
581 | agent_modify_eligibility (agent, RIL_E_SET); | 580 | agent_modify_eligibility (agent, RIL_E_SET); |
582 | if (agent_decide_exploration (agent)) | 581 | if (agent_decide_exploration (agent)) |
583 | { | 582 | { |
584 | a_next = agent_get_action_explore (agent, s_next); | 583 | a_next = agent_get_action_explore (agent, s_next); |
585 | } | 584 | } |
586 | else | 585 | else |
587 | { | 586 | { |
588 | a_next = agent_get_action_best (agent, s_next); | 587 | a_next = agent_get_action_best (agent, s_next); |
589 | } | 588 | } |
590 | //updates weights with selected action (on-policy) | 589 | //updates weights with selected action (on-policy) |
591 | agent_update_weights (agent, reward, s_next, a_next); | 590 | agent_update_weights (agent, reward, s_next, a_next); |
592 | break; | 591 | break; |
593 | 592 | ||
594 | case RIL_ALGO_Q: | 593 | case RIL_ALGO_Q: |
595 | //updates weights with best action, disregarding actually selected action (off-policy) | 594 | //updates weights with best action, disregarding actually selected action (off-policy) |
596 | a_next = agent_get_action_best (agent, s_next); | 595 | a_next = agent_get_action_best (agent, s_next); |
597 | agent_update_weights (agent, reward, s_next, a_next); | 596 | agent_update_weights (agent, reward, s_next, a_next); |
598 | if (agent_decide_exploration (agent)) | 597 | if (agent_decide_exploration (agent)) |
599 | { | 598 | { |
600 | a_next = agent_get_action_explore (agent, s_next); | 599 | a_next = agent_get_action_explore (agent, s_next); |
601 | agent_modify_eligibility(agent, RIL_E_ZERO); | 600 | agent_modify_eligibility (agent, RIL_E_ZERO); |
602 | } | 601 | } |
603 | else | 602 | else |
604 | { | 603 | { |
605 | a_next = agent_get_action_best (agent, s_next); | 604 | a_next = agent_get_action_best (agent, s_next); |
606 | agent_modify_eligibility(agent, RIL_E_SET); | 605 | agent_modify_eligibility (agent, RIL_E_SET); |
607 | } | 606 | } |
608 | break; | 607 | break; |
609 | } | 608 | } |
610 | 609 | ||
611 | GNUNET_assert (-1 != a_next); | 610 | GNUNET_assert(-1 != a_next); |
612 | 611 | ||
613 | agent_modify_eligibility (agent, RIL_E_ACCUMULATE); | 612 | agent_modify_eligibility (agent, RIL_E_ACCUMULATE); |
614 | 613 | ||
615 | envi_do_action(agent->envi, agent, a_next); | 614 | envi_do_action (agent->envi, agent, a_next); |
616 | 615 | ||
617 | GNUNET_free(agent->s_old); | 616 | GNUNET_free(agent->s_old); |
618 | agent->s_old = s_next; | 617 | agent->s_old = s_next; |
619 | agent->a_old = a_next; | 618 | agent->a_old = a_next; |
620 | 619 | ||
621 | agent->step_count += 1; | 620 | agent->step_count += 1; |
622 | } | 621 | } |
623 | 622 | ||
624 | /** | 623 | /** |
@@ -627,27 +626,25 @@ agent_step (struct RIL_Peer_Agent *agent) | |||
627 | * @param tc task context for the scheduler | 626 | * @param tc task context for the scheduler |
628 | */ | 627 | */ |
629 | static void | 628 | static void |
630 | ril_periodic_step (void *cls, | 629 | ril_periodic_step (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
631 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
632 | { | 630 | { |
633 | struct GAS_RIL_Handle *solver = cls; | 631 | struct GAS_RIL_Handle *solver = cls; |
634 | struct RIL_Peer_Agent *cur; | 632 | struct RIL_Peer_Agent *cur; |
635 | 633 | ||
636 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", solver->step_count); | 634 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "RIL step number %d\n", |
637 | 635 | solver->step_count); | |
638 | for (cur = solver->agents_head; NULL != cur; cur = cur->next) | 636 | |
639 | { | 637 | for (cur = solver->agents_head; NULL != cur; cur = cur->next) |
640 | if (cur->active) | 638 | { |
641 | { | 639 | if (cur->active) |
642 | agent_step (cur); | 640 | { |
643 | } | 641 | agent_step (cur); |
644 | } | 642 | } |
645 | 643 | } | |
646 | solver->step_count += 1; | 644 | |
647 | solver->next_step = GNUNET_SCHEDULER_add_delayed ( | 645 | solver->step_count += 1; |
648 | solver->step_time, | 646 | solver->next_step = GNUNET_SCHEDULER_add_delayed (solver->step_time, |
649 | &ril_periodic_step, | 647 | &ril_periodic_step, solver); |
650 | solver); | ||
651 | } | 648 | } |
652 | 649 | ||
653 | /** | 650 | /** |
@@ -657,32 +654,32 @@ ril_periodic_step (void *cls, | |||
657 | * @return handle to the new agent | 654 | * @return handle to the new agent |
658 | */ | 655 | */ |
659 | static struct RIL_Peer_Agent * | 656 | static struct RIL_Peer_Agent * |
660 | agent_init (void *s, | 657 | agent_init (void *s, const struct GNUNET_PeerIdentity *peer) |
661 | const struct GNUNET_PeerIdentity *peer) | ||
662 | { | 658 | { |
663 | int i; | 659 | int i; |
664 | struct GAS_RIL_Handle * solver = s; | 660 | struct GAS_RIL_Handle * solver = s; |
665 | struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent)); | 661 | struct RIL_Peer_Agent * agent = GNUNET_malloc (sizeof (struct RIL_Peer_Agent)); |
666 | 662 | ||
667 | agent->envi = solver; | 663 | agent->envi = solver; |
668 | agent->peer = *peer; | 664 | agent->peer = *peer; |
669 | agent->step_count = 0; | 665 | agent->step_count = 0; |
670 | agent->active = GNUNET_NO; | 666 | agent->active = GNUNET_NO; |
671 | agent->s_old = NULL; | 667 | agent->s_old = envi_get_state (solver); |
672 | agent->n = RIL_ACTION_TYPE_NUM; | 668 | agent->n = RIL_ACTION_TYPE_NUM; |
673 | agent->m = solver->networks_count * 4; | 669 | agent->m = solver->networks_count * 4; |
674 | agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n); | 670 | agent->W = (double **) GNUNET_malloc (sizeof (double) * agent->n); |
675 | for (i = 0; i < agent->n; i++) | 671 | for (i = 0; i < agent->n; i++) |
676 | { | 672 | { |
677 | (agent->W)[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m); | 673 | (agent->W)[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m); |
678 | } | 674 | } |
679 | agent->a_old = -1; | 675 | agent->a_old = -1; |
680 | agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m); | 676 | agent->e = (double *) GNUNET_malloc (sizeof (double) * agent->m); |
681 | agent_modify_eligibility (agent, RIL_E_ZERO); | 677 | agent_modify_eligibility (agent, RIL_E_ZERO); |
682 | 678 | ||
683 | GNUNET_CONTAINER_DLL_insert_tail (solver->agents_head, solver->agents_tail, agent); | 679 | GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, |
684 | 680 | agent); | |
685 | return agent; | 681 | |
682 | return agent; | ||
686 | } | 683 | } |
687 | 684 | ||
688 | /** | 685 | /** |
@@ -691,18 +688,17 @@ agent_init (void *s, | |||
691 | * @param agent the agent to retire | 688 | * @param agent the agent to retire |
692 | */ | 689 | */ |
693 | static void | 690 | static void |
694 | agent_die (struct GAS_RIL_Handle *solver, | 691 | agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent) |
695 | struct RIL_Peer_Agent *agent) | ||
696 | { | 692 | { |
697 | int i; | 693 | int i; |
698 | 694 | ||
699 | for (i = 0; i < agent->n; i++) | 695 | for (i = 0; i < agent->n; i++) |
700 | { | 696 | { |
701 | GNUNET_free((agent->W)[i]); | 697 | GNUNET_free((agent->W)[i]); |
702 | } | 698 | } |
703 | GNUNET_free(agent->W); | 699 | GNUNET_free(agent->W); |
704 | GNUNET_free(agent->e); | 700 | GNUNET_free(agent->e); |
705 | GNUNET_free(agent->s_old); | 701 | GNUNET_free(agent->s_old); |
706 | } | 702 | } |
707 | 703 | ||
708 | /** | 704 | /** |
@@ -712,21 +708,20 @@ agent_die (struct GAS_RIL_Handle *solver, | |||
712 | * @return number of agents | 708 | * @return number of agents |
713 | */ | 709 | */ |
714 | static int | 710 | static int |
715 | ril_count_agents (struct GAS_RIL_Handle *solver, | 711 | ril_count_agents (struct GAS_RIL_Handle *solver, int active_only) |
716 | int active_only) | ||
717 | { | 712 | { |
718 | int c; | 713 | int c; |
719 | struct RIL_Peer_Agent *cur; | 714 | struct RIL_Peer_Agent *cur; |
720 | 715 | ||
721 | c = 0; | 716 | c = 0; |
722 | for (cur = solver->agents_head; NULL != cur; cur = cur->next) | 717 | for (cur = solver->agents_head; NULL != cur; cur = cur->next) |
723 | { | 718 | { |
724 | if ((!active_only) || (active_only && cur->active)) | 719 | if ((!active_only) || (active_only && cur->active)) |
725 | { | 720 | { |
726 | c += 1; | 721 | c += 1; |
727 | } | 722 | } |
728 | } | 723 | } |
729 | return c; | 724 | return c; |
730 | } | 725 | } |
731 | 726 | ||
732 | /** | 727 | /** |
@@ -737,19 +732,19 @@ ril_count_agents (struct GAS_RIL_Handle *solver, | |||
737 | */ | 732 | */ |
738 | static struct RIL_Peer_Agent * | 733 | static struct RIL_Peer_Agent * |
739 | ril_get_agent (struct GAS_RIL_Handle *solver, | 734 | ril_get_agent (struct GAS_RIL_Handle *solver, |
740 | const struct GNUNET_PeerIdentity *peer) | 735 | const struct GNUNET_PeerIdentity *peer) |
741 | { | 736 | { |
742 | struct RIL_Peer_Agent *cur; | 737 | struct RIL_Peer_Agent *cur; |
743 | 738 | ||
744 | for (cur = solver->agents_head; NULL != cur; cur = cur->next) | 739 | for (cur = solver->agents_head; NULL != cur; cur = cur->next) |
745 | { | 740 | { |
746 | if (0 == GNUNET_CRYPTO_hash_cmp (&peer->hashPubKey, &cur->peer.hashPubKey)) | 741 | if (0 == GNUNET_CRYPTO_hash_cmp (&peer->hashPubKey, &cur->peer.hashPubKey)) |
747 | { | 742 | { |
748 | return cur; | 743 | return cur; |
749 | } | 744 | } |
750 | } | 745 | } |
751 | 746 | ||
752 | return agent_init (solver, peer); | 747 | return agent_init (solver, peer); |
753 | } | 748 | } |
754 | 749 | ||
755 | /** | 750 | /** |
@@ -761,30 +756,28 @@ ril_get_agent (struct GAS_RIL_Handle *solver, | |||
761 | * @return whether iterator should continue | 756 | * @return whether iterator should continue |
762 | */ | 757 | */ |
763 | static int | 758 | static int |
764 | ril_init_agents_it (void *cls, | 759 | ril_init_agents_it (void *cls, const struct GNUNET_HashCode *key, void *value) |
765 | const struct GNUNET_HashCode *key, | ||
766 | void *value) | ||
767 | { | 760 | { |
768 | struct GAS_RIL_Handle *solver = cls; | 761 | struct GAS_RIL_Handle *solver = cls; |
769 | struct ATS_Address *address = value; | 762 | struct ATS_Address *address = value; |
770 | struct RIL_Peer_Agent *agent; | 763 | struct RIL_Peer_Agent *agent; |
771 | uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__); | 764 | uint32_t min_bw = ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__); |
772 | 765 | ||
773 | agent = ril_get_agent (solver, &address->peer); | 766 | agent = ril_get_agent (solver, &address->peer); |
774 | 767 | ||
775 | GNUNET_assert (NULL != agent); | 768 | GNUNET_assert(NULL != agent); |
776 | 769 | ||
777 | if (NULL == agent->address) | 770 | if (NULL == agent->address) |
778 | { | 771 | { |
779 | agent->address = address; | 772 | agent->address = address; |
780 | agent->address->active = GNUNET_YES; | 773 | agent->address->active = GNUNET_YES; |
781 | agent->bw_in = min_bw; | 774 | agent->bw_in = min_bw; |
782 | agent->address->assigned_bw_in.value__ = htonl (min_bw); | 775 | agent->address->assigned_bw_in.value__ = htonl (min_bw); |
783 | agent->bw_out = min_bw; | 776 | agent->bw_out = min_bw; |
784 | agent->address->assigned_bw_out.value__ = htonl (min_bw); | 777 | agent->address->assigned_bw_out.value__ = htonl (min_bw); |
785 | } | 778 | } |
786 | 779 | ||
787 | return GNUNET_YES; | 780 | return GNUNET_YES; |
788 | } | 781 | } |
789 | 782 | ||
790 | /** | 783 | /** |
@@ -798,16 +791,15 @@ static struct RIL_Network * | |||
798 | ril_get_network (struct GAS_RIL_Handle *s, uint32_t type) | 791 | ril_get_network (struct GAS_RIL_Handle *s, uint32_t type) |
799 | { | 792 | { |
800 | int i; | 793 | int i; |
801 | for (i = 0 ; i < s->networks_count; i++) | 794 | for (i = 0; i < s->networks_count; i++) |
802 | { | 795 | { |
803 | if (s->network_entries[i].type == type) | 796 | if (s->network_entries[i].type == type) { |
804 | return &s->network_entries[i]; | 797 | return &s->network_entries[i]; |
805 | 798 | } | |
806 | } | 799 | } |
807 | return NULL; | 800 | return NULL; |
808 | } | 801 | } |
809 | 802 | ||
810 | |||
811 | /** | 803 | /** |
812 | * Solver API functions | 804 | * Solver API functions |
813 | * --------------------------- | 805 | * --------------------------- |
@@ -823,21 +815,18 @@ ril_get_network (struct GAS_RIL_Handle *s, uint32_t type) | |||
823 | */ | 815 | */ |
824 | void | 816 | void |
825 | GAS_ril_address_change_preference (void *s, | 817 | GAS_ril_address_change_preference (void *s, |
826 | const struct GNUNET_PeerIdentity *peer, | 818 | const struct GNUNET_PeerIdentity *peer, |
827 | enum GNUNET_ATS_PreferenceKind kind, | 819 | enum GNUNET_ATS_PreferenceKind kind, |
828 | double pref_rel) | 820 | double pref_rel) |
829 | { | 821 | { |
830 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 822 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
831 | "API_address_change_preference() Preference `%s' for peer `%s' changed to %.2f \n", | 823 | "API_address_change_preference() Preference `%s' for peer `%s' changed to %.2f \n", |
832 | GNUNET_ATS_print_preference_type (kind), | 824 | GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel); |
833 | GNUNET_i2s (peer), | 825 | /* |
834 | pref_rel); | 826 | * Nothing to do here. Preferences are considered during reward calculation. |
835 | /* | 827 | */ |
836 | * Nothing to do here. Preferences are considered during reward calculation. | ||
837 | */ | ||
838 | } | 828 | } |
839 | 829 | ||
840 | |||
841 | /** | 830 | /** |
842 | * Init the reinforcement learning problem solver | 831 | * Init the reinforcement learning problem solver |
843 | * | 832 | * |
@@ -869,98 +858,124 @@ GAS_ril_address_change_preference (void *s, | |||
869 | */ | 858 | */ |
870 | void * | 859 | void * |
871 | GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg, | 860 | GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg, |
872 | const struct GNUNET_STATISTICS_Handle *stats, | 861 | const struct GNUNET_STATISTICS_Handle *stats, |
873 | const struct GNUNET_CONTAINER_MultiHashMap *addresses, | 862 | const struct GNUNET_CONTAINER_MultiHashMap *addresses, |
874 | int *network, | 863 | int *network, |
875 | unsigned long long *out_quota, | 864 | unsigned long long *out_quota, |
876 | unsigned long long *in_quota, | 865 | unsigned long long *in_quota, |
877 | int dest_length, | 866 | int dest_length, |
878 | GAS_bandwidth_changed_cb bw_changed_cb, | 867 | GAS_bandwidth_changed_cb bw_changed_cb, |
879 | void *bw_changed_cb_cls, | 868 | void *bw_changed_cb_cls, |
880 | GAS_get_preferences get_preference, | 869 | GAS_get_preferences get_preference, |
881 | void *get_preference_cls, | 870 | void *get_preference_cls, |
882 | GAS_get_properties get_properties, | 871 | GAS_get_properties get_properties, |
883 | void *get_properties_cls) | 872 | void *get_properties_cls) |
884 | { | 873 | { |
885 | int c; | 874 | int c; |
886 | unsigned long long tmp; | 875 | unsigned long long tmp; |
887 | struct RIL_Network * cur; | 876 | char *string; |
888 | struct GAS_RIL_Handle *solver = GNUNET_malloc (sizeof (struct GAS_RIL_Handle)); | 877 | struct RIL_Network * cur; |
889 | 878 | struct GAS_RIL_Handle *solver = GNUNET_malloc (sizeof (struct GAS_RIL_Handle)); | |
890 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n"); | 879 | |
891 | 880 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_init() Initializing RIL solver\n"); | |
892 | GNUNET_assert (NULL != cfg); | 881 | |
893 | GNUNET_assert (NULL != stats); | 882 | GNUNET_assert(NULL != cfg); |
894 | GNUNET_assert (NULL != network); | 883 | GNUNET_assert(NULL != stats); |
895 | GNUNET_assert (NULL != bw_changed_cb); | 884 | GNUNET_assert(NULL != network); |
896 | GNUNET_assert (NULL != get_preference); | 885 | GNUNET_assert(NULL != bw_changed_cb); |
897 | GNUNET_assert (NULL != get_properties); | 886 | GNUNET_assert(NULL != get_preference); |
898 | 887 | GNUNET_assert(NULL != get_properties); | |
899 | if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time(cfg, "ats", "RIL_STEP_TIME", &solver->step_time)) | 888 | |
900 | { | 889 | if (GNUNET_OK |
901 | solver->step_time = RIL_DEFAULT_STEP_TIME; | 890 | != GNUNET_CONFIGURATION_get_value_time (cfg, "ats", "RIL_STEP_TIME", |
902 | } | 891 | &solver->step_time)) |
903 | if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size(cfg, "ats", "RIL_DISCOUNT_FACTOR", &tmp)) | 892 | { |
904 | { | 893 | solver->step_time = RIL_DEFAULT_STEP_TIME; |
905 | solver->parameters.gamma = (double) tmp / 100;; | 894 | } |
906 | } | 895 | if (GNUNET_OK |
907 | else | 896 | != GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "RIL_ALGORITHM", |
908 | { | 897 | &string)) |
909 | solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR; | 898 | { |
910 | } | 899 | if (0 == strcmp (string, "SARSA")) |
911 | if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size(cfg, "ats", "RIL_GRADIENT_STEP_SIZE", &tmp)) | 900 | { |
912 | { | 901 | solver->parameters.algorithm = RIL_ALGO_SARSA; |
913 | solver->parameters.alpha = (double) tmp / 100;; | 902 | } |
914 | } | 903 | } |
915 | else | 904 | else |
916 | { | 905 | { |
917 | solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE; | 906 | solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM; |
918 | } | 907 | } |
919 | if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_size(cfg, "ats", "RIL_TRACE_DECAY", &tmp)) | 908 | if (GNUNET_OK |
920 | { | 909 | == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_DISCOUNT_FACTOR", |
921 | solver->parameters.lambda = (double) tmp / 100;; | 910 | &tmp)) |
922 | } | 911 | { |
923 | else | 912 | solver->parameters.gamma = (double) tmp / 100; |
924 | { | 913 | ; |
925 | solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY; | 914 | } |
926 | } | 915 | else |
927 | 916 | { | |
928 | solver->stats = (struct GNUNET_STATISTICS_Handle *) stats; | 917 | solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_FACTOR; |
929 | solver->callbacks = GNUNET_malloc (sizeof (struct RIL_Callbacks)); | 918 | } |
930 | solver->callbacks->bw_changed = bw_changed_cb; | 919 | if (GNUNET_OK |
931 | solver->callbacks->bw_changed_cls = bw_changed_cb_cls; | 920 | == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", |
932 | solver->callbacks->get_preferences = get_preference; | 921 | "RIL_GRADIENT_STEP_SIZE", &tmp)) |
933 | solver->callbacks->get_preferences_cls = get_preference_cls; | 922 | { |
934 | solver->callbacks->get_properties = get_properties; | 923 | solver->parameters.alpha = (double) tmp / 100; |
935 | solver->callbacks->get_properties_cls = get_properties_cls; | 924 | ; |
936 | solver->networks_count = dest_length; | 925 | } |
937 | solver->network_entries = GNUNET_malloc (dest_length * sizeof (struct RIL_Network)); | 926 | else |
938 | solver->bulk_lock = GNUNET_NO; | 927 | { |
939 | solver->addresses = addresses; | 928 | solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE; |
940 | solver->step_count = 0; | 929 | } |
941 | 930 | if (GNUNET_OK | |
942 | for (c = 0; c < dest_length; c++) | 931 | == GNUNET_CONFIGURATION_get_value_size (cfg, "ats", "RIL_TRACE_DECAY", |
943 | { | 932 | &tmp)) |
944 | cur = &solver->network_entries[c]; | 933 | { |
945 | cur->type = network[c]; | 934 | solver->parameters.lambda = (double) tmp / 100; |
946 | cur->bw_in_available = in_quota[c]; | 935 | ; |
947 | cur->bw_in_assigned = 0; | 936 | } |
948 | cur->bw_out_available = out_quota[c]; | 937 | else |
949 | cur->bw_out_assigned = 0; | 938 | { |
950 | } | 939 | solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY; |
951 | 940 | } | |
952 | c = GNUNET_CONTAINER_multihashmap_iterate (addresses, &ril_init_agents_it, solver); | 941 | |
953 | 942 | solver->stats = (struct GNUNET_STATISTICS_Handle *) stats; | |
954 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "API_init() Solving ATS for %d addresses and %d peers\n", | 943 | solver->callbacks = GNUNET_malloc (sizeof (struct RIL_Callbacks)); |
955 | c, | 944 | solver->callbacks->bw_changed = bw_changed_cb; |
956 | ril_count_agents(solver, GNUNET_NO)); | 945 | solver->callbacks->bw_changed_cls = bw_changed_cb_cls; |
957 | 946 | solver->callbacks->get_preferences = get_preference; | |
958 | solver->next_step = GNUNET_SCHEDULER_add_delayed ( | 947 | solver->callbacks->get_preferences_cls = get_preference_cls; |
959 | GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), 1000), | 948 | solver->callbacks->get_properties = get_properties; |
960 | &ril_periodic_step, | 949 | solver->callbacks->get_properties_cls = get_properties_cls; |
961 | solver); | 950 | solver->networks_count = dest_length; |
962 | 951 | solver->network_entries = | |
963 | return solver; | 952 | GNUNET_malloc (dest_length * sizeof (struct RIL_Network)); |
953 | solver->bulk_lock = GNUNET_NO; | ||
954 | solver->addresses = addresses; | ||
955 | solver->step_count = 0; | ||
956 | |||
957 | for (c = 0; c < dest_length; c++) | ||
958 | { | ||
959 | cur = &solver->network_entries[c]; | ||
960 | cur->type = network[c]; | ||
961 | cur->bw_in_available = in_quota[c]; | ||
962 | cur->bw_in_assigned = 0; | ||
963 | cur->bw_out_available = out_quota[c]; | ||
964 | cur->bw_out_assigned = 0; | ||
965 | } | ||
966 | |||
967 | c = GNUNET_CONTAINER_multihashmap_iterate (addresses, &ril_init_agents_it, | ||
968 | solver); | ||
969 | |||
970 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | ||
971 | "API_init() Solving ATS for %d addresses and %d peers\n", c, | ||
972 | ril_count_agents(solver, GNUNET_NO)); | ||
973 | |||
974 | solver->next_step = GNUNET_SCHEDULER_add_delayed ( | ||
975 | GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_get_millisecond_ (), | ||
976 | 1000), &ril_periodic_step, solver); | ||
977 | |||
978 | return solver; | ||
964 | } | 979 | } |
965 | 980 | ||
966 | /** | 981 | /** |
@@ -971,27 +986,26 @@ GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
971 | void | 986 | void |
972 | GAS_ril_done (void * solver) | 987 | GAS_ril_done (void * solver) |
973 | { | 988 | { |
974 | struct GAS_RIL_Handle *s = solver; | 989 | struct GAS_RIL_Handle *s = solver; |
975 | struct RIL_Peer_Agent *cur_agent; | 990 | struct RIL_Peer_Agent *cur_agent; |
976 | struct RIL_Peer_Agent *next_agent; | 991 | struct RIL_Peer_Agent *next_agent; |
977 | |||
978 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n"); | ||
979 | |||
980 | cur_agent = s->agents_head; | ||
981 | while (NULL != cur_agent) | ||
982 | { | ||
983 | next_agent = cur_agent->next; | ||
984 | GNUNET_CONTAINER_DLL_remove (s->agents_head, s->agents_tail, cur_agent); | ||
985 | agent_die (s, cur_agent); | ||
986 | cur_agent = next_agent; | ||
987 | } | ||
988 | |||
989 | GNUNET_SCHEDULER_cancel (s->next_step); | ||
990 | GNUNET_free (s->callbacks); | ||
991 | GNUNET_free (s->network_entries); | ||
992 | GNUNET_free (s); | ||
993 | } | ||
994 | 992 | ||
993 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n"); | ||
994 | |||
995 | cur_agent = s->agents_head; | ||
996 | while (NULL != cur_agent) | ||
997 | { | ||
998 | next_agent = cur_agent->next; | ||
999 | GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent); | ||
1000 | agent_die (s, cur_agent); | ||
1001 | cur_agent = next_agent; | ||
1002 | } | ||
1003 | |||
1004 | GNUNET_SCHEDULER_cancel (s->next_step); | ||
1005 | GNUNET_free(s->callbacks); | ||
1006 | GNUNET_free(s->network_entries); | ||
1007 | GNUNET_free(s); | ||
1008 | } | ||
995 | 1009 | ||
996 | /** | 1010 | /** |
997 | * Add a single address within a network to the solver | 1011 | * Add a single address within a network to the solver |
@@ -1002,27 +1016,29 @@ GAS_ril_done (void * solver) | |||
1002 | */ | 1016 | */ |
1003 | void | 1017 | void |
1004 | GAS_ril_address_add (void *solver, | 1018 | GAS_ril_address_add (void *solver, |
1005 | struct ATS_Address *address, | 1019 | struct ATS_Address *address, |
1006 | uint32_t network) | 1020 | uint32_t network) |
1007 | { | 1021 | { |
1008 | struct GAS_RIL_Handle *s = solver; | 1022 | struct GAS_RIL_Handle *s = solver; |
1009 | //TODO! implement solver address add | 1023 | //TODO! implement solver address add |
1010 | /* | 1024 | /* |
1011 | * if (new peer) | 1025 | * if (new peer) |
1012 | * initialize new agent | 1026 | * initialize new agent |
1013 | * Add address | 1027 | * Add address |
1014 | * increase state vector | 1028 | * increase state vector |
1015 | * knowledge matrix | 1029 | * knowledge matrix |
1016 | * and action vector | 1030 | * and action vector |
1017 | */ | 1031 | */ |
1018 | 1032 | ||
1019 | /* | 1033 | /* |
1020 | * reiterate all addresses, create new agent if necessary and give the agent the address | 1034 | * reiterate all addresses, create new agent if necessary and give the agent the address |
1021 | */ | 1035 | */ |
1022 | GNUNET_CONTAINER_multihashmap_iterate (s->addresses, &ril_init_agents_it, solver); | 1036 | GNUNET_CONTAINER_multihashmap_iterate (s->addresses, &ril_init_agents_it, |
1023 | 1037 | solver); | |
1024 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s address for peer '%s'\n", | 1038 | |
1025 | address->plugin, GNUNET_i2s (&address->peer)); | 1039 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
1040 | "API_address_add() Added %s address for peer '%s'\n", address->plugin, | ||
1041 | GNUNET_i2s (&address->peer)); | ||
1026 | } | 1042 | } |
1027 | 1043 | ||
1028 | /** | 1044 | /** |
@@ -1034,40 +1050,40 @@ GAS_ril_address_add (void *solver, | |||
1034 | */ | 1050 | */ |
1035 | void | 1051 | void |
1036 | GAS_ril_address_delete (void *solver, | 1052 | GAS_ril_address_delete (void *solver, |
1037 | struct ATS_Address *address, | 1053 | struct ATS_Address *address, |
1038 | int session_only) | 1054 | int session_only) |
1039 | { | 1055 | { |
1040 | //TODO! implement solver address delete | 1056 | //TODO! implement solver address delete |
1041 | //TODO! delete session only | 1057 | //TODO! delete session only |
1042 | /* | 1058 | /* |
1043 | * remove address | 1059 | * remove address |
1044 | * if (last address of peer) | 1060 | * if (last address of peer) |
1045 | * remove agent | 1061 | * remove agent |
1046 | * else | 1062 | * else |
1047 | * decrease state vector | 1063 | * decrease state vector |
1048 | * decrease knowledge matrix | 1064 | * decrease knowledge matrix |
1049 | * decrease action vector | 1065 | * decrease action vector |
1050 | */ | 1066 | */ |
1051 | struct GAS_RIL_Handle *s = solver; | 1067 | struct GAS_RIL_Handle *s = solver; |
1052 | struct RIL_Peer_Agent *agent; | 1068 | struct RIL_Peer_Agent *agent; |
1053 | 1069 | ||
1054 | agent = ril_get_agent(s, &address->peer); | 1070 | agent = ril_get_agent (s, &address->peer); |
1055 | 1071 | ||
1056 | if (0 == memcmp (agent->address->addr, address->addr, address->addr_len)) //if used address deleted | 1072 | if (0 == memcmp (agent->address->addr, address->addr, address->addr_len)) //if used address deleted |
1057 | { | 1073 | { |
1058 | agent->address = NULL; //delete address | 1074 | agent->address = NULL; //delete address |
1059 | GNUNET_CONTAINER_multihashmap_iterate (s->addresses, &ril_init_agents_it, solver); //put another address | 1075 | GNUNET_CONTAINER_multihashmap_iterate (s->addresses, &ril_init_agents_it, |
1060 | if (NULL == agent->address) //no other address available | 1076 | solver); //put another address |
1061 | { | 1077 | if (NULL == agent->address) //no other address available |
1062 | agent->active = GNUNET_NO; | 1078 | { |
1063 | } | 1079 | agent->active = GNUNET_NO; |
1064 | } | 1080 | } |
1065 | 1081 | } | |
1066 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1082 | |
1067 | "API_address_delete() Deleted %s%s address for peer '%s'\n", | 1083 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
1068 | session_only ? "session for " : "", | 1084 | "API_address_delete() Deleted %s%s address for peer '%s'\n", |
1069 | address->plugin, | 1085 | session_only ? "session for " : "", address->plugin, |
1070 | GNUNET_i2s(&address->peer)); | 1086 | GNUNET_i2s (&address->peer)); |
1071 | } | 1087 | } |
1072 | 1088 | ||
1073 | /** | 1089 | /** |
@@ -1081,23 +1097,20 @@ GAS_ril_address_delete (void *solver, | |||
1081 | */ | 1097 | */ |
1082 | void | 1098 | void |
1083 | GAS_ril_address_property_changed (void *solver, | 1099 | GAS_ril_address_property_changed (void *solver, |
1084 | struct ATS_Address *address, | 1100 | struct ATS_Address *address, |
1085 | uint32_t type, | 1101 | uint32_t type, |
1086 | uint32_t abs_value, | 1102 | uint32_t abs_value, |
1087 | double rel_value) | 1103 | double rel_value) |
1088 | { | 1104 | { |
1089 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1105 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
1090 | "API_address_property_changed() Property `%s' for peer `%s' address %p changed " | 1106 | "API_address_property_changed() Property `%s' for peer `%s' address %p changed " |
1091 | "to %.2f \n", | 1107 | "to %.2f \n", GNUNET_ATS_print_property_type (type), |
1092 | GNUNET_ATS_print_property_type (type), | 1108 | GNUNET_i2s (&address->peer), address, rel_value); |
1093 | GNUNET_i2s (&address->peer), | 1109 | /* |
1094 | address, rel_value); | 1110 | * Nothing to do here, properties are considered in every reward calculation |
1095 | /* | 1111 | */ |
1096 | * Nothing to do here, properties are considered in every reward calculation | ||
1097 | */ | ||
1098 | } | 1112 | } |
1099 | 1113 | ||
1100 | |||
1101 | /** | 1114 | /** |
1102 | * Transport session for this address has changed | 1115 | * Transport session for this address has changed |
1103 | * | 1116 | * |
@@ -1110,18 +1123,17 @@ GAS_ril_address_property_changed (void *solver, | |||
1110 | */ | 1123 | */ |
1111 | void | 1124 | void |
1112 | GAS_ril_address_session_changed (void *solver, | 1125 | GAS_ril_address_session_changed (void *solver, |
1113 | struct ATS_Address *address, | 1126 | struct ATS_Address *address, |
1114 | uint32_t cur_session, | 1127 | uint32_t cur_session, |
1115 | uint32_t new_session) | 1128 | uint32_t new_session) |
1116 | { | 1129 | { |
1117 | //TODO? consider session changed in solver behaviour | 1130 | //TODO? consider session changed in solver behaviour |
1118 | /* | 1131 | /* |
1119 | * Potentially add session activity as a feature in state vector | 1132 | * Potentially add session activity as a feature in state vector |
1120 | */ | 1133 | */ |
1121 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n"); | 1134 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_address_session_changed()\n"); |
1122 | } | 1135 | } |
1123 | 1136 | ||
1124 | |||
1125 | /** | 1137 | /** |
1126 | * Usage for this address has changed | 1138 | * Usage for this address has changed |
1127 | * | 1139 | * |
@@ -1133,18 +1145,17 @@ GAS_ril_address_session_changed (void *solver, | |||
1133 | */ | 1145 | */ |
1134 | void | 1146 | void |
1135 | GAS_ril_address_inuse_changed (void *solver, | 1147 | GAS_ril_address_inuse_changed (void *solver, |
1136 | struct ATS_Address *address, | 1148 | struct ATS_Address *address, |
1137 | int in_use) | 1149 | int in_use) |
1138 | { | 1150 | { |
1139 | //TODO! consider address_inuse_changed according to matthias' email | 1151 | //TODO! consider address_inuse_changed according to matthias' email |
1140 | /** | 1152 | /** |
1141 | * See matthias' email | 1153 | * See matthias' email |
1142 | */ | 1154 | */ |
1143 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1155 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
1144 | "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n", | 1156 | "API_address_inuse_changed() Usage for %s address of peer '%s' changed to %s\n", |
1145 | address->plugin, | 1157 | address->plugin, GNUNET_i2s (&address->peer), |
1146 | GNUNET_i2s (&address->peer), | 1158 | (GNUNET_YES == in_use) ? "USED" : "UNUSED"); |
1147 | (GNUNET_YES == in_use) ? "USED" : "UNUSED"); | ||
1148 | } | 1159 | } |
1149 | 1160 | ||
1150 | /** | 1161 | /** |
@@ -1159,38 +1170,40 @@ GAS_ril_address_inuse_changed (void *solver, | |||
1159 | */ | 1170 | */ |
1160 | void | 1171 | void |
1161 | GAS_ril_address_change_network (void *solver, | 1172 | GAS_ril_address_change_network (void *solver, |
1162 | struct ATS_Address *address, | 1173 | struct ATS_Address *address, |
1163 | uint32_t current_network, | 1174 | uint32_t current_network, |
1164 | uint32_t new_network) | 1175 | uint32_t new_network) |
1165 | { | 1176 | { |
1166 | struct GAS_RIL_Handle *s = solver; | 1177 | struct GAS_RIL_Handle *s = solver; |
1167 | struct RIL_Peer_Agent *agent; | 1178 | struct RIL_Peer_Agent *agent; |
1168 | struct RIL_Network *net; | 1179 | struct RIL_Network *net; |
1169 | 1180 | ||
1170 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Network type changed, moving " | 1181 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
1171 | "%s address of peer %s from `%s' to `%s'\n", | 1182 | "API_address_change_network() Network type changed, moving " |
1172 | (GNUNET_YES == address->active) ? "active" : "inactive", | 1183 | "%s address of peer %s from `%s' to `%s'\n", |
1173 | GNUNET_i2s (&address->peer), | 1184 | (GNUNET_YES == address->active) ? "active" : "inactive", |
1174 | GNUNET_ATS_print_network_type (current_network), | 1185 | GNUNET_i2s (&address->peer), |
1175 | GNUNET_ATS_print_network_type (new_network)); | 1186 | GNUNET_ATS_print_network_type (current_network), |
1176 | 1187 | GNUNET_ATS_print_network_type (new_network)); | |
1177 | agent = ril_get_agent(s, &address->peer); | 1188 | |
1178 | 1189 | agent = ril_get_agent (s, &address->peer); | |
1179 | if (address->active) | 1190 | |
1180 | { | 1191 | if (address->active) |
1181 | //remove from old network | 1192 | { |
1182 | net = ril_get_network (s, current_network); | 1193 | //remove from old network |
1183 | net->bw_in_assigned -= agent->bw_in; | 1194 | net = ril_get_network (s, current_network); |
1184 | net->bw_out_assigned -= agent->bw_out; | 1195 | net->bw_in_assigned -= agent->bw_in; |
1185 | 1196 | net->bw_out_assigned -= agent->bw_out; | |
1186 | //add to new network | 1197 | |
1187 | net = ril_get_network (s, new_network); | 1198 | //add to new network |
1188 | net->bw_in_assigned += agent->bw_in; | 1199 | net = ril_get_network (s, new_network); |
1189 | net->bw_out_assigned += agent->bw_out; | 1200 | net->bw_in_assigned += agent->bw_in; |
1190 | 1201 | net->bw_out_assigned += agent->bw_out; | |
1191 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "API_address_change_network() Moved %d inbound and %d " | 1202 | |
1192 | "outbound\n", agent->bw_in, agent->bw_out); | 1203 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
1193 | } | 1204 | "API_address_change_network() Moved %d inbound and %d " |
1205 | "outbound\n", agent->bw_in, agent->bw_out); | ||
1206 | } | ||
1194 | } | 1207 | } |
1195 | 1208 | ||
1196 | /** | 1209 | /** |
@@ -1205,21 +1218,18 @@ GAS_ril_address_change_network (void *solver, | |||
1205 | */ | 1218 | */ |
1206 | void | 1219 | void |
1207 | GAS_ril_address_preference_feedback (void *solver, | 1220 | GAS_ril_address_preference_feedback (void *solver, |
1208 | void *application, | 1221 | void *application, |
1209 | const struct GNUNET_PeerIdentity *peer, | 1222 | const struct GNUNET_PeerIdentity *peer, |
1210 | const struct GNUNET_TIME_Relative scope, | 1223 | const struct GNUNET_TIME_Relative scope, |
1211 | enum GNUNET_ATS_PreferenceKind kind, | 1224 | enum GNUNET_ATS_PreferenceKind kind, |
1212 | double score) | 1225 | double score) |
1213 | { | 1226 | { |
1214 | //TODO! collect reward until next reward calculation | 1227 | //TODO! collect reward until next reward calculation |
1215 | //TODO! Find out application | 1228 | //TODO! Find out application |
1216 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1229 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
1217 | "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for " | 1230 | "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for " |
1218 | "preference %s for %d seconds\n", | 1231 | "preference %s for %d seconds\n", GNUNET_i2s (peer), "UNKNOWN", |
1219 | GNUNET_i2s (peer), | 1232 | GNUNET_ATS_print_preference_type (kind), scope.rel_value_us / 1000000); |
1220 | "UNKNOWN", | ||
1221 | GNUNET_ATS_print_preference_type(kind), | ||
1222 | scope.rel_value_us / 1000000); | ||
1223 | } | 1233 | } |
1224 | 1234 | ||
1225 | /** | 1235 | /** |
@@ -1230,27 +1240,26 @@ GAS_ril_address_preference_feedback (void *solver, | |||
1230 | void | 1240 | void |
1231 | GAS_ril_bulk_start (void *solver) | 1241 | GAS_ril_bulk_start (void *solver) |
1232 | { | 1242 | { |
1233 | //TODO? consideration: keep bulk counter and stop agents during bulk | 1243 | //TODO? consideration: keep bulk counter and stop agents during bulk |
1234 | /* | 1244 | /* |
1235 | * bulk counter up, but not really relevant, because there is no complete calculation of the | 1245 | * bulk counter up, but not really relevant, because there is no complete calculation of the |
1236 | * bandwidth assignment triggered anyway. Therefore, changes to addresses can come and go as | 1246 | * bandwidth assignment triggered anyway. Therefore, changes to addresses can come and go as |
1237 | * they want. Consideration: Step-pause during bulk-start-stop period... | 1247 | * they want. Consideration: Step-pause during bulk-start-stop period... |
1238 | */ | 1248 | */ |
1239 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start()\n"); | 1249 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_start()\n"); |
1240 | } | 1250 | } |
1241 | 1251 | ||
1242 | |||
1243 | /** | 1252 | /** |
1244 | * Bulk operation done | 1253 | * Bulk operation done |
1245 | */ | 1254 | */ |
1246 | void | 1255 | void |
1247 | GAS_ril_bulk_stop (void *solver) | 1256 | GAS_ril_bulk_stop (void *solver) |
1248 | { | 1257 | { |
1249 | //TODO? consideration: keep bulk counter and stop agents during bulk | 1258 | //TODO? consideration: keep bulk counter and stop agents during bulk |
1250 | /* | 1259 | /* |
1251 | * bulk counter down, see bulk_start() | 1260 | * bulk counter down, see bulk_start() |
1252 | */ | 1261 | */ |
1253 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop()\n"); | 1262 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "API_bulk_stop()\n"); |
1254 | } | 1263 | } |
1255 | 1264 | ||
1256 | /** | 1265 | /** |
@@ -1261,25 +1270,24 @@ GAS_ril_bulk_stop (void *solver) | |||
1261 | */ | 1270 | */ |
1262 | const struct ATS_Address * | 1271 | const struct ATS_Address * |
1263 | GAS_ril_get_preferred_address (void *solver, | 1272 | GAS_ril_get_preferred_address (void *solver, |
1264 | const struct GNUNET_PeerIdentity *peer) | 1273 | const struct GNUNET_PeerIdentity *peer) |
1265 | { | 1274 | { |
1266 | /* | 1275 | /* |
1267 | * activate agent, return currently chosen address | 1276 | * activate agent, return currently chosen address |
1268 | */ | 1277 | */ |
1269 | struct GAS_RIL_Handle *s = solver; | 1278 | struct GAS_RIL_Handle *s = solver; |
1270 | struct RIL_Peer_Agent *agent; | 1279 | struct RIL_Peer_Agent *agent; |
1271 | 1280 | ||
1272 | agent = ril_get_agent(s, peer); | 1281 | agent = ril_get_agent (s, peer); |
1273 | agent->active = GNUNET_YES; | 1282 | agent->active = GNUNET_YES; |
1274 | 1283 | ||
1275 | GNUNET_assert (NULL != agent->address); | 1284 | GNUNET_assert(NULL != agent->address); |
1276 | 1285 | ||
1277 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1286 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
1278 | "API_get_preferred_address() Activated agent for peer '%s' with %s address\n", | 1287 | "API_get_preferred_address() Activated agent for peer '%s' with %s address\n", |
1279 | GNUNET_i2s (peer), | 1288 | GNUNET_i2s (peer), agent->address->plugin); |
1280 | agent->address->plugin); | ||
1281 | 1289 | ||
1282 | return agent->address; | 1290 | return agent->address; |
1283 | } | 1291 | } |
1284 | 1292 | ||
1285 | /** | 1293 | /** |
@@ -1290,18 +1298,17 @@ GAS_ril_get_preferred_address (void *solver, | |||
1290 | */ | 1298 | */ |
1291 | void | 1299 | void |
1292 | GAS_ril_stop_get_preferred_address (void *solver, | 1300 | GAS_ril_stop_get_preferred_address (void *solver, |
1293 | const struct GNUNET_PeerIdentity *peer) | 1301 | const struct GNUNET_PeerIdentity *peer) |
1294 | { | 1302 | { |
1295 | struct GAS_RIL_Handle *s = solver; | 1303 | struct GAS_RIL_Handle *s = solver; |
1296 | struct RIL_Peer_Agent *agent; | 1304 | struct RIL_Peer_Agent *agent; |
1297 | 1305 | ||
1298 | agent = ril_get_agent(s, peer); | 1306 | agent = ril_get_agent (s, peer); |
1299 | agent->active = GNUNET_NO; | 1307 | agent->active = GNUNET_NO; |
1300 | 1308 | ||
1301 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1309 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
1302 | "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n", | 1310 | "API_stop_get_preferred_address() Paused agent for peer '%s' with %s address\n", |
1303 | GNUNET_i2s (peer), | 1311 | GNUNET_i2s (peer), agent->address->plugin); |
1304 | agent->address->plugin); | ||
1305 | } | 1312 | } |
1306 | 1313 | ||
1307 | /* end of gnunet-service-ats-solver_ril.c */ | 1314 | /* end of gnunet-service-ats-solver_ril.c */ |
diff --git a/src/ats/gnunet-service-ats-solver_ril.h b/src/ats/gnunet-service-ats-solver_ril.h index 132755974..0053e3b9d 100755 --- a/src/ats/gnunet-service-ats-solver_ril.h +++ b/src/ats/gnunet-service-ats-solver_ril.h | |||
@@ -1,22 +1,22 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | (C) 2011 Christian Grothoff (and other contributing authors) | 3 | (C) 2011 Christian Grothoff (and other contributing authors) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 5 | GNUnet is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published | 6 | it under the terms of the GNU General Public License as published |
7 | by the Free Software Foundation; either version 3, or (at your | 7 | by the Free Software Foundation; either version 3, or (at your |
8 | option) any later version. | 8 | option) any later version. |
9 | 9 | ||
10 | GNUnet is distributed in the hope that it will be useful, but | 10 | GNUnet is distributed in the hope that it will be useful, but |
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | General Public License for more details. | 13 | General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU General Public License | 15 | You should have received a copy of the GNU General Public License |
16 | along with GNUnet; see the file COPYING. If not, write to the | 16 | along with GNUnet; see the file COPYING. If not, write to the |
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file ats/gnunet-service-ats-solver_ril.h | 22 | * @file ats/gnunet-service-ats-solver_ril.h |
@@ -34,7 +34,6 @@ | |||
34 | * General description | 34 | * General description |
35 | */ | 35 | */ |
36 | 36 | ||
37 | |||
38 | /** | 37 | /** |
39 | * Changes the preferences for a peer in the problem | 38 | * Changes the preferences for a peer in the problem |
40 | * | 39 | * |
@@ -45,10 +44,9 @@ | |||
45 | */ | 44 | */ |
46 | void | 45 | void |
47 | GAS_ril_address_change_preference (void *solver, | 46 | GAS_ril_address_change_preference (void *solver, |
48 | const struct GNUNET_PeerIdentity *peer, | 47 | const struct GNUNET_PeerIdentity *peer, |
49 | enum GNUNET_ATS_PreferenceKind kind, | 48 | enum GNUNET_ATS_PreferenceKind kind, |
50 | double pref_rel); | 49 | double pref_rel); |
51 | |||
52 | 50 | ||
53 | /** | 51 | /** |
54 | * Init the reinforcement learning problem solver | 52 | * Init the reinforcement learning problem solver |
@@ -81,18 +79,18 @@ GAS_ril_address_change_preference (void *solver, | |||
81 | */ | 79 | */ |
82 | void * | 80 | void * |
83 | GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg, | 81 | GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg, |
84 | const struct GNUNET_STATISTICS_Handle *stats, | 82 | const struct GNUNET_STATISTICS_Handle *stats, |
85 | const struct GNUNET_CONTAINER_MultiHashMap *addresses, | 83 | const struct GNUNET_CONTAINER_MultiHashMap *addresses, |
86 | int *network, | 84 | int *network, |
87 | unsigned long long *out_quota, | 85 | unsigned long long *out_quota, |
88 | unsigned long long *in_quota, | 86 | unsigned long long *in_quota, |
89 | int dest_length, | 87 | int dest_length, |
90 | GAS_bandwidth_changed_cb bw_changed_cb, | 88 | GAS_bandwidth_changed_cb bw_changed_cb, |
91 | void *bw_changed_cb_cls, | 89 | void *bw_changed_cb_cls, |
92 | GAS_get_preferences get_preference, | 90 | GAS_get_preferences get_preference, |
93 | void *get_preference_cls, | 91 | void *get_preference_cls, |
94 | GAS_get_properties get_properties, | 92 | GAS_get_properties get_properties, |
95 | void *get_properties_cls); | 93 | void *get_properties_cls); |
96 | 94 | ||
97 | /** | 95 | /** |
98 | * Shutdown the reinforcement learning problem solver | 96 | * Shutdown the reinforcement learning problem solver |
@@ -102,7 +100,6 @@ GAS_ril_init (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
102 | void | 100 | void |
103 | GAS_ril_done (void * solver); | 101 | GAS_ril_done (void * solver); |
104 | 102 | ||
105 | |||
106 | /** | 103 | /** |
107 | * Add a single address within a network to the solver | 104 | * Add a single address within a network to the solver |
108 | * | 105 | * |
@@ -112,9 +109,8 @@ GAS_ril_done (void * solver); | |||
112 | */ | 109 | */ |
113 | void | 110 | void |
114 | GAS_ril_address_add (void *solver, | 111 | GAS_ril_address_add (void *solver, |
115 | struct ATS_Address *address, | 112 | struct ATS_Address *address, |
116 | uint32_t network); | 113 | uint32_t network); |
117 | |||
118 | 114 | ||
119 | /** | 115 | /** |
120 | * Transport properties for this address have changed | 116 | * Transport properties for this address have changed |
@@ -127,11 +123,10 @@ GAS_ril_address_add (void *solver, | |||
127 | */ | 123 | */ |
128 | void | 124 | void |
129 | GAS_ril_address_property_changed (void *solver, | 125 | GAS_ril_address_property_changed (void *solver, |
130 | struct ATS_Address *address, | 126 | struct ATS_Address *address, |
131 | uint32_t type, | 127 | uint32_t type, |
132 | uint32_t abs_value, | 128 | uint32_t abs_value, |
133 | double rel_value); | 129 | double rel_value); |
134 | |||
135 | 130 | ||
136 | /** | 131 | /** |
137 | * Transport session for this address has changed | 132 | * Transport session for this address has changed |
@@ -145,10 +140,9 @@ GAS_ril_address_property_changed (void *solver, | |||
145 | */ | 140 | */ |
146 | void | 141 | void |
147 | GAS_ril_address_session_changed (void *solver, | 142 | GAS_ril_address_session_changed (void *solver, |
148 | struct ATS_Address *address, | 143 | struct ATS_Address *address, |
149 | uint32_t cur_session, | 144 | uint32_t cur_session, |
150 | uint32_t new_session); | 145 | uint32_t new_session); |
151 | |||
152 | 146 | ||
153 | /** | 147 | /** |
154 | * Usage for this address has changed | 148 | * Usage for this address has changed |
@@ -161,8 +155,8 @@ GAS_ril_address_session_changed (void *solver, | |||
161 | */ | 155 | */ |
162 | void | 156 | void |
163 | GAS_ril_address_inuse_changed (void *solver, | 157 | GAS_ril_address_inuse_changed (void *solver, |
164 | struct ATS_Address *address, | 158 | struct ATS_Address *address, |
165 | int in_use); | 159 | int in_use); |
166 | 160 | ||
167 | /** | 161 | /** |
168 | * Network scope for this address has changed | 162 | * Network scope for this address has changed |
@@ -176,9 +170,9 @@ GAS_ril_address_inuse_changed (void *solver, | |||
176 | */ | 170 | */ |
177 | void | 171 | void |
178 | GAS_ril_address_change_network (void *solver, | 172 | GAS_ril_address_change_network (void *solver, |
179 | struct ATS_Address *address, | 173 | struct ATS_Address *address, |
180 | uint32_t current_network, | 174 | uint32_t current_network, |
181 | uint32_t new_network); | 175 | uint32_t new_network); |
182 | 176 | ||
183 | /** | 177 | /** |
184 | * Remove an address from the solver | 178 | * Remove an address from the solver |
@@ -189,7 +183,8 @@ GAS_ril_address_change_network (void *solver, | |||
189 | */ | 183 | */ |
190 | void | 184 | void |
191 | GAS_ril_address_delete (void *solver, | 185 | GAS_ril_address_delete (void *solver, |
192 | struct ATS_Address *address, int session_only); | 186 | struct ATS_Address *address, |
187 | int session_only); | ||
193 | 188 | ||
194 | /** | 189 | /** |
195 | * Get application feedback for a peer | 190 | * Get application feedback for a peer |
@@ -203,11 +198,11 @@ GAS_ril_address_delete (void *solver, | |||
203 | */ | 198 | */ |
204 | void | 199 | void |
205 | GAS_ril_address_preference_feedback (void *solver, | 200 | GAS_ril_address_preference_feedback (void *solver, |
206 | void *application, | 201 | void *application, |
207 | const struct GNUNET_PeerIdentity *peer, | 202 | const struct GNUNET_PeerIdentity *peer, |
208 | const struct GNUNET_TIME_Relative scope, | 203 | const struct GNUNET_TIME_Relative scope, |
209 | enum GNUNET_ATS_PreferenceKind kind, | 204 | enum GNUNET_ATS_PreferenceKind kind, |
210 | double score); | 205 | double score); |
211 | 206 | ||
212 | /** | 207 | /** |
213 | * Start a bulk operation | 208 | * Start a bulk operation |
@@ -217,7 +212,6 @@ GAS_ril_address_preference_feedback (void *solver, | |||
217 | void | 212 | void |
218 | GAS_ril_bulk_start (void *solver); | 213 | GAS_ril_bulk_start (void *solver); |
219 | 214 | ||
220 | |||
221 | /** | 215 | /** |
222 | * Bulk operation done | 216 | * Bulk operation done |
223 | */ | 217 | */ |
@@ -232,8 +226,7 @@ GAS_ril_bulk_stop (void *solver); | |||
232 | */ | 226 | */ |
233 | void | 227 | void |
234 | GAS_ril_stop_get_preferred_address (void *solver, | 228 | GAS_ril_stop_get_preferred_address (void *solver, |
235 | const struct GNUNET_PeerIdentity *peer); | 229 | const struct GNUNET_PeerIdentity *peer); |
236 | |||
237 | 230 | ||
238 | /** | 231 | /** |
239 | * Get the prefered address for a specific peer | 232 | * Get the prefered address for a specific peer |
@@ -243,6 +236,6 @@ GAS_ril_stop_get_preferred_address (void *solver, | |||
243 | */ | 236 | */ |
244 | const struct ATS_Address * | 237 | const struct ATS_Address * |
245 | GAS_ril_get_preferred_address (void *solver, | 238 | GAS_ril_get_preferred_address (void *solver, |
246 | const struct GNUNET_PeerIdentity *peer); | 239 | const struct GNUNET_PeerIdentity *peer); |
247 | 240 | ||
248 | /* end of gnunet-service-ats-solver_ril.h */ | 241 | /* end of gnunet-service-ats-solver_ril.h */ |