plugin_ats_ril.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011-2014 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file ats/plugin_ats_ril.c
  19. * @brief ATS reinforcement learning solver
  20. * @author Fabian Oehlmann
  21. * @author Matthias Wachs
  22. */
  23. #include "platform.h"
  24. #include <float.h>
  25. #include <math.h>
  26. #include "gnunet_ats_plugin.h"
  27. #include "gnunet-service-ats_addresses.h"
  28. #define LOG(kind,...) GNUNET_log_from (kind, "ats-ril",__VA_ARGS__)
  29. #define RIL_MIN_BW (5 * ntohl (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT.value__))
  30. #define RIL_MAX_BW GNUNET_ATS_MaxBandwidth
  31. #define RIL_ACTION_INVALID -1
  32. #define RIL_INTERVAL_EXPONENT 10
  33. #define RIL_UTILITY_DELAY_MAX 1000
  34. #define RIL_DEFAULT_STEP_TIME_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 200)
  35. #define RIL_DEFAULT_STEP_TIME_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 2000)
  36. #define RIL_DEFAULT_ALGORITHM RIL_ALGO_SARSA
  37. #define RIL_DEFAULT_SELECT RIL_SELECT_SOFTMAX
  38. #define RIL_DEFAULT_WELFARE RIL_WELFARE_NASH
  39. #define RIL_DEFAULT_DISCOUNT_BETA 0.6
  40. #define RIL_DEFAULT_DISCOUNT_GAMMA 0.5
  41. #define RIL_DEFAULT_GRADIENT_STEP_SIZE 0.01
  42. #define RIL_DEFAULT_TRACE_DECAY 0.5
  43. #define RIL_DEFAULT_EXPLORE_RATIO 1
  44. #define RIL_DEFAULT_EXPLORE_DECAY 0.95
  45. #define RIL_DEFAULT_RBF_DIVISOR 50
  46. #define RIL_DEFAULT_TEMPERATURE 0.1
  47. #define RIL_DEFAULT_TEMPERATURE_DECAY 1
  48. #define RIL_INC_DEC_STEP_SIZE 1
  49. #define RIL_NOP_DECAY 0.5
  50. /**
  51. * ATS reinforcement learning solver
  52. *
  53. * General description
  54. */
  55. /**
  56. * The actions, how an agent can manipulate the current assignment. I.e. how the bandwidth can be
  57. * changed for the currently chosen address. Not depicted in the enum are the actions of switching
  58. * to a particular address. The action of switching to address with index i is depicted by the
  59. * number (RIL_ACTION_TYPE_NUM + i).
  60. */
  61. enum RIL_Action_Type
  62. {
  63. RIL_ACTION_NOTHING = 0,
  64. RIL_ACTION_BW_IN_DBL = -2, //TODO? Potentially add more actions
  65. RIL_ACTION_BW_IN_HLV = -3,
  66. RIL_ACTION_BW_IN_INC = 1,
  67. RIL_ACTION_BW_IN_DEC = 2,
  68. RIL_ACTION_BW_OUT_DBL = -4,
  69. RIL_ACTION_BW_OUT_HLV = -5,
  70. RIL_ACTION_BW_OUT_INC = 3,
  71. RIL_ACTION_BW_OUT_DEC = 4,
  72. RIL_ACTION_TYPE_NUM = 5
  73. };
  74. enum RIL_Algorithm
  75. {
  76. RIL_ALGO_SARSA = 0,
  77. RIL_ALGO_Q = 1
  78. };
  79. enum RIL_Select
  80. {
  81. RIL_SELECT_SOFTMAX = 0,
  82. RIL_SELECT_EGREEDY = 1
  83. };
  84. enum RIL_Welfare
  85. {
  86. RIL_WELFARE_NASH,
  87. RIL_WELFARE_EGALITARIAN
  88. };
  89. enum RIL_E_Modification
  90. {
  91. RIL_E_DECAY,
  92. RIL_E_ZERO,
  93. RIL_E_ACCUMULATE,
  94. RIL_E_REPLACE
  95. };
  96. /**
  97. * Global learning parameters
  98. */
  99. struct RIL_Learning_Parameters
  100. {
  101. /**
  102. * The TD-algorithm to use
  103. */
  104. enum RIL_Algorithm algorithm;
  105. /**
  106. * Gradient-descent step-size
  107. */
  108. double alpha;
  109. /**
  110. * Learning discount variable in the TD-update for semi-MDPs
  111. */
  112. double beta;
  113. /**
  114. * Learning discount factor in the TD-update for MDPs
  115. */
  116. double gamma;
  117. /**
  118. * Trace-decay factor for eligibility traces
  119. */
  120. double lambda;
  121. /**
  122. * Whether to accumulate or replace eligibility traces
  123. */
  124. enum RIL_E_Modification eligibility_trace_mode;
  125. /**
  126. * Initial softmax action-selection temperature
  127. */
  128. double temperature_init;
  129. /**
  130. * Softmax action-selection temperature
  131. */
  132. double temperature;
  133. /**
  134. * Decay factor of the temperature value
  135. */
  136. double temperature_decay;
  137. /**
  138. * Which measure of social welfare should be used
  139. */
  140. enum RIL_Welfare social_welfare;
  141. /**
  142. * State space divisor
  143. */
  144. unsigned long long rbf_divisor;
  145. /**
  146. * Action selection strategy;
  147. */
  148. enum RIL_Select select;
  149. /**
  150. * Initial exploration ratio value
  151. */
  152. double epsilon_init;
  153. /**
  154. * Ratio, with what probability an agent should explore in the e-greed policy
  155. */
  156. double epsilon;
  157. /**
  158. * Decay factor of the explore ratio
  159. */
  160. double epsilon_decay;
  161. /**
  162. * Minimal interval time between steps in milliseconds
  163. */
  164. struct GNUNET_TIME_Relative step_time_min;
  165. /**
  166. * Maximum interval time between steps in milliseconds
  167. */
  168. struct GNUNET_TIME_Relative step_time_max;
  169. };
  170. /**
  171. * Wrapper for addresses to store them in agent's linked list
  172. */
  173. struct RIL_Address_Wrapped
  174. {
  175. /**
  176. * Next in DLL
  177. */
  178. struct RIL_Address_Wrapped *next;
  179. /**
  180. * Previous in DLL
  181. */
  182. struct RIL_Address_Wrapped *prev;
  183. /**
  184. * The address
  185. */
  186. struct ATS_Address *address_naked;
  187. };
  188. struct RIL_Peer_Agent
  189. {
  190. /**
  191. * Next agent in solver's linked list
  192. */
  193. struct RIL_Peer_Agent *next;
  194. /**
  195. * Previous agent in solver's linked list
  196. */
  197. struct RIL_Peer_Agent *prev;
  198. /**
  199. * Environment handle
  200. */
  201. struct GAS_RIL_Handle *envi;
  202. /**
  203. * Peer ID
  204. */
  205. struct GNUNET_PeerIdentity peer;
  206. /**
  207. * Whether the agent is active or not
  208. */
  209. int is_active;
  210. /**
  211. * Number of performed time-steps
  212. */
  213. unsigned long long step_count;
  214. /**
  215. * Experience matrix W
  216. */
  217. double ** W;
  218. /**
  219. * Number of rows of W / Number of state-vector features
  220. */
  221. unsigned int m;
  222. /**
  223. * Number of columns of W / Number of actions
  224. */
  225. unsigned int n;
  226. /**
  227. * Last perceived state feature vector
  228. */
  229. double *s_old;
  230. /**
  231. * Last chosen action
  232. */
  233. int a_old;
  234. /**
  235. * Eligibility traces
  236. */
  237. double ** E;
  238. /**
  239. * Whether to reset the eligibility traces to 0 after a Q-exploration step
  240. */
  241. int eligibility_reset;
  242. /**
  243. * Address in use
  244. */
  245. struct ATS_Address *address_inuse;
  246. /**
  247. * Head of addresses DLL
  248. */
  249. struct RIL_Address_Wrapped *addresses_head;
  250. /**
  251. * Tail of addresses DLL
  252. */
  253. struct RIL_Address_Wrapped *addresses_tail;
  254. /**
  255. * Inbound bandwidth assigned by the agent
  256. */
  257. uint32_t bw_in;
  258. /**
  259. * Outbound bandwidth assigned by the agent
  260. */
  261. uint32_t bw_out;
  262. /**
  263. * Flag whether a suggestion has to be issued
  264. */
  265. int suggestion_issue;
  266. /**
  267. * The address which has to be issued
  268. */
  269. struct ATS_Address *suggestion_address;
  270. /**
  271. * The agent's last objective value
  272. */
  273. double objective_old;
  274. /**
  275. * NOP bonus
  276. */
  277. double nop_bonus;
  278. };
  279. struct RIL_Scope
  280. {
  281. /**
  282. * ATS network type
  283. */
  284. enum GNUNET_ATS_Network_Type type;
  285. /**
  286. * Total available inbound bandwidth
  287. */
  288. uint32_t bw_in_available;
  289. /**
  290. * Bandwidth inbound assigned in network after last step
  291. */
  292. uint32_t bw_in_assigned;
  293. /**
  294. * Bandwidth inbound actually utilized in the network
  295. */
  296. uint32_t bw_in_utilized;
  297. /**
  298. * Total available outbound bandwidth
  299. */
  300. uint32_t bw_out_available;
  301. /**
  302. * Bandwidth outbound assigned in network after last step
  303. */
  304. unsigned long long bw_out_assigned;
  305. /**
  306. * Bandwidth outbound actually utilized in the network
  307. */
  308. unsigned long long bw_out_utilized;
  309. /**
  310. * Number of active agents in scope
  311. */
  312. unsigned int active_agent_count;
  313. /**
  314. * The social welfare achieved in the scope
  315. */
  316. double social_welfare;
  317. };
  318. /**
  319. * A handle for the reinforcement learning solver
  320. */
  321. struct GAS_RIL_Handle
  322. {
  323. /**
  324. * The solver-plugin environment of the solver-plugin API
  325. */
  326. struct GNUNET_ATS_PluginEnvironment *env;
  327. /**
  328. * Number of performed steps
  329. */
  330. unsigned long long step_count;
  331. /**
  332. * Timestamp for the last time-step
  333. */
  334. struct GNUNET_TIME_Absolute step_time_last;
  335. /**
  336. * Task identifier of the next time-step to be executed
  337. */
  338. struct GNUNET_SCHEDULER_Task * step_next_task_id;
  339. /**
  340. * Variable discount factor, dependent on time between steps
  341. */
  342. double global_discount_variable;
  343. /**
  344. * Integrated variable discount factor, dependent on time between steps
  345. */
  346. double global_discount_integrated;
  347. /**
  348. * Lock for bulk operations
  349. */
  350. int bulk_lock;
  351. /**
  352. * Number of changes during a lock
  353. */
  354. int bulk_changes;
  355. /**
  356. * Learning parameters
  357. */
  358. struct RIL_Learning_Parameters parameters;
  359. /**
  360. * Array of networks with global assignment state
  361. */
  362. struct RIL_Scope * network_entries;
  363. /**
  364. * Networks count
  365. */
  366. unsigned int networks_count;
  367. /**
  368. * List of active peer-agents
  369. */
  370. struct RIL_Peer_Agent * agents_head;
  371. struct RIL_Peer_Agent * agents_tail;
  372. /**
  373. * Shutdown
  374. */
  375. int done;
  376. /**
  377. * Simulate steps, i.e. schedule steps immediately
  378. */
  379. unsigned long long simulate;
  380. };
  381. /*
  382. * "Private" functions
  383. * ---------------------------
  384. */
  385. /**
  386. * Estimate the current action-value for state s and action a
  387. *
  388. * @param agent agent performing the estimation
  389. * @param state s
  390. * @param action a
  391. * @return estimation value
  392. */
  393. static double
  394. agent_q (struct RIL_Peer_Agent *agent,
  395. const double *state,
  396. int action)
  397. {
  398. unsigned int i;
  399. double result = 0.0;
  400. for (i = 0; i < agent->m; i++)
  401. result += state[i] * agent->W[action][i];
  402. /* prevent crashes if learning diverges */
  403. if (isnan(result))
  404. return isnan(result) * UINT32_MAX;
  405. if (isinf(result))
  406. return isinf(result) * UINT32_MAX;
  407. return result;
  408. }
  409. /**
  410. * Get the index of the address in the agent's list.
  411. *
  412. * @param agent agent handle
  413. * @param address address handle
  414. * @return the index, starting with zero
  415. */
  416. static int
  417. agent_address_get_index (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
  418. {
  419. int i;
  420. struct RIL_Address_Wrapped *cur;
  421. i = -1;
  422. for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
  423. {
  424. i++;
  425. if (cur->address_naked == address)
  426. return i;
  427. }
  428. return i;
  429. }
  430. /**
  431. * Gets the wrapped address from the agent's list
  432. *
  433. * @param agent agent handle
  434. * @param address address handle
  435. * @return wrapped address
  436. */
  437. static struct RIL_Address_Wrapped *
  438. agent_address_get_wrapped (struct RIL_Peer_Agent *agent, struct ATS_Address *address)
  439. {
  440. struct RIL_Address_Wrapped *cur;
  441. for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
  442. if (cur->address_naked == address)
  443. return cur;
  444. return NULL;
  445. }
  446. static int
  447. agent_action_is_possible (struct RIL_Peer_Agent *agent, int action)
  448. {
  449. int address_index;
  450. switch (action)
  451. {
  452. case RIL_ACTION_NOTHING:
  453. return GNUNET_YES;
  454. break;
  455. case RIL_ACTION_BW_IN_INC:
  456. case RIL_ACTION_BW_IN_DBL:
  457. if (agent->bw_in >= RIL_MAX_BW)
  458. return GNUNET_NO;
  459. else
  460. return GNUNET_YES;
  461. break;
  462. case RIL_ACTION_BW_IN_DEC:
  463. case RIL_ACTION_BW_IN_HLV:
  464. if (agent->bw_in <= 0)
  465. return GNUNET_NO;
  466. else
  467. return GNUNET_YES;
  468. break;
  469. case RIL_ACTION_BW_OUT_INC:
  470. case RIL_ACTION_BW_OUT_DBL:
  471. if (agent->bw_out >= RIL_MAX_BW)
  472. return GNUNET_NO;
  473. else
  474. return GNUNET_YES;
  475. break;
  476. case RIL_ACTION_BW_OUT_DEC:
  477. case RIL_ACTION_BW_OUT_HLV:
  478. if (agent->bw_out <= 0)
  479. return GNUNET_NO;
  480. else
  481. return GNUNET_YES;
  482. break;
  483. default:
  484. if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
  485. {
  486. address_index = action - RIL_ACTION_TYPE_NUM;
  487. GNUNET_assert(address_index >= 0);
  488. GNUNET_assert(
  489. address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
  490. if ((agent_address_get_index(agent, agent->address_inuse) == address_index) ||
  491. agent->address_inuse->active)
  492. return GNUNET_NO;
  493. else
  494. return GNUNET_YES;
  495. break;
  496. }
  497. // error - action does not exist
  498. GNUNET_assert(GNUNET_NO);
  499. }
  500. }
  501. /**
  502. * Gets the action, with the maximal estimated Q-value (i.e. the one currently estimated to bring the
  503. * most reward in the future)
  504. *
  505. * @param agent agent performing the calculation
  506. * @param state the state from which to take the action
  507. * @return the action promising most future reward
  508. */
  509. static int
  510. agent_get_action_max (struct RIL_Peer_Agent *agent, double *state)
  511. {
  512. int i;
  513. int max_i = RIL_ACTION_INVALID;
  514. double cur_q;
  515. double max_q = -DBL_MAX;
  516. for (i = 0; i < agent->n; i++)
  517. {
  518. if (agent_action_is_possible(agent, i))
  519. {
  520. cur_q = agent_q (agent, state, i);
  521. if (cur_q > max_q)
  522. {
  523. max_q = cur_q;
  524. max_i = i;
  525. }
  526. }
  527. }
  528. GNUNET_assert(RIL_ACTION_INVALID != max_i);
  529. return max_i;
  530. }
  531. /**
  532. * Chooses a random action from the set of possible ones
  533. *
  534. * @param agent the agent performing the action
  535. * @return the action index
  536. */
  537. static int
  538. agent_get_action_random (struct RIL_Peer_Agent *agent)
  539. {
  540. int i;
  541. int is_possible[agent->n];
  542. int sum = 0;
  543. int r;
  544. for (i = 0; i<agent->n; i++)
  545. {
  546. if (agent_action_is_possible(agent, i))
  547. {
  548. is_possible[i] = GNUNET_YES;
  549. sum++;
  550. }
  551. else
  552. {
  553. is_possible[i] = GNUNET_NO;
  554. }
  555. }
  556. r = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, sum);
  557. sum = -1;
  558. for (i = 0; i<agent->n; i++)
  559. {
  560. if (is_possible[i])
  561. {
  562. sum++;
  563. if (sum == r)
  564. return i;
  565. }
  566. }
  567. GNUNET_assert(GNUNET_NO);
  568. return RIL_ACTION_INVALID;
  569. }
  570. /**
  571. * Updates the weights (i.e. coefficients) of the weight vector in matrix W for action a
  572. *
  573. * @param agent the agent performing the update
  574. * @param reward the reward received for the last action
  575. * @param s_next the new state, the last step got the agent into
  576. * @param a_prime the new
  577. */
  578. static void
  579. agent_update (struct RIL_Peer_Agent *agent, double reward, double *s_next, int a_prime)
  580. {
  581. int i;
  582. int k;
  583. double delta;
  584. double **theta = agent->W;
  585. delta = agent->envi->global_discount_integrated * reward; //reward
  586. delta += agent->envi->global_discount_variable * agent_q (agent, s_next, a_prime); //discounted future value
  587. delta -= agent_q (agent, agent->s_old, agent->a_old); //one step
  588. // LOG(GNUNET_ERROR_TYPE_INFO, "update() Step# %llu Q(s,a): %f a: %f r: %f y: %f Q(s+1,a+1) = %f delta: %f\n",
  589. // agent->step_count,
  590. // agent_q (agent, agent->s_old, agent->a_old),
  591. // agent->envi->parameters.alpha,
  592. // reward,
  593. // agent->envi->global_discount_variable,
  594. // agent_q (agent, s_next, a_prime),
  595. // delta);
  596. for (k = 0; k < agent->n; k++)
  597. {
  598. for (i = 0; i < agent->m; i++)
  599. {
  600. // LOG(GNUNET_ERROR_TYPE_INFO, "alpha = %f delta = %f e[%d] = %f\n",
  601. // agent->envi->parameters.alpha,
  602. // delta,
  603. // i,
  604. // agent->e[i]);
  605. theta[k][i] += agent->envi->parameters.alpha * delta * agent->E[k][i];
  606. }
  607. }
  608. }
  609. /**
  610. * Changes the eligibility trace vector e in various manners:
  611. * #RIL_E_ACCUMULATE - adds @a feature to each component as in accumulating eligibility traces
  612. * #RIL_E_REPLACE - resets each component to @a feature as in replacing traces
  613. * #RIL_E_DECAY - multiplies e with discount factor and lambda as in the update rule
  614. * #RIL_E_ZERO - sets e to 0 as in Watkin's Q-learning algorithm when exploring and when initializing
  615. *
  616. * @param agent the agent handle
  617. * @param mod the kind of modification
  618. * @param feature the feature vector
  619. * @param action the action to take
  620. */
  621. static void
  622. agent_modify_eligibility (struct RIL_Peer_Agent *agent,
  623. enum RIL_E_Modification mod,
  624. double *feature,
  625. int action)
  626. {
  627. int i;
  628. int k;
  629. for (i = 0; i < agent->m; i++)
  630. {
  631. switch (mod)
  632. {
  633. case RIL_E_ACCUMULATE:
  634. agent->E[action][i] += feature[i];
  635. break;
  636. case RIL_E_REPLACE:
  637. agent->E[action][i] = agent->E[action][i] > feature[i] ? agent->E[action][i] : feature[i];
  638. break;
  639. case RIL_E_DECAY:
  640. for (k = 0; k < agent->n; k++)
  641. {
  642. agent->E[k][i] *= agent->envi->global_discount_variable * agent->envi->parameters.lambda;
  643. }
  644. break;
  645. case RIL_E_ZERO:
  646. for (k = 0; k < agent->n; k++)
  647. {
  648. agent->E[k][i] = 0;
  649. }
  650. break;
  651. }
  652. }
  653. }
  654. /**
  655. * Informs the environment about the status of the solver
  656. *
  657. * @param solver
  658. * @param op
  659. * @param stat
  660. */
  661. static void
  662. ril_inform (struct GAS_RIL_Handle *solver,
  663. enum GAS_Solver_Operation op,
  664. enum GAS_Solver_Status stat)
  665. {
  666. solver->env->info_cb (solver->env->cls,
  667. op,
  668. stat,
  669. GAS_INFO_NONE);
  670. }
  671. /**
  672. * Calculates the maximum bandwidth an agent can assign in a network scope
  673. *
  674. * @param net
  675. */
  676. static unsigned long long
  677. ril_get_max_bw (struct RIL_Scope *net)
  678. {
  679. return GNUNET_MIN(2 * GNUNET_MAX(net->bw_in_available, net->bw_out_available), GNUNET_ATS_MaxBandwidth);
  680. }
  681. /**
  682. * Changes the active assignment suggestion of the handler and invokes the bw_changed callback to
  683. * notify ATS of its new decision
  684. *
  685. * @param solver solver handle
  686. * @param agent agent handle
  687. * @param new_address the address which is to be used
  688. * @param new_bw_in the new amount of inbound bandwidth set for this address
  689. * @param new_bw_out the new amount of outbound bandwidth set for this address
  690. * @param silent disables invocation of the bw_changed callback, if #GNUNET_YES
  691. */
  692. static void
  693. envi_set_active_suggestion (struct GAS_RIL_Handle *solver,
  694. struct RIL_Peer_Agent *agent,
  695. struct ATS_Address *new_address,
  696. unsigned long long new_bw_in,
  697. unsigned long long new_bw_out,
  698. int silent)
  699. {
  700. int notify = GNUNET_NO;
  701. LOG(GNUNET_ERROR_TYPE_DEBUG,
  702. " set_active_suggestion() for peer '%s'\n",
  703. GNUNET_i2s (&agent->peer));
  704. //address change
  705. if (agent->address_inuse != new_address)
  706. {
  707. if (NULL != agent->address_inuse)
  708. {
  709. agent->address_inuse->active = GNUNET_NO;
  710. agent->address_inuse->assigned_bw_in = 0;
  711. agent->address_inuse->assigned_bw_out = 0;
  712. }
  713. if (NULL != new_address)
  714. {
  715. LOG(GNUNET_ERROR_TYPE_DEBUG, " set address active: %s\n", agent->is_active ? "yes" : "no");
  716. new_address->active = agent->is_active;
  717. new_address->assigned_bw_in = agent->bw_in;
  718. new_address->assigned_bw_out = agent->bw_out;
  719. }
  720. notify |= GNUNET_YES;
  721. }
  722. if (new_address)
  723. {
  724. //activity change
  725. if (new_address->active != agent->is_active)
  726. {
  727. new_address->active = agent->is_active;
  728. notify |= GNUNET_YES;
  729. }
  730. //bw change
  731. if (agent->bw_in != new_bw_in)
  732. {
  733. agent->bw_in = new_bw_in;
  734. new_address->assigned_bw_in = new_bw_in;
  735. notify |= GNUNET_YES;
  736. }
  737. if (agent->bw_out != new_bw_out)
  738. {
  739. agent->bw_out = new_bw_out;
  740. new_address->assigned_bw_out = new_bw_out;
  741. notify |= GNUNET_YES;
  742. }
  743. }
  744. if (notify && agent->is_active && (GNUNET_NO == silent))
  745. {
  746. if (new_address)
  747. {
  748. LOG(GNUNET_ERROR_TYPE_DEBUG, " envi_set_active_suggestion() notify\n");
  749. agent->suggestion_issue = GNUNET_YES;
  750. agent->suggestion_address = new_address;
  751. }
  752. else if (agent->address_inuse)
  753. {
  754. /* disconnect case, no new address */
  755. GNUNET_assert(0 == agent->address_inuse->assigned_bw_in);
  756. GNUNET_assert(0 == agent->address_inuse->assigned_bw_out);
  757. agent->bw_in = 0;
  758. agent->bw_out = 0;
  759. agent->suggestion_issue = GNUNET_YES;
  760. agent->suggestion_address = agent->address_inuse;
  761. }
  762. }
  763. agent->address_inuse = new_address;
  764. }
  765. /**
  766. * Allocates a state vector and fills it with the features present
  767. * @param solver the solver handle
  768. * @param agent the agent handle
  769. * @return pointer to the state vector
  770. */
  771. static double *
  772. envi_get_state (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
  773. {
  774. double *state;
  775. double y[2];
  776. double x[2];
  777. double d[2];
  778. double sigma;
  779. double f;
  780. int m;
  781. int i;
  782. int k;
  783. unsigned long long max_bw;
  784. state = GNUNET_malloc (sizeof(double) * agent->m);
  785. max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
  786. y[0] = (double) agent->bw_out;
  787. y[1] = (double) agent->bw_in;
  788. m = agent_address_get_index (agent, agent->address_inuse) * (solver->parameters.rbf_divisor+1) * (solver->parameters.rbf_divisor+1);
  789. for (i = 0; i <= solver->parameters.rbf_divisor; i++)
  790. {
  791. for (k = 0; k <= solver->parameters.rbf_divisor; k++)
  792. {
  793. x[0] = (double) i * (double) max_bw / (double) solver->parameters.rbf_divisor;
  794. x[1] = (double) k * (double) max_bw / (double) solver->parameters.rbf_divisor;
  795. d[0] = x[0]-y[0];
  796. d[1] = x[1]-y[1];
  797. sigma = (((double) max_bw / ((double) solver->parameters.rbf_divisor + 1)) * 0.5);
  798. f = exp(-((d[0]*d[0] + d[1]*d[1]) / (2 * sigma * sigma)));
  799. state[m++] = f;
  800. }
  801. }
  802. return state;
  803. }
  804. /**
  805. * Returns the utility value of the connection an agent manages
  806. *
  807. * @param agent the agent in question
  808. * @return the utility value
  809. */
  810. static double
  811. agent_get_utility (struct RIL_Peer_Agent *agent)
  812. {
  813. const double *preferences;
  814. double delay_atsi;
  815. double delay_norm;
  816. double pref_match;
  817. preferences = agent->envi->env->get_preferences (agent->envi->env->cls,
  818. &agent->peer);
  819. delay_atsi = agent->address_inuse->norm_delay.norm;
  820. delay_norm = RIL_UTILITY_DELAY_MAX*exp(-delay_atsi*0.00001);
  821. pref_match = preferences[GNUNET_ATS_PREFERENCE_LATENCY] * delay_norm;
  822. pref_match += preferences[GNUNET_ATS_PREFERENCE_BANDWIDTH] *
  823. sqrt((double) (agent->bw_in/RIL_MIN_BW) * (double) (agent->bw_out/RIL_MIN_BW));
  824. return pref_match;
  825. }
  826. /**
  827. * Calculates the social welfare within a network scope according to what social
  828. * welfare measure is set in the configuration.
  829. *
  830. * @param solver the solver handle
  831. * @param scope the network scope in question
  832. * @return the social welfare value
  833. */
  834. static double
  835. ril_network_get_social_welfare (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
  836. {
  837. struct RIL_Peer_Agent *cur;
  838. double result;
  839. switch (solver->parameters.social_welfare)
  840. {
  841. case RIL_WELFARE_EGALITARIAN:
  842. result = DBL_MAX;
  843. for (cur = solver->agents_head; NULL != cur; cur = cur->next)
  844. {
  845. if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
  846. {
  847. result = GNUNET_MIN(result, agent_get_utility(cur));
  848. }
  849. }
  850. return result;
  851. case RIL_WELFARE_NASH:
  852. result = 0;
  853. for (cur = solver->agents_head; NULL != cur; cur = cur->next)
  854. {
  855. if (cur->is_active && cur->address_inuse && (cur->address_inuse->solver_information == scope))
  856. {
  857. result *= pow(agent_get_utility(cur), 1.0 / (double) scope->active_agent_count);
  858. }
  859. }
  860. return result;
  861. }
  862. GNUNET_assert(GNUNET_NO);
  863. return 1;
  864. }
  865. static double
  866. envi_get_penalty (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
  867. {
  868. struct RIL_Scope *net;
  869. unsigned long long over_max;
  870. unsigned long long over_in = 0;
  871. unsigned long long over_out = 0;
  872. net = agent->address_inuse->solver_information;
  873. if (net->bw_in_utilized > net->bw_in_available)
  874. {
  875. over_in = net->bw_in_utilized - net->bw_in_available;
  876. if (RIL_ACTION_BW_IN_INC == agent->a_old)
  877. {
  878. /* increase quadratically */
  879. over_in *= over_in;
  880. }
  881. }
  882. if (net->bw_out_utilized > net->bw_out_available)
  883. {
  884. over_out = net->bw_out_utilized - net->bw_out_available;
  885. if (RIL_ACTION_BW_OUT_INC == agent->a_old)
  886. {
  887. /* increase quadratically */
  888. over_out *= over_out;
  889. }
  890. }
  891. over_max = (over_in + over_out) / (RIL_MIN_BW * RIL_MIN_BW);
  892. return -1.0 * (double) over_max;
  893. }
  894. /**
  895. * Gets the reward for the last performed step, which is calculated in equal
  896. * parts from the local (the peer specific) and the global (for all peers
  897. * identical) reward.
  898. *
  899. * @param solver the solver handle
  900. * @param agent the agent handle
  901. * @return the reward
  902. */
  903. static double
  904. envi_get_reward (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
  905. {
  906. struct RIL_Scope *net;
  907. double objective;
  908. double delta;
  909. double steady;
  910. double penalty;
  911. double reward;
  912. net = agent->address_inuse->solver_information;
  913. penalty = envi_get_penalty(solver, agent);
  914. objective = (agent_get_utility (agent) + net->social_welfare) / 2;
  915. delta = objective - agent->objective_old;
  916. agent->objective_old = objective;
  917. if (delta != 0 && penalty == 0)
  918. {
  919. agent->nop_bonus = delta * RIL_NOP_DECAY;
  920. }
  921. else
  922. {
  923. agent->nop_bonus *= RIL_NOP_DECAY;
  924. }
  925. steady = (RIL_ACTION_NOTHING == agent->a_old) ? agent->nop_bonus : 0;
  926. reward = delta + steady;
  927. return reward + penalty;
  928. }
  929. /**
  930. * Doubles the bandwidth for the active address
  931. *
  932. * @param solver solver handle
  933. * @param agent agent handle
  934. * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise the outbound bandwidth
  935. */
  936. static void
  937. envi_action_bw_double (struct GAS_RIL_Handle *solver,
  938. struct RIL_Peer_Agent *agent,
  939. int direction_in)
  940. {
  941. unsigned long long new_bw;
  942. unsigned long long max_bw;
  943. max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
  944. if (direction_in)
  945. {
  946. new_bw = agent->bw_in * 2;
  947. if (new_bw < agent->bw_in || new_bw > max_bw)
  948. new_bw = max_bw;
  949. envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
  950. agent->bw_out, GNUNET_NO);
  951. }
  952. else
  953. {
  954. new_bw = agent->bw_out * 2;
  955. if (new_bw < agent->bw_out || new_bw > max_bw)
  956. new_bw = max_bw;
  957. envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
  958. new_bw, GNUNET_NO);
  959. }
  960. }
  961. /**
  962. * Cuts the bandwidth for the active address in half. The least amount of bandwidth suggested, is
  963. * the minimum bandwidth for a peer, in order to not invoke a disconnect.
  964. *
  965. * @param solver solver handle
  966. * @param agent agent handle
  967. * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
  968. * bandwidth
  969. */
  970. static void
  971. envi_action_bw_halven (struct GAS_RIL_Handle *solver,
  972. struct RIL_Peer_Agent *agent,
  973. int direction_in)
  974. {
  975. unsigned long long new_bw;
  976. if (direction_in)
  977. {
  978. new_bw = agent->bw_in / 2;
  979. if (new_bw <= 0 || new_bw > agent->bw_in)
  980. new_bw = 0;
  981. envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
  982. GNUNET_NO);
  983. }
  984. else
  985. {
  986. new_bw = agent->bw_out / 2;
  987. if (new_bw <= 0 || new_bw > agent->bw_out)
  988. new_bw = 0;
  989. envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
  990. GNUNET_NO);
  991. }
  992. }
  993. /**
  994. * Increases the bandwidth by 5 times the minimum bandwidth for the active address.
  995. *
  996. * @param solver solver handle
  997. * @param agent agent handle
  998. * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
  999. * bandwidth
  1000. */
  1001. static void
  1002. envi_action_bw_inc (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
  1003. {
  1004. unsigned long long new_bw;
  1005. unsigned long long max_bw;
  1006. max_bw = ril_get_max_bw((struct RIL_Scope *) agent->address_inuse->solver_information);
  1007. if (direction_in)
  1008. {
  1009. new_bw = agent->bw_in + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
  1010. if (new_bw < agent->bw_in || new_bw > max_bw)
  1011. new_bw = max_bw;
  1012. envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw,
  1013. agent->bw_out, GNUNET_NO);
  1014. }
  1015. else
  1016. {
  1017. new_bw = agent->bw_out + (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
  1018. if (new_bw < agent->bw_out || new_bw > max_bw)
  1019. new_bw = max_bw;
  1020. envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in,
  1021. new_bw, GNUNET_NO);
  1022. }
  1023. }
  1024. /**
  1025. * Decreases the bandwidth by 5 times the minimum bandwidth for the active address. The least amount
  1026. * of bandwidth suggested, is the minimum bandwidth for a peer, in order to not invoke a disconnect.
  1027. *
  1028. * @param solver solver handle
  1029. * @param agent agent handle
  1030. * @param direction_in if GNUNET_YES, change inbound bandwidth, otherwise change the outbound
  1031. * bandwidth
  1032. */
  1033. static void
  1034. envi_action_bw_dec (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int direction_in)
  1035. {
  1036. unsigned long long new_bw;
  1037. if (direction_in)
  1038. {
  1039. new_bw = agent->bw_in - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
  1040. if (new_bw <= 0 || new_bw > agent->bw_in)
  1041. new_bw = 0;
  1042. envi_set_active_suggestion (solver, agent, agent->address_inuse, new_bw, agent->bw_out,
  1043. GNUNET_NO);
  1044. }
  1045. else
  1046. {
  1047. new_bw = agent->bw_out - (RIL_INC_DEC_STEP_SIZE * RIL_MIN_BW);
  1048. if (new_bw <= 0 || new_bw > agent->bw_out)
  1049. new_bw = 0;
  1050. envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, new_bw,
  1051. GNUNET_NO);
  1052. }
  1053. }
  1054. /**
  1055. * Switches to the address given by its index
  1056. *
  1057. * @param solver solver handle
  1058. * @param agent agent handle
  1059. * @param address_index index of the address as it is saved in the agent's list, starting with zero
  1060. */
  1061. static void
  1062. envi_action_address_switch (struct GAS_RIL_Handle *solver,
  1063. struct RIL_Peer_Agent *agent,
  1064. unsigned int address_index)
  1065. {
  1066. struct RIL_Address_Wrapped *cur;
  1067. int i = 0;
  1068. //cur = agent_address_get_wrapped(agent, agent->address_inuse);
  1069. for (cur = agent->addresses_head; NULL != cur; cur = cur->next)
  1070. {
  1071. if (i == address_index)
  1072. {
  1073. envi_set_active_suggestion (solver, agent, cur->address_naked, agent->bw_in, agent->bw_out,
  1074. GNUNET_NO);
  1075. return;
  1076. }
  1077. i++;
  1078. }
  1079. //no address with address_index exists, in this case this action should not be callable
  1080. GNUNET_assert(GNUNET_NO);
  1081. }
  1082. /**
  1083. * Puts the action into effect by calling the according function
  1084. *
  1085. * @param solver the solver handle
  1086. * @param agent the action handle
  1087. * @param action the action to perform by the solver
  1088. */
  1089. static void
  1090. envi_do_action (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int action)
  1091. {
  1092. int address_index;
  1093. switch (action)
  1094. {
  1095. case RIL_ACTION_NOTHING:
  1096. break;
  1097. case RIL_ACTION_BW_IN_DBL:
  1098. envi_action_bw_double (solver, agent, GNUNET_YES);
  1099. break;
  1100. case RIL_ACTION_BW_IN_HLV:
  1101. envi_action_bw_halven (solver, agent, GNUNET_YES);
  1102. break;
  1103. case RIL_ACTION_BW_IN_INC:
  1104. envi_action_bw_inc (solver, agent, GNUNET_YES);
  1105. break;
  1106. case RIL_ACTION_BW_IN_DEC:
  1107. envi_action_bw_dec (solver, agent, GNUNET_YES);
  1108. break;
  1109. case RIL_ACTION_BW_OUT_DBL:
  1110. envi_action_bw_double (solver, agent, GNUNET_NO);
  1111. break;
  1112. case RIL_ACTION_BW_OUT_HLV:
  1113. envi_action_bw_halven (solver, agent, GNUNET_NO);
  1114. break;
  1115. case RIL_ACTION_BW_OUT_INC:
  1116. envi_action_bw_inc (solver, agent, GNUNET_NO);
  1117. break;
  1118. case RIL_ACTION_BW_OUT_DEC:
  1119. envi_action_bw_dec (solver, agent, GNUNET_NO);
  1120. break;
  1121. default:
  1122. if ((action >= RIL_ACTION_TYPE_NUM) && (action < agent->n)) //switch address action
  1123. {
  1124. address_index = action - RIL_ACTION_TYPE_NUM;
  1125. GNUNET_assert(address_index >= 0);
  1126. GNUNET_assert(
  1127. address_index <= agent_address_get_index (agent, agent->addresses_tail->address_naked));
  1128. envi_action_address_switch (solver, agent, address_index);
  1129. break;
  1130. }
  1131. // error - action does not exist
  1132. GNUNET_assert(GNUNET_NO);
  1133. }
  1134. }
  1135. /**
  1136. * Selects the next action using the e-greedy strategy. I.e. with a probability
  1137. * of (1-e) the action with the maximum expected return will be chosen
  1138. * (=> exploitation) and with probability (e) a random action will be chosen.
  1139. * In case the Q-learning rule is set, the function also resets the eligibility
  1140. * traces in the exploration case (after Watkin's Q-learning).
  1141. *
  1142. * @param agent the agent selecting an action
  1143. * @param state the current state-feature vector
  1144. * @return the action index
  1145. */
  1146. static int
  1147. agent_select_egreedy (struct RIL_Peer_Agent *agent, double *state)
  1148. {
  1149. int action;
  1150. double r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
  1151. UINT32_MAX) / (double) UINT32_MAX;
  1152. if (r < agent->envi->parameters.epsilon) //explore
  1153. {
  1154. action = agent_get_action_random(agent);
  1155. if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
  1156. {
  1157. agent->eligibility_reset = GNUNET_YES;
  1158. }
  1159. agent->envi->parameters.epsilon *= agent->envi->parameters.epsilon_decay;
  1160. return action;
  1161. }
  1162. else //exploit
  1163. {
  1164. action = agent_get_action_max(agent, state);
  1165. return action;
  1166. }
  1167. }
  1168. /**
  1169. * Selects the next action with a probability corresponding to its value. The
  1170. * probability is calculated using a Boltzmann distribution with a temperature
  1171. * value. The higher the temperature, the more are the action selection
  1172. * probabilities the same. With a temperature of 0, the selection is greedy,
  1173. * i.e. always the action with the highest value is chosen.
  1174. * @param agent
  1175. * @param state
  1176. * @return
  1177. */
  1178. static int
  1179. agent_select_softmax (struct RIL_Peer_Agent *agent, double *state)
  1180. {
  1181. int i;
  1182. int a_max;
  1183. double eqt[agent->n];
  1184. double p[agent->n];
  1185. double sum = 0;
  1186. double r;
  1187. a_max = agent_get_action_max(agent, state);
  1188. for (i=0; i<agent->n; i++)
  1189. {
  1190. if (agent_action_is_possible(agent, i))
  1191. {
  1192. eqt[i] = exp(agent_q(agent,state,i) / agent->envi->parameters.temperature);
  1193. if (isinf (eqt[i]))
  1194. eqt[i] = isinf(eqt[i]) * UINT32_MAX;
  1195. sum += eqt[i];
  1196. }
  1197. }
  1198. for (i=0; i<agent->n; i++)
  1199. {
  1200. if (agent_action_is_possible(agent, i))
  1201. {
  1202. p[i] = eqt[i]/sum;
  1203. }
  1204. else
  1205. {
  1206. p[i] = 0;
  1207. }
  1208. }
  1209. r = (double) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
  1210. UINT32_MAX) / (double) UINT32_MAX;
  1211. sum = 0;
  1212. for (i=0; i<agent->n; i++)
  1213. {
  1214. if (sum + p[i] > r)
  1215. {
  1216. if (i != a_max)
  1217. {
  1218. if (RIL_ALGO_Q == agent->envi->parameters.algorithm)
  1219. agent->eligibility_reset = GNUNET_YES;
  1220. agent->envi->parameters.temperature *= agent->envi->parameters.temperature_decay;
  1221. }
  1222. return i;
  1223. }
  1224. sum += p[i];
  1225. }
  1226. GNUNET_assert(GNUNET_NO);
  1227. return RIL_ACTION_INVALID;
  1228. }
  1229. /**
  1230. * Select the next action of an agent either according to the e-greedy strategy
  1231. * or the softmax strategy.
  1232. *
  1233. * @param agent the agent in question
  1234. * @param state the current state-feature vector
  1235. * @return the action index
  1236. */
  1237. static int
  1238. agent_select_action (struct RIL_Peer_Agent *agent, double *state)
  1239. {
  1240. if (agent->envi->parameters.select == RIL_SELECT_EGREEDY)
  1241. {
  1242. return agent_select_egreedy(agent, state);
  1243. }
  1244. else
  1245. {
  1246. return agent_select_softmax(agent, state);
  1247. }
  1248. }
  1249. /**
  1250. * Performs one step of the Markov Decision Process. Other than in the literature the step starts
  1251. * after having done the last action a_old. It observes the new state s_next and the reward
  1252. * received. Then the coefficient update is done according to the SARSA or Q-learning method. The
  1253. * next action is put into effect.
  1254. *
  1255. * @param agent the agent performing the step
  1256. */
  1257. static void
  1258. agent_step (struct RIL_Peer_Agent *agent)
  1259. {
  1260. int a_next = RIL_ACTION_INVALID;
  1261. int a_max;
  1262. double *s_next;
  1263. double reward;
  1264. LOG(GNUNET_ERROR_TYPE_DEBUG, " agent_step() Peer '%s', algorithm %s\n",
  1265. GNUNET_i2s (&agent->peer),
  1266. agent->envi->parameters.algorithm ? "Q" : "SARSA");
  1267. s_next = envi_get_state (agent->envi, agent);
  1268. reward = envi_get_reward (agent->envi, agent);
  1269. if (agent->eligibility_reset)
  1270. {
  1271. agent_modify_eligibility(agent, RIL_E_ZERO, NULL, -1);
  1272. agent->eligibility_reset = GNUNET_NO;
  1273. }
  1274. else
  1275. {
  1276. agent_modify_eligibility (agent, RIL_E_DECAY, NULL, -1);
  1277. }
  1278. if (RIL_ACTION_INVALID != agent->a_old)
  1279. {
  1280. agent_modify_eligibility (agent, agent->envi->parameters.eligibility_trace_mode, agent->s_old, agent->a_old);
  1281. }
  1282. switch (agent->envi->parameters.algorithm)
  1283. {
  1284. case RIL_ALGO_SARSA:
  1285. a_next = agent_select_action (agent, s_next);
  1286. if (RIL_ACTION_INVALID != agent->a_old)
  1287. {
  1288. //updates weights with selected action (on-policy), if not first step
  1289. agent_update (agent, reward, s_next, a_next);
  1290. }
  1291. break;
  1292. case RIL_ALGO_Q:
  1293. a_max = agent_get_action_max (agent, s_next);
  1294. if (RIL_ACTION_INVALID != agent->a_old)
  1295. {
  1296. //updates weights with best action, disregarding actually selected action (off-policy), if not first step
  1297. agent_update (agent, reward, s_next, a_max);
  1298. }
  1299. a_next = agent_select_action (agent, s_next);
  1300. break;
  1301. }
  1302. GNUNET_assert(RIL_ACTION_INVALID != a_next);
  1303. LOG (GNUNET_ERROR_TYPE_DEBUG, "step() Step# %llu R: %f IN %llu OUT %llu A: %d\n",
  1304. agent->step_count,
  1305. reward,
  1306. agent->bw_in/1024,
  1307. agent->bw_out/1024,
  1308. a_next);
  1309. envi_do_action (agent->envi, agent, a_next);
  1310. GNUNET_free(agent->s_old);
  1311. agent->s_old = s_next;
  1312. agent->a_old = a_next;
  1313. agent->step_count += 1;
  1314. }
  1315. /**
  1316. * Prototype of the ril_step() procedure
  1317. *
  1318. * @param solver the solver handle
  1319. */
  1320. static void
  1321. ril_step (struct GAS_RIL_Handle *solver);
  1322. /**
  1323. * Task for the scheduler, which performs one step and lets the solver know that
  1324. * no further step is scheduled.
  1325. *
  1326. * @param cls the solver handle
  1327. * @param tc the task context for the scheduler
  1328. */
  1329. static void
  1330. ril_step_scheduler_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  1331. {
  1332. struct GAS_RIL_Handle *solver = cls;
  1333. solver->step_next_task_id = NULL;
  1334. ril_step (solver);
  1335. }
  1336. /**
  1337. * Determines how much of the available bandwidth is assigned. If more is
  1338. * assigned than available it returns 1. The function is used to determine the
  1339. * step size of the adaptive stepping.
  1340. *
  1341. * @param solver the solver handle
  1342. * @return the ratio
  1343. */
  1344. static double
  1345. ril_get_used_resource_ratio (struct GAS_RIL_Handle *solver)
  1346. {
  1347. int i;
  1348. struct RIL_Scope net;
  1349. unsigned long long sum_assigned = 0;
  1350. unsigned long long sum_available = 0;
  1351. double ratio;
  1352. for (i = 0; i < solver->networks_count; i++)
  1353. {
  1354. net = solver->network_entries[i];
  1355. if (net.bw_in_assigned > 0) //only consider scopes where an address is actually active
  1356. {
  1357. sum_assigned += net.bw_in_utilized;
  1358. sum_assigned += net.bw_out_utilized;
  1359. sum_available += net.bw_in_available;
  1360. sum_available += net.bw_out_available;
  1361. }
  1362. }
  1363. if (sum_available > 0)
  1364. {
  1365. ratio = ((double) sum_assigned) / ((double) sum_available);
  1366. }
  1367. else
  1368. {
  1369. ratio = 0;
  1370. }
  1371. return ratio > 1 ? 1 : ratio; //overutilization is possible, cap at 1
  1372. }
  1373. /**
  1374. * Lookup network struct by type
  1375. *
  1376. * @param s the solver handle
  1377. * @param type the network type
  1378. * @return the network struct
  1379. */
  1380. static struct RIL_Scope *
  1381. ril_get_network (struct GAS_RIL_Handle *s, uint32_t type)
  1382. {
  1383. int i;
  1384. for (i = 0; i < s->networks_count; i++)
  1385. {
  1386. if (s->network_entries[i].type == type)
  1387. {
  1388. return &s->network_entries[i];
  1389. }
  1390. }
  1391. return NULL ;
  1392. }
  1393. /**
  1394. * Determines whether more connections are allocated in a network scope, than
  1395. * they would theoretically fit. This is used as a heuristic to determine,
  1396. * whether a new connection can be allocated or not.
  1397. *
  1398. * @param solver the solver handle
  1399. * @param network the network scope in question
  1400. * @return GNUNET_YES if there are theoretically enough resources left
  1401. */
  1402. static int
  1403. ril_network_is_not_full (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
  1404. {
  1405. struct RIL_Scope *net;
  1406. struct RIL_Peer_Agent *agent;
  1407. unsigned long long address_count = 0;
  1408. for (agent = solver->agents_head; NULL != agent; agent = agent->next)
  1409. {
  1410. if (agent->address_inuse && agent->is_active)
  1411. {
  1412. net = agent->address_inuse->solver_information;
  1413. if (net->type == network)
  1414. {
  1415. address_count++;
  1416. }
  1417. }
  1418. }
  1419. net = ril_get_network (solver, network);
  1420. return (net->bw_in_available > RIL_MIN_BW * address_count) && (net->bw_out_available > RIL_MIN_BW * address_count);
  1421. }
  1422. /**
  1423. * Unblocks an agent for which a connection request is there, that could not
  1424. * be satisfied. Iterates over the addresses of the agent, if one of its
  1425. * addresses can now be allocated in its scope the agent is unblocked,
  1426. * otherwise it remains unchanged.
  1427. *
  1428. * @param solver the solver handle
  1429. * @param agent the agent in question
  1430. * @param silent
  1431. */
  1432. static void
  1433. ril_try_unblock_agent (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent, int silent)
  1434. {
  1435. struct RIL_Address_Wrapped *addr_wrap;
  1436. struct RIL_Scope *net;
  1437. unsigned long long start_in;
  1438. unsigned long long start_out;
  1439. for (addr_wrap = agent->addresses_head; NULL != addr_wrap; addr_wrap = addr_wrap->next)
  1440. {
  1441. net = addr_wrap->address_naked->solver_information;
  1442. if (ril_network_is_not_full(solver, net->type))
  1443. {
  1444. if (NULL == agent->address_inuse)
  1445. {
  1446. start_in = net->bw_in_available < net->bw_in_utilized ? (net->bw_in_available - net->bw_in_utilized) / 2 : RIL_MIN_BW;
  1447. start_out = net->bw_out_available < net->bw_out_utilized ? (net->bw_out_available - net->bw_out_utilized) / 2 : RIL_MIN_BW;
  1448. envi_set_active_suggestion (solver, agent, addr_wrap->address_naked, start_in, start_out, silent);
  1449. }
  1450. return;
  1451. }
  1452. }
  1453. agent->address_inuse = NULL;
  1454. }
  1455. /**
  1456. * Determines how much the reward needs to be discounted depending on the amount
  1457. * of time, which has passed since the last time-step.
  1458. *
  1459. * @param solver the solver handle
  1460. */
  1461. static void
  1462. ril_calculate_discount (struct GAS_RIL_Handle *solver)
  1463. {
  1464. struct GNUNET_TIME_Absolute time_now;
  1465. struct GNUNET_TIME_Relative time_delta;
  1466. double tau;
  1467. // MDP case only for debugging purposes
  1468. if (solver->simulate)
  1469. {
  1470. solver->global_discount_variable = solver->parameters.gamma;
  1471. solver->global_discount_integrated = 1;
  1472. return;
  1473. }
  1474. // semi-MDP case
  1475. //calculate tau, i.e. how many real valued time units have passed, one time unit is one minimum time step
  1476. time_now = GNUNET_TIME_absolute_get ();
  1477. time_delta = GNUNET_TIME_absolute_get_difference (solver->step_time_last, time_now);
  1478. solver->step_time_last = time_now;
  1479. tau = (double) time_delta.rel_value_us
  1480. / (double) solver->parameters.step_time_min.rel_value_us;
  1481. //calculate reward discounts (once per step for all agents)
  1482. solver->global_discount_variable = pow (M_E, ((-1.0) * ((double) solver->parameters.beta) * tau));
  1483. solver->global_discount_integrated = (1.0 - solver->global_discount_variable)
  1484. / (double) solver->parameters.beta;
  1485. }
  1486. /**
  1487. * Count the number of active agents/connections in a network scope
  1488. *
  1489. * @param solver the solver handle
  1490. * @param scope the network scope in question
  1491. * @return the number of allocated connections
  1492. */
  1493. static int
  1494. ril_network_count_active_agents (struct GAS_RIL_Handle *solver, struct RIL_Scope *scope)
  1495. {
  1496. int c = 0;
  1497. struct RIL_Peer_Agent *cur_agent;
  1498. for (cur_agent = solver->agents_head; NULL != cur_agent; cur_agent = cur_agent->next)
  1499. {
  1500. if (cur_agent->is_active && cur_agent->address_inuse && (cur_agent->address_inuse->solver_information == scope))
  1501. {
  1502. c++;
  1503. }
  1504. }
  1505. return c;
  1506. }
  1507. /**
  1508. * Calculates how much bandwidth is assigned in sum in a network scope, either
  1509. * in the inbound or in the outbound direction.
  1510. *
  1511. * @param solver the solver handle
  1512. * @param type the type of the network scope in question
  1513. * @param direction_in GNUNET_YES if the inbound direction should be summed up,
  1514. * otherwise the outbound direction will be summed up
  1515. * @return the sum of the assigned bandwidths
  1516. */
  1517. static unsigned long long
  1518. ril_network_get_assigned (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
  1519. {
  1520. struct RIL_Peer_Agent *cur;
  1521. struct RIL_Scope *net;
  1522. unsigned long long sum = 0;
  1523. for (cur = solver->agents_head; NULL != cur; cur = cur->next)
  1524. {
  1525. if (cur->is_active && cur->address_inuse)
  1526. {
  1527. net = cur->address_inuse->solver_information;
  1528. if (net->type == type)
  1529. {
  1530. if (direction_in)
  1531. sum += cur->bw_in;
  1532. else
  1533. sum += cur->bw_out;
  1534. }
  1535. }
  1536. }
  1537. return sum;
  1538. }
  1539. /**
  1540. * Calculates how much bandwidth is actually utilized in sum in a network scope,
  1541. * either in the inbound or in the outbound direction.
  1542. *
  1543. * @param solver the solver handle
  1544. * @param type the type of the network scope in question
  1545. * @param direction_in GNUNET_YES if the inbound direction should be summed up,
  1546. * otherwise the outbound direction will be summed up
  1547. * @return the sum of the utilized bandwidths (in bytes/second)
  1548. */
  1549. static unsigned long long
  1550. ril_network_get_utilized (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type type, int direction_in)
  1551. {
  1552. struct RIL_Peer_Agent *cur;
  1553. struct RIL_Scope *net;
  1554. unsigned long long sum = 0;
  1555. for (cur = solver->agents_head; NULL != cur; cur = cur->next)
  1556. {
  1557. if (cur->is_active && cur->address_inuse)
  1558. {
  1559. net = cur->address_inuse->solver_information;
  1560. if (net->type == type)
  1561. {
  1562. if (direction_in)
  1563. sum += cur->address_inuse->norm_utilization_in.norm;
  1564. else
  1565. sum += cur->address_inuse->norm_utilization_out.norm;
  1566. }
  1567. }
  1568. }
  1569. return sum;
  1570. }
  1571. /**
  1572. * Retrieves the state of the network scope, so that its attributes are up-to-
  1573. * date.
  1574. *
  1575. * @param solver the solver handle
  1576. */
  1577. static void
  1578. ril_networks_update_state (struct GAS_RIL_Handle *solver)
  1579. {
  1580. int c;
  1581. struct RIL_Scope *net;
  1582. for (c = 0; c < solver->networks_count; c++)
  1583. {
  1584. net = &solver->network_entries[c];
  1585. net->bw_in_assigned = ril_network_get_assigned(solver, net->type, GNUNET_YES);
  1586. net->bw_in_utilized = ril_network_get_utilized(solver, net->type, GNUNET_YES);
  1587. net->bw_out_assigned = ril_network_get_assigned(solver, net->type, GNUNET_NO);
  1588. net->bw_out_utilized = ril_network_get_utilized(solver, net->type, GNUNET_NO);
  1589. net->active_agent_count = ril_network_count_active_agents(solver, net);
  1590. net->social_welfare = ril_network_get_social_welfare(solver, net);
  1591. }
  1592. }
  1593. /**
  1594. * Schedules the next global step in an adaptive way. The more resources are
  1595. * left, the earlier the next step is scheduled. This serves the reactivity of
  1596. * the solver to changed inputs.
  1597. *
  1598. * @param solver the solver handle
  1599. */
  1600. static void
  1601. ril_step_schedule_next (struct GAS_RIL_Handle *solver)
  1602. {
  1603. double used_ratio;
  1604. double factor;
  1605. double y;
  1606. double offset;
  1607. struct GNUNET_TIME_Relative time_next;
  1608. used_ratio = ril_get_used_resource_ratio (solver);
  1609. GNUNET_assert(
  1610. solver->parameters.step_time_min.rel_value_us
  1611. <= solver->parameters.step_time_max.rel_value_us);
  1612. factor = (double) GNUNET_TIME_relative_subtract (solver->parameters.step_time_max,
  1613. solver->parameters.step_time_min).rel_value_us;
  1614. offset = (double) solver->parameters.step_time_min.rel_value_us;
  1615. y = factor * pow (used_ratio, RIL_INTERVAL_EXPONENT) + offset;
  1616. GNUNET_assert(y <= (double) solver->parameters.step_time_max.rel_value_us);
  1617. GNUNET_assert(y >= (double) solver->parameters.step_time_min.rel_value_us);
  1618. time_next = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS, (unsigned long long) y);
  1619. // LOG (GNUNET_ERROR_TYPE_INFO, "ratio: %f, factor: %f, offset: %f, y: %f\n",
  1620. // used_ratio,
  1621. // factor,
  1622. // offset,
  1623. // y);
  1624. if (solver->simulate)
  1625. {
  1626. time_next = GNUNET_TIME_UNIT_ZERO;
  1627. }
  1628. if ((NULL == solver->step_next_task_id) && (GNUNET_NO == solver->done))
  1629. {
  1630. solver->step_next_task_id = GNUNET_SCHEDULER_add_delayed (time_next, &ril_step_scheduler_task,
  1631. solver);
  1632. }
  1633. }
  1634. /**
  1635. * Triggers one step per agent
  1636. *
  1637. * @param solver
  1638. */
  1639. static void
  1640. ril_step (struct GAS_RIL_Handle *solver)
  1641. {
  1642. struct RIL_Peer_Agent *cur;
  1643. if (GNUNET_YES == solver->bulk_lock)
  1644. {
  1645. solver->bulk_changes++;
  1646. return;
  1647. }
  1648. ril_inform (solver, GAS_OP_SOLVE_START, GAS_STAT_SUCCESS);
  1649. LOG(GNUNET_ERROR_TYPE_DEBUG, " RIL step number %d\n", solver->step_count);
  1650. if (0 == solver->step_count)
  1651. {
  1652. solver->step_time_last = GNUNET_TIME_absolute_get ();
  1653. }
  1654. ril_calculate_discount (solver);
  1655. ril_networks_update_state (solver);
  1656. //trigger one step per active, unblocked agent
  1657. for (cur = solver->agents_head; NULL != cur; cur = cur->next)
  1658. {
  1659. if (cur->is_active)
  1660. {
  1661. if (NULL == cur->address_inuse)
  1662. {
  1663. ril_try_unblock_agent(solver, cur, GNUNET_NO);
  1664. }
  1665. if (cur->address_inuse)
  1666. {
  1667. agent_step (cur);
  1668. }
  1669. }
  1670. }
  1671. ril_networks_update_state (solver);
  1672. solver->step_count++;
  1673. ril_step_schedule_next (solver);
  1674. ril_inform (solver, GAS_OP_SOLVE_STOP, GAS_STAT_SUCCESS);
  1675. ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_START, GAS_STAT_SUCCESS);
  1676. for (cur = solver->agents_head; NULL != cur; cur = cur->next)
  1677. {
  1678. if (cur->suggestion_issue) {
  1679. solver->env->bandwidth_changed_cb (solver->env->cls,
  1680. cur->suggestion_address);
  1681. cur->suggestion_issue = GNUNET_NO;
  1682. }
  1683. }
  1684. ril_inform (solver, GAS_OP_SOLVE_UPDATE_NOTIFICATION_STOP, GAS_STAT_SUCCESS);
  1685. }
  1686. /**
  1687. * Initializes the matrix W of parameter vectors theta with small random numbers.
  1688. *
  1689. * @param agent The respective agent
  1690. */
  1691. static void
  1692. agent_w_init (struct RIL_Peer_Agent *agent)
  1693. {
  1694. int i;
  1695. int k;
  1696. for (i = 0; i < agent->n; i++)
  1697. {
  1698. for (k = 0; k < agent->m; k++)
  1699. {
  1700. agent->W[i][k] = agent->envi->parameters.alpha * (1.0 - 2.0 * ((double) GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, UINT32_MAX)/(double) UINT32_MAX));
  1701. }
  1702. }
  1703. }
  1704. /**
  1705. * Initialize an agent without addresses and its knowledge base
  1706. *
  1707. * @param s ril solver
  1708. * @param peer the one in question
  1709. * @return handle to the new agent
  1710. */
  1711. static struct RIL_Peer_Agent *
  1712. agent_init (void *s, const struct GNUNET_PeerIdentity *peer)
  1713. {
  1714. int i;
  1715. struct GAS_RIL_Handle * solver = s;
  1716. struct RIL_Peer_Agent * agent = GNUNET_new (struct RIL_Peer_Agent);
  1717. agent->envi = solver;
  1718. agent->peer = *peer;
  1719. agent->step_count = 0;
  1720. agent->is_active = GNUNET_NO;
  1721. agent->bw_in = RIL_MIN_BW;
  1722. agent->bw_out = RIL_MIN_BW;
  1723. agent->suggestion_issue = GNUNET_NO;
  1724. agent->n = RIL_ACTION_TYPE_NUM;
  1725. agent->m = 0;
  1726. agent->W = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
  1727. agent->E = (double **) GNUNET_malloc (sizeof (double *) * agent->n);
  1728. for (i = 0; i < agent->n; i++)
  1729. {
  1730. agent->W[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
  1731. agent->E[i] = (double *) GNUNET_malloc (sizeof (double) * agent->m);
  1732. }
  1733. agent_w_init(agent);
  1734. agent->eligibility_reset = GNUNET_NO;
  1735. agent->a_old = RIL_ACTION_INVALID;
  1736. agent->s_old = GNUNET_malloc (sizeof (double) * agent->m);
  1737. agent->address_inuse = NULL;
  1738. agent->objective_old = 0;
  1739. agent->nop_bonus = 0;
  1740. return agent;
  1741. }
  1742. /**
  1743. * Deallocate agent
  1744. *
  1745. * @param solver the solver handle
  1746. * @param agent the agent to retire
  1747. */
  1748. static void
  1749. agent_die (struct GAS_RIL_Handle *solver, struct RIL_Peer_Agent *agent)
  1750. {
  1751. int i;
  1752. for (i = 0; i < agent->n; i++)
  1753. {
  1754. GNUNET_free_non_null(agent->W[i]);
  1755. GNUNET_free_non_null(agent->E[i]);
  1756. }
  1757. GNUNET_free_non_null(agent->W);
  1758. GNUNET_free_non_null(agent->E);
  1759. GNUNET_free_non_null(agent->s_old);
  1760. GNUNET_free(agent);
  1761. }
  1762. /**
  1763. * Returns the agent for a peer
  1764. *
  1765. * @param solver the solver handle
  1766. * @param peer the identity of the peer
  1767. * @param create whether or not to create an agent, if none is allocated yet
  1768. * @return the agent
  1769. */
  1770. static struct RIL_Peer_Agent *
  1771. ril_get_agent (struct GAS_RIL_Handle *solver, const struct GNUNET_PeerIdentity *peer, int create)
  1772. {
  1773. struct RIL_Peer_Agent *cur;
  1774. for (cur = solver->agents_head; NULL != cur; cur = cur->next)
  1775. {
  1776. if (0 == memcmp (peer, &cur->peer, sizeof(struct GNUNET_PeerIdentity)))
  1777. {
  1778. return cur;
  1779. }
  1780. }
  1781. if (create)
  1782. {
  1783. cur = agent_init (solver, peer);
  1784. GNUNET_CONTAINER_DLL_insert_tail(solver->agents_head, solver->agents_tail, cur);
  1785. return cur;
  1786. }
  1787. return NULL ;
  1788. }
  1789. /**
  1790. * Determine whether at least the minimum bandwidth is set for the network. Otherwise the network is
  1791. * considered inactive and not used. Addresses in an inactive network are ignored.
  1792. *
  1793. * @param solver solver handle
  1794. * @param network the network type
  1795. * @return whether or not the network is considered active
  1796. */
  1797. static int
  1798. ril_network_is_active (struct GAS_RIL_Handle *solver, enum GNUNET_ATS_Network_Type network)
  1799. {
  1800. struct RIL_Scope *net;
  1801. net = ril_get_network (solver, network);
  1802. return net->bw_out_available >= RIL_MIN_BW;
  1803. }
  1804. /**
  1805. * Cuts a slice out of a vector of elements. This is used to decrease the size of the matrix storing
  1806. * the reward function approximation. It copies the memory, which is not cut, to the new vector,
  1807. * frees the memory of the old vector, and redirects the pointer to the new one.
  1808. *
  1809. * @param old pointer to the pointer to the first element of the vector
  1810. * @param element_size byte size of the vector elements
  1811. * @param hole_start the first element to cut out
  1812. * @param hole_length the number of elements to cut out
  1813. * @param old_length the length of the old vector
  1814. */
  1815. static void
  1816. ril_cut_from_vector (void **old,
  1817. size_t element_size,
  1818. unsigned int hole_start,
  1819. unsigned int hole_length,
  1820. unsigned int old_length)
  1821. {
  1822. char *tmpptr;
  1823. char *oldptr = (char *) *old;
  1824. size_t size;
  1825. unsigned int bytes_before;
  1826. unsigned int bytes_hole;
  1827. unsigned int bytes_after;
  1828. GNUNET_assert(old_length >= hole_length);
  1829. GNUNET_assert(old_length >= (hole_start + hole_length));
  1830. size = element_size * (old_length - hole_length);
  1831. bytes_before = element_size * hole_start;
  1832. bytes_hole = element_size * hole_length;
  1833. bytes_after = element_size * (old_length - hole_start - hole_length);
  1834. if (0 == size)
  1835. {
  1836. tmpptr = NULL;
  1837. }
  1838. else
  1839. {
  1840. tmpptr = GNUNET_malloc (size);
  1841. memcpy (tmpptr, oldptr, bytes_before);
  1842. memcpy (tmpptr + bytes_before, oldptr + (bytes_before + bytes_hole), bytes_after);
  1843. }
  1844. if (NULL != *old)
  1845. {
  1846. GNUNET_free(*old);
  1847. }
  1848. *old = (void *) tmpptr;
  1849. }
  1850. /*
  1851. * Solver API functions
  1852. * ---------------------------
  1853. */
  1854. /**
  1855. * Change relative preference for quality in solver
  1856. *
  1857. * @param solver the solver handle
  1858. * @param peer the peer to change the preference for
  1859. * @param kind the kind to change the preference
  1860. * @param pref_rel the normalized preference value for this kind over all clients
  1861. */
  1862. static void
  1863. GAS_ril_address_change_preference (void *solver,
  1864. const struct GNUNET_PeerIdentity *peer,
  1865. enum GNUNET_ATS_PreferenceKind kind,
  1866. double pref_rel)
  1867. {
  1868. LOG(GNUNET_ERROR_TYPE_DEBUG,
  1869. "API_address_change_preference() Preference '%s' for peer '%s' changed to %.2f \n",
  1870. GNUNET_ATS_print_preference_type (kind), GNUNET_i2s (peer), pref_rel);
  1871. struct GAS_RIL_Handle *s = solver;
  1872. s->parameters.temperature = s->parameters.temperature_init;
  1873. s->parameters.epsilon = s->parameters.epsilon_init;
  1874. ril_step (s);
  1875. }
  1876. /**
  1877. * Add a new address for a peer to the solver
  1878. *
  1879. * The address is already contained in the addresses hashmap!
  1880. *
  1881. * @param solver the solver Handle
  1882. * @param address the address to add
  1883. * @param network network type of this address
  1884. */
  1885. static void
  1886. GAS_ril_address_add (void *solver,
  1887. struct ATS_Address *address,
  1888. uint32_t network)
  1889. {
  1890. struct GAS_RIL_Handle *s = solver;
  1891. struct RIL_Peer_Agent *agent;
  1892. struct RIL_Address_Wrapped *address_wrapped;
  1893. struct RIL_Scope *net;
  1894. unsigned int m_new;
  1895. unsigned int m_old;
  1896. unsigned int n_new;
  1897. unsigned int n_old;
  1898. int i;
  1899. unsigned int zero;
  1900. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1901. "API_address_add()\n");
  1902. net = ril_get_network (s, network);
  1903. address->solver_information = net;
  1904. if (!ril_network_is_active (s, network))
  1905. {
  1906. LOG(GNUNET_ERROR_TYPE_DEBUG,
  1907. "API_address_add() Did not add %s address %s for peer '%s', network does not have enough bandwidth\n",
  1908. address->plugin, address->addr, GNUNET_i2s (&address->peer));
  1909. return;
  1910. }
  1911. s->parameters.temperature = s->parameters.temperature_init;
  1912. s->parameters.epsilon = s->parameters.epsilon_init;
  1913. agent = ril_get_agent (s, &address->peer, GNUNET_YES);
  1914. //add address
  1915. address_wrapped = GNUNET_new (struct RIL_Address_Wrapped);
  1916. address_wrapped->address_naked = address;
  1917. GNUNET_CONTAINER_DLL_insert_tail(agent->addresses_head, agent->addresses_tail, address_wrapped);
  1918. //increase size of W
  1919. m_new = agent->m + ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
  1920. m_old = agent->m;
  1921. n_new = agent->n + 1;
  1922. n_old = agent->n;
  1923. GNUNET_array_grow(agent->W, agent->n, n_new);
  1924. agent->n = n_old;
  1925. GNUNET_array_grow(agent->E, agent->n, n_new);
  1926. for (i = 0; i < n_new; i++)
  1927. {
  1928. if (i < n_old)
  1929. {
  1930. agent->m = m_old;
  1931. GNUNET_array_grow(agent->W[i], agent->m, m_new);
  1932. agent->m = m_old;
  1933. GNUNET_array_grow(agent->E[i], agent->m, m_new);
  1934. }
  1935. else
  1936. {
  1937. zero = 0;
  1938. GNUNET_array_grow(agent->W[i], zero, m_new);
  1939. zero = 0;
  1940. GNUNET_array_grow(agent->E[i], zero, m_new);
  1941. }
  1942. }
  1943. //increase size of old state vector
  1944. agent->m = m_old;
  1945. GNUNET_array_grow(agent->s_old, agent->m, m_new);
  1946. ril_try_unblock_agent(s, agent, GNUNET_NO);
  1947. ril_step (s);
  1948. LOG(GNUNET_ERROR_TYPE_DEBUG, "API_address_add() Added %s %s address %s for peer '%s'\n",
  1949. address->active ? "active" : "inactive", address->plugin, address->addr,
  1950. GNUNET_i2s (&address->peer));
  1951. }
  1952. /**
  1953. * Delete an address in the solver
  1954. *
  1955. * The address is not contained in the address hashmap anymore!
  1956. *
  1957. * @param solver the solver handle
  1958. * @param address the address to remove
  1959. */
  1960. static void
  1961. GAS_ril_address_delete (void *solver,
  1962. struct ATS_Address *address)
  1963. {
  1964. struct GAS_RIL_Handle *s = solver;
  1965. struct RIL_Peer_Agent *agent;
  1966. struct RIL_Address_Wrapped *address_wrapped;
  1967. int address_index;
  1968. unsigned int m_new;
  1969. unsigned int n_new;
  1970. int i;
  1971. struct RIL_Scope *net;
  1972. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1973. "API_address_delete() Delete %s %s address %s for peer '%s'\n",
  1974. address->active ? "active" : "inactive",
  1975. address->plugin,
  1976. address->addr,
  1977. GNUNET_i2s (&address->peer));
  1978. agent = ril_get_agent (s, &address->peer, GNUNET_NO);
  1979. if (NULL == agent)
  1980. {
  1981. net = address->solver_information;
  1982. GNUNET_assert(! ril_network_is_active (s, net->type));
  1983. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1984. "No agent allocated for peer yet, since address was in inactive network\n");
  1985. return;
  1986. }
  1987. s->parameters.temperature = s->parameters.temperature_init;
  1988. s->parameters.epsilon = s->parameters.epsilon_init;
  1989. address_index = agent_address_get_index (agent, address);
  1990. address_wrapped = agent_address_get_wrapped (agent, address);
  1991. if (NULL == address_wrapped)
  1992. {
  1993. net = address->solver_information;
  1994. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1995. "Address not considered by agent, address was in inactive network\n");
  1996. return;
  1997. }
  1998. GNUNET_CONTAINER_DLL_remove (agent->addresses_head,
  1999. agent->addresses_tail,
  2000. address_wrapped);
  2001. GNUNET_free (address_wrapped);
  2002. //decrease W
  2003. m_new = agent->m - ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1));
  2004. n_new = agent->n - 1;
  2005. for (i = 0; i < agent->n; i++)
  2006. {
  2007. ril_cut_from_vector ((void **) &agent->W[i], sizeof(double),
  2008. address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
  2009. ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
  2010. ril_cut_from_vector ((void **) &agent->E[i], sizeof(double),
  2011. address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
  2012. ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
  2013. }
  2014. GNUNET_free_non_null(agent->W[RIL_ACTION_TYPE_NUM + address_index]);
  2015. GNUNET_free_non_null(agent->E[RIL_ACTION_TYPE_NUM + address_index]);
  2016. ril_cut_from_vector ((void **) &agent->W, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
  2017. 1, agent->n);
  2018. ril_cut_from_vector ((void **) &agent->E, sizeof(double *), RIL_ACTION_TYPE_NUM + address_index,
  2019. 1, agent->n);
  2020. //correct last action
  2021. if (agent->a_old > (RIL_ACTION_TYPE_NUM + address_index))
  2022. {
  2023. agent->a_old -= 1;
  2024. }
  2025. else if (agent->a_old == (RIL_ACTION_TYPE_NUM + address_index))
  2026. {
  2027. agent->a_old = RIL_ACTION_INVALID;
  2028. }
  2029. //decrease old state vector
  2030. ril_cut_from_vector ((void **) &agent->s_old, sizeof(double),
  2031. address_index * ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)),
  2032. ((s->parameters.rbf_divisor+1) * (s->parameters.rbf_divisor+1)), agent->m);
  2033. agent->m = m_new;
  2034. agent->n = n_new;
  2035. if (agent->address_inuse == address)
  2036. {
  2037. if (NULL != agent->addresses_head) //if peer has an address left, use it
  2038. {
  2039. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2040. "Active address died, suggesting alternative!\n");
  2041. envi_set_active_suggestion (s,
  2042. agent,
  2043. agent->addresses_head->address_naked,
  2044. agent->bw_in,
  2045. agent->bw_out,
  2046. GNUNET_YES);
  2047. }
  2048. else
  2049. {
  2050. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2051. "Active address died, suggesting disconnect!\n");
  2052. envi_set_active_suggestion (s, agent, NULL, 0, 0, GNUNET_NO);
  2053. }
  2054. }
  2055. ril_step (solver);
  2056. if (agent->suggestion_address == address)
  2057. {
  2058. agent->suggestion_issue = GNUNET_NO;
  2059. agent->suggestion_address = NULL;
  2060. }
  2061. GNUNET_assert (agent->address_inuse != address);
  2062. }
  2063. /**
  2064. * Update the properties of an address in the solver
  2065. *
  2066. * @param solver solver handle
  2067. * @param address the address
  2068. */
  2069. static void
  2070. GAS_ril_address_property_changed (void *solver,
  2071. struct ATS_Address *address)
  2072. {
  2073. struct GAS_RIL_Handle *s = solver;
  2074. LOG(GNUNET_ERROR_TYPE_DEBUG,
  2075. "Properties for peer '%s' address changed\n",
  2076. GNUNET_i2s (&address->peer));
  2077. s->parameters.temperature = s->parameters.temperature_init;
  2078. s->parameters.epsilon = s->parameters.epsilon_init;
  2079. ril_step (s);
  2080. }
  2081. /**
  2082. * Give feedback about the current assignment
  2083. *
  2084. * @param solver the solver handle
  2085. * @param application the application
  2086. * @param peer the peer to change the preference for
  2087. * @param scope the time interval for this feedback: [now - scope .. now]
  2088. * @param kind the kind to change the preference
  2089. * @param score the score
  2090. */
  2091. static void
  2092. GAS_ril_address_preference_feedback (void *solver,
  2093. struct GNUNET_SERVER_Client *application,
  2094. const struct GNUNET_PeerIdentity *peer,
  2095. const struct GNUNET_TIME_Relative scope,
  2096. enum GNUNET_ATS_PreferenceKind kind,
  2097. double score)
  2098. {
  2099. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2100. "API_address_preference_feedback() Peer '%s' got a feedback of %+.3f from application %s for "
  2101. "preference %s for %d seconds\n",
  2102. GNUNET_i2s (peer),
  2103. "UNKNOWN",
  2104. GNUNET_ATS_print_preference_type (kind),
  2105. scope.rel_value_us / 1000000);
  2106. }
  2107. /**
  2108. * Start a bulk operation
  2109. *
  2110. * @param solver the solver
  2111. */
  2112. static void
  2113. GAS_ril_bulk_start (void *solver)
  2114. {
  2115. struct GAS_RIL_Handle *s = solver;
  2116. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2117. "API_bulk_start() lock: %d\n", s->bulk_lock+1);
  2118. s->bulk_lock++;
  2119. }
  2120. /**
  2121. * Bulk operation done
  2122. *
  2123. * @param solver the solver handle
  2124. */
  2125. static void
  2126. GAS_ril_bulk_stop (void *solver)
  2127. {
  2128. struct GAS_RIL_Handle *s = solver;
  2129. LOG(GNUNET_ERROR_TYPE_DEBUG,
  2130. "API_bulk_stop() lock: %d\n",
  2131. s->bulk_lock - 1);
  2132. if (s->bulk_lock < 1)
  2133. {
  2134. GNUNET_break(0);
  2135. return;
  2136. }
  2137. s->bulk_lock--;
  2138. if (0 < s->bulk_changes)
  2139. {
  2140. ril_step (solver);
  2141. s->bulk_changes = 0;
  2142. }
  2143. }
  2144. /**
  2145. * Tell solver to notify ATS if the address to use changes for a specific
  2146. * peer using the bandwidth changed callback
  2147. *
  2148. * The solver must only notify about changes for peers with pending address
  2149. * requests!
  2150. *
  2151. * @param solver the solver handle
  2152. * @param peer the identity of the peer
  2153. */
  2154. static void
  2155. GAS_ril_get_preferred_address (void *solver,
  2156. const struct GNUNET_PeerIdentity *peer)
  2157. {
  2158. struct GAS_RIL_Handle *s = solver;
  2159. struct RIL_Peer_Agent *agent;
  2160. LOG(GNUNET_ERROR_TYPE_DEBUG, "API_get_preferred_address()\n");
  2161. agent = ril_get_agent (s, peer, GNUNET_YES);
  2162. agent->is_active = GNUNET_YES;
  2163. envi_set_active_suggestion (solver, agent, agent->address_inuse, agent->bw_in, agent->bw_out, GNUNET_YES);
  2164. ril_try_unblock_agent(solver, agent, GNUNET_YES);
  2165. if (agent->address_inuse)
  2166. {
  2167. LOG(GNUNET_ERROR_TYPE_DEBUG,
  2168. "API_get_preferred_address() Activated agent for peer '%s' with %s address %s\n",
  2169. GNUNET_i2s (peer), agent->address_inuse->plugin, agent->address_inuse->addr);
  2170. }
  2171. else
  2172. {
  2173. LOG(GNUNET_ERROR_TYPE_DEBUG,
  2174. "API_get_preferred_address() Activated agent for peer '%s', but no address available\n",
  2175. GNUNET_i2s (peer));
  2176. s->parameters.temperature = s->parameters.temperature_init;
  2177. s->parameters.epsilon = s->parameters.epsilon_init;
  2178. }
  2179. if (NULL != agent->address_inuse)
  2180. s->env->bandwidth_changed_cb (s->env->cls,
  2181. agent->address_inuse);
  2182. }
  2183. /**
  2184. * Tell solver stop notifying ATS about changes for this peers
  2185. *
  2186. * The solver must only notify about changes for peers with pending address
  2187. * requests!
  2188. *
  2189. * @param solver the solver handle
  2190. * @param peer the peer
  2191. */
  2192. static void
  2193. GAS_ril_stop_get_preferred_address (void *solver,
  2194. const struct GNUNET_PeerIdentity *peer)
  2195. {
  2196. struct GAS_RIL_Handle *s = solver;
  2197. struct RIL_Peer_Agent *agent;
  2198. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2199. "API_stop_get_preferred_address()");
  2200. agent = ril_get_agent (s, peer, GNUNET_NO);
  2201. if (NULL == agent)
  2202. {
  2203. GNUNET_break(0);
  2204. return;
  2205. }
  2206. if (GNUNET_NO == agent->is_active)
  2207. {
  2208. GNUNET_break(0);
  2209. return;
  2210. }
  2211. s->parameters.temperature = s->parameters.temperature_init;
  2212. s->parameters.epsilon = s->parameters.epsilon_init;
  2213. agent->is_active = GNUNET_NO;
  2214. envi_set_active_suggestion (s, agent, agent->address_inuse, agent->bw_in, agent->bw_out,
  2215. GNUNET_YES);
  2216. ril_step (s);
  2217. LOG(GNUNET_ERROR_TYPE_DEBUG,
  2218. "API_stop_get_preferred_address() Paused agent for peer '%s'\n",
  2219. GNUNET_i2s (peer));
  2220. }
  2221. /**
  2222. * Entry point for the plugin
  2223. *
  2224. * @param cls pointer to the 'struct GNUNET_ATS_PluginEnvironment'
  2225. */
  2226. void *
  2227. libgnunet_plugin_ats_ril_init (void *cls)
  2228. {
  2229. static struct GNUNET_ATS_SolverFunctions sf;
  2230. struct GNUNET_ATS_PluginEnvironment *env = cls;
  2231. struct GAS_RIL_Handle *solver = GNUNET_new (struct GAS_RIL_Handle);
  2232. struct RIL_Scope * cur;
  2233. int c;
  2234. char *string;
  2235. float f_tmp;
  2236. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2237. "API_init() Initializing RIL solver\n");
  2238. GNUNET_assert (NULL != env);
  2239. GNUNET_assert (NULL != env->cfg);
  2240. GNUNET_assert (NULL != env->stats);
  2241. GNUNET_assert (NULL != env->bandwidth_changed_cb);
  2242. GNUNET_assert (NULL != env->get_preferences);
  2243. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(env->cfg, "ats", "RIL_RBF_DIVISOR", &solver->parameters.rbf_divisor))
  2244. {
  2245. solver->parameters.rbf_divisor = RIL_DEFAULT_RBF_DIVISOR;
  2246. }
  2247. if (GNUNET_OK
  2248. != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MIN",
  2249. &solver->parameters.step_time_min))
  2250. {
  2251. solver->parameters.step_time_min = RIL_DEFAULT_STEP_TIME_MIN;
  2252. }
  2253. if (GNUNET_OK
  2254. != GNUNET_CONFIGURATION_get_value_time (env->cfg, "ats", "RIL_STEP_TIME_MAX",
  2255. &solver->parameters.step_time_max))
  2256. {
  2257. solver->parameters.step_time_max = RIL_DEFAULT_STEP_TIME_MAX;
  2258. }
  2259. if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_ALGORITHM", &string))
  2260. {
  2261. GNUNET_STRINGS_utf8_toupper (string, string);
  2262. if (0 == strcmp (string, "SARSA"))
  2263. {
  2264. solver->parameters.algorithm = RIL_ALGO_SARSA;
  2265. }
  2266. if (0 == strcmp (string, "Q-LEARNING"))
  2267. {
  2268. solver->parameters.algorithm = RIL_ALGO_Q;
  2269. }
  2270. GNUNET_free (string);
  2271. }
  2272. else
  2273. {
  2274. solver->parameters.algorithm = RIL_DEFAULT_ALGORITHM;
  2275. }
  2276. if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SELECT", &string))
  2277. {
  2278. solver->parameters.select = !strcmp (string, "EGREEDY") ? RIL_SELECT_EGREEDY : RIL_SELECT_SOFTMAX;
  2279. GNUNET_free (string);
  2280. }
  2281. else
  2282. {
  2283. solver->parameters.select = RIL_DEFAULT_SELECT;
  2284. }
  2285. solver->parameters.beta = RIL_DEFAULT_DISCOUNT_BETA;
  2286. if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
  2287. "RIL_DISCOUNT_BETA", &f_tmp))
  2288. {
  2289. if (f_tmp < 0.0)
  2290. {
  2291. LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
  2292. "RIL_DISCOUNT_BETA", f_tmp);
  2293. }
  2294. else
  2295. {
  2296. solver->parameters.beta = f_tmp;
  2297. LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
  2298. "RIL_DISCOUNT_BETA", f_tmp);
  2299. }
  2300. }
  2301. solver->parameters.gamma = RIL_DEFAULT_DISCOUNT_GAMMA;
  2302. if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
  2303. "RIL_DISCOUNT_GAMMA", &f_tmp))
  2304. {
  2305. if ((f_tmp < 0.0) || (f_tmp > 1.0))
  2306. {
  2307. LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
  2308. "RIL_DISCOUNT_GAMMA", f_tmp);
  2309. }
  2310. else
  2311. {
  2312. solver->parameters.gamma = f_tmp;
  2313. LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
  2314. "RIL_DISCOUNT_GAMMA", f_tmp);
  2315. }
  2316. }
  2317. solver->parameters.alpha = RIL_DEFAULT_GRADIENT_STEP_SIZE;
  2318. if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
  2319. "RIL_GRADIENT_STEP_SIZE", &f_tmp))
  2320. {
  2321. if ((f_tmp < 0.0) || (f_tmp > 1.0))
  2322. {
  2323. LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
  2324. "RIL_GRADIENT_STEP_SIZE", f_tmp);
  2325. }
  2326. else
  2327. {
  2328. solver->parameters.alpha = f_tmp;
  2329. LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
  2330. "RIL_GRADIENT_STEP_SIZE", f_tmp);
  2331. }
  2332. }
  2333. solver->parameters.lambda = RIL_DEFAULT_TRACE_DECAY;
  2334. if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
  2335. "RIL_TRACE_DECAY", &f_tmp))
  2336. {
  2337. if ((f_tmp < 0.0) || (f_tmp > 1.0))
  2338. {
  2339. LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
  2340. "RIL_TRACE_DECAY", f_tmp);
  2341. }
  2342. else
  2343. {
  2344. solver->parameters.lambda = f_tmp;
  2345. LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
  2346. "RIL_TRACE_DECAY", f_tmp);
  2347. }
  2348. }
  2349. solver->parameters.epsilon_init = RIL_DEFAULT_EXPLORE_RATIO;
  2350. if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
  2351. "RIL_EXPLORE_RATIO", &f_tmp))
  2352. {
  2353. if ((f_tmp < 0.0) || (f_tmp > 1.0))
  2354. {
  2355. LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
  2356. "RIL_EXPLORE_RATIO", f_tmp);
  2357. }
  2358. else
  2359. {
  2360. solver->parameters.epsilon_init = f_tmp;
  2361. LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
  2362. "RIL_EXPLORE_RATIO", f_tmp);
  2363. }
  2364. }
  2365. solver->parameters.epsilon_decay = RIL_DEFAULT_EXPLORE_DECAY;
  2366. if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
  2367. "RIL_EXPLORE_DECAY", &f_tmp))
  2368. {
  2369. if ((f_tmp < 0.0) || (f_tmp > 1.0))
  2370. {
  2371. LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
  2372. "RIL_EXPLORE_DECAY", f_tmp);
  2373. }
  2374. else
  2375. {
  2376. solver->parameters.epsilon_decay = f_tmp;
  2377. LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
  2378. "RIL_EXPLORE_DECAY", f_tmp);
  2379. }
  2380. }
  2381. solver->parameters.temperature_init = RIL_DEFAULT_TEMPERATURE;
  2382. if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
  2383. "RIL_TEMPERATURE", &f_tmp))
  2384. {
  2385. if (f_tmp <= 0.0)
  2386. {
  2387. LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
  2388. "RIL_TEMPERATURE", f_tmp);
  2389. }
  2390. else
  2391. {
  2392. solver->parameters.temperature_init = f_tmp;
  2393. LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
  2394. "RIL_TEMPERATURE", f_tmp);
  2395. }
  2396. }
  2397. solver->parameters.temperature_decay = RIL_DEFAULT_TEMPERATURE_DECAY;
  2398. if (GNUNET_SYSERR != GNUNET_CONFIGURATION_get_value_float (env->cfg, "ats",
  2399. "RIL_TEMPERATURE_DECAY", &f_tmp))
  2400. {
  2401. if ((f_tmp <= 0.0) || solver->parameters.temperature_decay > 1)
  2402. {
  2403. LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid %s configuration %f \n"),
  2404. "RIL_TEMPERATURE_DECAY", f_tmp);
  2405. }
  2406. else
  2407. {
  2408. solver->parameters.temperature_decay = f_tmp;
  2409. LOG (GNUNET_ERROR_TYPE_INFO, "Using %s of %.3f\n",
  2410. "RIL_TEMPERATURE_DECAY", f_tmp);
  2411. }
  2412. }
  2413. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "ats", "RIL_SIMULATE", &solver->simulate))
  2414. {
  2415. solver->simulate = GNUNET_NO;
  2416. }
  2417. if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(env->cfg, "ats", "RIL_REPLACE_TRACES"))
  2418. {
  2419. solver->parameters.eligibility_trace_mode = RIL_E_REPLACE;
  2420. }
  2421. else
  2422. {
  2423. solver->parameters.eligibility_trace_mode = RIL_E_ACCUMULATE;
  2424. }
  2425. if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (env->cfg, "ats", "RIL_SOCIAL_WELFARE", &string))
  2426. {
  2427. solver->parameters.social_welfare = !strcmp (string, "NASH") ? RIL_WELFARE_NASH : RIL_WELFARE_EGALITARIAN;
  2428. GNUNET_free (string);
  2429. }
  2430. else
  2431. {
  2432. solver->parameters.social_welfare = RIL_DEFAULT_WELFARE;
  2433. }
  2434. solver->env = env;
  2435. sf.cls = solver;
  2436. sf.s_add = &GAS_ril_address_add;
  2437. sf.s_address_update_property = &GAS_ril_address_property_changed;
  2438. sf.s_get = &GAS_ril_get_preferred_address;
  2439. sf.s_get_stop = &GAS_ril_stop_get_preferred_address;
  2440. sf.s_pref = &GAS_ril_address_change_preference;
  2441. sf.s_feedback = &GAS_ril_address_preference_feedback;
  2442. sf.s_del = &GAS_ril_address_delete;
  2443. sf.s_bulk_start = &GAS_ril_bulk_start;
  2444. sf.s_bulk_stop = &GAS_ril_bulk_stop;
  2445. solver->networks_count = env->network_count;
  2446. solver->network_entries = GNUNET_malloc (env->network_count * sizeof (struct RIL_Scope));
  2447. solver->step_count = 0;
  2448. solver->done = GNUNET_NO;
  2449. for (c = 0; c < env->network_count; c++)
  2450. {
  2451. cur = &solver->network_entries[c];
  2452. cur->type = c;
  2453. cur->bw_in_available = env->in_quota[c];
  2454. cur->bw_out_available = env->out_quota[c];
  2455. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2456. "init() Quotas for %s network: IN %llu - OUT %llu\n",
  2457. GNUNET_ATS_print_network_type(cur->type),
  2458. cur->bw_in_available/1024,
  2459. cur->bw_out_available/1024);
  2460. }
  2461. LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Parameters:\n");
  2462. LOG(GNUNET_ERROR_TYPE_DEBUG, "init() Algorithm = %s, alpha = %f, beta = %f, lambda = %f\n",
  2463. solver->parameters.algorithm ? "Q" : "SARSA",
  2464. solver->parameters.alpha,
  2465. solver->parameters.beta,
  2466. solver->parameters.lambda);
  2467. LOG(GNUNET_ERROR_TYPE_DEBUG, "init() exploration_ratio = %f, temperature = %f, ActionSelection = %s\n",
  2468. solver->parameters.epsilon,
  2469. solver->parameters.temperature,
  2470. solver->parameters.select ? "EGREEDY" : "SOFTMAX");
  2471. LOG(GNUNET_ERROR_TYPE_DEBUG, "init() RBF_DIVISOR = %llu\n",
  2472. solver->parameters.rbf_divisor);
  2473. return &sf;
  2474. }
  2475. /**
  2476. * Exit point for the plugin
  2477. *
  2478. * @param cls the solver handle
  2479. */
  2480. void *
  2481. libgnunet_plugin_ats_ril_done (void *cls)
  2482. {
  2483. struct GNUNET_ATS_SolverFunctions *sf = cls;
  2484. struct GAS_RIL_Handle *s = sf->cls;
  2485. struct RIL_Peer_Agent *cur_agent;
  2486. struct RIL_Peer_Agent *next_agent;
  2487. LOG(GNUNET_ERROR_TYPE_DEBUG, "API_done() Shutting down RIL solver\n");
  2488. s->done = GNUNET_YES;
  2489. cur_agent = s->agents_head;
  2490. while (NULL != cur_agent)
  2491. {
  2492. next_agent = cur_agent->next;
  2493. GNUNET_CONTAINER_DLL_remove(s->agents_head, s->agents_tail, cur_agent);
  2494. agent_die (s, cur_agent);
  2495. cur_agent = next_agent;
  2496. }
  2497. if (NULL != s->step_next_task_id)
  2498. {
  2499. GNUNET_SCHEDULER_cancel (s->step_next_task_id);
  2500. }
  2501. GNUNET_free(s->network_entries);
  2502. GNUNET_free(s);
  2503. return NULL;
  2504. }
  2505. /* end of plugin_ats_ril.c */